private void ProcessOrderReplaceMessage(OrderReplaceMessage replaceMsg)
		{
			var condition = (SterlingOrderCondition)replaceMsg.Condition;

			var replaceOrder = new STIOrder
			{
				Account = replaceMsg.PortfolioName, 
				Quantity = (int) replaceMsg.Volume,
				Display = (int)(replaceMsg.VisibleVolume ?? replaceMsg.Volume), 
				ClOrderID = replaceMsg.TransactionId.To<string>(), 
				LmtPrice = (double) replaceMsg.Price, 
				Symbol = replaceMsg.SecurityId.SecurityCode, 
				Destination = replaceMsg.SecurityId.BoardCode, 
				Tif = replaceMsg.TimeInForce.ToSterlingTif(replaceMsg.TillDate), 
				PriceType = replaceMsg.OrderType.Value.ToSterlingPriceType(condition), 
				User = replaceMsg.Comment
			};

			if (replaceMsg.TillDate != null && replaceMsg.TillDate != DateTimeOffset.MaxValue)
				replaceOrder.EndTime = replaceMsg.TillDate.Value.ToString("yyyyMMdd");

			if (replaceMsg.Currency != null)
				replaceOrder.Currency = replaceMsg.Currency.ToString();

			replaceOrder.ReplaceOrder(0, replaceMsg.OldTransactionId.To<string>());
		}
        private void ProcessOrderReplaceMessage(OrderReplaceMessage replaceMsg)
        {
            var condition = (SterlingOrderCondition)replaceMsg.Condition;

            var replaceOrder = new STIOrder
            {
                Account     = replaceMsg.PortfolioName,
                Quantity    = (int)replaceMsg.Volume,
                Display     = (int)(replaceMsg.VisibleVolume ?? replaceMsg.Volume),
                ClOrderID   = replaceMsg.TransactionId.To <string>(),
                LmtPrice    = (double)replaceMsg.Price,
                Symbol      = replaceMsg.SecurityId.SecurityCode,
                Destination = replaceMsg.SecurityId.BoardCode,
                Tif         = replaceMsg.TimeInForce.ToSterlingTif(replaceMsg.TillDate),
                PriceType   = replaceMsg.OrderType.ToSterlingPriceType(condition),
                User        = replaceMsg.Comment
            };

            if (replaceMsg.TillDate != null && replaceMsg.TillDate != DateTimeOffset.MaxValue)
            {
                replaceOrder.EndTime = replaceMsg.TillDate.Value.ToString("yyyyMMdd");
            }

            if (replaceMsg.Currency != null)
            {
                replaceOrder.Currency = replaceMsg.Currency.ToString();
            }

            replaceOrder.ReplaceOrder(0, replaceMsg.OldTransactionId.To <string>());
        }
Esempio n. 3
0
        private void ProcessOrderRegisterMessage(OrderRegisterMessage regMsg)
        {
            var condition = (SterlingOrderCondition)regMsg.Condition;
            var orderType = regMsg.OrderType.Value;

            var order = new STIOrder
            {
                Account     = regMsg.PortfolioName,
                Quantity    = (int)regMsg.Volume,
                Display     = (int)(regMsg.VisibleVolume ?? regMsg.Volume),
                ClOrderID   = regMsg.TransactionId.To <string>(),
                LmtPrice    = (double)regMsg.Price,
                Symbol      = regMsg.SecurityId.SecurityCode,
                Destination = regMsg.SecurityId.BoardCode,
                Tif         = regMsg.TimeInForce.ToSterlingTif(regMsg.TillDate),
                PriceType   = orderType.ToSterlingPriceType(condition),
                User        = regMsg.Comment,
                Side        = regMsg.Side.ToSterlingSide()
            };

            if (regMsg.TillDate != null && regMsg.TillDate != DateTimeOffset.MaxValue)
            {
                order.EndTime = regMsg.TillDate.Value.ToString("yyyyMMdd");
            }

            if (regMsg.Currency != null)
            {
                order.Currency = regMsg.Currency.ToString();
            }

            if (orderType == OrderTypes.Conditional)
            {
                order.Discretion   = condition.Discretion.ToDouble();
                order.ExecInst     = condition.ExecutionInstruction.ToSterling();
                order.ExecBroker   = condition.ExecutionBroker;
                order.ExecPriceLmt = condition.ExecutionPriceLimit.ToDouble();
                order.PegDiff      = condition.PegDiff.ToDouble();
                order.TrailAmt     = condition.TrailingVolume.ToDouble();
                order.TrailInc     = condition.TrailingIncrement.ToDouble();
                order.StpPrice     = condition.StopPrice.ToDouble();
                order.MinQuantity  = condition.MinVolume.ToInt();
                order.AvgPriceLmt  = condition.AveragePriceLimit.ToDouble();
                order.Duration     = condition.Duration ?? 0;

                order.LocateBroker = condition.LocateBroker;
                order.LocateQty    = condition.LocateVolume.ToInt();
                order.LocateTime   = condition.LocateTime.ToSterling();

                order.OpenClose    = condition.Options.IsOpen.ToSterling();
                order.Maturity     = condition.Options.Maturity.ToSterling();
                order.PutCall      = condition.Options.Type.ToSterling();
                order.Underlying   = condition.Options.UnderlyingCode;
                order.CoverUncover = condition.Options.IsCover.ToSterling();
                order.Instrument   = condition.Options.UnderlyingType.ToSterling();
                order.StrikePrice  = condition.Options.StrikePrice.ToDouble();
            }

            order.SubmitOrder();
        }
Esempio n. 4
0
        public override int SubmitOrder()
        {
            int rs;

            stiOrder = typeMapper.Map<Order, STIOrderClass>(this);
            rs = stiOrder.SubmitOrder();

            return rs;
        }
		private void ProcessOrderRegisterMessage(OrderRegisterMessage regMsg)
		{
			var condition = (SterlingOrderCondition)regMsg.Condition;
			var orderType = regMsg.OrderType.Value;

			var order = new STIOrder
			{
				Account = regMsg.PortfolioName, 
				Quantity = (int)regMsg.Volume,
				Display = (int)(regMsg.VisibleVolume ?? regMsg.Volume), 
				ClOrderID = regMsg.TransactionId.To<string>(), 
				LmtPrice = (double)regMsg.Price, 
				Symbol = regMsg.SecurityId.SecurityCode, 
				Destination = regMsg.SecurityId.BoardCode, 
				Tif = regMsg.TimeInForce.ToSterlingTif(regMsg.TillDate), 
				PriceType = orderType.ToSterlingPriceType(condition), 
				User = regMsg.Comment, 
				Side = regMsg.Side.ToSterlingSide()
			};

			if (regMsg.TillDate != null && regMsg.TillDate != DateTimeOffset.MaxValue)
				order.EndTime = regMsg.TillDate.Value.ToString("yyyyMMdd");

			if (regMsg.Currency != null)
				order.Currency = regMsg.Currency.ToString();

			if (orderType == OrderTypes.Conditional)
			{
				order.Discretion = condition.Discretion.ToDouble();
				order.ExecInst = condition.ExecutionInstruction.ToSterling();
				order.ExecBroker = condition.ExecutionBroker;
				order.ExecPriceLmt = condition.ExecutionPriceLimit.ToDouble();
				order.PegDiff = condition.PegDiff.ToDouble();
				order.TrailAmt = condition.TrailingVolume.ToDouble();
				order.TrailInc = condition.TrailingIncrement.ToDouble();
				order.StpPrice = condition.StopPrice.ToDouble();
				order.MinQuantity = condition.MinVolume.ToInt();
				order.AvgPriceLmt = condition.AveragePriceLimit.ToDouble();
				order.Duration = condition.Duration ?? 0;

				order.LocateBroker = condition.LocateBroker;
				order.LocateQty = condition.LocateVolume.ToInt();
				order.LocateTime = condition.LocateTime.ToSterling();

				order.OpenClose = condition.Options.IsOpen.ToSterling();
				order.Maturity = condition.Options.Maturity.ToSterling();
				order.PutCall = condition.Options.Type.ToSterling();
				order.Underlying = condition.Options.UnderlyingCode;
				order.CoverUncover = condition.Options.IsCover.ToSterling();
				order.Instrument = condition.Options.UnderlyingType.ToSterling();
				order.StrikePrice = condition.Options.StrikePrice.ToDouble();
			}

			order.SubmitOrder();
		}
Esempio n. 6
0
        public override int SubmitOrder(ref OrderModel orderM)
        {
            int rs;

            stiOrder = typeMapper.Map<Order, STIOrderClass>(this);
            var sctSTIOrder = typeMapper.Map<OrderModel, structSTIOrder>(orderM);
            rs = stiOrder.SubmitOrderStruct(ref sctSTIOrder);
            orderM = typeMapper.Map<structSTIOrder, OrderModel>(sctSTIOrder);

            return rs;
        }
Esempio n. 7
0
        void background(object param)
        {
            while (_runbg)
            {
                try
                {
                    // orders
                    while (!_orderq.isEmpty)
                    {
                        STIOrder order = new STIOrder();
                        Order o = _orderq.Read();
                        if (VerboseDebugging)
                            debug("client order received: " + o.ToString());
                        if ((o.id == 0) && AutosetUnsetId)
                        {
                            o.id = _idt.AssignId;
                        }
                        if (isPaperTradeEnabled)
                        {
                            ptt.sendorder(o);
                        }
                        else
                        {
                            o.price = Math.Round(o.price, FixOrderDecimalPlace);
                            o.stopp = Math.Round(o.stopp, FixOrderDecimalPlace);

                            if (o.ex == string.Empty)
                                o.ex = o.symbol.Length > 3 ? "NSDQ" : "NYSE";
                            order.Destination = o.Exchange;
                            order.Side = getside(o.symbol, o.side);
                            order.Symbol = o.symbol;
                            order.Quantity = o.UnsignedSize;
                            string acct = Account != string.Empty ? Account : string.Empty;
                            order.Account = o.Account != string.Empty ? o.Account : acct;
                            order.Destination = o.Exchange != "" ? o.ex : "NYSE";
                            bool close = o.ValidInstruct == OrderInstructionType.MOC;
                            bool pegged = (o.ValidInstruct >= OrderInstructionType.PEG2MID) && (o.ValidInstruct <= OrderInstructionType.PEG2BST);
                            order.Tif = tif2tif(o.TIF);
                            if (!pegged)
                            {
                                order.LmtPrice = (double)o.price;
                                order.StpPrice = (double)o.stopp;
                            }
                            if (close)
                            {
                                if (o.isMarket)
                                    order.PriceType = STIPriceTypes.ptSTIMktClo;
                                else if (o.isLimit)
                                    order.PriceType = STIPriceTypes.ptSTILmtClo;
                                else
                                    order.PriceType = STIPriceTypes.ptSTIClo;
                            }
                            else if (pegged)
                            {
                                order.PriceType = STIPriceTypes.ptSTIPegged;
                                if (o.price<=0)
                                    order.PegDiff = (double)o.price;
                                else
                                    order.LmtPrice = (double)o.price;
                                if (o.ValidInstruct== OrderInstructionType.PEG2BST)
                                    order.ExecInst = "T";
                                else if (o.ValidInstruct== OrderInstructionType.PEG2MID)
                                    order.ExecInst = "M";
                                else if (o.ValidInstruct== OrderInstructionType.PEG2MKT)
                                    order.ExecInst = "P";
                                else if (o.ValidInstruct== OrderInstructionType.PEG2PRI)
                                    order.ExecInst = "R";
                            }
                            else if (o.isMarket)
                                order.PriceType = STIPriceTypes.ptSTIMkt;
                            else if (o.isLimit && o.isStop)
                                order.PriceType = STIPriceTypes.ptSTISvrStpLmt;
                            else if (o.isLimit)
                                order.PriceType = STIPriceTypes.ptSTILmt;
                            else if (o.isStop)
                                order.PriceType = STIPriceTypes.ptSTISvrStp;
                            else if (o.isTrail)
                                order.PriceType = STIPriceTypes.ptSTITrailStp;
                            order.ClOrderID = o.id.ToString();
                            int err = order.SubmitOrder();
                            if (VerboseDebugging)
                                debug("client order sent: " + order.ClOrderID);
                            string tmp = "";
                            if ((err == 0) && (!idacct.TryGetValue(o.id, out tmp)))
                            {
                                // save account/id relationship for canceling
                                idacct.Add(o.id, order.Account);
                                // wait briefly between orders
                                Thread.Sleep(_ORDERSLEEP);
                            }
                            if (err < 0)
                                debug("Error sending order: " + Util.PrettyError(tl.newProviderName, err) + o.ToString());
                            if (err == -1)
                                debug("Make sure you have set the account in sending program.");
                        }
                    }

                    // new quotes
                    if (!_symsq.isEmpty)
                    {
                        _symsq.Read();
                        foreach (string sym in symquotes.Split(','))
                        {
                            stiQuote.RegisterQuote(sym, "*");
                        }
                    }
                    // old quotes
                    while (removesym.hasItems)
                    {
                        string rem = removesym.Read();
                        stiQuote.DeRegisterQuote(rem, "*");
                    }

                    // cancels
                    if (!_cancelq.isEmpty)
                    {
                        long number = _cancelq.Read();
                        if (isPaperTradeEnabled)
                        {
                            ptt.sendcancel(number);
                        }
                        else
                        {
                            string acct = "";
                            if (idacct.TryGetValue(number, out acct))
                            {
                                // get unique cancel id
                                long cancelid = _canceltracker.AssignId;
                                // save cancel to order id relationship
                                _cancel2order.Add(cancelid, number);
                                bool isman;
                                // see if it's a manual order
                                if (!ismanorder.TryGetValue(number, out isman))
                                    isman = false;
                                // send cancel
                                if (isman) // manual orders use nOrderRercordId
                                    stiOrder.CancelOrder(acct, (int)number, null, cancelid.ToString());
                                else
                                    stiOrder.CancelOrder(acct, 0, number.ToString(), cancelid.ToString());
                                if (VerboseDebugging)
                                    debug("client cancel requested: " + number.ToString() + " " + cancelid.ToString());
                            }
                            else
                                debug("No record of order id: " + number.ToString());
                            // see if empty yet
                            if (_cancelq.hasItems)
                                Thread.Sleep(_CANCELWAIT);
                        }
                    }

                    // messages
                    if (_msgq.hasItems)
                    {
                        GenericMessage gm = _msgq.Read();
                        switch (gm.Type)
                        {
                            case MessageTypes.SENDORDERPEGMIDPOINT:
                                {
                                    // create order
                                    STIOrder order = new STIOrder();
                                    // pegged 2 midmarket
                                    order.ExecInst = "M";
                                    // get order
                                    Peg2Midpoint o = Peg2Midpoint.Deserialize(gm.Request);
                                    if (!o.isValid) break;
                                    if (VerboseDebugging)
                                        debug("client P2M order: " + o.ToString());
                                    order.Symbol = o.symbol;
                                    order.PegDiff = (double)o.pegdiff;
                                    order.PriceType = STIPriceTypes.ptSTIPegged;
                                    bool side = o.size > 0;
                                    order.Side = getside(o.symbol, side);
                                    order.Quantity = Math.Abs(o.size);
                                    order.Destination = o.ex;
                                    order.ClOrderID = o.id.ToString();
                                    order.Tif = "D";
                                    string acct = Account != string.Empty ? Account : string.Empty;
                                    order.Account = o.Account != string.Empty ? o.Account : acct;
                                    int err = order.SubmitOrder();
                                    string tmp = "";
                                    if ((err == 0) && (!idacct.TryGetValue(o.id, out tmp)))
                                        idacct.Add(o.id, order.Account);
                                    if (err < 0)
                                        debug("Error sending order: " + Util.PrettyError(tl.newProviderName, err) + o.ToString());
                                    if (err == -1)
                                        debug("Make sure you have set the account in sending program.");

                                }
                                break;
                        }
                    }

                    if (_lastimbalance != _imbalance)
                    {
                        _lastimbalance = _imbalance;
                        // register for imbalance data
                        stiQuote.RegisterForAllMdx(true);
                    }
                }
                catch (Exception ex)
                {
                    debug(ex.Message + ex.StackTrace);
                }
                if (_symsq.isEmpty && _orderq.isEmpty && _cancelq.isEmpty)
                    Thread.Sleep(_SLEEP);
            }
        }
Esempio n. 8
0
        void tt_Tick(object sender, EventArgs e)
        {
            // orders
            while (or < oc)
            {
                STIOrder order = new STIOrder();
                if (or == oq.Length)
                {
                    or = 0;
                }
                Order o = oq[or++];
                order.LmtPrice    = (double)o.price;
                order.StpPrice    = (double)o.stopp;
                order.Destination = o.Exchange;
                order.Side        = o.side ? "B" : "S";
                order.Symbol      = o.symbol;
                order.Quantity    = o.UnsignedSize;
                string acct = accts.Count > 0 ? accts[0] : "";
                order.Account     = o.Account != "" ? o.Account : acct;
                order.Destination = o.Exchange != "" ? o.ex : "NYSE";
                order.Tif         = o.TIF;
                order.PriceType   = o.isMarket ? STIPriceTypes.ptSTIMkt : (o.isLimit ? STIPriceTypes.ptSTILmt : STIPriceTypes.ptSTISvrStp);
                order.ClOrderID   = o.id.ToString();
                int    err = order.SubmitOrder();
                string tmp = "";
                if ((err == 0) && (!idacct.TryGetValue(o.id, out tmp)))
                {
                    idacct.Add(o.id, order.Account);
                }
                if (err < 0)
                {
                    debug("Error sending order: " + Util.PrettyError(tl.newProviderName, err) + o.ToString());
                }
                if (err == -1)
                {
                    debug("Make sure you have set the account in sending program.");
                }
            }

            // quotes
            if (qc > qr)
            {
                foreach (string sym in symquotes.Split(','))
                {
                    stiQuote.RegisterQuote(sym, "*");
                }
                qr = qc;
            }

            // cancels
            if (ic > ir)
            {
                if (ir == idq.Length)
                {
                    ir = 0;
                }
                uint   number = idq[ir++];
                string acct   = "";
                if (idacct.TryGetValue(number, out acct))
                {
                    stiOrder.CancelOrder(acct, 0, number.ToString(), DateTime.Now.Ticks.ToString() + acct);
                    tl.newOrderCancel(number);
                }
                else
                {
                    debug("No record of id: " + number.ToString());
                }
            }
        }
Esempio n. 9
0
        void background(object param)
        {
            while (_runbg)
            {
                try
                {
                    // new quotes
                    if (!_symsq.isEmpty)
                    {
                        _symsq.Read();
                        // clear index
                        ssym2longsymidx.Clear();
                        for (int i = 0; i<syms.Length; i++)
                        {
                            string sym = syms[i];
                            // get security
                            Security sec = SecurityImpl.Parse(sym);
                            if (sec.hasDest)
                                stiQuote.RegisterQuote(sec.Symbol_Spaces,sec.DestEx);
                            else if (sec.Type== SecurityType.OPT)
                                stiQuote.RegisterQuote(sec.Symbol_Spaces, "O");
                            else
                                stiQuote.RegisterQuote(sec.Symbol_Spaces, "*");
                            // save relationship
                            if (!ssym2longsymidx.ContainsKey(sec.Symbol_Spaces))
                                ssym2longsymidx.Add(sec.Symbol_Spaces, i);
                            else
                            {
                                v(sec.Symbol_Spaces + " replacing index: " + ssym2longsymidx[sec.Symbol_Spaces] + " with: " + i);
                                ssym2longsymidx[sec.Symbol_Spaces] = i;
                            }

                        }
                        // wait moment for quotes to load
                        Thread.Sleep(PostSymSubscribeWait);
                    }

                    // orders
                    while (!_orderq.isEmpty)
                    {
                        STIOrder order = new STIOrder();
                        Order o = _orderq.Read();
                        if (VerboseDebugging)
                            debug("client order received: " + o.ToString());
                        if ((o.id == 0) && AutosetUnsetId)
                        {
                            o.id = _idt.AssignId;
                        }
                        if (isPaperTradeEnabled)
                        {
                            ptt.sendorder(o);
                        }
                        else
                        {
                            o.price = Math.Round(o.price, FixOrderDecimalPlace);
                            o.stopp = Math.Round(o.stopp, FixOrderDecimalPlace);

                            if (o.ex == string.Empty)
                                o.ex = o.symbol.Length > 3 ? "NSDQ" : "NYSE";
                            order.Destination = o.Exchange;
                            order.Side = getside(o);
                            Security sec = SecurityImpl.Parse(o.symbol);
                            order.Instrument = getinstrument(sec);
                            order.Symbol = sec.Symbol_Spaces;
                            order.Quantity = o.UnsignedSize;
                            string acct = Account != string.Empty ? Account : string.Empty;
                            order.Account = o.Account != string.Empty ? o.Account : acct;
                            order.Destination = o.Exchange != "" ? o.ex : "NYSE";
                            bool close = o.ValidInstruct == OrderInstructionType.MOC;
                            bool pegged = (o.ValidInstruct >= OrderInstructionType.PEG2MID) && (o.ValidInstruct <= OrderInstructionType.PEG2BST);
                            order.Tif = tif2tif(o.TIF);
                            if (!pegged)
                            {
                                order.LmtPrice = (double)o.price;
                                order.StpPrice = (double)o.stopp;
                            }
                            if (close)
                            {
                                if (o.isMarket)
                                    order.PriceType = STIPriceTypes.ptSTIMktClo;
                                else if (o.isLimit)
                                    order.PriceType = STIPriceTypes.ptSTILmtClo;
                                else
                                    order.PriceType = STIPriceTypes.ptSTIClo;
                            }
                            else if (pegged)
                            {
                                order.PriceType = STIPriceTypes.ptSTIPegged;
                                if (o.price<=0)
                                    order.PegDiff = (double)o.price;
                                else
                                    order.LmtPrice = (double)o.price;
                                if (o.ValidInstruct== OrderInstructionType.PEG2BST)
                                    order.ExecInst = "T";
                                else if (o.ValidInstruct== OrderInstructionType.PEG2MID)
                                    order.ExecInst = "M";
                                else if (o.ValidInstruct== OrderInstructionType.PEG2MKT)
                                    order.ExecInst = "P";
                                else if (o.ValidInstruct== OrderInstructionType.PEG2PRI)
                                    order.ExecInst = "R";
                            }
                            else if (o.isMarket)
                                order.PriceType = STIPriceTypes.ptSTIMkt;
                            else if (o.isLimit && o.isStop)
                            {
                                if (UseServerStops)
                                    order.PriceType = STIPriceTypes.ptSTISvrStpLmt;
                                else
                                    order.PriceType = STIPriceTypes.ptSTILmtStp;
                            }
                            else if (o.isLimit)
                                order.PriceType = STIPriceTypes.ptSTILmt;
                            else if (o.isStop)
                            {
                                if (UseServerStops)
                                    order.PriceType = STIPriceTypes.ptSTISvrStp;
                                else
                                {
                                    order.PriceType = STIPriceTypes.ptSTILmtStp;
                                }
                            }
                            else if (o.isTrail)
                                order.PriceType = STIPriceTypes.ptSTITrailStp;
                            // sterling options need some information provided twice
                            if (sec.Type == SecurityType.OPT)
                            {
                                if (SecurityImpl.ParseOptionOSI(sec.Symbol_Spaces, ref sec, v))
                                {
                                    order.Maturity = sec.Date.ToString();
                                    order.PutCall = sec.isCall ? "C" : "P";
                                    order.StrikePrice = (double)sec.Strike;
                                    order.Underlying = sec.Symbol;
                                    string symbol = order.Symbol;
                                    // if we're flat or going with our side, it's an open
                                    if (pt[symbol, o.Account].isFlat || (pt[symbol, o.Account].isLong == o.side))
                                    {
                                        v(o.symbol + " marking option order as open: " + o.ToString() + " from pos: " + pt[symbol, o.Account]);
                                        order.OpenClose = "O";
                                    }
                                    // if going against our side, it's a close
                                    else if (!pt[symbol, o.Account].isFlat && (pt[symbol, o.Account].isLong == o.side))
                                    {
                                        v(o.symbol + " marking option order as close: " + o.ToString() + " from pos: " + pt[symbol, o.Account]);
                                        order.OpenClose = "C";
                                    }
                                }
                                else
                                    debug("error parsing option osi: " + o.symbol + " on order: " + o.ToString());
                            }
                            order.ClOrderID = o.id.ToString();
                            int err = order.SubmitOrder();
                            if (VerboseDebugging)
                                debug("client order sent: " + order.ClOrderID);
                            string tmp = "";
                            if ((err == 0) && (!idacct.TryGetValue(o.id, out tmp)))
                            {
                                // save account/id relationship for canceling
                                idacct.Add(o.id, order.Account);
                                // wait briefly between orders
                                Thread.Sleep(_ORDERSLEEP);
                            }
                            // if order was sent and we're following short regulations, start tracking this order
                            if ((err == 0) && RegSHOShorts)
                            {
                                // keep track that we sent the order
                                sho.GotOrder(o);
                            }
                            if (err < 0)
                            {
                                debug("Error sending order: " + Util.PrettyError(tl.newProviderName, err) + o.ToString());
                                if (err == -1)
                                    debug("Make sure you have set the account in sending program.");
                                if (SendCancelOnError)
                                {
                                    v("sending cancel ack for error on order id: " + o.id);
                                    newcancel(o.id);
                                }

                            }
                        }
                    }

                    
                    // old quotes
                    while (removesym.hasItems)
                    {
                        string rem = removesym.Read();
                        stiQuote.DeRegisterQuote(rem, "*");
                    }

                    // cancels
                    if (!_cancelq.isEmpty)
                    {
                        long number = _cancelq.Read();
                        if (isPaperTradeEnabled)
                        {
                            ptt.sendcancel(number);
                        }
                        else
                        {
                            string acct = "";
                            if (idacct.TryGetValue(number, out acct))
                            {
                                // get unique cancel id
                                long cancelid = _idt.AssignId;
                                // save cancel to order id relationship
                                _cancel2order.Add(cancelid, number);
                                bool isman;
                                // see if it's a manual order
                                if (!ismanorder.TryGetValue(number, out isman))
                                    isman = false;
                                // send cancel
                                if (isman) // manual orders use nOrderRercordId
                                    stiOrder.CancelOrder(acct, (int)number, null, cancelid.ToString());
                                else
                                    stiOrder.CancelOrder(acct, 0, number.ToString(), cancelid.ToString());
                                if (VerboseDebugging)
                                    debug("client cancel requested: " + number.ToString() + " " + cancelid.ToString());
                            }
                            else
                                debug("No record of order id: " + number.ToString());
                            // see if empty yet
                            if (_cancelq.hasItems)
                                Thread.Sleep(_CANCELWAIT);
                        }
                    }

                    // messages
                    if (_msgq.hasItems)
                    {
                        GenericMessage gm = _msgq.Read();
                        switch (gm.Type)
                        {
                            case MessageTypes.SENDORDERPEGMIDPOINT:
                                {
                                    // create order
                                    STIOrder order = new STIOrder();
                                    // pegged 2 midmarket
                                    order.ExecInst = "M";
                                    // get order
                                    Peg2Midpoint o = Peg2Midpoint.Deserialize(gm.Request);
                                    if (!o.isValid) break;
                                    if (VerboseDebugging)
                                        debug("client P2M order: " + o.ToString());
                                    order.Symbol = o.symbol;
                                    order.PegDiff = (double)o.pegdiff;
                                    order.PriceType = STIPriceTypes.ptSTIPegged;
                                    bool side = o.size > 0;
                                    order.Side = getside(order.Symbol, side);
                                    order.Quantity = Math.Abs(o.size);
                                    order.Destination = o.ex;
                                    order.ClOrderID = o.id.ToString();
                                    order.Tif = "D";
                                    string acct = Account != string.Empty ? Account : string.Empty;
                                    order.Account = o.Account != string.Empty ? o.Account : acct;
                                    int err = order.SubmitOrder();
                                    string tmp = "";
                                    if ((err == 0) && (!idacct.TryGetValue(o.id, out tmp)))
                                        idacct.Add(o.id, order.Account);
                                    if (err < 0)
                                        debug("Error sending order: " + Util.PrettyError(tl.newProviderName, err) + o.ToString());
                                    if (err == -1)
                                        debug("Make sure you have set the account in sending program.");

                                }
                                break;
                        }
                    }

                    if (_lastimbalance != _imbalance)
                    {
                        _lastimbalance = _imbalance;
                        // register for imbalance data
                        stiQuote.RegisterForAllMdx(true);
                    }
                }
                catch (Exception ex)
                {
                    debug(ex.Message + ex.StackTrace);
                }
                if (_symsq.isEmpty && _orderq.isEmpty && _cancelq.isEmpty)
                    Thread.Sleep(_SLEEP);
            }
        }
Esempio n. 10
0
        void background(object param)
        {
            while (_runbg)
            {
                try
                {
                    // orders
                    while (!_orderq.isEmpty)
                    {
                        STIOrder order = new STIOrder();
                        Order    o     = _orderq.Read();
                        if (VerboseDebugging)
                        {
                            debug("client order received: " + o.ToString());
                        }
                        if ((o.id == 0) && AutosetUnsetId)
                        {
                            o.id = _idt.AssignId;
                        }
                        if (isPaperTradeEnabled)
                        {
                            ptt.sendorder(o);
                        }
                        else
                        {
                            o.price = Math.Round(o.price, FixOrderDecimalPlace);
                            o.stopp = Math.Round(o.stopp, FixOrderDecimalPlace);

                            if (o.ex == string.Empty)
                            {
                                o.ex = o.symbol.Length > 3 ? "NSDQ" : "NYSE";
                            }
                            order.Destination = o.Exchange;
                            order.Side        = getside(o.symbol, o.side);
                            order.Symbol      = o.symbol;
                            order.Quantity    = o.UnsignedSize;
                            string acct = Account != string.Empty ? Account : string.Empty;
                            order.Account     = o.Account != string.Empty ? o.Account : acct;
                            order.Destination = o.Exchange != "" ? o.ex : "NYSE";
                            bool close  = o.ValidInstruct == OrderInstructionType.MOC;
                            bool pegged = (o.ValidInstruct >= OrderInstructionType.PEG2MID) && (o.ValidInstruct <= OrderInstructionType.PEG2BST);
                            order.Tif = tif2tif(o.TIF);
                            if (!pegged)
                            {
                                order.LmtPrice = (double)o.price;
                                order.StpPrice = (double)o.stopp;
                            }
                            if (close)
                            {
                                if (o.isMarket)
                                {
                                    order.PriceType = STIPriceTypes.ptSTIMktClo;
                                }
                                else if (o.isLimit)
                                {
                                    order.PriceType = STIPriceTypes.ptSTILmtClo;
                                }
                                else
                                {
                                    order.PriceType = STIPriceTypes.ptSTIClo;
                                }
                            }
                            else if (pegged)
                            {
                                order.PriceType = STIPriceTypes.ptSTIPegged;
                                if (o.price <= 0)
                                {
                                    order.PegDiff = (double)o.price;
                                }
                                else
                                {
                                    order.LmtPrice = (double)o.price;
                                }
                                if (o.ValidInstruct == OrderInstructionType.PEG2BST)
                                {
                                    order.ExecInst = "T";
                                }
                                else if (o.ValidInstruct == OrderInstructionType.PEG2MID)
                                {
                                    order.ExecInst = "M";
                                }
                                else if (o.ValidInstruct == OrderInstructionType.PEG2MKT)
                                {
                                    order.ExecInst = "P";
                                }
                                else if (o.ValidInstruct == OrderInstructionType.PEG2PRI)
                                {
                                    order.ExecInst = "R";
                                }
                            }
                            else if (o.isMarket)
                            {
                                order.PriceType = STIPriceTypes.ptSTIMkt;
                            }
                            else if (o.isLimit && o.isStop)
                            {
                                order.PriceType = STIPriceTypes.ptSTISvrStpLmt;
                            }
                            else if (o.isLimit)
                            {
                                order.PriceType = STIPriceTypes.ptSTILmt;
                            }
                            else if (o.isStop)
                            {
                                order.PriceType = STIPriceTypes.ptSTISvrStp;
                            }
                            else if (o.isTrail)
                            {
                                order.PriceType = STIPriceTypes.ptSTITrailStp;
                            }
                            order.ClOrderID = o.id.ToString();
                            int err = order.SubmitOrder();
                            if (VerboseDebugging)
                            {
                                debug("client order sent: " + order.ClOrderID);
                            }
                            string tmp = "";
                            if ((err == 0) && (!idacct.TryGetValue(o.id, out tmp)))
                            {
                                // save account/id relationship for canceling
                                idacct.Add(o.id, order.Account);
                                // wait briefly between orders
                                Thread.Sleep(_ORDERSLEEP);
                            }
                            if (err < 0)
                            {
                                debug("Error sending order: " + Util.PrettyError(tl.newProviderName, err) + o.ToString());
                            }
                            if (err == -1)
                            {
                                debug("Make sure you have set the account in sending program.");
                            }
                        }
                    }

                    // new quotes
                    if (!_symsq.isEmpty)
                    {
                        _symsq.Read();
                        foreach (string sym in symquotes.Split(','))
                        {
                            stiQuote.RegisterQuote(sym, "*");
                        }
                    }
                    // old quotes
                    while (removesym.hasItems)
                    {
                        string rem = removesym.Read();
                        stiQuote.DeRegisterQuote(rem, "*");
                    }

                    // cancels
                    if (!_cancelq.isEmpty)
                    {
                        long number = _cancelq.Read();
                        if (isPaperTradeEnabled)
                        {
                            ptt.sendcancel(number);
                        }
                        else
                        {
                            string acct = "";
                            if (idacct.TryGetValue(number, out acct))
                            {
                                // get unique cancel id
                                long cancelid = _canceltracker.AssignId;
                                // save cancel to order id relationship
                                _cancel2order.Add(cancelid, number);
                                bool isman;
                                // see if it's a manual order
                                if (!ismanorder.TryGetValue(number, out isman))
                                {
                                    isman = false;
                                }
                                // send cancel
                                if (isman) // manual orders use nOrderRercordId
                                {
                                    stiOrder.CancelOrder(acct, (int)number, null, cancelid.ToString());
                                }
                                else
                                {
                                    stiOrder.CancelOrder(acct, 0, number.ToString(), cancelid.ToString());
                                }
                                if (VerboseDebugging)
                                {
                                    debug("client cancel requested: " + number.ToString() + " " + cancelid.ToString());
                                }
                            }
                            else
                            {
                                debug("No record of order id: " + number.ToString());
                            }
                            // see if empty yet
                            if (_cancelq.hasItems)
                            {
                                Thread.Sleep(_CANCELWAIT);
                            }
                        }
                    }

                    // messages
                    if (_msgq.hasItems)
                    {
                        GenericMessage gm = _msgq.Read();
                        switch (gm.Type)
                        {
                        case MessageTypes.SENDORDERPEGMIDPOINT:
                        {
                            // create order
                            STIOrder order = new STIOrder();
                            // pegged 2 midmarket
                            order.ExecInst = "M";
                            // get order
                            Peg2Midpoint o = Peg2Midpoint.Deserialize(gm.Request);
                            if (!o.isValid)
                            {
                                break;
                            }
                            if (VerboseDebugging)
                            {
                                debug("client P2M order: " + o.ToString());
                            }
                            order.Symbol    = o.symbol;
                            order.PegDiff   = (double)o.pegdiff;
                            order.PriceType = STIPriceTypes.ptSTIPegged;
                            bool side = o.size > 0;
                            order.Side        = getside(o.symbol, side);
                            order.Quantity    = Math.Abs(o.size);
                            order.Destination = o.ex;
                            order.ClOrderID   = o.id.ToString();
                            order.Tif         = "D";
                            string acct = Account != string.Empty ? Account : string.Empty;
                            order.Account = o.Account != string.Empty ? o.Account : acct;
                            int    err = order.SubmitOrder();
                            string tmp = "";
                            if ((err == 0) && (!idacct.TryGetValue(o.id, out tmp)))
                            {
                                idacct.Add(o.id, order.Account);
                            }
                            if (err < 0)
                            {
                                debug("Error sending order: " + Util.PrettyError(tl.newProviderName, err) + o.ToString());
                            }
                            if (err == -1)
                            {
                                debug("Make sure you have set the account in sending program.");
                            }
                        }
                        break;
                        }
                    }

                    if (_lastimbalance != _imbalance)
                    {
                        _lastimbalance = _imbalance;
                        // register for imbalance data
                        stiQuote.RegisterForAllMdx(true);
                    }
                }
                catch (Exception ex)
                {
                    debug(ex.Message + ex.StackTrace);
                }
                if (_symsq.isEmpty && _orderq.isEmpty && _cancelq.isEmpty)
                {
                    Thread.Sleep(_SLEEP);
                }
            }
        }
Esempio n. 11
0
        void tt_Tick(object sender, EventArgs e)
        {
            // orders
            while (or < oc)
            {
                STIOrder order = new STIOrder();
                if (or == oq.Length) or = 0;
                Order o = oq[or++];
                order.LmtPrice = (double)o.price;
                order.StpPrice = (double)o.stopp;
                order.Destination = o.Exchange;
                order.Side = o.side ? "B" : "S";
                order.Symbol = o.symbol;
                order.Quantity = o.UnsignedSize;
                string acct = accts.Count>0 ? accts[0] : "";
                order.Account = o.Account != "" ? o.Account : acct;
                order.Destination = o.Exchange != "" ? o.ex : "NYSE";
                order.Tif = o.TIF;
                order.PriceType = o.isMarket ? STIPriceTypes.ptSTIMkt : (o.isLimit ? STIPriceTypes.ptSTILmt : STIPriceTypes.ptSTISvrStp);
                order.ClOrderID = o.id.ToString();
                int err = order.SubmitOrder();
                string tmp = "";
                if ((err==0) && (!idacct.TryGetValue(o.id, out tmp)))
                    idacct.Add(o.id,order.Account);
                if (err < 0)
                    debug("Error sending order: " + Util.PrettyError(tl.newProviderName, err) + o.ToString());
                if (err == -1)
                    debug("Make sure you have set the account in sending program.");
            }

            // quotes
            if (qc > qr)
            {
                foreach (string sym in symquotes.Split(','))
                    stiQuote.RegisterQuote(sym, "*");
                qr = qc;
            }

            // cancels
            if (ic > ir)
            {
                if (ir == idq.Length) ir = 0;
                uint number = idq[ir++];
                string acct = "";
                if (idacct.TryGetValue(number, out acct))
                {
                    stiOrder.CancelOrder(acct, 0, number.ToString(), DateTime.Now.Ticks.ToString() + acct);
                    tl.newOrderCancel(number);
                }
                else
                    debug("No record of id: " + number.ToString());
            }


            
        }