Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

GetCurrentBidVolume & GetCurrentAskVolume

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    GetCurrentBidVolume & GetCurrentAskVolume

    Hello,

    I have a fairly simple question: does the function GetCurrentBidVolume return the volume at the current bid price (Level I), or the total bid volume from Level II?

    If it returns only the current bid volume, how can I add up the volume at the current bid + the volume at bid tick-1 + the volume at bid tick-2, and so on?

    I would like to get the total bid volume or the 5 bid volume

    Thx
    Thomas

    #2
    Hello Thomas79,

    That is correct, it returns the level 1 current bid volume.

    To add up volume at each price level you would need to use level 2 data, you can do that from the OnMarketDepth override. There is a sample in the following link that goes over making an orderbook so you can access the values at each price level.

    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.

    Comment


      #3
      Hello Jesse,
      Thank you for your reply. I’ve been working on creating a new indicator based on the sample you shared in your previous message.

      The idea behind my indicator is to display on the chart the total bid and total ask from Level II data (i.e., the sum of the orders within a 10-tick range above and below the current price).

      Here is the script I wrote — the text displays correctly on the chart, but the volume values are missing, as shown in the attached image.

      Could you help me with this?


      here the script :

      region Using declarations
      using System;
      using System.Collections.Generic;
      using System.Windows.Media;
      using NinjaTrader.Cbi;
      using NinjaTrader.Data;
      using NinjaTrader.Gui.Tools;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.NinjaScript.DrawingTools;
      #endregion

      namespace NinjaTrader.NinjaScript.Indicators
      {
      public class BidAskDepthSnapshot : Indicator
      {
      private List<LadderRow> askRows = new List<LadderRow>(10);
      private List<LadderRow> bidRows = new List<LadderRow>(10);

      private bool firstAskEvent = true;
      private bool firstBidEvent = true;

      private class LadderRow
      {
      public double Price;
      public long Volume;
      }

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Name = "BidAskDepthSnapshot";
      Calculate = Calculate.OnEachTick;
      IsOverlay = true;
      }

      else if (State == State.Historical)
      {
      // Pas de DOM sur les données historiques
      return;
      }
      }

      protected override void OnMarketDepth(MarketDepthEventArgs e)
      {
      lock (e.Instrument.SyncMarketDepth)
      {
      List<LadderRow> rows = (e.MarketDataType == MarketDataType.Ask ? askRows : bidRows);
      LadderRow row = new LadderRow { Price = e.Price, Volume = e.Volume };

      // Ajouter ou mettre à jour
      if (e.Operation == Operation.Add || (e.Operation == Operation.Update && (rows.Count == 0 || rows.Count <= e.Position)))
      {
      if (rows.Count <= e.Position)
      rows.Add(row);
      else
      rows.Insert(e.Position, row);
      }
      else if (e.Operation == Operation.Remove && rows.Count > e.Position)
      {
      rows.RemoveAt(e.Position);
      }
      else if (e.Operation == Operation.Update)
      {
      if (rows[e.Position] == null)
      {
      rows[e.Position] = row;
      }

      else
      {
      rows[e.Position].Price = e.Price;
      rows[e.Position].Volume = e.Volume;
      }
      }
      }
      }

      protected override void OnBarUpdate()
      {
      if (CurrentBar < 1 || bidRows.Count == 0 || askRows.Count == 0)
      return;

      long totalBid = 0;
      long totalAsk = 0;

      for (int idx = 0; idx < askRows.Count; idx++)
      totalBid += bidRows[idx].Volume;

      for (int idx = 0; idx < bidRows.Count; idx++)
      totalAsk += askRows[idx].Volume;

      // Ne pas dessiner si aucune donnée
      if (totalBid == 0 && totalAsk == 0)
      return;

      string label = "Bid: {totalBid} / Ask: {totalAsk}";

      // Remplacer l'ancien texte s’il existe (utiliser un ID fixe)
      Draw.Text(this, "DepthVol", false, label, 0, High[0] + TickSize * 3, 0, Brushes.LightGreen,
      new SimpleFont("Arial", 12), System.Windows.TextAlignment.Left, Brushes.Transparent, Brushes.Transparent, 0);

      }
      }
      }​
      Attached Files

      Comment


        #4
        Hello Thomas79,

        You need to use a dollar sign for strings that use curly braces as variable input, that is called String interpolation in C#.

        string label = "Bid: {totalBid} / Ask: {totalAsk}";

        to

        string label = $"Bid: {totalBid} / Ask: {totalAsk}";

        String interpolation using the `$` token provides a more readable and convenient syntax to format string output than traditional string composite formatting.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by abelsheila, 05-14-2025, 07:38 PM
        2 responses
        34 views
        0 likes
        Last Post hglover945  
        Started by nailz420, 05-14-2025, 09:14 AM
        1 response
        73 views
        0 likes
        Last Post NinjaTrader_ChristopherJ  
        Started by NinjaTrader_Brett, 05-12-2025, 03:19 PM
        0 responses
        353 views
        1 like
        Last Post NinjaTrader_Brett  
        Started by domjabs, 05-12-2025, 01:55 PM
        2 responses
        67 views
        0 likes
        Last Post domjabs
        by domjabs
         
        Started by Morning Cup Of Trades, 05-12-2025, 11:50 AM
        1 response
        87 views
        0 likes
        Last Post NinjaTrader_ChristopherJ  
        Working...
        X