Beispiel #1
0
        public bool AlterarOrdem(OrdemInfo ordem, long ini = 0, long fim = 0, long oriini = 0, long orifim = 0, int delay = 0, string mnemonic = "", string senderSubID = "", string extraTags = "")
        {
            //Cria a mensagem FIX de NewOrderSingle
            QuickFix.FIX42.OrderCancelReplaceRequest ordercrr = new QuickFix.FIX42.OrderCancelReplaceRequest();

            ordercrr.Set(new Account(ordem.Account.ToString()));
            if (!string.IsNullOrEmpty(mnemonic))
            {
                ordercrr.SetField(new Account(mnemonic), true);
            }
            if (!string.IsNullOrEmpty(senderSubID))
            {
                ordercrr.Header.SetField(new SenderSubID(senderSubID), true);
            }
            ordercrr.Set(new Symbol(ordem.Symbol));
            ordercrr.Set(new ClOrdID(ordem.ClOrdID));
            ordercrr.Set(new OrigClOrdID(ordem.OrigClOrdID));

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

            // Armazena ClOrdID em Memo (5149) para posterior referência nos tratamentos de retornos
            QuickFix.Fields.StringField field5149 = new QuickFix.Fields.StringField(5149, ordem.ClOrdID);
            ordercrr.SetField(field5149);

            //ordersingle.Set(new IDSource());
            if (ordem.Side == OrdemDirecaoEnum.Venda)
            {
                ordercrr.Set(new Side(Side.SELL));
            }
            else
            {
                ordercrr.Set(new Side(Side.BUY));
            }

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

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

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

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

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

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

            // Para versao 4.2, os novos tipos de OrdType a serem enviados (5, A ou B);
            if (!string.IsNullOrEmpty(ordem.Memo5149))
            {
                ordercrr.SetField(new OrdType(Convert.ToChar(ordem.Memo5149)), true);
            }

            // Verifica envio do preco
            switch (ordem.OrdType)
            {
            case OrdemTipoEnum.Limitada:
            case OrdemTipoEnum.MarketWithLeftOverLimit:
            case OrdemTipoEnum.StopLimitada:
                ordercrr.Set(new Price(Convert.ToDecimal(ordem.Price)));
                break;

            case OrdemTipoEnum.StopStart:
                ordercrr.Set(new Price(Convert.ToDecimal(ordem.Price)));
                ordercrr.Set(new StopPx(Convert.ToDecimal(ordem.StopPrice)));
                break;

            case OrdemTipoEnum.Mercado:
            case OrdemTipoEnum.OnClose:
                ordercrr.Set(new Price(Convert.ToDecimal(ordem.Price)));
                break;

            default:
                ordercrr.Set(new Price(Convert.ToDecimal(ordem.Price)));
                break;
            }

            ordercrr.Set(new TransactTime(DateTime.Now));
            ordercrr.Set(new HandlInst('1'));
            ordercrr.Set(new ExecBroker("227"));

            if (ordem.MaxFloor > 0)
            {
                ordercrr.Set(new MaxFloor(Convert.ToDecimal(ordem.MaxFloor)));
            }

            if (ordem.MinQty > 0)
            {
                ordercrr.Set(new MinQty(Convert.ToDecimal(ordem.MinQty)));
            }

            QuickFix.FIX42.OrderCancelReplaceRequest.NoAllocsGroup noAllocsGrp = new QuickFix.FIX42.OrderCancelReplaceRequest.NoAllocsGroup();
            noAllocsGrp.Set(new AllocAccount("67"));
            ordercrr.AddGroup(noAllocsGrp);

            bool bRet = false;

            // Tags Customizadas Bloomberg
            if (!string.IsNullOrEmpty(extraTags))
            {
                string[] arr = extraTags.Split(';');
                for (int i = 0; i < arr.Length; i++)
                {
                    if (!string.IsNullOrEmpty(arr[i]))
                    {
                        string[]    valor = arr[i].Split('=');
                        StringField fld   = new StringField(Convert.ToInt32(valor[0]), valor[1]);
                        ordercrr.SetField(fld);
                    }
                }
            }

            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());
                    ordercrr.ClOrdID     = xx;
                    ordercrr.OrigClOrdID = xx2;

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

            return(bRet);
        }
        /*
         * 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);
        }
        public bool EnviarOrdem(OrdemInfo ordem, long ini = 0, long fim = 0, int delay = 0, string mnemonic = "")
        {
            //Cria a mensagem FIX de NewOrderSingle
            QuickFix.FIX44.NewOrderSingle ordersingle = new QuickFix.FIX44.NewOrderSingle();

            if (ordem.Account > 0)
            {
                ordersingle.Set(new Account(ordem.Account.ToString()));
            }
            if (!string.IsNullOrEmpty(mnemonic))
            {
                ordersingle.SetField(new Account(mnemonic), true);
            }
            // Instrument Identification Block
            ordersingle.Set(new Symbol(ordem.Symbol));
            if (!string.IsNullOrEmpty(ordem.SecurityID))
            {
                ordersingle.Set(new SecurityID(ordem.SecurityID));
            }
            ordersingle.Set(new SecurityIDSource(SecurityIDSource.EXCHANGE_SYMBOL));
            //long clOrd =0;

            ordersingle.Set(new ClOrdID(ordem.ClOrdID));

            if (ordem.Side == OrdemDirecaoEnum.Venda)
            {
                ordersingle.Set(new Side(Side.SELL));
            }
            else
            {
                ordersingle.Set(new Side(Side.BUY));
            }


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

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

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

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

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

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

            // Verifica envio do preco
            switch (ordem.OrdType)
            {
            case OrdemTipoEnum.Limitada:
            case OrdemTipoEnum.MarketWithLeftOverLimit:
                ordersingle.Set(new Price(Convert.ToDecimal(ordem.Price)));
                break;

            case OrdemTipoEnum.StopLimitada:
            case OrdemTipoEnum.StopStart:
                ordersingle.Set(new Price(Convert.ToDecimal(ordem.Price)));
                ordersingle.Set(new StopPx(Convert.ToDecimal(ordem.StopPrice)));
                break;

            case OrdemTipoEnum.Mercado:
            case OrdemTipoEnum.OnClose:
                break;

            default:
                ordersingle.Set(new Price(Convert.ToDecimal(ordem.Price)));
                break;
            }

            ordersingle.Set(new TransactTime(DateTime.Now));
            ordersingle.Set(new HandlInst('1'));

            if (ordem.MaxFloor > 0)
            {
                ordersingle.Set(new MaxFloor(Convert.ToDecimal(ordem.MaxFloor)));
            }

            if (ordem.MinQty > 0)
            {
                ordersingle.Set(new MinQty(Convert.ToDecimal(ordem.MinQty)));
            }

            // Cliente
            QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup PartyIDGroup1 = new QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup();
            //PartyIDGroup1.set(new PartyID(ordem.Account.ToString()));
            PartyIDGroup1.Set(new PartyID(ordem.ExecBroker));
            PartyIDGroup1.Set(new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE));
            PartyIDGroup1.Set(new PartyRole(PartyRole.ENTERING_TRADER));

            // Corretora
            QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup PartyIDGroup2 = new QuickFix.FIX44.NewOrderSingle.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.NewOrderSingle.NoPartyIDsGroup PartyIDGroup3 = new QuickFix.FIX44.NewOrderSingle.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(PartyRole.SENDER_LOCATION));

            // Corretora
            QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup PartyIDGroup4 = new QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup();
            PartyIDGroup4.Set(new PartyID("245427"));
            PartyIDGroup4.Set(new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE));
            PartyIDGroup4.Set(new PartyRole(PartyRole.ENTERING_FIRM));

            ordersingle.AddGroup(PartyIDGroup1);
            ordersingle.AddGroup(PartyIDGroup2);
            ordersingle.AddGroup(PartyIDGroup3);
            //ordersingle.AddGroup(PartyIDGroup4);

            //BEI - 2012-Nov-13
            if (ordem.ForeignFirm != null && ordem.ForeignFirm.Length > 0)
            {
                QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup PartyIDGroup5 = new QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup();
                PartyIDGroup5.Set(new PartyID(ordem.ForeignFirm));
                PartyIDGroup5.Set(new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE));
                PartyIDGroup5.Set(new PartyRole(PartyRole.FOREIGN_FIRM));
                ordersingle.AddGroup(PartyIDGroup5);
            }

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

                ordersingle.AddGroup(PartyIDGroup6);
            }

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

                ordersingle.AddGroup(PartyIDGroup7);
            }

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

            if (ordem.PositionEffect != null && ordem.PositionEffect.Equals("C"))
            {
                ordersingle.Set(new PositionEffect('C'));
            }

            // 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);

                ordersingle.SetField(memo5149);
            }

            // AccountType
            if (ordem.AcountType == ContaTipoEnum.REMOVE_ACCOUNT_INFORMATION)
            {
                IntField int581 = new IntField(581, 38);
                ordersingle.SetField(int581);
            }
            else if (ordem.AcountType == ContaTipoEnum.GIVE_UP_LINK_IDENTIFIER)
            {
                IntField int581 = new IntField(581, 40);
                ordersingle.SetField(int581);
            }
            bool bRet = false;

            if (ini == 0)
            {
                bRet = Session.SendToTarget(ordersingle, _session.SessionID);
            }
            else
            {
                //_times = times;
                //Thread th = new Thread(new ParameterizedThreadStart(ExecOrders));
                //th.Start(ordersingle);
                long times = fim - ini;
                logger.Info("=====================================> INICIO ========> Qtd: " + times);
                for (long i = 0; i < times; i++)
                {
                    //logger.Info("XXX: " + i);

                    ClOrdID xx = new ClOrdID(ini.ToString());
                    ordersingle.ClOrdID = xx;
                    bRet = Session.SendToTarget(ordersingle, _session.SessionID);
                    if (!bRet)
                    {
                        logger.Info("erro");
                        break;
                    }
                    if (0 != delay)
                    {
                        Thread.Sleep(delay);
                    }
                    ini++;
                    //System.Windows.Forms.Application.DoEvents();
                }
                logger.Info("=====================================> FIM ========> Qtd: " + times);
            }
            return(bRet);
        }
Beispiel #4
0
        public bool AlterarOrdem(OrdemInfo ordem)
        {
            //Cria a mensagem FIX de NewOrderSingle
            QuickFix.FIX42.OrderCancelReplaceRequest ordercrr = new QuickFix.FIX42.OrderCancelReplaceRequest();

            ordercrr.Set(new Account(ordem.Account.ToString()));
            ordercrr.Set(new Symbol(ordem.Symbol));
            ordercrr.Set(new ClOrdID(ordem.ClOrdID));
            ordercrr.Set(new OrigClOrdID(ordem.OrigClOrdID));

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

            // Armazena ClOrdID em Memo (5149) para posterior referência nos tratamentos de retornos
            QuickFix.Fields.StringField field5149 = new QuickFix.Fields.StringField(5149, ordem.ClOrdID);
            ordercrr.SetField(field5149);

            //ordersingle.Set(new IDSource());
            if (ordem.Side == OrdemDirecaoEnum.Venda)
            {
                ordercrr.Set(new Side(Side.SELL));
            }
            else
            {
                ordercrr.Set(new Side(Side.BUY));
            }

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

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

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

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

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

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

            // Verifica envio do preco
            switch (ordem.OrdType)
            {
            case OrdemTipoEnum.Limitada:
            case OrdemTipoEnum.MarketWithLeftOverLimit:
            case OrdemTipoEnum.StopLimitada:
                ordercrr.Set(new Price(Convert.ToDecimal(ordem.Price)));
                break;

            case OrdemTipoEnum.StopStart:
                ordercrr.Set(new Price(Convert.ToDecimal(ordem.Price)));
                ordercrr.Set(new StopPx(Convert.ToDecimal(ordem.StopPrice)));
                break;

            case OrdemTipoEnum.Mercado:
            case OrdemTipoEnum.OnClose:
                ordercrr.Set(new Price(Convert.ToDecimal(ordem.Price)));
                break;

            default:
                ordercrr.Set(new Price(Convert.ToDecimal(ordem.Price)));
                break;
            }

            ordercrr.Set(new TransactTime(DateTime.Now));
            ordercrr.Set(new HandlInst('1'));
            ordercrr.Set(new ExecBroker("227"));

            if (ordem.MaxFloor > 0)
            {
                ordercrr.Set(new MaxFloor(Convert.ToDecimal(ordem.MaxFloor)));
            }

            if (ordem.MinQty > 0)
            {
                ordercrr.Set(new MinQty(Convert.ToDecimal(ordem.MinQty)));
            }

            QuickFix.FIX42.OrderCancelReplaceRequest.NoAllocsGroup noAllocsGrp = new QuickFix.FIX42.OrderCancelReplaceRequest.NoAllocsGroup();
            noAllocsGrp.Set(new AllocAccount("67"));
            ordercrr.AddGroup(noAllocsGrp);

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

            return(bRet);
        }
Beispiel #5
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);
        }
Beispiel #6
0
        public bool EnviarOrdem(OrdemInfo ordem)
        {
            //Cria a mensagem FIX de NewOrderSingle
            QuickFix.FIX44.NewOrderSingle ordersingle = new QuickFix.FIX44.NewOrderSingle();

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

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

            ordersingle.Set(new ClOrdID(ordem.ClOrdID));

            if (ordem.Side == OrdemDirecaoEnum.Venda)
            {
                ordersingle.Set(new Side(Side.SELL));
            }
            else
            {
                ordersingle.Set(new Side(Side.BUY));
            }


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

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

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

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

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

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

            // Verifica envio do preco
            switch (ordem.OrdType)
            {
            case OrdemTipoEnum.Limitada:
            case OrdemTipoEnum.MarketWithLeftOverLimit:
                ordersingle.Set(new Price(Convert.ToDecimal(ordem.Price)));
                break;

            case OrdemTipoEnum.StopLimitada:
            case OrdemTipoEnum.StopStart:
                ordersingle.Set(new Price(Convert.ToDecimal(ordem.Price)));
                ordersingle.Set(new StopPx(Convert.ToDecimal(ordem.StopPrice)));
                break;

            case OrdemTipoEnum.Mercado:
            case OrdemTipoEnum.OnClose:
                break;

            default:
                ordersingle.Set(new Price(Convert.ToDecimal(ordem.Price)));
                break;
            }

            ordersingle.Set(new TransactTime(DateTime.Now));
            ordersingle.Set(new HandlInst('1'));

            if (ordem.MaxFloor > 0)
            {
                ordersingle.Set(new MaxFloor(Convert.ToDecimal(ordem.MaxFloor)));
            }

            if (ordem.MinQty > 0)
            {
                ordersingle.Set(new MinQty(Convert.ToDecimal(ordem.MinQty)));
            }

            // Cliente
            QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup PartyIDGroup1 = new QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup();
            //PartyIDGroup1.set(new PartyID(ordem.Account.ToString()));
            PartyIDGroup1.Set(new PartyID(ordem.ExecBroker));
            PartyIDGroup1.Set(new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE));
            PartyIDGroup1.Set(new PartyRole(PartyRole.ENTERING_TRADER));

            // Corretora
            QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup PartyIDGroup2 = new QuickFix.FIX44.NewOrderSingle.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.NewOrderSingle.NoPartyIDsGroup PartyIDGroup3 = new QuickFix.FIX44.NewOrderSingle.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(PartyRole.SENDER_LOCATION));

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

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

                ordersingle.AddGroup(PartyIDGroup5);
            }

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

                ordersingle.AddGroup(PartyIDGroup6);
            }

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

                ordersingle.AddGroup(PartyIDGroup7);
            }

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

            if (ordem.PositionEffect != null && ordem.PositionEffect.Equals("C"))
            {
                ordersingle.Set(new PositionEffect('C'));
            }

            // 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);

                ordersingle.SetField(memo5149);
            }

            // AccountType
            if (ordem.AcountType == ContaTipoEnum.REMOVE_ACCOUNT_INFORMATION)
            {
                IntField int581 = new IntField(581, 38);
                ordersingle.SetField(int581);
            }
            else if (ordem.AcountType == ContaTipoEnum.GIVE_UP_LINK_IDENTIFIER)
            {
                IntField int581 = new IntField(581, 40);
                ordersingle.SetField(int581);
            }

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

            return(bRet);
        }