Beispiel #1
0
 public ExecutionReport(
     QuickFix.OrderID aOrderID,
     QuickFix.ExecID aExecID,
     QuickFix.ExecTransType aExecTransType,
     QuickFix.ExecType aExecType,
     QuickFix.OrdStatus aOrdStatus,
     QuickFix.Symbol aSymbol,
     QuickFix.Side aSide,
     QuickFix.OrderQty aOrderQty,
     QuickFix.LastShares aLastShares,
     QuickFix.LastPx aLastPx,
     QuickFix.LeavesQty aLeavesQty,
     QuickFix.CumQty aCumQty,
     QuickFix.AvgPx aAvgPx)
     : base(MsgType())
 {
     set(aOrderID);
     set(aExecID);
     set(aExecTransType);
     set(aExecType);
     set(aOrdStatus);
     set(aSymbol);
     set(aSide);
     set(aOrderQty);
     set(aLastShares);
     set(aLastPx);
     set(aLeavesQty);
     set(aCumQty);
     set(aAvgPx);
 }
 public QuickFix.ExecType getExecType()
 { QuickFix.ExecType value = new QuickFix.ExecType();
   getField(value); return value; }
Beispiel #3
0
        /// <summary>
        /// apply an order status change
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void TWS_orderStatus(object sender, AxTWSLib._DTwsEvents_orderStatusEvent e)
        {
            try
            {
                if (m_DriverLog.IsDebugEnabled)
                {
                    m_DriverLog.Debug("TWS_openOrderEx:" + e.clientId.ToString() + ": " + e.ToString() + ": " + e.status.ToString());
                }

                QuickFix.OrdStatus myOrdStatus;
                QuickFix.ExecType myExecType = new QuickFix.ExecType(QuickFix.ExecType.ORDER_STATUS);

                double myLastFill = 0.0;
                // get the order contect using the IB ID
                DriverBase.OrderContext myCntx = null;

                if (this.m_ApiIDOrderMap.ContainsKey(e.id.ToString()))
                {
                    myCntx = m_ApiIDOrderMap[e.id.ToString()];
                }
                if (myCntx == null)
                {
                    string myErr = "IB TWS: no context found for IB ID:" + e.id.ToString();
                    Exception myE = new Exception(myErr);
                    throw myE;
                }

                myLastFill = myCntx.LeavesQty - e.remaining;
                myCntx.CumQty = e.filled;
                // get the original order from the context
                QuickFix.Message myOrder = myCntx.QFOrder;

                switch (e.status)
                {
                    case "PendingSubmit":
                        myOrdStatus = new QuickFix.OrdStatus(QuickFix.OrdStatus.PENDING_NEW);
                        sendExecReport(myOrder, new QuickFix.OrderID(e.id.ToString()), myOrdStatus, myExecType, myLastFill, e.remaining, e.filled, e.lastFillPrice, e.avgFillPrice);
                        break;
                    case "PendingCancel":
                        myOrdStatus = new QuickFix.OrdStatus(QuickFix.OrdStatus.PENDING_CANCEL);
                        sendExecReport(myOrder, new QuickFix.OrderID(e.id.ToString()), myOrdStatus, myExecType,   myLastFill, e.remaining, e.filled, e.lastFillPrice, e.avgFillPrice);
                        break;
                    case "PreSubmitted":
                        break;

                    case "Inactive":
                        break;
                    case "Submitted":

                        if (e.filled > 0)
                        {
                            myOrdStatus = new QuickFix.OrdStatus(QuickFix.OrdStatus.PARTIALLY_FILLED);
                            myExecType = new QuickFix.ExecType(QuickFix.ExecType.PARTIAL_FILL);
                        }
                        else
                        {
                            myOrdStatus = new QuickFix.OrdStatus(QuickFix.OrdStatus.NEW);

                        }
                        // mark the context as done for the sub
                        SetContextCommand(myCntx, DriverBase.ORCommand.Submit, DriverBase.ORCommand.Undefined);

                        sendExecReport(myOrder, new QuickFix.OrderID(e.id.ToString()), myOrdStatus, myExecType,  myLastFill, e.remaining, e.filled, e.lastFillPrice, e.avgFillPrice);
                        break;

                    case "Cancelled":
                        myOrdStatus = new QuickFix.OrdStatus(QuickFix.OrdStatus.CANCELED);
                        sendExecReport(myOrder, new QuickFix.OrderID(e.id.ToString()), myOrdStatus, myExecType,  myLastFill, e.remaining, e.filled, e.lastFillPrice, e.avgFillPrice);
                        // mark the context as done for the sub
                        SetContextCommand(myCntx, DriverBase.ORCommand.Pull, DriverBase.ORCommand.Undefined);

                        break;

                    case "Filled":
                        myOrdStatus = new QuickFix.OrdStatus(QuickFix.OrdStatus.FILLED);
                        myExecType = new QuickFix.ExecType(QuickFix.ExecType.FILL);
                        sendExecReport(myOrder, new QuickFix.OrderID(e.id.ToString()), myOrdStatus, myExecType,  myLastFill, e.remaining, e.filled, e.lastFillPrice, e.avgFillPrice);
                        // mark the context as done for the sub
                        SetContextCommand(myCntx, DriverBase.ORCommand.Undefined, DriverBase.ORCommand.Undefined);
                        break;

                    default:
                        _log.Error("TWS_orderStatus:unknown status" + e.status);
                        break;
                }
            }
            catch (Exception myE)
            {
                _log.Error("TWS_orderStatus", myE);
            }
        }
Beispiel #4
0
        /// <summary>
        /// submit an order to IB using a incomming FIX new order single
        /// </summary>
        /// <param name="myMsg"></param>
        private void submitOrder(KaiTrade.Interfaces.IMessage myMsg)
        {
            QuickFix.Message myQFOrder = null;
            try
            {
                // Extract the raw FIX Message from the inbound message
                string strOrder = myMsg.Data;

                // Use QuickFix to handle the message
                myQFOrder = new QuickFix.Message(strOrder);

                // Use product manager to validate the product specified on
                // the order exists for this adapter

                // Get the product associated with the FIX message

                //QuickFix.Symbol symbol = new QuickFix.Symbol();
                //myQFOrder.getField(symbol);

                QuickFix.Side mySide = new QuickFix.Side();
                QuickFix.OrdType myOrdType = new QuickFix.OrdType();
                QuickFix.OrderQty myOrderQty = new QuickFix.OrderQty();
                QuickFix.Price myPrice = new QuickFix.Price();
                QuickFix.StopPx myStopPx = new QuickFix.StopPx();
                QuickFix.Account myAccount = new QuickFix.Account();

                QuickFix.ClOrdID myClOrdID = new QuickFix.ClOrdID();
                QuickFix.TimeInForce myTimeInForce = new QuickFix.TimeInForce();

                // the account code is mandatory
                if (myQFOrder.isSetField(myAccount))
                {
                    myQFOrder.getField(myAccount);
                }
                else
                {
                    this.SendAdvisoryMessage("IB TWS: you need to provide a valid account");
                    throw new Exception("IB TWS: you need to provide a valid account");
                }

                myQFOrder.getField(myClOrdID);

                // Get the Order type
                myQFOrder.getField(myOrdType);

                // Get the QTY
                myQFOrder.getField(myOrderQty);

                // get the Side of the order
                myQFOrder.getField(mySide);

                // Set order duration
                myQFOrder.getField(myTimeInForce);

                // Prices
                if (myQFOrder.isSetField(myPrice))
                {
                    myQFOrder.getField(myPrice);
                }

                if (myQFOrder.isSetField(myStopPx))
                {
                    myQFOrder.getField(myStopPx);
                }

                // get the contract
                TWSLib.IContract myContract = getIBContract(myQFOrder);
                if (myContract == null)
                {
                    this.SendAdvisoryMessage("IB TWS: cannot find a valid contract");
                    throw new Exception("IB TWS: cannot find a valid contract");
                }

                // create an IB Order
                TWSLib.IOrder myIBOrder = m_Host.TWS.createOrder();

                myIBOrder.whatIf = 0;
                myIBOrder.account = myAccount.getValue();

                if(myOrdType.getValue() == QuickFix.OrdType.LIMIT)
                {
                    myIBOrder.orderType = "LMT";
                    myIBOrder.lmtPrice = myPrice.getValue();
                }
                else if(myOrdType.getValue() == QuickFix.OrdType.MARKET)
                {
                    myIBOrder.orderType = "MKT";
                }
                else if (myOrdType.getValue() == QuickFix.OrdType.STOP)
                {
                    myIBOrder.orderType = "STP";
                    myIBOrder.auxPrice = myStopPx.getValue();
                    if (myPrice.getValue() == -1)
                    {
                        myIBOrder.lmtPrice = myStopPx.getValue();
                    }
                    else
                    {
                        myIBOrder.lmtPrice = myPrice.getValue();
                    }
                }
                else if (myOrdType.getValue() == QuickFix.OrdType.STOP_LIMIT)
                {
                    myIBOrder.orderType = "STP";
                    myIBOrder.auxPrice = myStopPx.getValue();
                    if (myPrice.getValue() == -1)
                    {
                        myIBOrder.lmtPrice = myStopPx.getValue();
                    }
                    else
                    {
                        myIBOrder.lmtPrice = myPrice.getValue();
                    }
                }
                else
                {
                    this.SendAdvisoryMessage("IB TWS: order type not supported");
                    throw new Exception("IB TWS: order type not supported");
                }

                myIBOrder.totalQuantity = (int) myOrderQty.getValue();

                // Side
                string myAction = KaiUtil.QFUtils.DecodeSide(mySide).ToUpper();
                myIBOrder.action = myAction;

                // Time in force
                string myTIF = KaiUtil.QFUtils.DecodeTimeInForce(myTimeInForce).ToUpper();
                myIBOrder.timeInForce = myTIF;

                if (myOrdType.getValue() == QuickFix.OrdType.LIMIT)
                {
                    myIBOrder.lmtPrice = myPrice.getValue();
                }

                DriverBase.OrderContext myCntx = RecordOrderContext(myClOrdID.getValue(), myQFOrder, null, m_NextID.ToString());
                SetContextCommand(myCntx, DriverBase.ORCommand.Undefined, DriverBase.ORCommand.Submit);

                myCntx.OrderQty = myIBOrder.totalQuantity;
                myCntx.CumQty = 0;

                m_Host.TWS.placeOrderEx(m_NextID++, myContract, myIBOrder);

            }
            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
                this.SendAdvisoryMessage("IB TWS:submitOrder: problem submitting order:" + myE.ToString());

                QuickFix.OrdStatus myOrdStatus;
                QuickFix.ExecType myExecType = new QuickFix.ExecType(QuickFix.ExecType.REJECTED);

                myOrdStatus = new QuickFix.OrdStatus(QuickFix.OrdStatus.REJECTED);
                QuickFix.OrderQty orderQty = new QuickFix.OrderQty();
                if (myQFOrder != null)
                {
                    myQFOrder.getField(orderQty);
                    QuickFix.OrdRejReason myRejReason = new QuickFix.OrdRejReason(QuickFix.OrdRejReason.OTHER);
                    sendExecReport(myQFOrder, new QuickFix.OrderID("UNKNOWN"), myOrdStatus, myExecType, 0.0, (int)orderQty.getValue(), 0, 0, 0, myE.Message, myRejReason);
                }

            }
        }
Beispiel #5
0
        void rejectOrder(string id, string msg)
        {
            try
            {

                QuickFix.OrdStatus myOrdStatus;
                QuickFix.ExecType myExecType = new QuickFix.ExecType(QuickFix.ExecType.ORDER_STATUS);

                double myLastFill = 0.0;
                // get the order contect using the IB ID
                DriverBase.OrderContext myCntx = null;

                if (this.m_ApiIDOrderMap.ContainsKey(id))
                {
                    myCntx = m_ApiIDOrderMap[id];
                }
                if (myCntx == null)
                {
                    string myErr = "IB TWS: no context found for IB ID:" + id;
                    Exception myE = new Exception(myErr);
                    throw myE;
                }

                // get the original order from the context
                QuickFix.Message myOrder = myCntx.QFOrder;

                myOrdStatus = new QuickFix.OrdStatus(QuickFix.OrdStatus.REJECTED);
                QuickFix.OrdRejReason reason = new QuickFix.OrdRejReason(QuickFix.OrdRejReason.UNKNOWN);
                sendExecReport(myOrder, new QuickFix.OrderID(id), myOrdStatus, myExecType, myLastFill, 0, 0, 0, 0,msg, reason );
                // mark the context as done for the sub
                SetContextCommand(myCntx, DriverBase.ORCommand.Pull, DriverBase.ORCommand.Undefined);

            }
            catch (Exception myE)
            {
                _log.Error("rejectOrder", myE);
            }
        }
Beispiel #6
0
 public bool isSet(QuickFix.ExecType field)
 {
     return(isSetField(field));
 }
Beispiel #7
0
 public QuickFix.ExecType getExecType()
 {
     QuickFix.ExecType value = new QuickFix.ExecType();
     getField(value); return(value);
 }
Beispiel #8
0
 public QuickFix.ExecType get(QuickFix.ExecType value)
 {
     getField(value); return(value);
 }
Beispiel #9
0
 public void set(QuickFix.ExecType value)
 {
     setField(value);
 }
Beispiel #10
0
        /// <summary>
        /// Handle an incomming execution report
        /// </summary>
        /// <param name="myMsg"></param>
        public void HandleExecReport(KaiTrade.Interfaces.Message myMsg)
        {
            try
            {
                if (m_Log.IsInfoEnabled)
                {
                    m_Log.Info("OrderService:HandleExecReport:" + myMsg.Data);
                }
                if (m_ORLog.IsInfoEnabled)
                {
                    m_ORLog.Info("OrderService:HandleExecReport:" + myMsg.Data);
                }
                // Get order manager
                KaiTrade.Interfaces.OrderManager myOM = KTAFacade.Instance().Factory.GetOrderManager();

                //Create a quickfix message object
                QuickFix.Message myFixMsg = new QuickFix.Message(myMsg.Data);

                QuickFix.ExecID myExecID = new QuickFix.ExecID();
                myFixMsg.getField(myExecID);

                // check if we have already processed this report
                if (!myOM.RecordExecutionReport(myExecID.getValue(), myFixMsg))
                {
                    // we have processed this already
                    m_Log.Warn("HandleExecReport: Duplicate Exec Report:" + myMsg.Data);
                    return;
                }

                // these fields must be present
                QuickFix.OrderID myOrderID = new QuickFix.OrderID();
                myFixMsg.getField(myOrderID);

                QuickFix.ExecType myExecType = new QuickFix.ExecType();
                myFixMsg.getField(myExecType);

                QuickFix.OrdStatus myOrdStatus = new QuickFix.OrdStatus();
                myFixMsg.getField(myOrdStatus);

                // we need the clordid to update an existing order - if
                // it is not present or unknow we create a synthetic order
                QuickFix.ClOrdID myClOrdID = new QuickFix.ClOrdID();
                KaiTrade.Interfaces.Order myOrder = null;
                if (myFixMsg.isSetField(myClOrdID))
                {
                    myFixMsg.getField(myClOrdID);
                    myOrder = myOM.GetOrderWithClOrdIDID(myClOrdID.getValue());
                }
                if (myOrder == null)
                {
                    myOrder = CreateSyntheticOrder(myFixMsg);
                }

                // add the ExecRep to the Order
                KaiTrade.Interfaces.Fill myFill = new K2DataObjects.FillData();
                myFill.OrderID = myOrder.Identity;
                myFill.SetUpFromFixExecReport(myFixMsg.ToString());

                myOrder.FillsList.Add(myFill);

                // update mandatory fields
                myOrder.OrderID = myOrderID;
                myOrder.OrdStatus = myOrdStatus;
                QuickFix.Text myText = new QuickFix.Text();
                if (myFixMsg.isSetField(myText))
                {
                    myFixMsg.getField(myText);
                    myOrder.Text = myText.getValue();
                }

                QuickFix.Account myAccount = new QuickFix.Account();
                if (myFixMsg.isSetField(myAccount))
                {
                    myFixMsg.getField(myAccount);

                    myOrder.Account = myAccount;
                }

                // process the execution depanding on type of ExecReport
                switch (myExecType.getValue())
                {
                    case QuickFix.ExecType.NEW:
                        processGeneralExecRep(ref myOrder, myFixMsg);
                        /*
                        if (order.LastOrderCommand == KaiTrade.Interfaces.LastOrderCommand.neworder)
                        {
                            order.LastOrderCommand = KaiTrade.Interfaces.LastOrderCommand.none;
                        }
                        else
                        {
                            m_Log.Error("HandleExecReport:OrderStatus new not expected");
                        }
                         * */
                        break;
                    case QuickFix.ExecType.FILL:
                        if (order.LastOrderCommand == KaiTrade.Interfaces.LastOrderCommand.neworder)
                        {
                            order.LastOrderCommand = KaiTrade.Interfaces.LastOrderCommand.none;
                        }

                        processFill(ref myOrder, myFixMsg);
                        break;
                    case QuickFix.ExecType.PARTIAL_FILL:
                        if (order.LastOrderCommand == KaiTrade.Interfaces.LastOrderCommand.neworder)
                        {
                            order.LastOrderCommand = KaiTrade.Interfaces.LastOrderCommand.none;
                        }
                        processFill(ref myOrder, myFixMsg);
                        break;
                    case QuickFix.ExecType.ORDER_STATUS:
                        processGeneralExecRep(ref myOrder, myFixMsg);
                        break;
                    default:
                        processGeneralExecRep(ref myOrder, myFixMsg);
                        break;
                }

                myOM.SetChanged(myOrder.Identity);

                // Check if the order is part of a strategy
                if (myOrder.ParentIdentity != null)
                {
                    // get the strategy concerned
                    KaiTrade.Interfaces.IStrategy myStrat = KTAFacade.Instance().Factory.GetStrategyManager().GetStrategy(myOrder.ParentIdentity);
                    if (myStrat != null)
                    {
                        myStrat.HandleExecReport(myFixMsg, myOrder);
                    }
                    else
                    {
                        m_Log.Error("Strategy not found for:" + myOrder.ParentIdentity);
                    }
                }

                if (myOrder.OCAGroupName.Length > 0)
                {
                    KTAFacade.Instance().Factory.GetOCOManager().OnOrderTraded(myOrder);
                }
                if (m_ORLog.IsInfoEnabled)
                {
                    m_ORLog.Info("OrderService:HandleExecRep:Exit:" + myOrder.ToString());
                }
            }
            catch (Exception myE)
            {
                m_Log.Error("HandleExecReport", myE);
            }
        }
Beispiel #11
0
        /// <summary>
        /// Create a synthetic order
        /// </summary>
        /// <param name="myMsg"></param>
        /// <returns>n order</returns>
        public KaiTrade.Interfaces.Order CreateSyntheticOrder(QuickFix.Message myFixMsg)
        {
            KaiTrade.Interfaces.Order myOrder = null;
            try
            {
                if (m_Log.IsInfoEnabled)
                {
                    m_Log.Info("exec report recd:" + myFixMsg.ToString());
                }

                // Get order manager
                KaiTrade.Interfaces.OrderManager myOM = KTAFacade.Instance().Factory.GetOrderManager();

                // these fields must be present
                QuickFix.OrderID myOrderID = new QuickFix.OrderID();
                myFixMsg.getField(myOrderID);

                QuickFix.ExecType myExecType = new QuickFix.ExecType();
                myFixMsg.getField(myExecType);

                QuickFix.ExecID myExecID = new QuickFix.ExecID();
                myFixMsg.getField(myExecID);

                QuickFix.OrdStatus myOrdStatus = new QuickFix.OrdStatus();
                myFixMsg.getField(myOrdStatus);

                // Create the order
                myOrder = myOM.CreateOrder();

                // we need the clordid to update an existing order - if
                // it is not present or unknow we create a synthetic order
                QuickFix.ClOrdID myClOrdID = new QuickFix.ClOrdID();
                if (myFixMsg.isSetField(myClOrdID))
                {
                    myOrder.ClOrdID = myClOrdID;
                }
            }
            catch (Exception myE)
            {
                m_Log.Error("CreateSyntheticOrder", myE);
            }
            return myOrder;
        }