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 CarlTrading, 03-31-2026, 09:41 PM
      1 response
      45 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      21 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      31 views
      1 like
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      50 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      42 views
      0 likes
      Last Post CarlTrading  
      Working...
      X