Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Reset Order Flow Cumulative Delta to 0

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

    Reset Order Flow Cumulative Delta to 0

    Hi,

    I'm working on a strategy where Cumulative Delta would reset to 0 if a large volume where to occur and the beginning of a session. Would the code below be able to reset the cumulative delta to 0, especially the areas highlighted in red?

    Thanks!

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class Name : Strategy
    {
    private VOL VOL1;

    private double CD;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "Name";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 3;
    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;
    UserDefinedVolume = 10000;
    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Tick, 1);
    }
    else if (State == State.DataLoaded)
    {
    ClearOutputWindow();
    VOL1 = VOL(Closes[0]);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 10)
    return;

    if (BarsInProgress == 0)
    {
    // Print the close of the cumulative delta bar with a delta type of Bid Ask and with a Session period
    CD = 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);
    }


    // Reset the tradeCounter value at the first tick of the first bar of each session.
    if (Bars.IsFirstBarOfSession && IsFirstTickOfBar)
    {
    CD = 0;
    }


    if (VOL1[0]>UserDefinedVolume)
    {
    CD = 0;
    }


    if (ToTime(Times[0][0]) >= StartTradingTime)
    {
    if (CD > UserDefinedVolume)
    EnterLong();

    if (CD < -UserDefinedVolume)
    EnterShort();
    }

    #2
    Hello AgriTrdr,

    The first areas that you marked red are just a requirement of how to use that indicator. The second area where you marked red would be a way to reset your variable to 0. To know if this is working or not you would need to test it and add prints to output the values the script is observing.

    Comment


      #3
      Does anyone have a code for a simple Cumulative Delta indicator that they would be willing to share? I don't need anything fancy and would like to be able to reset the indicator to 0 at the beginning of the session and then again when large volume occurs.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      81 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      149 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      79 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      52 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      59 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X