Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Historical Bid/Ask and Cumulative Delta

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

    Historical Bid/Ask and Cumulative Delta

    Hello community,

    I am looking to write my own cumulative delta indicator, there were a few things that I am wondering about/having trouble with.

    OnMarketData works just fine for comparing last price values with bid and ask values, but it doesn't allow for historical comparisons. For this I am using an added tick series. Do I need to add a bid and ask series as well to have accurate values? because this is untenable with my current cpu resources. Is GetAsk(barsAgo) sufficient?

    Once I have successfully accumulated the ask and bid smashes, I need to plot. I found a wonderful indicator from NT Alan that demonstrates rendering candlestick charts with non price series data, and I was planning on using this for my cumulative delta. But one thing I was having an issue with was high and low. Open[0] would be Value[1] and Close[0] would be Value[0], so High[0] and Low[0] would have to be MAX and MIN from the current tick to the start of the candle. I tried this with the MAX and MIN indicator and it would not work. Am I taking the wrong approach?

    Finally, there are a few other things I am looking to develop using the bid and ask. As a start I would just like to ask how I can use OnMarketData in the manner described at the beginning of this post (comparing e.Last/e.Price with e.Ask and e.Bid), but filtered by time such that I am only accumulating buys and sells say, in the last 5 seconds of a 1min candle. Can I reference bar time within the OnMarketData method directly?

    Thank you for any code examples and guidance you may be able to offer.

    #2
    P.S. I thought of another question I had. Unless I am mistaken, it seems like all of the indicators I have looked at thus far, whether they are using OnMarketData or attempting to use a secondary tick series, are comparing the current tick's closing price with the same tick's bid or ask. How can the close of that tick possibly be greater or less than the bid/ask? wouldn't you want to be comparing the current tick's closing price with the previous tick's bid/ask? What am I missing here?

    Thanks

    Comment


      #3
      Hello lunardiplomacy,

      Thanks for your post.

      OnMarketData works with realtime data, and MarketDataType.Last events can be processed historically if Tick Replay is enabled.

      Alternatively, you could consider adding a single tick data series and accessing the ask and bid from the single tick Last data series. This would take advantage of bid/ask stamped ticks and would be more performant than Tick Replay.

      You would not need to add historical bid/ask series.

      I have attached an example BuySellVolume indicator that uses a single tick data series. This can be compared against our BuySellVolume indicator that comes with the platform to compare a Multi Time Frame approach to a Tick Replay Approach.

      For creating your own cumulative delta indicator, you could consider creating 4 plots for the OHLC values of your cumulative delta. Value[0] represents the primary plot value for the current bar while Value[1] would be the primary plot value for the previous bar.

      AddPlot - https://ninjatrader.com/support/help...8/?addplot.htm

      You could consider checking IsFirstTickOfBar on the primary data series (BarsInProgress == 0) to see when a new bar is developing to assign the Open plot value, and then you can continuously update the Close plot value.

      I may suggest tracking high and low with class level variables and updating the High plot value when the developing value is greater than the current High, and updating the Low plot value when the developing value is less than the current High.

      For the thread's reference a script that draws bars using 4 different plots can be found here - https://ninjatraderecosystem.com/use...heiken-ashi-8/

      The link above is publicly available.

      The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.


      You could consider adding the BuySellVolume Indicator on a 5 second data series to differentiate what happened in the last 5 seconds, or you could consider checking the timestamp of a tick in OnMarketData with MarketDataEventArgs. If you take a Multi time Frame approach instead of using OnMarketData, you could check Time[0] when the BarsInProgress of the single tick data series is updating to reference the timestamp of that tick. From there you could keep some variables up to date that hold your "last 5 seconds" data.

      MarketDataEventArgs - https://ninjatrader.com/support/help...aeventargs.htm

      A buy or sell is identified when the Last tick price is greater or less than current Ask/Bid. This is what the BuySellVolume indicator checks as well as the alternate version I have attached.

      We look forward to assisting.
      Attached Files

      Comment


        #4
        Jim, just want to say, excellent post. You hit every single point, much appreciated.

        Comment


          #5
          Originally posted by NinjaTrader_Jim View Post
          Hello lunardiplomacy,

          Thanks for your post.

          OnMarketData works with realtime data, and MarketDataType.Last events can be processed historically if Tick Replay is enabled.

          Alternatively, you could consider adding a single tick data series and accessing the ask and bid from the single tick Last data series. This would take advantage of bid/ask stamped ticks and would be more performant than Tick Replay.

          You would not need to add historical bid/ask series.

          I have attached an example BuySellVolume indicator that uses a single tick data series. This can be compared against our BuySellVolume indicator that comes with the platform to compare a Multi Time Frame approach to a Tick Replay Approach.

          For creating your own cumulative delta indicator, you could consider creating 4 plots for the OHLC values of your cumulative delta. Value[0] represents the primary plot value for the current bar while Value[1] would be the primary plot value for the previous bar.

          AddPlot - https://ninjatrader.com/support/help...8/?addplot.htm

          You could consider checking IsFirstTickOfBar on the primary data series (BarsInProgress == 0) to see when a new bar is developing to assign the Open plot value, and then you can continuously update the Close plot value.

          I may suggest tracking high and low with class level variables and updating the High plot value when the developing value is greater than the current High, and updating the Low plot value when the developing value is less than the current High.

          For the thread's reference a script that draws bars using 4 different plots can be found here - https://ninjatraderecosystem.com/use...heiken-ashi-8/

          The link above is publicly available.

          The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.


          You could consider adding the BuySellVolume Indicator on a 5 second data series to differentiate what happened in the last 5 seconds, or you could consider checking the timestamp of a tick in OnMarketData with MarketDataEventArgs. If you take a Multi time Frame approach instead of using OnMarketData, you could check Time[0] when the BarsInProgress of the single tick data series is updating to reference the timestamp of that tick. From there you could keep some variables up to date that hold your "last 5 seconds" data.

          MarketDataEventArgs - https://ninjatrader.com/support/help...aeventargs.htm

          A buy or sell is identified when the Last tick price is greater or less than current Ask/Bid. This is what the BuySellVolume indicator checks as well as the alternate version I have attached.

          We look forward to assisting.
          Jim, thanks to your help yesterday I now have a cumulative delta which plots historical values (which I think is accurate but it's hard to tell because delta values are so arbitrary), as well as a cumulative delta that plots in realtime.
          Code:
          protected override void OnBarUpdate()
          {
          double ask = BarsArray[1].GetAsk(CurrentBars[1]);
          double bid = BarsArray[1].GetBid(CurrentBars[1]);
          double last = BarsArray[1].GetClose(CurrentBars[1]);
          double vol = BarsArray[1].GetVolume(CurrentBars[1]);
          
          if (last >= ask)
          buys += BarsArray[1].GetVolume(CurrentBars[1]);
          else if (last <= bid)
          sells += BarsArray[1].GetVolume(CurrentBars[1]);
          
          double delta = buys - sells;
          
          if (IsFirstTickOfBar)
          {
          buys = 0;
          sells = 0;
          delta = 0;
          }
          
          if (CurrentBar == 0)
          {
          cdClose[0] = delta;
          cdOpen[0] = 0;
          }
          else
          {
          cdClose[0] = cdClose[1] + delta;
          cdOpen[0] = cdClose[1];
          }
          
          }
          The above code plots cum delta correctly in realtime. I don't understand why it won't plot historically, I thought that referencing bid and ask from the tick series would accomplish this.

          Like I said above, I was able to get the cum delta values to calculate historically using the indicator you provided 'BuySellVolumeOneTick' just by switching out the calculation in the "SetValues" method, but as soon as it transitions to realtime the calculation gets completely out of scale.

          Can you please point out to me exactly what in the indicator code is allowing it to calculate on historical data? Or what I could add to my code above that would allow that?

          Thank you.

          Comment


            #6
            Hello lunardiplomacy,

            If you are using the Multi Time Frame approach, I suggest modelling after the BuySellVolumeOneTick example. We have another multi time frame template example that can be referenced to a better grasp on the approach.

            I have recorded a demonstration showing some modifications I have made. I will not be posting the changes here since we have a Cumulative Delta indicator in our Order Flow + package, but I hope the information given will help you to build indicators like this on your own.

            Demo - https://drive.google.com/file/d/1q0U...w?usp=drivesdk

            We look forward to assisting.
            Attached Files

            Comment


              #7
              Right, I knew going into this question that I was getting into potentially proprietary territory, so I wasn't expecting much and would've been appreciative of really any amount of guidance on this topic, but you've really gone above and beyond, Jim. Thank you.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              597 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              343 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              103 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              556 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              555 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X