Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Getting values from OrderFlowCumulativeDelta Indicator

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

    Getting values from OrderFlowCumulativeDelta Indicator

    Hi, I'm trying to get the values from OrderFlowCumulativeDelta Indicator on 1 min timeframe. I have the following code:

    HTML 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.
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class test2 : Strategy
        {
            //private NinjaTrader.NinjaScript.Indicators.Sim22.Sim22_DeltaV2 Sim22_DeltaV21;
            private OrderFlowCumulativeDelta CumDelta;
            private EMA EMA1;
            private SMA SMA1;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Joan strategy";
                    Name                                        = "test2";
                    Calculate                                    = Calculate.OnEachTick;
                    EntriesPerDirection                            = 1;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = false;
                    ExitOnSessionCloseSeconds                    = 30;
                    IsFillLimitOnTouch                            = true;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.Infinite;
                    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("YM 03-23", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
                    AddDataSeries("YM 03-23", Data.BarsPeriodType.Second, 15, Data.MarketDataType.Last);
                    AddDataSeries("YM 03-23", Data.BarsPeriodType.Tick, 1, Data.MarketDataType.Last);
                    AddDataSeries("YM 03-23", Data.BarsPeriodType.Volume, 1, Data.MarketDataType.Last);
                }
                else if (State == State.DataLoaded)
                {                
                    CumDelta = OrderFlowCumulativeDelta(CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Bar, 0);
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0)
                    return;
    
                if (CurrentBars[1] < 1)
                    return;
    
                Print("------------------");
                // [0] means that we want to get the actual bar ([1] would be 1 bar ago)
                Print(Time[0] + " --> " + CumDelta.DeltaClose[0]);
            }
        }
    }
    ​
    And when I execute It, I'm getting the correct values as output, as you can see on the screenshot:

    Click image for larger version

Name:	scr1.png
Views:	255
Size:	136.2 KB
ID:	1244145

    But I don't understand why does it output 30 time the same Time (6:00:00) and with different values. The output shouldn't be something like:

    20/01/2023 5:58:00 --> 4
    20/01/2023 5:59:00 --> 1
    20/01/2023 6:00:00 --> 5

    Thanks so much in advance.

    #2
    Hello speedytrade02,

    Thanks for your post.

    I see in the code you shared that the indicator is set to use Calculate.OnEachTick.

    This means that OnBarUpdate() logic would process for each incoming tick that occurs. If there are multiple ticks that occur within the 1-Minute timeframe, you would see multiple prints in the Output window for that 1-Minute bar.

    You may consider running the script with the Calculate mode set to OnBarClose for prints to only occur each time the 1-Minute bar closes.

    See this help guide page about Calculate: https://ninjatrader.com/support/help.../calculate.htm

    Also, please note from the Order Flow Cumulative Delta help guide page that it is important toupdate the secondary series of the hosted indicator to make sure the values we get in BarsInProgress == 0 are in sync. An example of this could be seen in the sample code on the help guide page linked below.

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

    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 Brandon, thanks for the info. I've just tryied to change Calculate.OnBarClose but still have the same problem, where it doesn't show only the close value of each delta candle

      HTML 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.
      namespace NinjaTrader.NinjaScript.Strategies
      {
          public class test2 : Strategy
          {
              //private NinjaTrader.NinjaScript.Indicators.Sim22.Sim22_DeltaV2 Sim22_DeltaV21;
              private OrderFlowCumulativeDelta CumDelta;
              private EMA EMA1;
              private SMA SMA1;
      
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                                    = @"Joan strategy";
                      Name                                        = "test2";
                      Calculate                                    = Calculate.OnBarClose;
                      EntriesPerDirection                            = 1;
                      EntryHandling                                = EntryHandling.AllEntries;
                      IsExitOnSessionCloseStrategy                = false;
                      ExitOnSessionCloseSeconds                    = 30;
                      IsFillLimitOnTouch                            = true;
                      MaximumBarsLookBack                            = MaximumBarsLookBack.Infinite;
                      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("YM 03-23", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
                      AddDataSeries("YM 03-23", Data.BarsPeriodType.Second, 15, Data.MarketDataType.Last);
                      AddDataSeries("YM 03-23", Data.BarsPeriodType.Tick, 1, Data.MarketDataType.Last);
                      AddDataSeries("YM 03-23", Data.BarsPeriodType.Volume, 1, Data.MarketDataType.Last);
                  }
                  else if (State == State.DataLoaded)
                  {                
                      CumDelta = OrderFlowCumulativeDelta(BarsArray[1], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Bar, 0);
                  }
              }
      
              protected override void OnBarUpdate()
              {
      
                  if (BarsInProgress != 0)
                      return;
      
                  if (CurrentBars[1] < 1)
                      return;
      
                  Print("------------------");
                  // [0] means that we want to get the actual bar ([1] would be 1 bar ago)
                  Print(Time[0] + " --> " + CumDelta.DeltaClose[0]);
      
              }
          }
      }
      ​
      It show the following output

      Click image for larger version

Name:	image.png
Views:	224
Size:	106.4 KB
ID:	1244247

      Any guess on what could be the problem?

      Thanks!!

      Comment


        #4
        Did you change the Calculate to Calculate.OnBarClose in the code but not change the one that's already on the chart? Those are just defaults in the code and can be overridden by the specific instance on the chart.

        If you want to enforce that they can't change it, set Calculate = Calculate.OnBarClose in your strategy's State.Configure.
        Bruce DeVault
        QuantKey Trading Vendor Services
        NinjaTrader Ecosystem Vendor - QuantKey

        Comment


          #5
          Thanks Bruce, the problem was what you've said. I've added Calculate = Calculate.OnBarClos to State.Configure. and worked fine

          Comment


            #6
            Okay. Just FYI you probably don't want to do that as a matter of course - just change the properties of the one on the chart. But, if you're certain you want to ignore the user's selection and always run the strategy as OnBarClose you can do it that way. Understand that this isn't enforced completely for indicators, only for strategies - the reason is because indicators can be nested so if you put an indicator inside of a strategy, and the strategy is running with Calculate = Calculate.OnEachTick or Calculate = Calculate.OnPriceChange, then just because you set Calculate = Calculate.OnBarClose in State.Configure of the nested indicator does not mean it will be enforced there - it will always run as the Calculate mode of the outer (hosting) NinjaScript. So, for this reason, best not to get too comfortable with the idea that you can force it in all circumstances because you cannot. What you could do, if your script is not designed to run properly in other modes, is simply check if it's in the wrong mode and give an error message explaining to the user that it must be in the mode it was designed for, then exit.
            Bruce DeVault
            QuantKey Trading Vendor Services
            NinjaTrader Ecosystem Vendor - QuantKey

            Comment


              #7
              Just another question. How can I get the 3 values from 15s candle inside 1m candle?. So wahat i want to do is that if a Delta value on 1m candle is greather than 100 or less than -100, i want to check the delta value of 15s candle inside this specific 1m candle, and place a short or long

              Comment


                #8
                You'll need to have an instance of that nested indicator attached to the 1 minute series and a different instance attached to the 15-sec series. You will also have to stop doing "if (BarsInProgress != 0) return;" because otherwise you won't run the code below that except on the chart bars. You can check if BarsInProgress is equal to the series in question as needed.
                Bruce DeVault
                QuantKey Trading Vendor Services
                NinjaTrader Ecosystem Vendor - QuantKey

                Comment


                  #9
                  Nice, thanks!!

                  And how can I get the 3 15seconds delta values which are inside 1m specific candle?

                  Comment


                    #10
                    You could either do that using the timestamps to see which ones they are or by keeping track of the bar number on the 1-minute candle to see when a new 1-minute bar has begun. There are probably other ways as well. Remember they are sequential so you could just note when a new 1-minute bar has begun and get the next three 15-sec ones or something like that. Try it and see what works for you.
                    Bruce DeVault
                    QuantKey Trading Vendor Services
                    NinjaTrader Ecosystem Vendor - QuantKey

                    Comment


                      #11
                      Perfect Bruce, I’ll try and let you know how it goes. Thanks so much for the info again!!

                      Comment


                        #12
                        Hello speedytrade02,

                        Thanks for your notes.

                        QuantKey_Bruce is correct. You could remove the if (BarsinProgress != 0) return; condition from your script and print the Order Flow Cumulative Delta indicator value from the added 15-second series and the 1-minute series in your script by checking for the BarsInProgress index processing.

                        For example, since the 1-minute series is the first added series in the script you would check if (BarsInProgress == 1) and get the Order Flow Cumulative Delta value within that condition. Since the 15-second series is the second added series, you could check if (BarsInProgress == 2) and get the Order Flow Cumulative Delta value in that condition.

                        See the help guide documentation below for more information about working with multi-timeframe NinjaScripts.

                        Working with Multi-Timeframe and Multi-Instrument NinjaScripts: https://ninjatrader.com/support/help...nstruments.htm
                        BarsInProgress: https://ninjatrader.com/support/help...inprogress.htm

                        Please 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


                          #13
                          Hi Brandon and thanks for answering.

                          I've tryied to remove if (BarsinProgress != 0), and now my script looks like this:

                          HTML 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.
                          namespace NinjaTrader.NinjaScript.Strategies
                          {
                              public class test2 : Strategy
                              {
                                  //private NinjaTrader.NinjaScript.Indicators.Sim22.Sim22_DeltaV2 Sim22_DeltaV21;
                                  private OrderFlowCumulativeDelta CumDelta;
                                  private EMA EMA1;
                                  private SMA SMA1;
                          
                                  protected override void OnStateChange()
                                  {
                                      if (State == State.SetDefaults)
                                      {
                                          Description                                    = @"Joan strategy";
                                          Name                                        = "test2";
                                          Calculate                                    = Calculate.OnBarClose;
                                          EntriesPerDirection                            = 1;
                                          EntryHandling                                = EntryHandling.AllEntries;
                                          IsExitOnSessionCloseStrategy                = false;
                                          ExitOnSessionCloseSeconds                    = 30;
                                          IsFillLimitOnTouch                            = true;
                                          MaximumBarsLookBack                            = MaximumBarsLookBack.Infinite;
                                          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)
                                      {
                                          Calculate = Calculate.OnBarClose;
                                          AddDataSeries("YM 03-23", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
                                          AddDataSeries("YM 03-23", Data.BarsPeriodType.Second, 15, Data.MarketDataType.Last);
                                          AddDataSeries("YM 03-23", Data.BarsPeriodType.Tick, 1, Data.MarketDataType.Last);
                                          AddDataSeries("YM 03-23", Data.BarsPeriodType.Volume, 1, Data.MarketDataType.Last);
                                      }
                                      else if (State == State.DataLoaded)
                                      {                
                                          CumDelta = OrderFlowCumulativeDelta(BarsArray[1], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Bar, 0);
                                      }
                                  }
                          
                                  protected override void OnBarUpdate()
                                  {
                                      // Get Delta value for the current closing candle
                                      // [0] means that we want to get the actual bar ([1] would be 1 bar ago)
                                      double delta = CumDelta.DeltaClose[0];
                                      if (CurrentBars[1] < 1)
                                          return;
                          
                                      if (BarsInProgress == 1){
                                          Print(delta);                
                                      }
                          
                                      if (delta > 100 || delta < -100){
                          
                                          Print(Time[0]+ " --> "+ delta +"Hola");
                                      }
                                  }
                              }
                          }
                          ​
                          But when I run the script, I get the following error:

                          "Strategy 'test2': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."

                          Comment


                            #14
                            Hello speedytrade02,

                            Thanks for your note.

                            This error message indicates that you are trying to access a BarsAgo value that is not valid.

                            Currently, you are only checking if the first added series in the script has at least 1 bar processed (if (CurrentBars[1] < 1) return;). You should add a CurrentBars check in your script for the primary series and each added data series in the script to make sure you have enough bars processed in the series you are accessing.

                            See this help guide page for more information about adding a CurrentBars check to check if there are enough bars for each added series: https://ninjatrader.com/support/help...urrentbars.htm

                            And, see this help guide page also: https://ninjatrader.com/support/help...nough_bars.htm

                            Let me know if I may further assist.
                            <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


                              #15
                              I have an issue referencing OrderFlowCumulativeDelta in an indicator. It worked before the update. The 1 tick series is the second added series hence bip 2. Here's the code that uses delta:

                              Code:
                                          //Declarations
                                          private OrderFlowCumulativeDelta cumulativeDelta;
                                          private Series<double> highs, lows, closes;
                              
                                          //State.Configure
                                          AddDataSeries(Data.BarsPeriodType.Tick, 1);
                              
                                          //State.DataLoaded
                                           cumulativeDelta = OrderFlowCumulativeDelta(CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0);
                                           ​highs = new Series<double>(this); lows = new Series<double>(this); closes = new Series<double>(this);
                              
                                          //OnBarUpdate
                                          if (CurrentBars[0] < 60 || CurrentBars[1] < 60 || CurrentBars[2] < 60)
                                              return;
                              
                                          if (BarsInProgress == 0
                                                Print("Delta Close: " + cumulativeDelta.DeltaClose[0] + "  " + Time[0]);
                              
                                          if (BarsInProgress == 2)
                                          {
                                                cumulativeDelta.Update(cumulativeDelta.BarsArray[2].Count - 1, 2);
                                          }​
                              
                                          if (divInstrument == DivInstrument.Delta)
                                              {
                                                  highs[0] = cumulativeDelta.DeltaHigh[0];
                                                  lows[0] = cumulativeDelta.DeltaLow[0];
                                                  closes[0] = cumulativeDelta.DeltaClose[0];
                                              }​
                              Click image for larger version

Name:	image.png
Views:	155
Size:	47.2 KB
ID:	1246040

                              This used to always work so not sure what's going on?​

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              656 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              371 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              574 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              579 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X