Beispiel #1
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);
                }
            }
        }