private void QuotesLogin(MessageMbtQuotes message) { var writePacket = QuoteSocket.MessageFactory.Create(); string textMessage = "G|100=DEMOXJSP;8055=demo01\n"; if (debug) { log.Debug("Login response: " + textMessage); } writePacket.DataOut.Write(textMessage.ToCharArray()); QuotePacketQueue.Enqueue(writePacket, message.SendUtcTime); }
private unsafe void SymbolRequest(LimeQuoteMessage message) { LimeQuotesInterop.subscription_request_msg *subRequest = (LimeQuotesInterop.subscription_request_msg *)message.Ptr; String symbol = ""; for (int i = 0; subRequest->syb_symbols[i] != 0; i++) { symbol += (char)subRequest->syb_symbols[i]; } var symbolInfo = Factory.Symbol.LookupSymbol(symbol); log.Info("Lime: Received symbol request for " + symbolInfo); ProviderSimulator.AddSymbol(symbolInfo.Symbol); var writePacket = (LimeQuoteMessage)QuoteSocket.MessageFactory.Create(); LimeQuotesInterop.subscription_reply_msg *reply = (LimeQuotesInterop.subscription_reply_msg *)writePacket.Ptr; reply->msg_type = LimeQuotesInterop.limeq_message_type.SUBSCRIPTION_REPLY; var msg_len = (ushort)sizeof(LimeQuotesInterop.subscription_reply_msg); reply->msg_len = Reverse(msg_len); writePacket.Length = msg_len; reply->outcome = LimeQuotesInterop.subscription_outcome.SUBSCRIPTION_SUCCESSFUL; for (int i = 0; i < 4; i++) { reply->qsid[i] = subRequest->qsid[i]; } QuotePacketQueue.Enqueue(writePacket, message.SendUtcTime); var bookRebuildMessage = (LimeQuoteMessage)QuoteSocket.MessageFactory.Create(); LimeQuotesInterop.book_rebuild_msg *book = (LimeQuotesInterop.book_rebuild_msg *)bookRebuildMessage.Ptr; book->msg_type = LimeQuotesInterop.limeq_message_type.BOOK_REBUILD; msg_len = (ushort)sizeof(LimeQuotesInterop.book_rebuild_msg); book->msg_len = Reverse(msg_len); bookRebuildMessage.Length = msg_len; book->symbol_index = (uint)symbolInfo.BinaryIdentifier; for (int i = 0; i < symbol.Length; i++) { book->symbol[i] = (byte)symbol[i]; } QuotePacketQueue.Enqueue(bookRebuildMessage, message.SendUtcTime); }
private unsafe void QuotesLogin(LimeQuoteMessage packetQuotes) { LimeQuotesInterop.login_request_msg *message = (LimeQuotesInterop.login_request_msg *)packetQuotes.Ptr; if (Reverse(message->msg_len) != 80 || message->msg_type != LimeQuotesInterop.limeq_message_type.LOGIN_REQUEST || message->ver_major != LimeQuotesInterop.LIMEQ_MAJOR_VER || message->ver_minor != LimeQuotesInterop.LIMEQ_MINOR_VER || message->session_type != LimeQuotesInterop.app_type.CPP_API || message->auth_type != LimeQuotesInterop.auth_types.CLEAR_TEXT || message->heartbeat_interval != LimeQuotesInterop.heartbeat) { log.Error("Login message not matched"); } string userName = "";; for (int i = 0; i < LimeQuotesInterop.UNAME_LEN && message->uname[i] > 0; i++) { userName += (char)message->uname[i]; } string password = "";; for (int i = 0; i < LimeQuotesInterop.PASSWD_LEN && message->passwd[i] > 0; i++) { password += (char)message->passwd[i]; } var writePacket = (LimeQuoteMessage)QuoteSocket.MessageFactory.Create(); LimeQuotesInterop.login_response_msg *reseponse = (LimeQuotesInterop.login_response_msg *)writePacket.Ptr; reseponse->msg_type = LimeQuotesInterop.limeq_message_type.LOGIN_RESPONSE; reseponse->msg_len = Reverse(8); writePacket.Length = 8; reseponse->ver_minor = message->ver_minor; reseponse->ver_major = message->ver_major; reseponse->heartbeat_interval = message->heartbeat_interval; reseponse->timeout_interval = message->timeout_interval; reseponse->response_code = LimeQuotesInterop.reject_reason_code.LOGIN_SUCCEEDED; QuotePacketQueue.Enqueue(writePacket, packetQuotes.SendUtcTime); }
unsafe private void SendSide(SymbolInfo symbol, TickIO tick, bool isSell) { var symbolID = symbol.BinaryIdentifier; var lastTick = lastTicks[symbol.BinaryIdentifier]; var message = QuoteSocket.MessageFactory.Create(); var quoteMessage = (LimeQuoteMessage)message; var isTrade = tick.IsTrade; var isQuote = tick.IsQuote; if (trace) { log.TraceFormat("quote isTrade={0} isQuote={1}", isTrade, isQuote); } if (isTrade) { var trade = (LimeQuotesInterop.trade_msg *)quoteMessage.Ptr; trade->common.msg_type = LimeQuotesInterop.limeq_message_type.TRADE; var msg_len = (ushort)sizeof(LimeQuotesInterop.trade_msg); trade->common.msg_len = Reverse(msg_len); quoteMessage.Length = msg_len; trade->common.shares = LimeQuotesProvider.Reverse((uint)tick.Size); trade->total_volume = LimeQuotesProvider.Reverse((uint)tick.Volume); switch (tick.Side) { case TradeSide.Buy: trade->common.side = LimeQuotesInterop.quote_side.BUY; break; case TradeSide.Sell: trade->common.side = LimeQuotesInterop.quote_side.SELL; break; default: trade->common.side = LimeQuotesInterop.quote_side.NONE; break; } Int32 mantissa; sbyte exponent; LimeQuoteMessage.DoubleToPrice(tick.Price, out mantissa, out exponent); trade->common.price_mantissa = mantissa; trade->common.price_exponent = exponent; trade->common.symbol_index = (uint)symbolID; // We steal the order_id field to send the upper 32 bits of the timaestamp trade->common.timestamp = (uint)tick.UtcTime.Internal; trade->common.order_id = (uint)(tick.UtcTime.Internal >> 32); } else if (isQuote) { bool priceChanged = true; if (isSell && tick.Bid != lastTick.Bid) { priceChanged = true; } else if (!isSell && tick.Ask != lastTick.Ask) { priceChanged = true; } if (priceChanged) { var order = (LimeQuotesInterop.order_msg *)quoteMessage.Ptr; order->common.msg_type = LimeQuotesInterop.limeq_message_type.ORDER; var msg_len = (ushort)sizeof(LimeQuotesInterop.order_msg); order->common.msg_len = Reverse(msg_len); quoteMessage.Length = msg_len; order->common.quote_flags = 1; order->common.shares = LimeQuotesProvider.Reverse((uint)Math.Max(tick.Size, 1)); Int32 mantissa; sbyte exponent; if (isSell) { order->common.side = LimeQuotesInterop.quote_side.SELL; } else { order->common.side = LimeQuotesInterop.quote_side.BUY; } if (order->common.side == LimeQuotesInterop.quote_side.BUY) { LimeQuoteMessage.DoubleToPrice(tick.Bid, out mantissa, out exponent); order->common.price_mantissa = mantissa; order->common.price_exponent = exponent; if (trace) { log.TraceFormat("Sending Ask {0}", tick.Bid); } } else if (order->common.side == LimeQuotesInterop.quote_side.SELL) { LimeQuoteMessage.DoubleToPrice(tick.Ask, out mantissa, out exponent); order->common.price_mantissa = mantissa; order->common.price_exponent = exponent; if (trace) { log.TraceFormat("Sending Bid {0}", tick.Ask); } } order->common.symbol_index = (uint)symbolID; // We steal the order_id field to send the upper 32 bits of the timaestamp order->common.timestamp = (uint)tick.UtcTime.Internal; order->common.order_id = (uint)(tick.UtcTime.Internal >> 32); QuotePacketQueue.Enqueue(quoteMessage, tick.UtcTime.Internal); if (trace) { log.Trace("Enqueued tick packet: " + new TimeStamp(tick.UtcTime.Internal)); } } } else { throw new NotImplementedException("Tick is neither Trade nor Quote"); } }
protected override void TrySendTick(SymbolInfo symbol, TickIO tick) { if (trace) { log.Trace("TrySendTick( " + symbol + " " + tick + ")"); } var quoteMessage = QuoteSocket.MessageFactory.Create(); var lastTick = lastTicks[symbol.BinaryIdentifier]; var buffer = quoteMessage.Data.GetBuffer(); var position = quoteMessage.Data.Position; quoteMessage.Data.SetLength(1024); if (tick.IsTrade) { var index = 0; var snippet = tradeSnippetBytes[index]; Array.Copy(snippet, 0, buffer, position, snippet.Length); position += snippet.Length; // Symbol var value = symbol.Symbol.ToCharArray(); for (var i = 0; i < value.Length; i++) { buffer[position] = (byte)value[i]; ++position; } ++index; snippet = tradeSnippetBytes[index]; Array.Copy(snippet, 0, buffer, position, snippet.Length); position += snippet.Length; // Price var len = ConvertPriceToBytes(tick.lPrice); var pos = len; for (var i = 0; i < len; i++) { buffer[position] = (byte)bytebuffer[--pos]; ++position; } ++index; snippet = tradeSnippetBytes[index]; Array.Copy(snippet, 0, buffer, position, snippet.Length); position += snippet.Length; // Size value = tick.Size.ToString().ToCharArray(); for (var i = 0; i < value.Length; i++) { buffer[position] = (byte)value[i]; ++position; } ++index; snippet = tradeSnippetBytes[index]; Array.Copy(snippet, 0, buffer, position, snippet.Length); position += snippet.Length; // Bid if (tick.lBid != lastTick.lBid) { value = ("2003=" + tick.Bid + ";").ToCharArray(); for (var i = 0; i < value.Length; i++) { buffer[position] = (byte)value[i]; ++position; } } ++index; snippet = tradeSnippetBytes[index]; Array.Copy(snippet, 0, buffer, position, snippet.Length); position += snippet.Length; // Ask if (tick.lAsk != lastTick.lAsk) { value = ("2004=" + tick.Ask + ";").ToCharArray(); for (var i = 0; i < value.Length; i++) { buffer[position] = (byte)value[i]; ++position; } } ++index; snippet = tradeSnippetBytes[index]; Array.Copy(snippet, 0, buffer, position, snippet.Length); position += snippet.Length; // Ask size var askSize = Math.Max((int)tick.AskLevel(0), 1); if (askSize != lastTick.AskLevel(0)) { value = ("2005=" + askSize + ";").ToCharArray(); for (var i = 0; i < value.Length; i++) { buffer[position] = (byte)value[i]; ++position; } } ++index; snippet = tradeSnippetBytes[index]; Array.Copy(snippet, 0, buffer, position, snippet.Length); position += snippet.Length; // Bid size var bidSize = Math.Max((int)tick.BidLevel(0), 1); if (bidSize != lastTick.BidLevel(0)) { value = ("2006=" + bidSize + ";").ToCharArray(); for (var i = 0; i < value.Length; i++) { buffer[position] = (byte)value[i]; ++position; } } ++index; snippet = tradeSnippetBytes[index]; Array.Copy(snippet, 0, buffer, position, snippet.Length); position += snippet.Length; } else { var index = 0; var snippet = quoteSnippetBytes[index]; Array.Copy(snippet, 0, buffer, position, snippet.Length); position += snippet.Length; var value = symbol.Symbol.ToCharArray(); for (var i = 0; i < value.Length; i++) { buffer[position] = (byte)value[i]; ++position; } ++index; snippet = quoteSnippetBytes[index]; Array.Copy(snippet, 0, buffer, position, snippet.Length); position += snippet.Length; if (tick.lBid != lastTick.lBid) { value = ("2003=" + tick.Bid + ";").ToCharArray(); for (var i = 0; i < value.Length; i++) { buffer[position] = (byte)value[i]; ++position; } } ++index; snippet = quoteSnippetBytes[index]; Array.Copy(snippet, 0, buffer, position, snippet.Length); position += snippet.Length; if (tick.lAsk != lastTick.lAsk) { value = ("2004=" + tick.Ask + ";").ToCharArray(); for (var i = 0; i < value.Length; i++) { buffer[position] = (byte)value[i]; ++position; } } ++index; snippet = quoteSnippetBytes[index]; Array.Copy(snippet, 0, buffer, position, snippet.Length); position += snippet.Length; var askSize = Math.Max((int)tick.AskLevel(0), 1); if (askSize != lastTick.AskLevel(0)) { value = ("2005=" + askSize + ";").ToCharArray(); for (var i = 0; i < value.Length; i++) { buffer[position] = (byte)value[i]; ++position; } } ++index; snippet = quoteSnippetBytes[index]; Array.Copy(snippet, 0, buffer, position, snippet.Length); position += snippet.Length; var bidSize = Math.Max((int)tick.BidLevel(0), 1); if (bidSize != lastTick.BidLevel(0)) { value = ("2006=" + bidSize + ";").ToCharArray(); for (var i = 0; i < value.Length; i++) { buffer[position] = (byte)value[i]; ++position; } } ++index; snippet = quoteSnippetBytes[index]; Array.Copy(snippet, 0, buffer, position, snippet.Length); position += snippet.Length; } var strValue = (tick.UtcTime.TimeOfDay + "." + tick.UtcTime.Microsecond.ToString("000") + ";2015=" + tick.UtcTime.Month.ToString("00") + "/" + tick.UtcTime.Day.ToString("00") + "/" + tick.UtcTime.Year + "\n").ToCharArray(); for (var i = 0; i < strValue.Length; i++) { buffer[position] = (byte)strValue[i]; ++position; } if (trace) { var message = Encoding.ASCII.GetString(buffer, 0, (int)position); log.Trace("Tick message: " + message); } quoteMessage.Data.Position = position; quoteMessage.Data.SetLength(position); lastTick.Inject(tick.Extract()); if (trace) { log.Trace("Added tick to packet: " + tick.UtcTime); } quoteMessage.SendUtcTime = tick.UtcTime.Internal; if (quoteMessage.Data.GetBuffer().Length == 0) { return; } QuotePacketQueue.Enqueue(quoteMessage, quoteMessage.SendUtcTime); if (trace) { log.Trace("Enqueued tick packet: " + new TimeStamp(quoteMessage.SendUtcTime)); } }