public bool AddOrder(string chave, TOOrderSession item) { try { lock (_dicMsgsCl) { _dicMsgsCl.Add(chave, item); } return(true); } catch (Exception ex) { logger.Error("Erro ao adicionar o TOOrderSession: " + ex.Message, ex); return(false); } }
/// <summary> /// Manipulacao de dicionario /// Mensagem BusinessMessageReject (BMR and RReject Messages) /// </summary> /// <param name="ss"></param> /// <param name="dic"></param> /// <param name="strChave"></param> //public void VerifyOrderSituationBMRandR(SessionID ss, Dictionary<string, TOOrderSession> dic, string strChave) public void VerifyOrderSituationBMRandR(SessionID ss, OrderSessionManager dic, string strChave) { try { if (null != ss) // Registry found { // Se houve rejeicao de cancelamento, entao somente excluir do dicionario lock (dic) { TOOrderSession toOS = null; dic.GetOrder(strChave, out toOS); toOS = null; dic.RemoveOrder(strChave); } } } catch (Exception ex) { logger.Error("VerifyOrderSituationMBR(): " + ex.Message, ex); } }
/// <summary> /// Manipulacao do dicionario em situacoes /// (Mensagens de Order Cancel Reject - OCR) /// </summary> /// <param name="ss"></param> /// <param name="dic"></param> /// <param name="strChave"></param> //public void VerifyOrderSituationOCR(SessionID ss, Dictionary<string, TOOrderSession> dic, string strChave) // OBS :strChave sempre ClOrdID até momento public void VerifyOrderSituationOCR(OrderCancelReject msg, SessionID ss, OrderSessionManager dic, string strChave, string strChaveExch) { try { if (null != ss) // Registry found { TOOrderSession aux = null; dic.GetOrder(strChave, out aux); if (null != aux && msg.IsSetField(Tags.OrderID) && msg.OrderID.getValue() != "NONE") { if (msg.IsSetField(Tags.Account)) { aux.ExchangeNumberID = msg.OrderID.getValue() + "-" + msg.Account.getValue() + "-" + msg.Symbol.getValue(); } } // Se houve rejeicao de cancelamento, entao somente excluir do dicionario lock (dic) { TOOrderSession toOS = null; int ret = dic.GetOrder(strChave, out toOS, strChaveExch); if (null != toOS) { if (ret == FindType.EXCHANGE_NUMBER) { dic.RemoveOrder(toOS.ChaveDicionario); } else { dic.RemoveOrder(strChave); } } toOS = null; } } } catch (Exception ex) { logger.Error("VerifyOrderSituationOCR(): " + ex.Message, ex); } }
// 1: return by chave // 2: return by chaveExchNumber public int GetOrder(string chave, out TOOrderSession to, string chaveExchNumber = "", int keyType = 1, string exchange = "") { try { //TOOrderSession ord = null; lock (_dicMsgsCl) { if (_dicMsgsCl.TryGetValue(chave, out to)) { return(FindType.CLORDID); } else { //if (string.IsNullOrEmpty(chaveExchNumber)) // return FindType.CLORDID; { //KeyValuePair<string, TOOrderSession> item = new KeyValuePair<string, TOOrderSession>(); List <TOOrderSession> lst = _dicMsgsCl.Select(x => x.Value).Where(x => x.ExchangeNumberID == chaveExchNumber).ToList(); TOOrderSession item = null; if (KeyType.CLORDID == keyType) { item = lst.OrderByDescending(x => x.ExchangeSeqNum).FirstOrDefault(); } else { item = lst.OrderByDescending(x => x.ExchangeSeqNum).LastOrDefault(); } to = item; if (null != to) { return(FindType.EXCHANGE_NUMBER); } // Nao achou a ordem. Tentará busca a partir do banco de dados. //TODO[FF]: Fazer a busca e remontar o TO com as informacoes existentes else { string[] arr = chave.Split('-'); string clord; string strAcc = string.Empty; int account; string symbol; if (arr.Length != 3) { return(FindType.UNDEFINED); } clord = arr[0]; if (exchange.Equals(ExchangePrefixes.BOVESPA, StringComparison.InvariantCultureIgnoreCase)) { strAcc = arr[1].Substring(0, arr[1].Length - 1); } else { strAcc = arr[1]; } account = Convert.ToInt32(strAcc); symbol = arr[2]; DbFix db = new DbFix(); OrderDbInfo retOrd = db.BuscarOrdem(clord, account, symbol); if (0 == retOrd.OrderID) { to = null; return(FindType.UNDEFINED); } else { TOOrderSession toAux = this.OrderDbInfo2OrderSession(retOrd, chave); to = toAux; return(FindType.DB); } } } } } to = null; return(FindType.UNDEFINED); } catch (Exception ex) { logger.Error("Erro ao buscar o TOOrderSession: " + ex.Message, ex); to = null; return(FindType.UNDEFINED); } }
private TOOrderSession OrderDbInfo2OrderSession(OrderDbInfo ord, string chave) { try { TOOrderSession ret = new TOOrderSession(); string ss = ord.SessionIDOriginal; string [] aux = ss.Split(new string[] { ":", "/", "->" }, StringSplitOptions.RemoveEmptyEntries); string beginstr; string sender; string sendersub = string.Empty; string target; string targetsub = string.Empty; // Se tiver '/', existe sender sub e targetsub bool isSub = false; if (ss.IndexOf("/") > 0) { beginstr = aux[0]; sender = aux[1]; sendersub = aux[2]; target = aux[3]; targetsub = aux[4]; isSub = true; } else { beginstr = aux[0]; sender = aux[1]; target = aux[2]; } if (isSub) { ret.Sessao = new QuickFix.SessionID(beginstr, sender, sendersub, target, targetsub); } else { ret.Sessao = new QuickFix.SessionID(beginstr, sender, target); } ret.TipoExpiracao = ord.TimeInForce; ret.DataExpiracao = ord.ExpireDate == DateTime.MinValue? "" : ord.ExpireDate.ToString("yyyyMMdd"); ret.DataEnvio = ord.RegisterTime.ToString("yyyyMMdd-HH:mm:ss.fff"); ret.MsgSeqNum = ord.FixMsgSeqNum; ret.OrigClOrdID = ord.OrigClOrdID; ret.ClOrdID = ord.ClOrdID; ret.Account = ord.Account; ret.ExchangeNumberID = ord.ExchangeNumberID; ret.ExchangeSeqNum = 1; ret.ChaveDicionario = chave; // Remontar a mensagem fix string msg = ord.MsgFix.Replace('|', '\x01'); ret.MensagemQF = new QuickFix.Message(msg, this.Dict, true); // Remontar party id a partir da mensagem fix int lenPID = ret.MensagemQF.IsSetField(Tags.NoPartyIDs) ? ret.MensagemQF.GetInt(Tags.NoPartyIDs) : 0; for (int x = 1; x <= lenPID; x += 1) { Group noPartyIds = new Group(ret.MensagemQF.GetGroup(x, Tags.NoPartyIDs)); ret.PartyIDs.Add(noPartyIds); } return(ret); } catch (Exception ex) { logger.Error("OrderDbInfo2OrderSession(): " + ex.Message, ex); return(null); } }
public void ExpireOrderSessions() { try { List <string> lstToPurge = new List <string>(); Dictionary <string, TOOrderSession> dicAux = _dicMsgsCl; lock (dicAux) { if (dicAux.Count > 0) { foreach (KeyValuePair <string, TOOrderSession> item in dicAux) { TOOrderSession aux = item.Value; DateTime dtSendingTime = DateTime.MinValue; DateTime dtExpireDate; switch (aux.TipoExpiracao) { // Validar a DateTimeNow com SendingTime case "": // Day case "0": // Day case "3": // Immediate or Cancel case "4": // Fill Or Kill case "7": // At the close case "A": // Good For Auction { // Valida a data de envio para expiracao da ordem if (string.IsNullOrEmpty(aux.DataEnvio)) { if (aux.MensagemQF != null) { string dtAux = aux.MensagemQF.Header.GetField(Tags.SendingTime); dtSendingTime = !string.IsNullOrEmpty(dtAux)? DateTime.ParseExact(dtAux, "yyyyMMdd-HH:mm:ss.fff", CultureInfo.InvariantCulture).ToLocalTime() : DateTime.MinValue; } } else { dtSendingTime = DateTime.ParseExact(aux.DataEnvio, "yyyyMMdd-HH:mm:ss.fff", CultureInfo.InvariantCulture).ToLocalTime(); } if (dtSendingTime.Date < DateTime.Now.Date || dtSendingTime == DateTime.MinValue) { lstToPurge.Add(item.Key); } } break; case "6": // Good till Date { if (!string.IsNullOrEmpty(aux.DataExpiracao)) { dtExpireDate = DateTime.ParseExact(aux.DataExpiracao, "yyyyMMdd", CultureInfo.InvariantCulture); if (dtExpireDate.Date < DateTime.Now.Date) { lstToPurge.Add(item.Key); } } } break; } } } // Delete itens from dictionary foreach (string it in lstToPurge) { TOOrderSession xx = null; dicAux.TryGetValue(it, out xx); xx = null; dicAux.Remove(it); logger.Info("Chave removida: " + it); } lstToPurge.Clear(); lstToPurge = null; } } catch (Exception ex) { logger.Error("ExpireOrderSessions(): " + ex.Message, ex); } }
/* * 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); } }
/// <summary> /// Validar os estados da ordem e executar as respectivas operacoes no dicionario de mensagens /// (Mensagens de Execution Report - ER) /// </summary> /// <param name="msg"></param> /// <param name="eType"></param> /// <param name="oStatus"></param> /// <param name="dic"></param> /// Retorna o TOORderSession referente ao Execution Report /// OBS: strChave - no momento esta considerando sempre clOrdID public void VerifyOrderSituationER(ExecutionReport msg, char e, char o, SessionID ss, OrderSessionManager dic, string strChave, out TOOrderSession ord) { ord = null; try { string strOrigClChave = string.Empty; string strExchNumberChave = msg.OrderID.ToString() + "-" + msg.Account.ToString() + "-" + msg.Symbol.ToString(); if (null != ss)// Registry found { // Atualiza o TO com o ExchangeNumber (Tag 37, order ID) TOOrderSession aux = null; dic.GetOrder(strChave, out aux); if (null != aux) { aux.ExchangeNumberID = strExchNumberChave; } // New Order, Exec: New, OrdStatus: New - nao faz nada, somente retorna o TOOrderSession correspondente if (e == ExecType.NEW && o == OrdStatus.NEW) { // Por ser new order, nao se aplica fazer busca pelo exchange number id // pois ainda não foi atualizado dic.GetOrder(strChave, out ord); //ord.ExchangeNumberID = msg.OrderID.getValue() + "-" + msg.Account.getValue() + "-" + msg.Symbol.getValue(); } // Retornar o TOOrderSession correspondente para if (o == OrdStatus.FILLED || o == OrdStatus.PARTIALLY_FILLED) { dic.GetOrder(strChave, out ord, strExchNumberChave); //ord.ExchangeNumberID = msg.OrderID.getValue() + "-" + msg.Account.getValue() + "-" + msg.Symbol.getValue(); } // Stop Order Entry, Exec: New, OrdStatus: New - nao faz nada // No request stop order trigger, Exec: New, OrdStatus: New - nao faz nada // Order with on close, Exec: New, OrdStatus: New // Order with on close attribute is activated when the closing auction starts, Exec: New, Order: New // MinQty order entry, not enough quantity, Exec: new, Order: new // New Order, Exec: Rejected, OrdStatus: Rejected - excluir a chave // New Order, Exec: Suspended, OrdStatus: Suspended // No request, Exec: Trade, OrdStatus: Filled if ((e == ExecType.REJECTED && o == OrdStatus.REJECTED) || (e == ExecType.SUSPENDED && o == OrdStatus.SUSPENDED) || (e == ExecType.TRADE && o == OrdStatus.FILLED) || (e == ExecType.EXPIRED && o == OrdStatus.EXPIRED)) { lock (dic) { TOOrderSession toOS = null; int ret = dic.GetOrder(strChave, out toOS, strExchNumberChave); ord = toOS; toOS = null; if (ret == FindType.EXCHANGE_NUMBER) { dic.RemoveOrder(ord.ChaveDicionario); } else { dic.RemoveOrder(strChave); } } } // Order Modify, Exec: Replace, OrdStatus: Replaced if (e == ExecType.REPLACE && o == OrdStatus.REPLACED) { strOrigClChave = msg.OrigClOrdID.ToString() + "-" + msg.Account.ToString() + "-" + msg.Symbol.ToString(); lock (dic) { //if (dic.ExistOrder(strOrigClChave)) //{ TOOrderSession toOS = null; int ret = dic.GetOrder(strOrigClChave, out toOS, strExchNumberChave, KeyType.ORIGCLORDID); ord = toOS; toOS = null; if (ret == FindType.EXCHANGE_NUMBER) { dic.RemoveOrder(ord.ChaveDicionario); } else { dic.RemoveOrder(strOrigClChave); } //} } } // Cancelation, Exec: Cancelled, OrdStatus: Cancelled // No request FAK Partially Filled, Exec: Canceled, OrdStatus: Canceled // No request FOK Partially Filled, Exec: Canceled, OrdStatus: Canceled // MinQty order entry, not enough quantity rejected, Exec: Cancelled, Order: Cancelled if (e == ExecType.CANCELED && o == OrdStatus.CANCELED) { bool processOrig = true; // Tratamento de outros tipos de timeinforce (para execucao e cancelamento, o orig clord id não é fornecido) if (msg.IsSetField(Tags.TimeInForce)) { switch (msg.TimeInForce.getValue()) { case TimeInForce.IMMEDIATE_OR_CANCEL: case TimeInForce.FILL_OR_KILL: processOrig = false; strOrigClChave = msg.ClOrdID.ToString() + "-" + msg.Account.ToString() + "-" + msg.Symbol.ToString(); break; default: { // Caso o cancelamento tenha partido da bolsa, tambem nao eh fornecido o OrigClOrdID // entao tenta-se utilizar o ClOrdID vindo do ExecutionReport string aux1 = msg.IsSetOrigClOrdID() ? msg.OrigClOrdID.ToString() : msg.ClOrdID.ToString(); strOrigClChave = aux1 + "-" + msg.Account.ToString() + "-" + msg.Symbol.ToString(); } break; } } else { // TimeInForce = 0 (DAY) - Default // Caso o Orig nao esteja na mensagem, tenta-se buscar pelo ClOrdID string aux1 = msg.IsSetOrigClOrdID() ? msg.OrigClOrdID.ToString() : msg.ClOrdID.ToString(); strOrigClChave = aux1 + "-" + msg.Account.ToString() + "-" + msg.Symbol.ToString(); } lock (dic) { TOOrderSession toOS = null; int ret = 0; ret = dic.GetOrder(strOrigClChave, out toOS, strExchNumberChave, KeyType.ORIGCLORDID); if (null != toOS) { ord = toOS; toOS = null; if (ret == FindType.EXCHANGE_NUMBER) { dic.RemoveOrder(ord.ChaveDicionario); } else { dic.RemoveOrder(strOrigClChave); } } if (processOrig) { ret = dic.GetOrder(strChave, out toOS, strExchNumberChave); if (ret == FindType.EXCHANGE_NUMBER) { dic.RemoveOrder(toOS.ChaveDicionario); } else { dic.RemoveOrder(strChave); } toOS = null; } } } } } catch (Exception ex) { logger.Error("VerifyOrderSituationER(): " + ex.Message, ex); } }