Exemplo n.º 1
0
        public void ProcessOrderCancelReplaceRequest(QuickFix.Message msg, string bolsa)
        {
            try
            {
                OrderCancelReplaceRequest ocrr = (OrderCancelReplaceRequest)msg;
                string origclordid             = ocrr.OrigClOrdID.getValue();
                int    account;
                if (bolsa.Equals(ExchangePrefixes.BOVESPA, StringComparison.InvariantCultureIgnoreCase))
                {
                    string acc = ocrr.Account.getValue();
                    acc     = (acc.Remove(acc.Length - 1));
                    account = Convert.ToInt32(acc);
                }
                else
                {
                    account = Convert.ToInt32(ocrr.Account.getValue());
                }
                // Buscar ordem original
                OrderDbInfo orderOrig = _db.BuscarOrdem(origclordid, account, ocrr.Symbol.getValue());
                // Adicionar as informacoes da ordem original na tabela de upodate
                if (orderOrig.OrderID == 0)
                {
                    logger.Info("OCRR - Nao achou a ordem em questao!!!: " + origclordid);
                    return;
                }
                // Adicionar informacoes originais na tabela update
                OrderDbUpdateInfo updt = new OrderDbUpdateInfo();
                updt.Account             = orderOrig.Account;
                updt.Instrumento         = orderOrig.Symbol;
                updt.OrderID             = orderOrig.OrderID;
                updt.ClOrdID             = orderOrig.ClOrdID;
                updt.OrdStatusID         = orderOrig.OrdStatus;
                updt.Price               = orderOrig.Price;
                updt.Quantidade          = orderOrig.OrderQty;
                updt.Quantidade_Exec     = orderOrig.CumQty;
                updt.Quantidade_Aparente = Convert.ToInt32(orderOrig.OrderQtyApar);
                updt.Quantidade_Minima   = Convert.ToInt32(orderOrig.OrderQtyMin);
                updt.Dt_Validade         = orderOrig.ExpireDate;
                updt.Dt_Atualizacao      = orderOrig.TransactTime;
                updt.TimeInForce         = orderOrig.TimeInForce;
                if (!_db.InserirOrdermUpdate(updt))
                {
                    logger.Error("Erro ao inserir o registro na tabela tb_fix_order_update");
                    return;
                }

                // Adicionar as novas informacoes pertinentes na tabela detail
                OrderDbDetalheInfo detail = new OrderDbDetalheInfo();
                detail.OrderID       = orderOrig.OrderID;
                detail.TransactID    = "";
                detail.OrderQty      = Convert.ToInt32(ocrr.OrderQty.getValue());
                detail.Price         = ocrr.IsSetField(Tags.Price)? ocrr.Price.getValue(): Decimal.Zero;
                detail.OrderStatusID = (int)FixOrderStatus.SUBSTITUICAO_SOLICITADA;
                detail.TransactTime  = ocrr.TransactTime.getValue();
                detail.Description   = DescMsg.OCRR_PENDING_MODIFICATION;
                detail.FixMsgSeqNum  = Convert.ToInt32(ocrr.Header.GetField(Tags.MsgSeqNum));
                if (!_db.InserirOrdemDetalhe(detail, orderOrig.ClOrdID))
                {
                    logger.Error("Erro ao inserir o registro na tabela tb_fix_order_update - msg de modificacao");
                    return;
                }

                // Atualizar a informacao original na tabela tb_fix_order
                // Atualiza os campos gravados na tabela tb_fix_order_update
                // Ordem original já se encontra em orderOrig
                orderOrig.OrigClOrdID  = ocrr.OrigClOrdID.getValue();
                orderOrig.ClOrdID      = ocrr.ClOrdID.getValue();
                orderOrig.OrdStatus    = (int)FixOrderStatus.SUBSTITUICAO_SOLICITADA;
                orderOrig.TransactTime = ocrr.TransactTime.getValue();
                if (ocrr.IsSetField(Tags.ExpireDate))
                {
                    orderOrig.ExpireDate = DateTime.ParseExact(ocrr.ExpireDate.getValue() + "235959", "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
                }
                else
                {
                    orderOrig.ExpireDate = DateTime.MinValue;
                }
                orderOrig.TimeInForce  = ocrr.IsSetField(Tags.TimeInForce) ? ocrr.TimeInForce.getValue().ToString() : "0";
                orderOrig.OrderQty     = Convert.ToInt32(ocrr.OrderQty.getValue());
                orderOrig.OrderQtyMin  = ocrr.IsSetField(Tags.MinQty) ? ocrr.MinQty.getValue() : 0;
                orderOrig.OrderQtyApar = ocrr.IsSetField(Tags.MaxFloor) ? ocrr.MaxFloor.getValue() : 0;
                orderOrig.Price        = ocrr.IsSetField(Tags.Price)? ocrr.Price.getValue(): Decimal.Zero;
                orderOrig.Memo         = ocrr.IsSetField(Tags.Memo) ? ocrr.GetField(Tags.Memo): string.Empty;
                orderOrig.FixMsgSeqNum = Convert.ToInt32(ocrr.Header.GetField(Tags.MsgSeqNum));
                orderOrig.MsgFix       = ocrr.ToString().Replace('\x01', '|');
                orderOrig.HandlInst    = ocrr.IsSetField(Tags.HandlInst) ? ocrr.HandlInst.getValue().ToString() : string.Empty;
                if (!_db.AtualizarOrdem(orderOrig))
                {
                    logger.Info("Problemas na atualizacao da ordem (mensagem de modificacao). ClOrdID: " + orderOrig.ClOrdID);
                    return;
                }

                DropCopyCallbackManager.Instance.EnqueueCallback(orderOrig);
            }
            catch (Exception ex)
            {
                logger.Error("Problemas no processamento da mensagem OrderCancelReplaceRequest: " + ex.Message, ex);
            }
        }
Exemplo n.º 2
0
        public static void ConsistirAlteracaoOrdem(OrderCancelReplaceRequest occr, string exchange)
        {
            // Basic order identification
            if (string.IsNullOrEmpty(occr.Account.ToString()))
            {
                throw new ArgumentException("Account field must be provided");
            }

            if (string.IsNullOrEmpty(occr.OrigClOrdID.ToString()))
            {
                //if (ordem.OrigClOrdID == null || ordem.OrigClOrdID.Length <= 0)
                throw new ArgumentException("OrigClOrdID field must be provided");
            }

            if (string.IsNullOrEmpty(occr.ClOrdID.ToString()))
            {
                throw new ArgumentException("ClOrdID field must be provided");
            }

            // Instrument Identification Block
            if (string.IsNullOrEmpty(occr.Symbol.ToString()))
            {
                throw new ArgumentException("Symbol must be provided");
            }

            // "obrigatorio" somente para bmf (nao obrigatorio na mensageria, mas dah erro se enviar
            // sem este campo para bmf)
            //if (exchange.Equals(ExchangePrefixes.BMF, StringComparison.InvariantCultureIgnoreCase))
            //{
            //    if (string.IsNullOrEmpty(occr.SecurityID.ToString()))
            //        throw new ArgumentException("SecurityID field must be provided");
            //}

            if (string.IsNullOrEmpty(occr.OrderQty.ToString()))
            {
                throw new ArgumentException("OrderQty must be > 0 ");
            }

            if (occr.IsSetField(Tags.NoPartyIDs))
            {
                try
                {
                    Group partyIDs = occr.GetGroup(1, Tags.NoPartyIDs);
                    if (string.IsNullOrEmpty(partyIDs.GetField(Tags.PartyID)))
                    {
                        throw new ArgumentException("ExecBroker must be provided");
                    }
                }
                catch
                {
                    throw new ArgumentException("ExecBroker must be provided");
                }
            }
            // Valido somente se estiver atribuido, campo nao obrigatorio
            if (occr.IsSetField(Tags.TimeInForce))
            {
                TimeInForce tif = occr.TimeInForce;
                if (tif == null)
                {
                    throw new ArgumentException("TimeInForce is invalid");
                }

                if (tif.ToString().Equals(TimeInForce.GOOD_TILL_DATE.ToString()))
                {
                    if (!string.IsNullOrEmpty(occr.ExpireDate.ToString()))
                    {
                        DateTime dtExpireDate = DateTime.ParseExact(occr.ExpireDate.ToString(), "yyyyMMdd", CultureInfo.InvariantCulture);
                        if (dtExpireDate.Date < DateTime.Now.Date)
                        {
                            throw new ArgumentException("ExpireDate is invalid");
                        }
                    }
                    else
                    {
                        throw new ArgumentException("ExpireDate is invalid");
                    }
                }
            }

            OrdType ordType = occr.OrdType;

            if (ordType == null)
            {
                throw new ArgumentException("OrdType is invalid");
            }

            // Verifica envio do preco
            switch (ordType.ToString()[0])
            {
            case OrdType.LIMIT:                         // OrdemTipoEnum.Limitada:
            case OrdType.MARKET_WITH_LEFTOVER_AS_LIMIT: // OrdemTipoEnum.MarketWithLeftOverLimit:
                if (occr.Price.getValue() <= new Decimal(0.0))
                {
                    throw new ArgumentException("Price must be > 0");
                }
                break;

            case OrdType.STOP_LIMIT:    // OrdemTipoEnum.StopLimitada:
                //case OrdemTipoEnum.StopStart:
                if (occr.Price.getValue() <= new Decimal(0.0))
                {
                    throw new ArgumentException("Price must be > 0");
                }
                if (occr.StopPx.getValue() <= new Decimal(0.0))
                {
                    throw new ArgumentException("StopPrice must be > 0");
                }
                break;

            case OrdType.MARKET:    // OrdemTipoEnum.Mercado:
            case OrdType.ON_CLOSE:  //OrdemTipoEnum.OnClose:
                break;

            default:
                if (occr.Price.getValue() <= new Decimal(0.0))
                {
                    throw new ArgumentException("Price must be > 0");
                }
                break;
            }

            if (occr.IsSetMaxFloor())
            {
                if (occr.MaxFloor.getValue() < new Decimal(0))
                {
                    throw new ArgumentException("MaxFloor must be >= 0");
                }
            }

            if (occr.IsSetMinQty())
            {
                if (occr.MinQty.getValue() < new Decimal(0))
                {
                    throw new ArgumentException("MinQty must be >= 0");
                }
            }
        }