Skip to content

MarketDepth

Summary

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

Signature

1
public abstract interface MarketDepth

Namespace

cAlgo.API

Properties

Name Description
AskEntries { get; } The total number of Ask entries
BidEntries { get; } The total number of Bid entries

Events

Name Description
Updated The event at which the market depth gets updated

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    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);
            }
        }
    }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    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){}
        }
    }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 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)
         {
         }
     }
 }

Last update: June 26, 2022

Comments