Market Depth
In order to get current prices we need to subscribe to the necessary quote types, namely Quote.Level2 and Quote.Trades. Also we need to register the event handler OnLevel2 and OnTrade which will be called by the system in the moment of the corresponding quote arrival.
// Obtaining of the current instrument instrument = Instruments.Current; Instruments.NewTrade += OnTrade; //Creation of the event on the new trades Instruments.NewLevel2 += OnQuote; //Creation of the event on quotes update Instruments.Subscribe(instrument, QuoteTypes.Trade | QuoteTypes.Level2); //subscription on the trades made
Access to the Level2 quote’s fields, such as price, time, volume, MMID etc. is made through the properties and methods of the Level2 class. Current array of the Bid and Ask price levels can be obtained through the methods getBidDepth and getAskDepth of an active instrument.
At first we draw Bid prices by formatting them to the instrument precision through the method formatPrice and applying to them the size displaying in lots. Ask prices are outputted similarly.
private void DrawLevel2(Level2[] quotes, Graphics gr, Rectangle rect, int offset) { //Displaying of the columns headers gr.DrawString("Price", font, brush, rect.X + offset, rect.Y + 40); gr.DrawString("Size", font, brush, rect.X + offset + 70, rect.Y + 40); //Displaying of the values for (int i = 0; i < quotes.Length; i++) { Level2 quote = quotes[i]; //Obtaining the information about Level2 quote double price = quote.Price; double size = quote.Size; //Obtaining of the Bid size and price from the current element string text_bid_price = String.Format("{0}", instrument.FormatPrice(price)); string text_bid_size = String.Format("{0:N2}", size / Instruments.Current.LotSize); //Displaying of the values gr.DrawString(text_bid_price, font, brush, rect.X + offset, rect.Y + i * 20 + 60); gr.DrawString(text_bid_size, font, brush, rect.X + offset + 70, rect.Y + i * 20 + 60); } }
Information about the trade is stored in the Trade class. Obtained trades are saved in the collection for the next displaying in the Time & Sales. The last known trade can be gotten from the instrument through getLastTrade method.
private void DrawTrades(Graphics gr, Rectangle rect) { //Displaying of the columns headers gr.DrawString("Time", font, brush, rect.X + 400, rect.Y + 40); gr.DrawString("Price", font, brush, rect.X + 500, rect.Y + 40); gr.DrawString("Size", font, brush, rect.X + 600, rect.Y + 40); //Displaying of the values for (int i = 0; i < lastTrades.Count; i++) { Trade trade = lastTrades[i]; //Obtaining the information about this trade DateTime time = trade.Time; double price = trade.Price; double size = trade.Size; //obtaining of the time, size and price from the current element string text_trade_time = String.Format("{0:HH:mm:ss}", time); string text_trade_price = String.Format("{0}", instrument.FormatPrice(price)); string text_trade_size = String.Format("{0:N2}", size / instrument.LotSize); // Displaying of the values gr.DrawString(text_trade_time, font, brush, rect.X + 400, rect.Y + i * 20 + 60); gr.DrawString(text_trade_price, font, brush, rect.X + 500, rect.Y + i * 20 + 60); gr.DrawString(text_trade_size, font, brush, rect.X + 600, rect.Y + i * 20 + 60); } }
After compiling the obtained indicator we can add it to any chart through the context menu AddIndicator - > MyIndName. For the correct work we need an instrument with support of Level2 and Trades, for example, ES on our demo-server test.protraderfx.net