Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Accessing Cumulative Delta close value in strategy

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

    Accessing Cumulative Delta close value in strategy

    Hi,

    I am developing a strategy using the NinjaScript Editor and I want to be able to access the Delta Close value from the order flow cumulative delta indicator in my strategy.

    I've read the documentation (https://ninjatrader.com/support/help...ive_delta2.htm) and I'm struggling to understand how to implement this.

    I need to use the 'BidAsk' delta type with the 'Session' period, I'll be calculating the value on bar close on the 5min timeframe.

    Can you help guide me through this?

    In addition, I'd like to generate a 50-period SMA of the Delta Close values...any guidance on this too would be appreciated.

    Thanks in advance.

    Neil
    Last edited by burtoninlondon; 06-07-2023, 06:37 AM.

    #2
    Hello Neil,

    Thank you for your inquiry.

    Working with the Order Flow Cumulative Delta in NinjaScript does require a 1-tick series, so the information on the following page about multi-time frame and instruments is helpful to keep in mind for working with a multi-series script:


    The examples found on the help guide page for Order Flow Cumulative Delta demonstrate working with a cumulative delta with a bid ask delta type and a session period. Here is the first example, and I have added some additional comments that might be useful to break it down a little further:
    Code:
    // A 1 tick data series must be added to the OnStateChange() as this indicator runs off of tick data
    else if (State == State.Configure)
    {
      AddDataSeries(Data.BarsPeriodType.Tick, 1);
    }
    
    
    // OnBarUpdate() logic
    // When BarsInProgress is 0, that means the primary series is calling OnBarUpdate(). In your case, this would be when the 5-minute bars calls OnBarUpdate()
    if (BarsInProgress == 0)
    {
    // Print the close of the cumulative delta bar with a delta type of Bid Ask and with a Session period
    Print("Delta Close: " + OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0).DeltaClose[0]);
    }
    // When BarsInProgress is 1, that means the added 1-tick data series is calling OnBarUpdate.  The Update() method is being used here to update the cumulative delta value to ensure it functions and is calculated properly
    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);
    }
    ​
    The 1-tick series is added with AddDataSeries()Some of the logic is separated to when BarsInProgress is 0 for the primary series, and other logic is separated to when BarsInProgress is 1 for the added 1-tick data seriesThere is a note on the OnBarUpdate() page about the Update() method, and an additional note about the cumulative delta indicator on the Update() page:
    In addition, I'd like to generate a 50-period SMA of the Delta Close values...any guidance on this too would be appreciated.
    One way to do this would be to save the DeltaClose values to a series<double> and then use that series as the input for the SMA indicator. There is more information about using a custom series object here:


    You could make your own series, similar to how myDoubleSeries is made on that page, then then the SMA could be calculated when BarsInProgress == 0. I added logic to the prior sample to try and demonstrate this:
    Code:
    private Series<double> myDeltaSeries;
    
    // A 1 tick data series must be added to the OnStateChange() as this indicator runs off of tick data
    else if (State == State.Configure)
    {
      AddDataSeries(Data.BarsPeriodType.Tick, 1);
    }
    else if (State==State.DataLoaded)
    {
    myDeltaSeries = new Series<double>(this);
    }
    
    
    // OnBarUpdate() logic
    // When BarsInProgress is 0, that means the primary series is calling OnBarUpdate(). In your case, this would be when the 5-minute bars call OnBarUpdate()
    if (BarsInProgress == 0)
    {
    // Print the close of the cumulative delta bar with a delta type of Bid Ask and with a Session period
    Print("Delta Close: " + OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0).DeltaClose[0]);
    myDeltaSeries[0] = OrderFlowCumulativeDelta(BarsArray[0], CumulativeDeltaType.BidAsk, CumulativeDeltaPeriod.Session, 0).DeltaClose[0];
    // Print the value of a 50 period SMA based on the delta close values that we saved to myDeltaSeries
    Print("50 period SMA of the delta close values: " + SMA(myDeltaSeries, 50)[0]);
    }
    // When BarsInProgress is 1, that means the added 1-tick data series is calling OnBarUpdate.  The Update() method is being used here to update the cumulative delta value to ensure it functions and is calculated properly
    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);
    }​​
    Please feel free to reach out with any additional questions or concerns.

    Comment


      #3
      Hi NinjaTrader_Emily, that is really helpful. thanks!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      65 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      139 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      75 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      45 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      50 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X