public void ProcessNewOrderSingle(QuickFix.Message msg, string bolsa, int operador) { try { NewOrderSingle nos = (NewOrderSingle)msg; // OBS1: Campos nao atribuidos, nao vem na mensagem; // OBS2: Esta seguindo a ordem dos campos da tabela OrderDbInfo order = new OrderDbInfo(); order.ClOrdID = nos.ClOrdID.getValue(); if (bolsa.Equals(ExchangePrefixes.BOVESPA, StringComparison.InvariantCultureIgnoreCase)) { string acc = nos.Account.getValue(); order.Account = Convert.ToInt32(acc.Remove(acc.Length - 1)); } else { order.Account = Convert.ToInt32(nos.Account.getValue()); } order.Symbol = nos.Symbol.getValue(); order.SecurityExchangeID = nos.IsSetField(Tags.SecurityID)? nos.SecurityID.getValue(): string.Empty; //Para BMF order.OrdTypeID = Conversions.Parse2FixOrderType(nos.OrdType.getValue()); order.OrdStatus = (int)FixOrderStatus.NOVA_ORDEM_SOLICITADA; // (nova ordem) order.TransactTime = nos.TransactTime.getValue(); if (nos.IsSetField(Tags.ExpireDate)) { order.ExpireDate = DateTime.ParseExact(nos.ExpireDate.getValue() + "235959", "yyyyMMddHHmmss", CultureInfo.InvariantCulture); } else { order.ExpireDate = DateTime.MinValue; } order.TimeInForce = nos.IsSetField(Tags.TimeInForce) ? nos.TimeInForce.getValue().ToString() : "0"; order.ChannelID = operador; int len = nos.NoPartyIDs.getValue(); for (int i = 0; i < len; i++) { Group grp = nos.GetGroup(i + 1, Tags.NoPartyIDs); if (null != grp) { string enteringTrader = grp.GetField(Tags.PartyRole); if (enteringTrader.Equals(PartyRole.ENTERING_TRADER.ToString())) { order.ExecBroker = grp.GetField(Tags.PartyID); break; } } } order.Side = Convert.ToInt32(nos.Side.getValue().ToString()); order.OrderQty = Convert.ToInt32(nos.OrderQty.getValue()); order.OrderQtyRemaining = order.OrderQty; // (new order, entao nada foi executado) order.OrderQtyMin = nos.IsSetField(Tags.MinQty) ? nos.MinQty.getValue() : Decimal.Zero; order.OrderQtyApar = nos.IsSetField(Tags.MaxFloor) ? nos.MaxFloor.getValue() : Decimal.Zero; order.Price = nos.IsSetField(Tags.Price) ? nos.Price.getValue() : Decimal.Zero; order.SystemID = "FixServer"; order.Memo = nos.IsSetField(Tags.Memo) ? nos.GetField(Tags.Memo): string.Empty; order.FixMsgSeqNum = Convert.ToInt32(nos.Header.GetField(Tags.MsgSeqNum)); order.SessionID = nos.GetSessionID(msg).ToString(); order.SessionIDOriginal = nos.GetString(CustomTags.ORIG_SESSION); order.IdFix = nos.GetInt(CustomTags.FIXID); order.MsgFix = nos.ToString().Replace('\x01', '|'); // Para order_detail order.Description = DescMsg.NOS_OPEN; order.HandlInst = nos.IsSetField(Tags.HandlInst) ? nos.GetField(Tags.HandlInst) : string.Empty; if (!_db.InserirOrdem(order)) { logger.Info("Problemas na insercao da ordem. ClOrdId: " + order.ClOrdID); } DropCopyCallbackManager.Instance.EnqueueCallback(order); } catch (Exception ex) { logger.Error("Problemas no processamento da mensagem NewOrderSingle: " + ex.Message, ex); } }
public static void ConsistirNovaOrdem(NewOrderSingle nos, string exchange) { // Basic order identification if (string.IsNullOrEmpty(nos.Account.ToString())) { throw new ArgumentException("Account field must be provided"); } if (string.IsNullOrEmpty(nos.ClOrdID.ToString())) { throw new ArgumentException("ClOrdID field must be provided"); } // Instrument Identification Block if (string.IsNullOrEmpty(nos.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(nos.SecurityID.ToString())) // throw new ArgumentException("SecurityID field must be provided"); //} if (string.IsNullOrEmpty(nos.OrderQty.ToString())) { throw new ArgumentException("OrderQty must be > 0 "); } // obrigatorio se for FIX.4.4 if (nos.IsSetField(Tags.NoPartyIDs)) { try { Group partyIDs = nos.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"); } } // Somente validar se o campo estiver atribuido, nao obrigatorio na mensageria if (nos.IsSetField(Tags.TimeInForce)) { TimeInForce tif = nos.TimeInForce; if (tif == null) { throw new ArgumentException("TimeInForce is invalid"); } if (tif.ToString().Equals(TimeInForce.GOOD_TILL_DATE.ToString())) { if (!string.IsNullOrEmpty(nos.ExpireDate.ToString())) { DateTime dtExpireDate = DateTime.ParseExact(nos.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 = nos.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 (nos.Price.getValue() <= new Decimal(0.0)) { throw new ArgumentException("Price must be > 0"); } break; case OrdType.STOP_LIMIT: // OrdemTipoEnum.StopLimitada: //case OrdType.STO OrdemTipoEnum.StopStart: if (nos.Price.getValue() <= new Decimal(0.0)) { throw new ArgumentException("Price must be > 0"); } if (nos.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 (nos.Price.getValue() <= new Decimal(0.0)) { throw new ArgumentException("Price must be > 0"); } break; } if (nos.IsSetMaxFloor()) { if (nos.MaxFloor.getValue() < new Decimal(0)) { throw new ArgumentException("MaxFloor must be >= 0"); } } if (nos.IsSetMinQty()) { if (nos.MinQty.getValue() < new Decimal(0)) { throw new ArgumentException("MinQty must be >= 0"); } } }