Example #1
0
        public TTOrder SendOrder(TTInstrument instrument, BuySell buySell, OrderType orderType, Quantity quantity, Price price)
        {
            TTOrder tto = null;

            OrderRoute orderRoute = instrument.OrderRoute;

            // If the order route has not been set for this instrument, see if there is
            // an OrderRouteInfo for this market (user sets using SetOrderRouteInfo method)
            if (instrument.OrderRoute == null && orderRouteInfo.ContainsKey(instrument.Market.Name))
            {
                OrderRouteInfo info          = orderRouteInfo[instrument.Market.Name];
                string         orderFeedName = info.OrderFeedName;
                AccountType    accountType   = (AccountType)Enum.Parse(typeof(AccountType), info.AccountTypeName);
                string         accountName   = info.AccountName;
                OrderFeed      feedToUse     = null;
                foreach (OrderFeed feed in instrument.EnabledOrderFeeds)
                {
                    if (feed.Name.Equals(orderFeedName))
                    {
                        feedToUse = feed;
                        break;
                    }
                }
                orderRoute = new OrderRoute(feedToUse, accountType, accountName);
            }

            OrderProfile prof = new OrderProfile(orderRoute.OrderFeed, instrument.TTAPI_Instrument);

            prof.BuySell       = buySell;
            prof.OrderType     = orderType;
            prof.OrderQuantity = quantity;
            prof.LimitPrice    = price;
            prof.AccountType   = orderRoute.AccountType;
            prof.AccountName   = orderRoute.AccountName;
            string tag = TTHelper.GetUniqueTag();

            prof.OrderTag = tag;
            instrument.TTAPI_Instrument.Session.SendOrder(prof);

            // Loop here to wait for the order to be returned via the API callbacks
            const int TIMEOUT_COUNT = 300;

            for (int i = 0; i < TIMEOUT_COUNT; i++)
            {
                if (sentOrders.ContainsKey(tag))
                {
                    tto = sentOrders[tag];
                    sentOrders.Remove(tag);
                    break;
                }
                Thread.Sleep(10);
            }

            return(tto);
        }
 void api_OnOrder(Instrument instrument, TTOrder order)
 {
     //cross thread - so you don't get the cross theading exception
     if (this.InvokeRequired)
     {
         this.BeginInvoke((MethodInvoker) delegate
         {
             api_OnOrder(instrument, order);
         });
         return;
     }
 }
Example #3
0
        void tto_OnOrderModify(TTModify modify, TTOrder order, Price price, Quantity quantity)
        {
            switch (modify)
            {
            case TTModify.Price:
                ModifyPrice(order, price);
                break;

            case TTModify.Quantity:
                ModifyQuantity(order, quantity);
                break;
            }
        }
Example #4
0
        public void ModifyQuantity(TTOrder order, Quantity quantity)
        {
            TTOrder tto = null;
            string  tag = order.Tag;

            order.TTAPI_Order.ModifyQuantity(quantity);

            // Loop here to wait for the order to be returned via the API callbacks
            const int TIMEOUT_COUNT = 300;

            for (int i = 0; i < TIMEOUT_COUNT; i++)
            {
                if (sentOrders.ContainsKey(tag))
                {
                    tto = sentOrders[tag];
                    sentOrders.Remove(tag);
                    break;
                }
                Thread.Sleep(10);
            }
        }
Example #5
0
        private TTOrder NewTTOrder(Order order)
        {
            TTOrder tto;
            string  key = order.SiteOrderKey;

            if (_ttOrders.ContainsKey(key))
            {
                tto = _ttOrders[key];
            }
            else
            {
                tto = new TTOrder(order);
                tto.OnOrderModify += new TTOrderModifyHandler(tto_OnOrderModify);
                //ttOrders.Add(key, tto);
                //Console.WriteLine("ttOrders size: {0}", ttOrders.Count);
            }
            // Initialize any TTOrder fields here:
            tto.Instrument = _ttInstruments[order.InstrumentKey];
            tto.Key        = key;
            return(tto);
        }
Example #6
0
        void processOrder(TTOrderStatus status, Order order, params Order[] orders)
        {
            TTOrder oldOrder = null;

            // If this order's instrument is not yet in our dictionary, then add the instrument
            // to our list and subscribe to the instrument updates if AutoSubscribeInstruments
            // is true.
            if (!_ttInstruments.ContainsKey(order.InstrumentKey))
            {
                if (AutoSubscribeInstruments)
                {
                    //TODO: figure out how to subscribe to instruments here
                    //SubscribeToInstrument(order.InstrumentKey);

                    /*InstrumentLookupSubscription ils = new InstrumentLookupSubscription(apiSession, Dispatcher.Current, order.InstrumentKey);
                     * ils.Update += new EventHandler<InstrumentLookupSubscriptionEventArgs>(ils_Update);
                     * ils.Start();*/
                }

                TTInstrument tti = new TTInstrument(order.InstrumentKey);
                _ttInstruments.Add(order.InstrumentKey, tti);
            }

            TTOrder tto;

            if (_workingTTOrders.ContainsKey(order.SiteOrderKey))
            {
                tto = _workingTTOrders[order.SiteOrderKey];
            }
            else
            {
                tto = NewTTOrder(order);
            }
            tto.Status = status;

            string orderKey = tto.Key;

            // Maintain our working orders
            switch (status)
            {
            case TTOrderStatus.Added:
                _workingTTOrders[orderKey] = tto;
                break;

            case TTOrderStatus.Deleted:
            case TTOrderStatus.Filled:
            case TTOrderStatus.Rejected:
                _workingTTOrders.Remove(orderKey);
                break;

            case TTOrderStatus.Updated:
                oldOrder        = tto.Clone();
                tto.TTAPI_Order = orders[0];
                //newOrder = NewTTOrder(orders[0]);
                string newOrderKey = orders[0].SiteOrderKey;
                _workingTTOrders.Remove(orderKey);
                _workingTTOrders.Add(newOrderKey, tto);
                System.Diagnostics.Debug.Assert(orderKey.Equals(newOrderKey));
                break;
            }

            // When we send an order using the SendOrder method, the method creates a
            // unique OrderTag and then blocks waiting for that order to be "created".
            // It waits for it to appear in the sentOrders dictionary with the OrderTag
            // that was generated as the key. This is the only way I could deduce to
            // be able to return a TTOrder object to the caller of SendOrder since all
            // the order sending methods appear to be asynchronous.
            if (!order.OrderTag.Equals(""))
            {
                _sentOrders[order.OrderTag] = tto;
            }

            //Console.WriteLine("Working orders size: {0}", workingTTOrders.Count);
            if (OnOrder != null)
            {
                OnOrder(status, _ttInstruments[order.InstrumentKey], tto, oldOrder);
            }
        }