/*
         * public void ExecOrders(object arg)
         * {
         *  NewOrderSingle ordem = (NewOrderSingle)arg;
         *  long clordid = Convert.ToInt64(DateTime.Now.ToString("yyyyMMddhhmmssfff"));
         *  bool bRet;
         *  logger.Info("=====================================> INICIO ========> Regs: " + _times);
         *  for (int i =0; i<_times; i++)
         *  {
         *      ClOrdID xx = new ClOrdID(clordid++.ToString());
         *      ordem.ClOrdID = xx;
         *      //Thread.Sleep(1);
         *      bRet = Session.SendToTarget(ordem, _session.SessionID);
         *  }
         *  logger.Info("=====================================> FIM ========> Regs: " + _times);
         * }
         */
        public bool AlterarOrdem(OrdemInfo ordem, long ini = 0, long fim = 0, long oriini = 0, long orifim = 0, int delay = 0, string mnemonic = "")
        {
            //Cria a mensagem FIX de NewOrderSingle
            QuickFix.FIX44.OrderCancelReplaceRequest orderCancelReplaceReq = new QuickFix.FIX44.OrderCancelReplaceRequest();

            orderCancelReplaceReq.Set(new OrigClOrdID(ordem.OrigClOrdID));

            if (ordem.Account > 0)
            {
                orderCancelReplaceReq.Set(new Account(ordem.Account.ToString()));
            }
            if (!string.IsNullOrEmpty(mnemonic))
            {
                orderCancelReplaceReq.SetField(new Account(mnemonic), true);
            }


            // Instrument Identification Block
            orderCancelReplaceReq.Set(new Symbol(ordem.Symbol));
            orderCancelReplaceReq.Set(new SecurityID(ordem.SecurityID));
            orderCancelReplaceReq.Set(new SecurityIDSource(SecurityIDSource.EXCHANGE_SYMBOL));

            if (ordem.ExchangeNumberID != null && ordem.ExchangeNumberID.Length > 0)
            {
                orderCancelReplaceReq.Set(new OrderID(ordem.ExchangeNumberID));
            }

            orderCancelReplaceReq.Set(new ClOrdID(ordem.ClOrdID));
            //ordersingle.set(new IDSource());
            if (ordem.Side == OrdemDirecaoEnum.Venda)
            {
                orderCancelReplaceReq.Set(new Side(Side.SELL));
            }
            else
            {
                orderCancelReplaceReq.Set(new Side(Side.BUY));
            }
            orderCancelReplaceReq.Set(new Price(Convert.ToDecimal(ordem.Price)));

            TimeInForce tif = FixMessageUtilities.deOrdemValidadeParaTimeInForce(ordem.TimeInForce);

            if (tif != null)
            {
                orderCancelReplaceReq.Set(tif);
            }

            orderCancelReplaceReq.Set(new OrderQty(ordem.OrderQty));


            if (ordem.TimeInForce == OrdemValidadeEnum.ValidoAteDeterminadaData)
            {
                DateTime expiredate = Convert.ToDateTime(ordem.ExpireDate);
                orderCancelReplaceReq.Set(new ExpireDate(expiredate.ToString("yyyyMMdd")));
            }

            OrdType ordType = FixMessageUtilities.deOrdemTipoParaOrdType(ordem.OrdType);

            if (ordType != null)
            {
                orderCancelReplaceReq.Set(ordType);
            }

            //if (ordem.OrdType == OrdemTipoEnum.StopLimitada )
            //{
            //    ordersingle.set(new StopPx(ordem.StopPx));
            //}

            orderCancelReplaceReq.Set(new TransactTime(DateTime.Now));
            // Cliente
            QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup PartyIDGroup1 = new QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup();
            PartyIDGroup1.Set(new PartyID(ordem.ExecBroker));
            PartyIDGroup1.Set(new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE));
            PartyIDGroup1.Set(new PartyRole(PartyRole.ENTERING_TRADER));

            // Corretora
            QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup PartyIDGroup2 = new QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup();
            PartyIDGroup2.Set(new PartyID("227"));
            PartyIDGroup2.Set(new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE));
            PartyIDGroup2.Set(new PartyRole(PartyRole.ENTERING_FIRM));

            // Location ID
            QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup PartyIDGroup3 = new QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup();
            if (ordem.SenderLocation != null && ordem.SenderLocation.Length > 0)
            {
                PartyIDGroup3.Set(new PartyID(ordem.SenderLocation));
            }
            else
            {
                PartyIDGroup3.Set(new PartyID("GRA"));
            }
            PartyIDGroup3.Set(new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE));
            PartyIDGroup3.Set(new PartyRole(54));

            orderCancelReplaceReq.AddGroup(PartyIDGroup1);
            orderCancelReplaceReq.AddGroup(PartyIDGroup2);
            orderCancelReplaceReq.AddGroup(PartyIDGroup3);

            //BEI - 2012-Nov-14
            if (ordem.ForeignFirm != null && ordem.ForeignFirm.Length > 0)
            {
                QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup PartyIDGroup4 = new QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup();
                PartyIDGroup4.Set(new PartyID(ordem.ForeignFirm));
                PartyIDGroup4.Set(new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE));
                PartyIDGroup4.Set(new PartyRole(PartyRole.FOREIGN_FIRM));

                orderCancelReplaceReq.AddGroup(PartyIDGroup4);
            }

            if (ordem.Account > 0)
            {
                QuickFix.FIX44.OrderCancelReplaceRequest.NoAllocsGroup noAllocsGrp = new QuickFix.FIX44.OrderCancelReplaceRequest.NoAllocsGroup();
                noAllocsGrp.Set(new AllocAccount(ordem.Account.ToString()));
                noAllocsGrp.Set(new AllocAcctIDSource(99));
                orderCancelReplaceReq.AddGroup(noAllocsGrp);
            }

            //SelfTradeProtection - 2012-Nov-27
            if (ordem.InvestorID != null && ordem.InvestorID.Length > 0)
            {
                QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup PartyIDGroup5 = new QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup();
                PartyIDGroup5.Set(new PartyID(ordem.InvestorID));
                PartyIDGroup5.Set(new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE));
                PartyIDGroup5.Set(new PartyRole(PartyRole.INVESTOR_ID));

                orderCancelReplaceReq.AddGroup(PartyIDGroup5);
            }

            if (ordem.ExecutingTrader != null && ordem.ExecutingTrader.Length > 0)
            {
                QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup PartyIDGroup7 = new QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup();
                PartyIDGroup7.Set(new PartyID(ordem.ExecutingTrader));
                PartyIDGroup7.Set(new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE));
                PartyIDGroup7.Set(new PartyRole(PartyRole.EXECUTING_TRADER));

                orderCancelReplaceReq.AddGroup(PartyIDGroup7);
            }

            // Memo Field
            if (ordem.Memo5149 != null && ordem.Memo5149.Length > 0)
            {
                if (ordem.Memo5149.Length > 50)
                {
                    ordem.Memo5149 = ordem.Memo5149.Substring(0, 50);
                }

                StringField memo5149 = new StringField(5149, ordem.Memo5149);

                orderCancelReplaceReq.SetField(memo5149);
            }
            bool bRet = false;

            if (oriini != 0 && orifim != 0)
            {
                long times = fim - ini;
                logger.Info("=====================================> INICIO ALTERAR ORDEM========> Qtd: " + times);
                for (long i = 0; i < times; i++)
                {
                    ClOrdID     xx  = new ClOrdID(ini.ToString());
                    OrigClOrdID xx2 = new OrigClOrdID(oriini.ToString());
                    orderCancelReplaceReq.ClOrdID     = xx;
                    orderCancelReplaceReq.OrigClOrdID = xx2;

                    bRet = Session.SendToTarget(orderCancelReplaceReq, _session.SessionID);
                    if (!bRet)
                    {
                        logger.Info("erro");
                        break;
                    }
                    if (0 != delay)
                    {
                        Thread.Sleep(delay);
                    }
                    ini++;
                    oriini++;
                    //System.Windows.Forms.Application.DoEvents();
                }
                logger.Info("=====================================> FIM ALTERAR ORDEM========> Qtd: " + times);
            }
            else
            {
                bRet = Session.SendToTarget(orderCancelReplaceReq, _session.SessionID);
            }

            return(bRet);
        }
Beispiel #2
0
        public bool AlterarOrdem(OrdemInfo ordem)
        {
            //Cria a mensagem FIX de NewOrderSingle
            QuickFix.FIX44.OrderCancelReplaceRequest orderCancelReplaceReq = new QuickFix.FIX44.OrderCancelReplaceRequest();

            orderCancelReplaceReq.Set(new OrigClOrdID(ordem.OrigClOrdID));

            if (ordem.Account > 0)
            {
                orderCancelReplaceReq.Set(new Account(ordem.Account.ToString()));
            }

            // Instrument Identification Block
            orderCancelReplaceReq.Set(new Symbol(ordem.Symbol));
            orderCancelReplaceReq.Set(new SecurityID(ordem.SecurityID));
            orderCancelReplaceReq.Set(new SecurityIDSource(SecurityIDSource.EXCHANGE_SYMBOL));

            if (ordem.ExchangeNumberID != null && ordem.ExchangeNumberID.Length > 0)
            {
                orderCancelReplaceReq.Set(new OrderID(ordem.ExchangeNumberID));
            }

            orderCancelReplaceReq.Set(new ClOrdID(ordem.ClOrdID));
            //ordersingle.set(new IDSource());
            if (ordem.Side == OrdemDirecaoEnum.Venda)
            {
                orderCancelReplaceReq.Set(new Side(Side.SELL));
            }
            else
            {
                orderCancelReplaceReq.Set(new Side(Side.BUY));
            }
            orderCancelReplaceReq.Set(new Price(Convert.ToDecimal(ordem.Price)));

            TimeInForce tif = FixMessageUtilities.deOrdemValidadeParaTimeInForce(ordem.TimeInForce);

            if (tif != null)
            {
                orderCancelReplaceReq.Set(tif);
            }

            orderCancelReplaceReq.Set(new OrderQty(ordem.OrderQty));


            if (ordem.TimeInForce == OrdemValidadeEnum.ValidoAteDeterminadaData)
            {
                DateTime expiredate = Convert.ToDateTime(ordem.ExpireDate);
                orderCancelReplaceReq.Set(new ExpireDate(expiredate.ToString("yyyyMMdd")));
            }

            OrdType ordType = FixMessageUtilities.deOrdemTipoParaOrdType(ordem.OrdType);

            if (ordType != null)
            {
                orderCancelReplaceReq.Set(ordType);
            }

            //if (ordem.OrdType == OrdemTipoEnum.StopLimitada )
            //{
            //    ordersingle.set(new StopPx(ordem.StopPx));
            //}

            orderCancelReplaceReq.Set(new TransactTime(DateTime.Now));
            // Cliente
            QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup PartyIDGroup1 = new QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup();
            PartyIDGroup1.Set(new PartyID(ordem.ExecBroker));
            PartyIDGroup1.Set(new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE));
            PartyIDGroup1.Set(new PartyRole(PartyRole.ENTERING_TRADER));

            // Corretora
            QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup PartyIDGroup2 = new QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup();
            PartyIDGroup2.Set(new PartyID("227"));
            PartyIDGroup2.Set(new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE));
            PartyIDGroup2.Set(new PartyRole(PartyRole.ENTERING_FIRM));

            // Location ID
            QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup PartyIDGroup3 = new QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup();
            if (ordem.SenderLocation != null && ordem.SenderLocation.Length > 0)
            {
                PartyIDGroup3.Set(new PartyID(ordem.SenderLocation));
            }
            else
            {
                PartyIDGroup3.Set(new PartyID("GRA"));
            }
            PartyIDGroup3.Set(new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE));
            PartyIDGroup3.Set(new PartyRole(54));

            orderCancelReplaceReq.AddGroup(PartyIDGroup1);
            orderCancelReplaceReq.AddGroup(PartyIDGroup2);
            orderCancelReplaceReq.AddGroup(PartyIDGroup3);

            //BEI - 2012-Nov-14
            if (ordem.ForeignFirm != null && ordem.ForeignFirm.Length > 0)
            {
                QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup PartyIDGroup4 = new QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup();
                PartyIDGroup4.Set(new PartyID(ordem.ForeignFirm));
                PartyIDGroup4.Set(new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE));
                PartyIDGroup4.Set(new PartyRole(PartyRole.FOREIGN_FIRM));

                orderCancelReplaceReq.AddGroup(PartyIDGroup4);
            }

            if (ordem.Account > 0)
            {
                QuickFix.FIX44.OrderCancelReplaceRequest.NoAllocsGroup noAllocsGrp = new QuickFix.FIX44.OrderCancelReplaceRequest.NoAllocsGroup();
                noAllocsGrp.Set(new AllocAccount(ordem.Account.ToString()));
                noAllocsGrp.Set(new AllocAcctIDSource(99));
                orderCancelReplaceReq.AddGroup(noAllocsGrp);
            }

            //SelfTradeProtection - 2012-Nov-27
            if (ordem.InvestorID != null && ordem.InvestorID.Length > 0)
            {
                QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup PartyIDGroup5 = new QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup();
                PartyIDGroup5.Set(new PartyID(ordem.InvestorID));
                PartyIDGroup5.Set(new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE));
                PartyIDGroup5.Set(new PartyRole(PartyRole.INVESTOR_ID));

                orderCancelReplaceReq.AddGroup(PartyIDGroup5);
            }

            if (ordem.ExecutingTrader != null && ordem.ExecutingTrader.Length > 0)
            {
                QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup PartyIDGroup7 = new QuickFix.FIX44.OrderCancelReplaceRequest.NoPartyIDsGroup();
                PartyIDGroup7.Set(new PartyID(ordem.ExecutingTrader));
                PartyIDGroup7.Set(new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE));
                PartyIDGroup7.Set(new PartyRole(PartyRole.EXECUTING_TRADER));

                orderCancelReplaceReq.AddGroup(PartyIDGroup7);
            }

            // Memo Field
            if (ordem.Memo5149 != null && ordem.Memo5149.Length > 0)
            {
                if (ordem.Memo5149.Length > 50)
                {
                    ordem.Memo5149 = ordem.Memo5149.Substring(0, 50);
                }

                StringField memo5149 = new StringField(5149, ordem.Memo5149);

                orderCancelReplaceReq.SetField(memo5149);
            }

            bool bRet = Session.SendToTarget(orderCancelReplaceReq, _session.SessionID);

            return(bRet);
        }