MarketDepth


Summary

Access to MarketDepth Ask Entries, Bid Entries and the event at which the market depth gets updated

Syntax

public interface MarketDepth

Members

NameTypeSummary
AskEntries PropertyThe total number of Ask entries
BidEntries PropertyThe total number of Bid entries
Updated EventThe event at which the market depth gets updated

Example 1

	using System;
	using System.Text;
	using cAlgo.API;
	namespace cAlgo.Indicators
	{
	    [Indicator]
	    public class MarketDepthIndicator : Indicator
	    {
	        private MarketDepth _marketDepth;
	        public override void Calculate(int index){}
	        protected override void Initialize()
	        {
             //  Get Market Depth
	            _marketDepth = MarketData.GetMarketDepth(Symbol);
             // subscribe to event Updated
	            _marketDepth.Updated += MarketDepthUpdated;
	        }
	        void MarketDepthUpdated()
	        {
             // Draw Market Depth Entries in the indicator panel
	            var se = new StringBuilder();
	            se.Append("Bid");
	            se.Append("                              ");
	            se.Append("Ask");
	            ChartObjects.DrawText("DOM", se.ToString(), StaticPosition.TopLeft, Colors.White);
	            se.Clear();
	            se.AppendLine();
	            se.AppendLine();
	            foreach (var entry in _marketDepth.BidEntries)
	            {
	                double dVolume  = Math.Round(entry.Volume / 1000000.0, 2);
	                string volume = string.Format("{0}{1}", dVolume, "m");
	                double entryPrice = entry.Price;
	                string askText = string.Format("{0}    {1}", entryPrice.ToString("0.00000"), volume);
	                se.AppendLine(askText);
	            }
	            ChartObjects.DrawText("Bid", se.ToString(), StaticPosition.TopLeft, Colors.Red);
	            se.Clear();
	            se.AppendLine();
	            se.AppendLine();
	            foreach (var entry in _marketDepth.AskEntries)
	            {
	                double dVolume = Math.Round(entry.Volume / 1000000.0, 2);
	                string volume = string.Format("{0}{1}", dVolume, "m");
	                double entryPrice = entry.Price;
	                se.Append("                                    ");
	                string bidText = string.Format("{0}     {1}", entryPrice.ToString("0.00000"), volume);
	                se.AppendLine(bidText);
	            }
	            ChartObjects.DrawText("Ask", se.ToString(), StaticPosition.TopLeft, Colors.Turquoise);
	        }
	    }
	}

Example 2

	using cAlgo.API;
	namespace cAlgo.Indicators
	{
	    [Indicator]
	    public class Level2 : Indicator
	    {
	        [Output("BidEntries", Color = Colors.Red, PlotType = PlotType.Histogram, Thickness = 5)]
	        public IndicatorDataSeries BidResult { get; set; }
	        [Output("AskEntries", Color = Colors.Blue, PlotType = PlotType.Histogram, Thickness = 5)]
	        public IndicatorDataSeries AskResult { get; set; }
	        MarketDepth GBPUSD;
	        private int _askNo;
	        private int _bidNo;
	        protected override void Initialize()
	        {
	            GBPUSD = MarketData.GetMarketDepth(Symbol);
	            GBPUSD.Updated += OnGbpUsdUpdated;
	        }
	        void OnGbpUsdUpdated()
	        {
	            _askNo = 0;
	            _bidNo = 0;
	            var index = MarketSeries.Close.Count - 1;
	            for (var i = 0; i < GBPUSD.AskEntries.Count; i++)
	                AskResult[index - i] = double.NaN;
	            foreach (var entry in GBPUSD.AskEntries)
	            {
	                AskResult[index - _askNo] = (-1) * entry.Volume;
	                _askNo++;
	            }
	            for (var i = 0; i < GBPUSD.BidEntries.Count; i++)
	                BidResult[index - i] = double.NaN;
	            foreach (var entry in GBPUSD.BidEntries)
	            {
	                BidResult[index - _bidNo] = entry.Volume;
	                _bidNo++;
	            }
	        }
	        public override void Calculate(int index){}
	    }
	}

Example 3

using cAlgo.API;
using cAlgo.API.Internals;
namespace cAlgo
{
    // This sample shows how to get a symbol market depth and use it
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MarketDepthSample : Indicator
    {
        private int _askNo;
        private int _bidNo;
        private MarketDepth _marketDepth;
        [Output("Bid Entries", LineColor = "Red", PlotType = PlotType.Histogram, Thickness = 5)]
        public IndicatorDataSeries BidResult { get; set; }
        [Output("Ask Entries", LineColor = "Blue", PlotType = PlotType.Histogram, Thickness = 5)]
        public IndicatorDataSeries AskResult { get; set; }
        protected override void Initialize()
        {
            _marketDepth = MarketData.GetMarketDepth(SymbolName);
            _marketDepth.Updated += MarketDepth_Updated; ;
        }
        private void MarketDepth_Updated()
        {
            _askNo = 0;
            _bidNo = 0;
            var index = Bars.ClosePrices.Count - 1;
            for (var i = 0; i < _marketDepth.AskEntries.Count; i++)
                AskResult[index - i] = double.NaN;
            foreach (var entry in _marketDepth.AskEntries)
            {
                AskResult[index - _askNo] = (-1) * entry.VolumeInUnits;
                _askNo++;
            }
            for (var i = 0; i < _marketDepth.BidEntries.Count; i++)
                BidResult[index - i] = double.NaN;
            foreach (var entry in _marketDepth.BidEntries)
            {
                BidResult[index - _bidNo] = entry.VolumeInUnits;
                _bidNo++;
            }
        }
        public override void Calculate(int index)
        {
        }
    }
}
Reference