Esempio n. 1
0
        /*
         * public void SaveSessionIDS(string filename)
         * {
         *  FileStream fs = File.Open(filename, FileMode.Create, FileAccess.Write);
         *  try
         *  {
         *      // Serialize Dictionary to dat file
         *      List<TOMessageBackup> lst = new List<TOMessageBackup>();
         *      if (!_serializing)
         *      {
         *          _serializing = true;
         *          Dictionary<string, TOOrderSession> regs = new Dictionary<string, TOOrderSession>(_dicMsgsCl);
         *
         *
         *          foreach (KeyValuePair<string, TOOrderSession> item in regs)
         *          {
         *              TOMessageBackup to = new TOMessageBackup();
         *
         *              to.Key = item.Key;
         *              to.BeginString = item.Value.Sessao.BeginString;
         *              to.SenderCompID = item.Value.Sessao.SenderCompID;
         *              to.SenderSubID = item.Value.Sessao.SenderSubID;
         *              to.TargetCompID = item.Value.Sessao.TargetCompID;
         *              to.TargetSubID = item.Value.Sessao.TargetSubID;
         *              to.TipoExpiracao = item.Value.TipoExpiracao;
         *              to.DataExpiracao = item.Value.DataExpiracao;
         *              to.DataEnvio = item.Value.DataEnvio;
         *              to.MsgSeqNum = item.Value.MsgSeqNum.ToString();
         *              to.ClOrdID = item.Value.ClOrdID;
         *              to.OrigClOrdID = item.Value.OrigClOrdID;
         *              to.Account = item.Value.Account.ToString();
         *              int lenPid = item.Value.PartyIDs.Count;
         *              for (int i = 0; i < lenPid; i++)
         *              {
         *                  PartyIDBackup pId = new PartyIDBackup();
         *                  pId.PartyID = item.Value.PartyIDs[i].GetField(Tags.PartyID);
         *                  pId.PartyIDSource = item.Value.PartyIDs[i].GetChar(Tags.PartyIDSource);
         *                  pId.PartyRole = item.Value.PartyIDs[i].GetInt(Tags.PartyRole);
         *                  to.PartyIDs.Add(pId);
         *              }
         *              // to.MensagemQF = item.Value.MensagemQF.ToString();
         *              to.TipoLimite = (int)item.Value.TipoLimite;
         *              to.Order = item.Value.Order;
         *              to.MensagemQF = item.Value.MensagemQF.ToString();
         *              to.ExchangeNumberID = item.Value.ExchangeNumberID;
         *              to.ExchangeSeqNum = item.Value.ExchangeSeqNum;
         *              //to.SecondaryOrderID = item.Value.SecondaryOrderID;
         *              //to.TradeDate = item.Value.TradeDate;
         *              lst.Add(to);
         *              to = null;
         *          }
         *
         *          BinaryFormatter bs = new BinaryFormatter();
         *          bs.Serialize(fs, lst);
         *          bs = null;
         *          logger.InfoFormat("SaveSessionIDS(): Registros serializados: [{0}] [{1}]", lst.Count, filename);
         *          // Efetuar limpeza da lista
         *          int len = lst.Count;
         *          for (int i = 0; i < len; i++)
         *          {
         *              TOMessageBackup aux = lst[i];
         *              aux = null;
         *          }
         *
         *          lst.Clear();
         *          lst = null;
         *          fs.Close();
         *          fs = null;
         *          regs.Clear();
         *          regs = null;
         *          _serializing = false;
         *      }
         *      else
         *      {
         *          if (_serializing)
         *              logger.Debug("SaveSessionIDS(): Processo de serializacao já em execucao!!!");
         *      }
         *  }
         *  catch (Exception ex)
         *  {
         *      logger.Error("SaveSessionIDS(): Erro na serializacao dos registros do dicionario: " + ex.Message, ex);
         *      _serializing = false; // mudar para false para tentar backupear no proximo "ciclo"
         *      fs.Close();
         *      fs = null;
         *  }
         * }
         */
        /*
         * public void LoadSessionIDs(string fileName, QuickFix.DataDictionary.DataDictionary dataDic)
         * {
         *  string msgQF = string.Empty;
         *  try
         *  {
         *      if (File.Exists(fileName))
         *      {
         *          List<TOMessageBackup> lst = new List<TOMessageBackup>();
         *          FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read);
         *          BinaryFormatter bformatter = new BinaryFormatter();
         *          lst = (List<TOMessageBackup>)bformatter.Deserialize(fs);
         *          int length = lst.Count;
         *          if (lst.Count > 0)
         *          {
         *              lock (_dicMsgsCl)
         *              {
         *                  for (int i = 0; i < length; i++)
         *                  {
         *                      TOMessageBackup to = lst[i];
         *                      TOOrderSession toOrder = new TOOrderSession();
         *                      SessionID ssID = null;
         *                      if (!string.IsNullOrEmpty(to.SenderSubID) && (!string.IsNullOrEmpty(to.TargetSubID)))
         *                          ssID = new SessionID(to.BeginString, to.SenderCompID, to.SenderSubID, to.TargetCompID, to.TargetSubID);
         *                      else
         *                          ssID = new SessionID(to.BeginString, to.SenderCompID, to.TargetCompID);
         *                      toOrder.Sessao = ssID;
         *                      toOrder.TipoExpiracao = to.TipoExpiracao;
         *                      toOrder.DataExpiracao = to.DataExpiracao;
         *                      toOrder.DataEnvio = to.DataEnvio;
         *                      toOrder.MsgSeqNum = Convert.ToInt32(to.MsgSeqNum);
         *                      toOrder.ClOrdID = to.ClOrdID;
         *                      toOrder.OrigClOrdID = to.OrigClOrdID;
         *                      toOrder.Account = Convert.ToInt32(to.Account);
         *                      int len = to.PartyIDs.Count;
         *                      for (int j = 0; j < len; j++)
         *                      {
         *                          QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup grp = new QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup();
         *                          grp.Set(new PartyID(to.PartyIDs[j].PartyID));
         *                          grp.Set(new PartyIDSource(to.PartyIDs[j].PartyIDSource));
         *                          grp.Set(new PartyRole(to.PartyIDs[j].PartyRole));
         *                          toOrder.PartyIDs.Add(grp);
         *                      }
         *
         *                      toOrder.TipoLimite = (TipoLimiteEnum)to.TipoLimite;
         *                      toOrder.Order = to.Order;
         *                      toOrder.MensagemQF = new QuickFix.Message(to.MensagemQF, dataDic, true);
         *                      toOrder.ExchangeNumberID = to.ExchangeNumberID;
         *                      toOrder.ExchangeSeqNum = to.ExchangeSeqNum;
         *                      //toOrder.SecondaryOrderID = to.SecondaryOrderID;
         *                      //toOrder.TradeDate = to.TradeDate;
         *                      _dicMsgsCl.Add(to.Key, toOrder);
         *                  }
         *              }
         *              logger.Info("LoadSessionIDs(): Registros recuperados: " + lst.Count);
         *              lst.Clear();
         *              lst = null;
         *          }
         *          if (fs != null)
         *          {
         *              fs.Close();
         *              fs = null;
         *          }
         *      }
         *  }
         *  catch (Exception ex)
         *  {
         *      logger.Error("LoadSessionIDs(): Erro na deserializacao dos registros do dicionario: MsgQF: " + msgQF + " " + ex.Message, ex);
         *  }
         * }
         */
        public void LoadOrderSessionIDsFromDB(int idSession, QuickFix.DataDictionary.DataDictionary dataDic)
        {
            try
            {
                DbFix dbFix = new DbFix();
                List <TOMessageBackup> lst = dbFix.BuscarOrderSessionIDs(idSession);

                int length = lst.Count;
                if (lst.Count > 0)
                {
                    lock (_dicMsgsCl)
                    {
                        for (int i = 0; i < length; i++)
                        {
                            TOMessageBackup to      = lst[i];
                            TOOrderSession  toOrder = new TOOrderSession();
                            SessionID       ssID    = null;
                            if (!string.IsNullOrEmpty(to.SenderSubID) && (!string.IsNullOrEmpty(to.TargetSubID)))
                            {
                                ssID = new SessionID(to.BeginString, to.SenderCompID, to.SenderSubID, to.TargetCompID, to.TargetSubID);
                            }
                            else
                            {
                                ssID = new SessionID(to.BeginString, to.SenderCompID, to.TargetCompID);
                            }
                            toOrder.Sessao        = ssID;
                            toOrder.TipoExpiracao = to.TipoExpiracao;
                            toOrder.DataExpiracao = to.DataExpiracao;
                            toOrder.DataEnvio     = to.DataEnvio;
                            toOrder.MsgSeqNum     = Convert.ToInt32(to.MsgSeqNum);
                            toOrder.ClOrdID       = to.ClOrdID;
                            toOrder.OrigClOrdID   = to.OrigClOrdID;
                            toOrder.Account       = Convert.ToInt32(to.Account);
                            int len = to.PartyIDs.Count;
                            for (int j = 0; j < len; j++)
                            {
                                QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup grp = new QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup();
                                grp.Set(new PartyID(to.PartyIDs[j].PartyID));
                                grp.Set(new PartyIDSource(to.PartyIDs[j].PartyIDSource));
                                grp.Set(new PartyRole(to.PartyIDs[j].PartyRole));
                                toOrder.PartyIDs.Add(grp);
                            }

                            toOrder.TipoLimite       = (TipoLimiteEnum)to.TipoLimite;
                            toOrder.Order            = to.Order;
                            toOrder.MensagemQF       = new QuickFix.Message(to.MensagemQF, dataDic, true);
                            toOrder.ExchangeNumberID = to.ExchangeNumberID;
                            toOrder.ExchangeSeqNum   = to.ExchangeSeqNum;
                            _dicMsgsCl.Add(to.Key, toOrder);
                        }
                    }
                    logger.Info("LoadOrderSessionIDsFromDB(): Registros recuperados: " + lst.Count);
                    lst.Clear();
                    lst = null;
                }
            }
            catch (Exception ex)
            {
                logger.Error("LoadOrderSessionIDsFromDB(): Erro na deserializacao dos registros a partir do banco de dados: " + ex.Message, ex);
            }
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        public bool EnviarOrdemCross(OrdemCrossInfo crossinfo)
        {
            OrdemInfo ordemCompra = crossinfo.OrdemInfoCompra;
            OrdemInfo ordemVenda  = crossinfo.OrdemInfoVenda;

            // Cria mensagem de new order cross -
            // Essa mensagem nao é padrao do 4.2 - some weird f*****g things can
            // f*****g happen with this f*****g shit code
            QuickFix.FIX44.NewOrderCross ordercross = new QuickFix.FIX44.NewOrderCross();

            ordercross.Set(new CrossID(crossinfo.CrossID));
            // Unico valor aceito 1 - Negocio executado total ou parcial
            ordercross.Set(new CrossType(1));
            // Prioridade de uma das pontas. Unico valor aceito: 0 - nenhuma
            ordercross.Set(new CrossPrioritization(0));


            // Ordem cross, sempre 2 pernas
            // Informacoes da perna de compra
            QuickFix.FIX44.NewOrderCross.NoSidesGroup sideGroupC = new QuickFix.FIX44.NewOrderCross.NoSidesGroup();

            sideGroupC.Set(new Side(Side.BUY));
            sideGroupC.Set(new ClOrdID(ordemCompra.ClOrdID));
            if (ordemCompra.Account > 0)
            {
                sideGroupC.Set(new Account(ordemCompra.Account.ToString()));
            }
            sideGroupC.Set(new OrderQty(ordemCompra.OrderQty));
            //sideGroupC.Set(new PositionEffect('C'));
            if (ordemCompra.PositionEffect != null && ordemCompra.PositionEffect.Equals("C"))
            {
                sideGroupC.Set(new PositionEffect('C'));
            }

            // PartIDs Compra
            // Cliente
            QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup PartyIDGroupC1 = new QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup();
            PartyIDGroupC1.Set(new PartyID(ordemCompra.ExecBroker));
            PartyIDGroupC1.Set(new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE));
            PartyIDGroupC1.Set(new PartyRole(PartyRole.ENTERING_TRADER));

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

            // Sender Location
            QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup PartyIDGroupC3 = new QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup();
            PartyIDGroupC3.Set(new PartyID("GRA"));
            PartyIDGroupC3.Set(new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE));
            PartyIDGroupC3.Set(new PartyRole(54));

            // Adiciono os partIDs ao Side (perna de Compra)
            sideGroupC.AddGroup(PartyIDGroupC1);
            sideGroupC.AddGroup(PartyIDGroupC2);
            sideGroupC.AddGroup(PartyIDGroupC3);

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

                sideGroupC.AddGroup(PartyIDGroupC4);
            }

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

                sideGroupC.AddGroup(PartyIDGroup4);
            }

            if (ordemCompra.Account > 0)
            {
                QuickFix.FIX44.NewOrderSingle.NoAllocsGroup noAllocsGrpC = new QuickFix.FIX44.NewOrderSingle.NoAllocsGroup();
                noAllocsGrpC.Set(new AllocAccount(ordemCompra.Account.ToString()));
                noAllocsGrpC.Set(new AllocAcctIDSource(99));
                sideGroupC.AddGroup(noAllocsGrpC);
            }



            // Insere informacoes da perna de venda
            QuickFix.FIX44.NewOrderCross.NoSidesGroup sideGroupV = new QuickFix.FIX44.NewOrderCross.NoSidesGroup();

            sideGroupV.Set(new Side(Side.SELL));
            sideGroupV.Set(new ClOrdID(ordemVenda.ClOrdID));
            if (ordemVenda.Account > 0)
            {
                sideGroupV.Set(new Account(ordemVenda.Account.ToString()));
            }
            sideGroupV.Set(new OrderQty(ordemVenda.OrderQty));;
            sideGroupV.Set(new PositionEffect('C'));
            if (ordemVenda.PositionEffect != null && ordemVenda.PositionEffect.Equals("C"))
            {
                sideGroupV.Set(new PositionEffect('C'));
            }

            // Cliente
            QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup PartyIDGroup1 = new QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup();
            PartyIDGroup1.Set(new PartyID(ordemVenda.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));

            // Sender Location ID
            QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup PartyIDGroup3 = new QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup();
            PartyIDGroup3.Set(new PartyID("GRA"));
            PartyIDGroup3.Set(new PartyIDSource(PartyIDSource.PROPRIETARY_CUSTOM_CODE));
            PartyIDGroup3.Set(new PartyRole(54));

            // Adiciono os partIDs ao Side (perna de venda)
            sideGroupV.AddGroup(PartyIDGroup1);
            sideGroupV.AddGroup(PartyIDGroup2);
            sideGroupV.AddGroup(PartyIDGroup3);

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

                sideGroupV.AddGroup(PartyIDGroupV4);
            }

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

                sideGroupV.AddGroup(PartyIDGroup4);
            }

            if (ordemVenda.Account > 0)
            {
                QuickFix.FIX44.NewOrderSingle.NoAllocsGroup noAllocsGrpV = new QuickFix.FIX44.NewOrderSingle.NoAllocsGroup();
                noAllocsGrpV.Set(new AllocAccount(ordemVenda.Account.ToString()));
                noAllocsGrpV.Set(new AllocAcctIDSource(99));
                sideGroupV.AddGroup(noAllocsGrpV);
            }

            // Insere os grupos das 2 pernas
            ordercross.AddGroup(sideGroupC);
            ordercross.AddGroup(sideGroupV);

            ordercross.Set(new Symbol(crossinfo.Symbol));
            if (crossinfo.SecurityID != null &&
                crossinfo.SecurityID.Length > 0)
            {
                ordercross.Set(new SecurityID(crossinfo.SecurityID));
                ordercross.Set(new SecurityIDSource("8"));
            }
            OrdType ordType = FixMessageUtilities.deOrdemTipoParaOrdType(crossinfo.OrdType);

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

            ordercross.Set(new Price(Convert.ToDecimal(crossinfo.Price)));
            ordercross.Set(new TransactTime(DateTime.Now));

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

                StringField memo5149 = new StringField(5149, crossinfo.Memo5149);
                ordercross.SetField(memo5149);
            }

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

            return(bRet);
        }
Esempio n. 4
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);
        }