Ejemplo n.º 1
0
        public void RenderOrderAsFix(out QuickFix.Message myNOS, out KaiTrade.Interfaces.Message myMsg, out string myDriverCode, KaiTrade.Interfaces.Order myOrder)
        {
            try
            {
                lock (this)
                {
                    // do not actually submit triggered orders

                    // register the order as a publisher
                    KaiTrade.Interfaces.Publisher myPub = null;
                    myPub = AppFactory.Instance().GetPublisherManager().GetPublisher(myOrder.Identity);
                    if (myPub == null)
                    {
                        myPub = myOrder as KaiTrade.Interfaces.Publisher;
                        AppFactory.Instance().GetPublisherManager().Add(myPub);
                    }

                    // Check the limits for the order
                    string myTextErr;
                    if (LimitChecker.Instance().BreaksLimits(out myTextErr, myOrder))
                    {
                        myOrder.OrdStatus = new QuickFix.OrdStatus(QuickFix.OrdStatus.REJECTED);
                        myOrder.Text = "order failed order qty limit test";
                        Exception myE = new Exception("process order failed order qty limit test");
                        throw myE;
                    }

                    // Create the FIX order message
                    myNOS = new QuickFix.Message();

                    // pending new at this stage
                    myOrder.OrdStatus = new QuickFix.OrdStatus(QuickFix.OrdStatus.PENDING_NEW);

                    myOrder.TransactTime = new QuickFix.TransactTime(DateTime.Now.ToUniversalTime());
                    myNOS.setField(myOrder.TransactTime);

                    //myOrder.ClOrdID = new QuickFix.ClOrdID(KaiUtil.Identities.Instance.genReqID());

                    // Associate the CLOrdID with this order
                    AppFactory.Instance().GetOrderManager().AssociateClOrdID(myOrder.ClOrdID.getValue(), myOrder);

                    // Set fix version
                    QuickFix.BeginString myBeginString = new QuickFix.BeginString("FIX.4.4");
                    myNOS.getHeader().setField(myBeginString);

                    // Set order message type
                    QuickFix.MsgType msgType = new QuickFix.MsgType();
                    msgType.setValue("D");
                    myNOS.getHeader().setField(msgType);

                    myNOS.setField(myOrder.ClOrdID);

                    // set up the product details in the message
                    myOrder.Product.Set(myNOS);

                    // Qty
                    myNOS.setField(myOrder.OrderQty);

                    // MaxFloor
                    if (myOrder.MaxFloor != null)
                    {
                        myNOS.setField(myOrder.MaxFloor);
                    }

                    // set the side
                    myNOS.setField(myOrder.Side);

                    // set the order type
                    myNOS.setField(myOrder.OrdType);

                    // Time in force
                    if (myOrder.TimeInForce != null)
                    {
                        myNOS.setField(myOrder.TimeInForce);
                    }
                    else
                    {
                        myOrder.TimeInForce = new QuickFix.TimeInForce(QuickFix.TimeInForce.DAY);
                        myNOS.setField(myOrder.TimeInForce);
                    }

                    // Validation - check they entered a date/time if needed
                    switch (myOrder.TimeInForce.getValue())
                    {
                        case QuickFix.TimeInForce.GOOD_TILL_DATE:
                            break;

                        default:
                            break;
                    }

                    // do not add the price to the fix message on Market orders
                    if (myOrder.OrdType.getValue() != QuickFix.OrdType.MARKET)
                    {
                        if (myOrder.Price.getValue() > 0)
                        {
                            myNOS.setField(myOrder.Price);
                        }
                        else
                        {
                            throw new Exception("Must specify price > 0 on Limit and other non Market orders");
                        }
                    }

                    if (myOrder.StopPx != null)
                    {
                        myNOS.setField(myOrder.StopPx);
                    }

                    // default the handlInst to automated
                    if (myOrder.HandlInst != null)
                    {
                        myNOS.setField(myOrder.HandlInst);
                    }
                    else
                    {
                        myOrder.HandlInst = new QuickFix.HandlInst(QuickFix.HandlInst.AUTOEXECPUB);
                        myNOS.setField(myOrder.HandlInst);
                    }

                    // Create the message wrapper used to send the order to the driver
                    myMsg = new KaiTCPComm.KaiMessageWrap();

                    myMsg.Label = "D";

                    myDriverCode = "";
                    // A trade venue must be specified on the order
                    KaiTrade.Interfaces.Venue myVenue = null;
                    if (myOrder.Product.TradeVenue.Length > 0)
                    {
                        // get the driver code and session details from the venue manager
                        myVenue = AppFactory.Instance().GetVenueManager().GetVenue(myOrder.Product.TradeVenue);
                        if (myVenue != null)
                        {
                            myDriverCode = myVenue.DriverCode;
                            if (myVenue.TargetVenue.Length > 0)
                            {
                                myMsg.VenueCode = myVenue.TargetVenue;
                            }
                            if (myVenue.BeginString.Length > 0)
                            {
                                // this is the FIX version the server wants, we convert between
                                // versions in the FIX client
                                myMsg.Format = myVenue.BeginString;
                            }

                            if (myVenue.SID.Length > 0)
                            {
                                QuickFix.SenderCompID mySID = new QuickFix.SenderCompID(myVenue.SID);
                                myNOS.getHeader().setField(mySID);
                                myMsg.ClientID = myVenue.SID;
                            }
                            if (myVenue.TID.Length > 0)
                            {
                                QuickFix.TargetCompID myTID = new QuickFix.TargetCompID(myVenue.TID);
                                myNOS.getHeader().setField(myTID);
                                myMsg.TargetID = myVenue.TID;
                            }
                        }
                        else
                        {
                            string myErr = "Invalid Venue Code:" + myOrder.Product.TradeVenue;
                            m_Log.Error(myErr);
                            Exception myE = new Exception(myErr);
                            throw myE;
                        }
                    }
                    else
                    {
                        string myErr = "No Venue Code specified on product:";
                        m_Log.Error(myErr);
                        Exception myE = new Exception(myErr);
                        throw myE;
                    }

                    // process any venue field bags - do this prior to the order bags
                    // to allow the order setting to override the venues
                    List<KaiTrade.Interfaces.Field> myFieldBag;
                    KTAFacade.Instance().SetBag(out myFieldBag, myVenue.NOSBag, ",");
                    foreach (KaiTrade.Interfaces.Field myField in myFieldBag)
                    {
                        try
                        {
                            myNOS.setString(int.Parse(myField.ID), myField.Value);
                        }
                        catch (Exception myE)
                        {
                            m_Log.Error("NOS Extra tags from venue error:", myE);
                        }
                    }

                    // process any additional tags for the order
                    foreach (KaiTrade.Interfaces.Field myField in myOrder.NOSBag)
                    {
                        try
                        {
                            myNOS.setString(int.Parse(myField.ID), myField.Value);
                        }
                        catch (Exception myE)
                        {
                            m_Log.Error("NOS Extra tags error:", myE);
                        }
                    }

                    // additional setting placed here will override the
                    // fields in the bags

                    if (myOrder.Account != null)
                    {
                        if (myOrder.Account.getValue().Length > 0)
                        {
                            myNOS.setField(myOrder.Account);
                        }
                    }

                    QuickFix.TargetStrategyParameters targetStrategyParameters = new QuickFix.TargetStrategyParameters(myOrder.StrategyName);
                    myNOS.setField(targetStrategyParameters);

                    // Process any strategy parameters
                    if (myOrder.K2Parameters.Count > 0)
                    {
                        QuickFix.NoStrategyParameters noStratParms = new QuickFix.NoStrategyParameters(myOrder.K2Parameters.Count);
                        myNOS.setField(noStratParms);
                        QuickFix50Sp2.NewOrderSingle.NoStrategyParameters group = new QuickFix50Sp2.NewOrderSingle.NoStrategyParameters();
                        QuickFix.StrategyParameterName strategyParameterName;
                        QuickFix.StrategyParameterType strategyParameterType;
                        QuickFix.StrategyParameterValue strategyParameterValue;

                        foreach (KaiTrade.Interfaces.K2Parameter param in myOrder.K2Parameters)
                        {
                            strategyParameterName = new QuickFix.StrategyParameterName(param.ParameterName);
                            group.setField(strategyParameterName);
                            strategyParameterType = new QuickFix.StrategyParameterType((int)param.ParameterType);
                            group.setField(strategyParameterType);
                            strategyParameterValue = new QuickFix.StrategyParameterValue(param.ParameterValue);
                            group.setField(strategyParameterValue);
                            myNOS.addGroup(group);
                        }
                    }

                    // Set the FIX string as message data
                    myMsg.Data = myNOS.ToString();

                    // inform order manager clients that the order has changed
                    AppFactory.Instance().GetOrderManager().SetChanged(myOrder.Identity);
                }
            }
            catch (Exception myE)
            {
                m_Log.Error("RenderOrderAsFix:", myE);
                myOrder.OrdStatus = new QuickFix.OrdStatus(QuickFix.OrdStatus.REJECTED);
                myOrder.Text = myE.Message;
                // inform order manager clients that the order has changed
                AppFactory.Instance().GetOrderManager().SetChanged(myOrder.Identity);
                throw (myE);
            }
        }