Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

cumulativeDelta with multiple BarsInProgress

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

    cumulativeDelta with multiple BarsInProgress

    Hi, I work with multiframe and I want to use cumulative delta on each of these multiframes.

    I don't quite understand when I should execute the cumulativeDelta.Update and with what parameters. I want to use the DeltaOpen[0] for current bar on different timefrime (volume or mins).
    How to change this code to be able to read DeltaOpen[0] for particular BarsInProgress?


    Code:
    protected override void OnStateChange()
    ...
    {
    ​else if (State == State.Configure)
    
     {
     AddDataSeries(Data.BarsPeriodType.Tick, 1);
     AddDataSeries(Data.BarsPeriodType.Min 5);
     }
    
     else if (State == State.DataLoaded)
     {
      cumulativeDelta = OrderFlowCumulativeDelta(CumulativeDeltaType.BidAs k, CumulativeDeltaPeriod.Session, 0);
     }
    ​}
    
    protected override void OnBarUpdate()
    {
     if (CurrentBars[0] < 1
     || CurrentBars[1] < 1
     || CurrentBars[2] < 1
    )
    
    return;
    
        if (BarsInProgress == 0)
        {
    Print("Delta Open: " + cumulativeDelta.DeltaOpen[0]);
    Print("Delta Close: " + cumulativeDelta.DeltaClose[0]);
    Print("Time: " + Time[0] + "; " + CurrentBar);
    ​}
    ​
    
    if (BarsInProgress == 1)
        {
    cumulativeDelta.Update(cumulativeDelta.BarsArray[[B]1[/B]].Count - 1, 1);
    Print("Delta Open: " + cumulativeDelta.DeltaOpen[0]);
    Print("Delta Close: " + cumulativeDelta.DeltaClose[0]);
    Print("Time: " + Time[0] + "; " + CurrentBar);​
    }
    
    if (BarsInProgress == 2)
        {
    cumulativeDelta.Update(cumulativeDelta.BarsArray[[B]2[/B]].Count - 1, 1);
    Print("Delta Open: " + cumulativeDelta.DeltaOpen[0]);
    Print("Delta Close: " + cumulativeDelta.DeltaClose[0]);
    Print("Time: " + Time[0] + "; " + CurrentBar);​
    }

    #2
    Hell iskier,

    Thanks for your post.

    Since the 1-Tick series being added to your script is the first added series, you would call cumulativeDelta.Update() when BarsInProgress is 1.

    An example of this could be seen in the help guide documentation linked below.

    Order Flow Cumulative Delta: https://ninjatrader.com/support/help...ive_delta2.htm

    To get the value for a particular BarsInProgress, you would create a condition that checks if BarsInProgress is equal to the bar object index you want to access and call cumulativeDelta.DeltaOpen[0] within that condition.

    BarsInProgress: https://ninjatrader.com/support/help...inprogress.htm
    Working with Multi-timeframe/Multi-Instrument NinjaScripts: https://ninjatrader.com/support/help...nstruments.htm
    BarsArray: https://ninjatrader.com/support/help.../barsarray.htm

    For example:
    Code:
    if (BarsInProgress == 0)
    {
        // Print the open of the cumulative delta bar with a delta type of Bid Ask and with a Session period
        Print("Delta Open: " + cumulativeDelta.DeltaOpen[0]);
    }
    else if (BarsInProgress == 1)
    {
        // We have to update the secondary series of the hosted indicator to make sure the values we get in BarsInProgress == 0 are in sync
        cumulativeDelta.Update(cumulativeDelta.BarsArray[1].Count - 1, 1);
    }
    Let me know if I may assist further.

    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hi,
      Thanks for reply.
      On BarsInProgress == 0 I just read the value. Clear
      On BarsInProgress == 1 (as it is 1 tick) I make an Update and then I can read the value. Clear
      How about BarsInProgress == 2 (every 5 mins). Do I need to make an update or just to read value within the BarsInProgress == 2?

      Comment


        #4
        Hello iskier,

        Thanks for your notes.

        Yes, you could just get the values within BarsInProgress 2 without having to call cumulativeDelta.Update() again.

        cumulativeDelta.Update() would only need to be called for BarsInProgress 1 since that is your added 1-tick series.

        Please let me know if you have further questions on this.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Hi,
          thanks for confirmation.
          I did it and I got bad values in the console. Any idea why?

          Comment


            #6
            Hello iskier,

            Thanks for your note.

            You could supply the BarsArray[2] or BarsArray[0] as the input series of the OrderFlowCumulativeDelta() method and just update with BarsArray[1] in BarsInProgress 1.

            OrderFlowCumulativeDelta(ISeries<double> input, CumulativeDeltaType deltaType, CumulativeDeltaPeriod period, int sizeFilter)

            See the sample code below that demonstrates this.
            Code:
            if (BarsInProgress == 0)
            {
                // Print the close of the cumulative delta bar with a delta type of Bid Ask and with a Session period in BIP0
                Print("Delta Close BIP0: " + OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0).DeltaClose[0]);
            }
            else if (BarsInProgress == 1)
            {
                // We have to update the secondary series of the cached indicator to make sure the values we get in BarsInProgress == 0 are in sync
                OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0).Update(OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0).BarsArray[1].Count - 1, 1);
            }​
            else if (BarsInProgress == 2)
            {
                // Print the close of the cumulative delta bar with a delta type of Bid Ask and with a Session period in BIP2
                Print("Delta Close BIP2: " + OrderFlowCumulativeDelta(BarsArray[2], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0).DeltaClose[0]);
            }
            If you are comparing prints from a strategy to the Order Flow Cumulative Delta indicator on a chart, make sure that the Order Flow Cumulative Delta indicator is using the same exact settings as the ones you are using in your OrderFlowCumulativeDelta() method.
            Attached Files
            Last edited by NinjaTrader_BrandonH; 04-24-2023, 02:32 PM.
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment


              #7
              Hi,

              Thanks for the support! I used your code and something is still wrong.
              1. I got different values for the delta compared to the chart - there are the same settings.
              2. Would the update trigger entering to the BarsInProgress? Because there are logs in the Output and there are no bars on the chart corresponding to them.
              - f.e. I got the output while there is no new closed bar:
              -- 500Vol: Time: 04.04.2023 17:44:51; 1523
              -- Range22: Time: 04.04.2023 17:44:51; 1523

              I upload a code and screenshots.

              Code:
              #region Using declarations
              using System;
              using System.Collections.Generic;
              using System.ComponentModel;
              using System.ComponentModel.DataAnnotations;
              using System.Linq;
              using System.Text;
              using System.Threading.Tasks;
              using System.Windows;
              using System.Windows.Input;
              using System.Windows.Media;
              using System.Xml.Serialization;
              using NinjaTrader.Cbi;
              using NinjaTrader.Gui;
              using NinjaTrader.Gui.Chart;
              using NinjaTrader.Gui.SuperDom;
              using NinjaTrader.Gui.Tools;
              using NinjaTrader.Data;
              using NinjaTrader.NinjaScript;
              using NinjaTrader.Core.FloatingPoint;
              using NinjaTrader.NinjaScript.Indicators;
              using NinjaTrader.NinjaScript.DrawingTools;
              
              #endregion
              
              //This namespace holds Strategies in this folder and is required. Do not change it.
              // DEMO FOR FORUM
              namespace NinjaTrader.NinjaScript.Strategies
              {
                  public class Vol500ColorHeikin : Strategy
                  {
                      private HeikenAshi8 HeikenAshi81;
              
                      private HeikenAshi8 HeikenAshi82;
                      private OrderFlowCumulativeDelta cumulativeDelta;
              
                      protected override void OnStateChange()
                      {
                          if (State == State.SetDefaults)
                          {
                              Description                                    = @"Enter the description for your new custom Strategy here.";
                              Name                                        = "Vol500ColorHeikin";
                              Calculate                                    = Calculate.OnBarClose;
                              EntriesPerDirection                            = 1;
                              EntryHandling                                = EntryHandling.AllEntries;
                              IsExitOnSessionCloseStrategy                = true;
                              ExitOnSessionCloseSeconds                    = 30;
                              IsFillLimitOnTouch                            = false;
                              MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                              OrderFillResolution                            = OrderFillResolution.Standard;
                              Slippage                                    = 0;
                              StartBehavior                                = StartBehavior.WaitUntilFlat;
                              TimeInForce                                    = TimeInForce.Gtc;
                              TraceOrders                                    = false;
                              RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                              StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                              BarsRequiredToTrade                            = 20;
                              // Disable this property for performance gains in Strategy Analyzer optimizations
                              // See the Help Guide for additional information
                              IsInstantiatedOnEachOptimizationIteration    = true;
                          }
                          else if (State == State.Configure)
                          {
                              AddDataSeries(Data.BarsPeriodType.Tick, 1);
                              AddDataSeries(Data.BarsPeriodType.Range, 22);
                          }
                          else if (State == State.DataLoaded)
                          {                
                              HeikenAshi81                = HeikenAshi8(Closes[0]);         // VOL500
                          }
                      }
              
                      protected override void OnBarUpdate()
                      {
                          if (CurrentBars[0] < 1 // 500 vol
                          || CurrentBars[1] < 1  // 1 tick
                          || CurrentBars[2] < 1  // 22 range
                          )
                              return;
              
                          // ------------------------------------------------------------------------
                          // ------------------------------- 500 VOL --------------------------------
                          // ------------------------------------------------------------------------
              
                          if (BarsInProgress == 0)
                          {
                              cumulativeDelta = OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0);
              
                              Print("500Vol: Time: " + Time[0] + "; " + CurrentBar);
                              Print("500Vol: Delta Open: " + cumulativeDelta.DeltaOpen[0]);
                              Print("500Vol: Delta Close: " + cumulativeDelta.DeltaClose[0]);
                          }
              
                          // ------------------------------------------------------------------------
                          // ------------------------------- 1 tick ----------------------------------
                          // ------------------------------------------------------------------------
              
                          if (BarsInProgress == 1)
                          {
                              OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0).Update(OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0).BarsArray[1].Count - 1, 1);
                          }
              
                          // ------------------------------------------------------------------------
                          // ------------------------------- 22 range ----------------------------------
                          // ------------------------------------------------------------------------
              
                          if (BarsInProgress == 2)
                          {
                              cumulativeDelta = OrderFlowCumulativeDelta(BarsArray[2], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0);
              
                              Print("Range22: Time: " + Time[0] + "; " + CurrentBar);
                              Print("Range22: Delta Open R22: " + cumulativeDelta.DeltaOpen[0]);
                              Print("Range22: Delta Close R22: " + cumulativeDelta.DeltaClose[0]);
                          }
                      }
                  }
              }
              
              ​
              Attached Files

              Comment


                #8
                Hello iskier,

                Thanks for your notes.

                When the indicator is running on real-time data, you will see the Order Flow Cumulative Delta print occur when the added series bar closes when running the script using Calculate.OnBarClose.

                For example, say you have a 1-Tick series and a 5-Minute secondary series added to the script and you are printing out the BarsInProgress 0 OF Cumulative Delta value and the BarsInProgress 2 OF Cumulative Delta value.

                When you enable the script on a 1-Minute chart. You will see realtime prints for BarsInProgress 0 occur in the Output window on each minute bar that closes. When the 5-minute added series bar closes, you will see a print for the BarsInProgress 0 print and a print for the BarsInProgress 2 print since both the 1-minute bar and 5-minute bar closes.

                See the attached image demonstrating this. I have also attached the example script used to test this for you to view.
                Attached Files
                <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                Comment


                  #9
                  Brandon, thanks for the script. I used it as a indicator and run it on Vol500 NQ.
                  The only change in the script I changed was from Minute 5 to Range 22.

                  I don't trust values I am getting.

                  1. The values are not the same as on the chart with delta. Values in the Output are different than in the Data Box.
                  2. I don't why I got double output with the same timestamp from 2 charts (selected with half circle on the screenshot).

                  Click image for larger version

Name:	Screenshot.png
Views:	285
Size:	586.5 KB
ID:	1255105

                  Code:
                  #region Using declarations
                  using System;
                  using System.Collections.Generic;
                  using System.ComponentModel;
                  using System.ComponentModel.DataAnnotations;
                  using System.Linq;
                  using System.Text;
                  using System.Threading.Tasks;
                  using System.Windows;
                  using System.Windows.Input;
                  using System.Windows.Media;
                  using System.Xml.Serialization;
                  using NinjaTrader.Cbi;
                  using NinjaTrader.Gui;
                  using NinjaTrader.Gui.Chart;
                  using NinjaTrader.Gui.SuperDom;
                  using NinjaTrader.Gui.Tools;
                  using NinjaTrader.Data;
                  using NinjaTrader.NinjaScript;
                  using NinjaTrader.Core.FloatingPoint;
                  using NinjaTrader.NinjaScript.DrawingTools;
                  #endregion
                  
                  //This namespace holds Indicators in this folder and is required. Do not change it.
                  namespace NinjaTrader.NinjaScript.Indicators
                  {
                      public class OFCumulativeDeltaExample : Indicator
                      {
                          protected override void OnStateChange()
                          {
                              if (State == State.SetDefaults)
                              {
                                  Description                                    = @"Enter the description for your new custom Indicator here.";
                                  Name                                        = "OFCumulativeDeltaExample";
                                  Calculate                                    = Calculate.OnBarClose;
                                  IsOverlay                                    = true;
                                  DisplayInDataBox                            = true;
                                  DrawOnPricePanel                            = true;
                                  DrawHorizontalGridLines                        = true;
                                  DrawVerticalGridLines                        = true;
                                  PaintPriceMarkers                            = true;
                                  ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                                  //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                                  //See Help Guide for additional information.
                                  IsSuspendedWhileInactive                    = true;
                              }
                              else if (State == State.Configure)
                              {
                                  AddDataSeries(Data.BarsPeriodType.Tick, 1);
                                  AddDataSeries(Data.BarsPeriodType.Range, 22);
                              }
                          }
                  
                          protected override void OnBarUpdate()
                          {
                              if (BarsInProgress == 0)
                              {
                                  // Print the close of the cumulative delta bar with a delta type of Bid Ask and with a Session period in BIP0
                                  Print(Time[0] + " Delta Close BIP0 500VOL: " + OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0).DeltaClose[0]);
                              }
                              else if (BarsInProgress == 1)
                              {
                                  // We have to update the secondary series of the cached indicator to make sure the values we get in BarsInProgress == 0 are in sync
                                  OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0).Update(OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0).BarsArray[1].Count - 1, 1);
                              }​
                              else if (BarsInProgress == 2)
                              {
                                  // Print the close of the cumulative delta bar with a delta type of Bid Ask and with a Session period in BIP2
                                  Print(Time[0] + " Delta Close BIP2 RANGE22: " + OrderFlowCumulativeDelta(BarsArray[2], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0).DeltaClose[0]);
                              }
                          }
                      }
                  }
                  
                  #region NinjaScript generated code. Neither change nor remove.
                  
                  namespace NinjaTrader.NinjaScript.Indicators
                  {
                      public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
                      {
                          private OFCumulativeDeltaExample[] cacheOFCumulativeDeltaExample;
                          public OFCumulativeDeltaExample OFCumulativeDeltaExample()
                          {
                              return OFCumulativeDeltaExample(Input);
                          }
                  
                          public OFCumulativeDeltaExample OFCumulativeDeltaExample(ISeries<double> input)
                          {
                              if (cacheOFCumulativeDeltaExample != null)
                                  for (int idx = 0; idx < cacheOFCumulativeDeltaExample.Length; idx++)
                                      if (cacheOFCumulativeDeltaExample[idx] != null &&  cacheOFCumulativeDeltaExample[idx].EqualsInput(input))
                                          return cacheOFCumulativeDeltaExample[idx];
                              return CacheIndicator<OFCumulativeDeltaExample>(new OFCumulativeDeltaExample(), input, ref cacheOFCumulativeDeltaExample);
                          }
                      }
                  }
                  
                  namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
                  {
                      public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
                      {
                          public Indicators.OFCumulativeDeltaExample OFCumulativeDeltaExample()
                          {
                              return indicator.OFCumulativeDeltaExample(Input);
                          }
                  
                          public Indicators.OFCumulativeDeltaExample OFCumulativeDeltaExample(ISeries<double> input )
                          {
                              return indicator.OFCumulativeDeltaExample(input);
                          }
                      }
                  }
                  
                  namespace NinjaTrader.NinjaScript.Strategies
                  {
                      public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
                      {
                          public Indicators.OFCumulativeDeltaExample OFCumulativeDeltaExample()
                          {
                              return indicator.OFCumulativeDeltaExample(Input);
                          }
                  
                          public Indicators.OFCumulativeDeltaExample OFCumulativeDeltaExample(ISeries<double> input )
                          {
                              return indicator.OFCumulativeDeltaExample(input);
                          }
                      }
                  }
                  
                  #endregion
                  ​

                  Comment


                    #10
                    Hello iskier,

                    Thanks for your notes.

                    I was able to replicate the behavior you are reporting and I am researching this more and have reached out to the Development team to get more information about this behavior

                    This forum thread will be updated as soon as I am finished researching this and hear back from the Development team on the topic.
                    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                    Comment


                      #11
                      Hi,
                      do you have any feedback?

                      Comment


                        #12
                        Hello iskier,

                        Thanks for your patience.

                        To accomplish your goal, two instances of the OrderFlowCumulativeDelta would need to be created, such as cumulativeDelta1 and cumulativeDelta2. As in, the indicator would need to be saved to a variable.

                        In State.DataLoaded, the first OrderFlowCumulativeDelta variable (cumulativeDelta1) would be instantiated for BarsArray[0] and the second OrderFlowCumulativeDelta variable (cumulativeDelta2) would be instantiated for BarsArray[2].

                        Then, you would print cumulativeDelta1.DeltaClose[0] in BarsInProgress 0, print cumulativeDelta2.DeltaClose[0] in BarsInProgress 2, and call .Update() on the variables in BarsInProgress 1.

                        Once you do the above, prints will match the 500 Volume primary series and the added 22 Range secondary series. See the demonstration video below.

                        Demonstration video: https://brandonh-ninjatrader.tinytak...NV8yMTYyMDU1NA

                        I have also attached the script used to test this for you to view.​
                        Attached Files
                        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by NullPointStrategies, Today, 05:17 AM
                        0 responses
                        44 views
                        0 likes
                        Last Post NullPointStrategies  
                        Started by argusthome, 03-08-2026, 10:06 AM
                        0 responses
                        124 views
                        0 likes
                        Last Post argusthome  
                        Started by NabilKhattabi, 03-06-2026, 11:18 AM
                        0 responses
                        65 views
                        0 likes
                        Last Post NabilKhattabi  
                        Started by Deep42, 03-06-2026, 12:28 AM
                        0 responses
                        42 views
                        0 likes
                        Last Post Deep42
                        by Deep42
                         
                        Started by TheRealMorford, 03-05-2026, 06:15 PM
                        0 responses
                        46 views
                        0 likes
                        Last Post TheRealMorford  
                        Working...
                        X