Working with Market Depth

API allows getting the data about updates of the MarketDepth. For this, the MarketDepthChanged(MarketDataArg arg) method should be redefined..

    public class SampleMD:Indicator
    {
        protected override void OnCalculate(int bar, decimal value)
        {
        }

        protected override void MarketDepthChanged(MarketDataArg arg)
        {
            
        }
    }

It is also possible to get the total volume of all Bids and total volume of all Asks with the help of the MarketDepthInfo object properties: CumulativeDomAsks and CumulativeDomBids.

Example of the indicator which reflects the history of the total values of the Asks and Bids volumes

public class DomPower : Indicator
    {
        private ValueDataSeries _asks;
        private ValueDataSeries _bids=new ValueDataSeries("Bids");

        private int _lastCalculatedBar = 0;
        privte bool _first = true;

        public DomPower():base(true)
        {
            Panel = IndicatorDataProvider.NewPanel;
            _asks = (ValueDataSeries)DataSeries[0];
            _asks.Name = "Asks";
            _bids.Color = Colors.Green;
            DataSeries.Add(_bids);
        }

        protected override void OnCalculate(int bar, decimal value)
        {
            
        }

        protected override void MarketDepthChanged(MarketDataArg arg)
        {
            if (_first)
            {
                _first = false;
                _lastCalculatedBar = CurrentBar - 1;
            }

            var lastCandle = CurrentBar - 1;
            var cumAsks = MarketDepthInfo.CumulativeDomAsks;
            var cumBids = MarketDepthInfo.CumulativeDomBids;
            
            for (int i = _lastCalculatedBar; i <= lastCandle; i++)
            {
                _asks[i] = -cumAsks;
                _bids[i] = cumBids;
            }

            _lastCalculatedBar = lastCandle;
        }
    }

In some cases it might be necessary to get a snapshot of the MaketDepth data. For this, the MarketDepthInfo.GetMarketDepthSnapshot() function could be used. The function returns a list of all levels in the DOM.

This article was helpful for 2 people. Is this article helpful for you?