コード例 #1
0
ファイル: KTASimulator.cs プロジェクト: junwin/TradingTools
        /// <summary>
        /// FIX loop back order 
        /// </summary>
        /// <param name="msg"></param>
        public void submitOrder(KaiTrade.Interfaces.IMessage myMsg)
        {
            KaiTrade.Interfaces.ISubmitRequest nos = null;
            try
            {
                nos = JsonConvert.DeserializeObject<K2DataObjects.SubmitRequest>(myMsg.Data);

                log.Error("SUBTEST:" + myMsg.Data);

                long quantity = nos.OrderQty;
                decimal myOrdPrice = nos.Price.Value;

                // Get the we want to order
                string myMnemonic = nos.Mnemonic;

                if (nos.SecurityID != null)
                {
                    if (nos.SecurityID.Length > 0)
                    {
                        // is this new market processing?
                        if (m_Markets.ContainsKey(nos.SecurityID))
                        {
                            m_Markets[nos.SecurityID].submitOrder(myMsg);
                            return;
                        }
                    }
                }

                SimulatorProduct myProd = getProduct( myMnemonic);

                if (myProd == null)
                {
                    // we dont simulate this so reject it
                    string myError = "Product not available";
                    Exception myE = new Exception(myError);
                    throw myE;
                }

                // simulate rejecting certail accounts
                if (nos.Account.Length>0)
                {
                    if (nos.Account == "TestBadAccount")
                    {
                        // we dont simulate this so reject it
                        string myError = "Account not valid:" + nos.Account;
                        Exception myE = new Exception(myError);
                        throw myE;
                    }
                }

                // put this into the internal book
                // send order in book exec report
                KaiTrade.Interfaces.IFill fill = new K2DataObjects.Fill();
                fill.OrderStatus = KaiTrade.Interfaces.OrderStatus.NEW;
                fill.ExecType = KaiTrade.Interfaces.ExecType.ORDER_STATUS;
                fill.OrderID = DriverBase.Identities.Instance.getNextOrderID();
                //sendExecReport(string orderID, string status, string execType, double lastQty, int leavesQty,int cumQty, double lastPx, double avePx, string text, string ordRejReason)

                if (myProd.IsAutoFill)
                {
                    DriverBase.OrderContext myContext = new DriverBase.OrderContext();
                    //myContext.QFOrder = myOrder;
                    myContext.ClOrdID = nos.ClOrdID;
                    myContext.OrderID = fill.OrderID;
                    myContext.OrderQty =(int) quantity;
                    //myContext.LeavesQty = quantity;
                    myContext.CumQty = 0;
                    // record the order in the context maps
                    RecordOrderContext(myContext.ClOrdID, myContext);
                    //m_OrderContextClOrdID.Add(myContext.ClOrdID, myContext);
                    m_OrderContextOrdID.Add(myContext.OrderID, myContext);

                    sendExecReport(myContext, fill.OrderID, fill.OrderStatus, fill.ExecType, 0.0, (int)quantity, 0, (double)myOrdPrice, (double)myOrdPrice, "", "");
                    if ((nos.OrdType == KaiTrade.Interfaces.OrderType.STOP) || (nos.OrdType == KaiTrade.Interfaces.OrderType.STOPLIMIT))
                    {
                        // Just do the default
                    }
                    else
                    {

                        // is this for auto fill over time?
                        if (nos.Mnemonic == "HPQ")
                        {
                            myContext.FillPattern = DriverBase.fillPattern.gradual;
                            myContext.UpdatePeriod = 1;
                            m_FillList.Add(myContext);
                        }
                        // is this for auto fill over time?
                        if (nos.Mnemonic == "VOD")
                        {
                            myContext.FillPattern = DriverBase.fillPattern.fixedAmount;
                            int fillAmount =(int) ( (double)quantity * 0.5);
                            if (fillAmount > 0)
                            {
                                myContext.TargetFillAmount = fillAmount;
                            }
                            else
                            {
                                myContext.TargetFillAmount = 1;
                            }

                            myContext.UpdatePeriod = 12;
                            myContext.TimerCount = 1;
                            m_FillList.Add(myContext);
                        }
                    }
                }
                else
                {
                    // put this into the internal book
                    // send order in book exec report
                    if (myOrdPrice == (decimal)0.0)
                    {
                        myOrdPrice = (decimal) this.getTradePx(myMnemonic);
                    }

                    // do nothing for now just let is stay working
                    DriverBase.OrderContext myContext = new DriverBase.OrderContext();
                    //myContext.QFOrder = myOrder;
                    myContext.ClOrdID = nos.ClOrdID;
                    myContext.OrderID = fill.OrderID;
                    myContext.OrderQty = (int)nos.OrderQty;
                    //myContext.LeavesQty = quantity;
                    myContext.CumQty = 0;

                    // record the order in the context maps
                    RecordOrderContext(myContext.ClOrdID, myContext);
                    //m_OrderContextClOrdID.Add(myContext.ClOrdID, myContext);
                    m_OrderContextOrdID.Add(myContext.OrderID, myContext);

                    sendExecReport(myContext, fill.OrderID, fill.OrderStatus, fill.ExecType, 0.0, (int)nos.OrderQty, 0, 0.0, 0.0,"","");

                    // do some simple STOP processing
                    if ((nos.OrdType == KaiTrade.Interfaces.OrderType.STOP) || (nos.OrdType == KaiTrade.Interfaces.OrderType.STOPLIMIT))
                    {

                    }
                    else
                    {

                        // is this for auto fill over time?
                        if (nos.Mnemonic == "MRK")
                        {
                            // partial fill
                            int lastQty = 1;

                            int leavesQty = 0;
                            if (quantity > 1)
                            {
                                lastQty = (int)(quantity / 2);

                                leavesQty = (int)quantity - lastQty;
                                quantity = (int)(quantity / 2);
                            }

                            fill.OrderStatus = KaiTrade.Interfaces.OrderStatus.PARTIALLY_FILLED;
                            fill.ExecType = KaiTrade.Interfaces.ExecType.PARTIAL_FILL;
                            sendExecReport(myContext, fill.OrderID, fill.OrderStatus, fill.ExecType, (double)lastQty, leavesQty, lastQty, (double)myOrdPrice, (double)myOrdPrice,"", "");

                            myContext.OrderQty = (int)nos.OrderQty;
                           // myContext.LeavesQty = quantity;
                            myContext.CumQty = 0;

                            // record the order in the context maps
                            RecordOrderContext(myContext.ClOrdID, myContext);
                            //m_OrderContextClOrdID.Add(myContext.ClOrdID, myContext);
                            m_OrderContextOrdID.Add(myContext.OrderID, myContext);

                        }
                        else if (nos.Mnemonic == "ZSELL")
                        {
                            int lastQty = 1;

                            int leavesQty = 0;
                            if (!m_ZSELLTrigger)
                            {
                                m_ZSELLTrigger = true;
                                // partial fill

                                if (quantity > 1)
                                {
                                    lastQty = (int)(quantity / 2);

                                    leavesQty = (int)quantity - lastQty;
                                    quantity = (int)(quantity / 2);
                                }

                                fill.OrderStatus = KaiTrade.Interfaces.OrderStatus.PARTIALLY_FILLED;
                                fill.ExecType = KaiTrade.Interfaces.ExecType.PARTIAL_FILL;

                            }
                            else
                            {
                                m_ZSELLTrigger = false;
                                lastQty = (int)(quantity);

                                leavesQty = 0;
                                fill.OrderStatus = KaiTrade.Interfaces.OrderStatus.PARTIALLY_FILLED;
                                fill.ExecType = KaiTrade.Interfaces.ExecType.PARTIAL_FILL;

                            }
                            sendExecReport(myContext, fill.OrderID, fill.OrderStatus, fill.ExecType, (double)lastQty, leavesQty, lastQty, (double)myOrdPrice, (double)myOrdPrice, "", "");

                            myContext.OrderQty = (int)quantity;
                            //myContext.LeavesQty = quantity;
                            myContext.CumQty = 0;

                            // record the order in the context maps
                            RecordOrderContext(myContext.ClOrdID, myContext);
                            //m_OrderContextClOrdID.Add(myContext.ClOrdID, myContext);
                            m_OrderContextOrdID.Add(myContext.OrderID, myContext);
                        }
                        else if ((nos.Mnemonic == "ZBUY") && (nos.Side == KaiTrade.Interfaces.Side.SELL))
                        {
                            int lastQty = 1;

                            int leavesQty = 0;
                            if (!m_ZBUYTrigger)
                            {
                                m_ZBUYTrigger = true;
                                // partial fill

                                if (quantity > 1)
                                {
                                    lastQty = (int)(quantity / 2);

                                    leavesQty = (int)quantity - lastQty;
                                    quantity = (int)(quantity / 2);
                                }

                                fill.OrderStatus = KaiTrade.Interfaces.OrderStatus.PARTIALLY_FILLED;
                                fill.ExecType = KaiTrade.Interfaces.ExecType.PARTIAL_FILL;
                            }
                            else
                            {
                                m_ZBUYTrigger = false;
                                lastQty = (int)(quantity);

                                leavesQty = 0;
                                fill.OrderStatus = KaiTrade.Interfaces.OrderStatus.FILLED;
                                fill.ExecType = KaiTrade.Interfaces.ExecType.FILL;

                            }

                            sendExecReport(myContext, fill.OrderID, fill.OrderStatus, fill.ExecType, (double)lastQty, leavesQty, lastQty, (double)myOrdPrice, (double)myOrdPrice, "", "");

                            myContext.OrderQty = (int) quantity;
                            //myContext.LeavesQty = quantity;
                            myContext.CumQty = 0;

                            // record the order in the context maps
                            RecordOrderContext(myContext.ClOrdID, myContext);
                            //m_OrderContextClOrdID.Add(myContext.ClOrdID, myContext);
                            m_OrderContextOrdID.Add(myContext.OrderID, myContext);
                        }
                        else
                        {
                            // fully fill
                             fill.OrderStatus = KaiTrade.Interfaces.OrderStatus.FILLED;
                             fill.ExecType = KaiTrade.Interfaces.ExecType.FILL;
                            sendExecReport(myContext, fill.OrderID, fill.OrderStatus, fill.ExecType, (double)quantity, 0, (int)quantity, (double)myOrdPrice, (double)myOrdPrice, "", "");

                        }
                    }

                }

            }
            catch (Exception myE)
            {
                log.Error("submitOrder", myE);
                // To provide the end user with more information
                // send an advisory message, again this is optional
                // and depends on the adpater
                SendAdvisoryMessage("KTA Simulator:submitOrder: problem submitting order:" + myE.ToString());

                if (nos != null)
                {
                    sendExecReport(nos, KaiTrade.Interfaces.OrderStatus.REJECTED, KaiTrade.Interfaces.ExecType.REJECTED, myE.ToString(), "OTHER");

                }

            }
        }
コード例 #2
0
ファイル: Market.cs プロジェクト: junwin/TradingTools
        /// <summary>
        /// FIX loop back order 
        /// </summary>
        /// <param name="msg"></param>
        public void submitOrder(KaiTrade.Interfaces.IMessage myMsg)
        {
            KaiTrade.Interfaces.ISubmitRequest nos = null;
            try
            {
                m_Parent.Log.Error("SUBTEST:" + myMsg.Data);
                nos = JsonConvert.DeserializeObject<K2DataObjects.SubmitRequest>(myMsg.Data);

                int quantity = (int)nos.OrderQty;

                decimal myOrdPrice = 99;
                if (nos.Price.HasValue)
                {

                    myOrdPrice = nos.Price.Value;
                }

                KaiTrade.Interfaces.IFill fill = new K2DataObjects.Fill();
                fill.OrderStatus = KaiTrade.Interfaces.OrderStatus.NEW;
                fill.ExecType = KaiTrade.Interfaces.ExecType.ORDER_STATUS;
                fill.OrderID = DriverBase.Identities.Instance.getNextOrderID();

                DriverBase.OrderContext myContext = new DriverBase.OrderContext();
                //myContext.QFOrder = myOrder;
                myContext.ClOrdID = nos.ClOrdID;
                myContext.OrderID = fill.OrderID;
                myContext.OrderQty = quantity;
                //myContext.LeavesQty = quantity;
                myContext.CumQty = 0;
                myContext.Price = myOrdPrice;
                myContext.Side = nos.Side;
                myContext.OrderType = nos.OrdType;
                // record the order in the context maps
                m_OrderContextClOrdID.Add(myContext.ClOrdID, myContext);
                m_OrderContextOrdID.Add(myContext.OrderID, myContext);
                myContext.OrdStatus = fill.OrderStatus;

                m_Parent.sendExecReport(myContext, fill.OrderID, fill.OrderStatus, fill.ExecType, 0.0, (int)nos.OrderQty, 0, 0.0, 0.0, "", "");

            }
            catch (Exception myE)
            {
                m_Parent.log.Error("submitOrder", myE);
                // To provide the end user with more information
                // send an advisory message, again this is optional
                // and depends on the adpater
                m_Parent.SendAdvisoryMessage("KTA Simulator:submitOrder: problem submitting order:" + myE.ToString());

                if (nos != null)
                {
                    m_Parent.sendExecReport(nos, KaiTrade.Interfaces.OrderStatus.REJECTED, KaiTrade.Interfaces.ExecType.REJECTED, myE.ToString(), "OTHER");

                }
            }
        }