Beispiel #1
0
 public BlockOrder(
     int stg_id,
     EClientInterface socket,
     TimeZoneInfo zone,
     ContractDetails details,
     XmlNode config,
     int ref_con_id = 0
     )
 {
     _stg_id        = stg_id;
     _socket        = socket;
     _con           = details.Summary;
     _ref_con_id    = ref_con_id;
     _minTick       = details.MinTick;
     _unitSize      = int.Parse(config["UnitSize"].InnerText);
     _maxOrderSize  = int.Parse(config["MaxBlockSize"].InnerText);
     _maxNOrders    = int.Parse(config["MaxNumberOfOrders"].InnerText);
     _maxBidAskUnit = config["MaxBidAskUnit"] == null ? 3 : int.Parse(config["MaxBidAskUnit"].InnerText);
     _zone          = zone;
     //_mktOrdersOnly = mktOrdersOnly;
     _entryTemplate  = getOrderTemplate(config, "EntryOrderTemplate");
     _profitTemplate = getOrderTemplate(config, "ProfitOrderTemplate");
     _stopTemplate   = getOrderTemplate(config, "StopOrderTemplate");
     _expiryTemplate = getOrderTemplate(config, "ExpiryOrderTemplate");
     _ids            = new List <int>();
     _tickers        = new List <int>();
     _quotes         = new Dictionary <int, double>();
     _filledQtys     = new Dictionary <int, double>();
     _openOrders     = new Dictionary <int, Order>();
     _quotes.Add(1, 0);  //bid
     _quotes.Add(2, 0);  //ask
     _quotes.Add(4, 0);  //last
     _quotes.Add(6, 0);  //high
     _quotes.Add(7, 0);  //low
     _quotes.Add(9, 0);  //close
     _placeOrderEvent = _tm.createEvent();
     _mktDataEvents   = new List <AutoResetEvent>();
     for (int i = 0; i <= 14; ++i)
     {
         _mktDataEvents.Add(null);
     }
     _mktDataEvents[1] = new AutoResetEvent(false);
     _mktDataEvents[2] = new AutoResetEvent(false);
     _mktDataEvents[4] = new AutoResetEvent(false);
 }
Beispiel #2
0
        public static List <Order> createBracketOrder(Order parent, double profitTarget, double stopTarget, DateTime expiry, OrderTemplate profitTemplate = null, OrderTemplate stopTemplate = null, OrderTemplate expiryTemplate = null)
        {
            List <Order> orders = new List <Order>();

            parent.Transmit = false;
            orders.Add(parent);
            if (expiry != DateTime.MinValue)
            {
                Order expiryOrder;
                if (expiryTemplate == null)
                {
                    expiryOrder           = new Order();
                    expiryOrder.OrderType = "MKT";
                }
                else
                {
                    expiryOrder = expiryTemplate.clone();
                    if (!expiryTemplate.isSet("OrderType"))
                    {
                        expiryOrder.OrderType = "MKT";
                    }
                }
                expiryOrder.GoodAfterTime = expiry.ToString("yyyyMMdd HH:mm:ss");
                expiryOrder.Action        = parent.Action.Equals("BUY") ? "SELL" : "BUY";
                expiryOrder.TotalQuantity = parent.TotalQuantity;
                //expiryOrder.OrderId = _og.get();
                expiryOrder.ParentId = parent.OrderId;
                expiryOrder.Transmit = false;
                orders.Add(expiryOrder);
            }
            if (profitTarget != 0)
            {
                Order profitOrder;
                if (profitTemplate == null)
                {
                    profitOrder           = new Order();
                    profitOrder.OrderType = "LMT";
                }
                else
                {
                    profitOrder = profitTemplate.clone();
                    if (!profitTemplate.isSet("OrderType"))
                    {
                        profitOrder.OrderType = "LMT";
                    }
                }
                profitOrder.Action        = parent.Action.Equals("BUY") ? "SELL" : "BUY";
                profitOrder.TotalQuantity = parent.TotalQuantity;
                profitOrder.LmtPrice      = profitTarget;
                //profitOrder.OrderId = _og.get();
                profitOrder.ParentId = parent.OrderId;
                profitOrder.Transmit = false;
                orders.Add(profitOrder);
            }
            if (stopTarget != 0)
            {
                Order stopOrder;
                if (stopTemplate == null)
                {
                    stopOrder           = new Order();
                    stopOrder.OrderType = "STP";
                }
                else
                {
                    stopOrder = stopTemplate.clone();
                    if (!stopTemplate.isSet("OrderType"))
                    {
                        stopOrder.OrderType = "STP";
                    }
                    if (stopOrder.OrderType == "STP LMT" && !stopTemplate.isSet("LmtPriceOffset"))
                    {
                        stopOrder.LmtPriceOffset = stopTarget * 0.1; // assume offset is 10% of stop price if not set
                    }
                }
                stopOrder.Action        = parent.Action.Equals("BUY") ? "SELL" : "BUY";
                stopOrder.TotalQuantity = parent.TotalQuantity;
                stopOrder.AuxPrice      = stopTarget;
                if (stopOrder.OrderType == "STP LMT")
                {
                    stopOrder.LmtPrice = stopTarget + stopOrder.LmtPriceOffset * (stopOrder.Action.Equals("BUY") ? 1 : -1);
                }
                //stopOrder.OrderId = _og.get();
                stopOrder.ParentId = parent.OrderId;
                stopOrder.Transmit = false;
                orders.Add(stopOrder);
            }
            orders[orders.Count() - 1].Transmit = true;
            return(orders);
        }
Beispiel #3
0
        public override void fromConfig(string config)
        {
            XmlDocument xml = new XmlDocument();

            xml.LoadXml(config);
            XmlNode xn1 = xml.SelectSingleNode("/Config");

            _model_id    = Int32.Parse(xn1["Model"].InnerText);
            _qty         = Int32.Parse(xn1["Quantity"].InnerText);
            _blockConfig = xn1["BlockOrder"];
            var tmpNode = _blockConfig["StopOrderTemplate"];

            if (tmpNode == null)
            {
                _stopTemplate = null;
            }
            else
            {
                _stopTemplate = new OrderTemplate(tmpNode);
            }

            string      model_xml = DBAccess.DBAccess.instance.getModel(_model_id);
            XmlDocument xmlm      = new XmlDocument();

            xmlm.LoadXml(model_xml);
            XmlNode xn = xmlm.SelectSingleNode("/Parameters");

            _con_id = Int32.Parse(xn["Asset"].InnerText);
            addContract(_con_id);

            string sideStr = xn["Side"].InnerText;

            _side             = sideStr == "Long" ? 1 : sideStr == "Short" ? -1 : 0;
            _trendOrReversion = xn["TrendOrReversion"].InnerText == "Trend";
            _entryStart       = TimeSpan.Parse(xn["EntryStartTime"].InnerText);
            _entryEnd         = TimeSpan.Parse(xn["EntryEndTime"].InnerText);
            _exitStart        = TimeSpan.Parse(xn["ExitStartTime"].InnerText);
            _exitEnd          = TimeSpan.Parse(xn["ExitEndTime"].InnerText);
            _expiry           = TimeSpan.Parse(xn["Expiry"].InnerText);

            _refClose = TimeSpan.Parse(xn["RefCloseTime"].InnerText);
            _useClose = Boolean.Parse(xn["UseClose"].InnerText);

            _entryOffset  = Double.Parse(xn["EntryOffset"].InnerText);
            _profitTarget = Double.Parse(xn["ProfitTarget"].InnerText);
            _stopTarget   = Double.Parse(xn["StopTarget"].InnerText);

            XmlNode _settlementNode = xn["SettlementPriceHtml"];

            _settlementUrl       = _settlementNode["Url"].InnerText;
            _settlementXPathLast = _settlementNode["XPathLast"].InnerText;
            _settlementXPathDate = _settlementNode["XPathDate"].InnerText;

            XmlNode _settlementNode2 = xn["SettlementPriceHtml2"];

            _settlementUrl2       = _settlementNode2["Url"].InnerText;
            _settlementXPathLast2 = _settlementNode2["XPathLast"].InnerText;
            _settlementXPathDate2 = _settlementNode2["XPathDate"].InnerText;

            _bufferTime = new TimeSpan(0, 0, 30); //wait for 30 secs

            //Testing only
            //_entryEnd = new TimeSpan(20, 0, 0);
            //_exitEnd = new TimeSpan(21, 0, 0);
        }