Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Why my indicator is 1 bar lag? Very weird.

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

    Why my indicator is 1 bar lag? Very weird.

    Hi,

    I developed an indicator that calculates on each tick. However, when it gets plotted on chart, the indicator value is one bar behind comparing to current bar. Please see my screenshot attached.

    Would you please help me understand why this could happen, and how it can be fixed?

    Thank you!

    #2
    Hi HiddenPhilosopher, thanks for your question.

    Are you updating the Value[0] array on every OnBarUpdate call? We would need details on the custom script to see what the problem is. Indicators like the SMA, VOL indicator work as expected when set to OnEachTick.

    I look forward to hearing from you.

    Comment


      #3
      Originally posted by NinjaTrader_ChrisL View Post
      Hi HiddenPhilosopher, thanks for your question.

      Are you updating the Value[0] array on every OnBarUpdate call? We would need details on the custom script to see what the problem is. Indicators like the SMA, VOL indicator work as expected when set to OnEachTick.

      I look forward to hearing from you.
      Hi Chris - Please see my code below:

      Code:
      protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                                    = @"Enter the description for your new custom Indicator here.";
                      Name                                        = "MyIndicator";
                      Calculate                                    = Calculate.OnEachTick;
                      IsOverlay                                    = false;
                      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;
                      AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.Bar, "MyBar");
                  }
                  else if (State == State.Configure)
                  {
                      AddDataSeries(Data.BarsPeriodType.Tick, 1);
                  }
              }
      
              protected override void OnBarUpdate()
              {
                  if (CurrentBars[1] < 100000)
                      return;
      
                  MyBar[0] += Volumes[1][0];
              }
      See my screenshot attached. If I compare my result with regular volume indicator, you can clearly see that it's 1 bar behind.
      Attached Files
      Last edited by HiddenPhilosopher; 06-27-2020, 07:37 PM.

      Comment


        #4
        Hi HiddenPhilosopher, thanks for your reply. Thats happening because you're not updating the plot on the primary series. The Plot will have an index for every bar of the primary series, not the secondary. I was able to get it to work like so:

        Code:
        public class TestIndicator : Indicator
            {
                protected override void OnStateChange()
                {
                    if (State == State.SetDefaults)
                    {
                        Calculate                                    = Calculate.OnEachTick;
                    }
                    else if (State == State.Configure)
                    {
                        AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.Bar, "MyBar");
                        AddDataSeries(Data.BarsPeriodType.Tick, 1);
                    }
        
                }
        
                double volumeSum = 0;
        
                protected override void OnBarUpdate()
                {
                    if(BarsInProgress == 0)
                    {
                        if(IsFirstTickOfBar)
                        {
                            volumeSum = Volumes[1][0];
                        }
                        else 
                        {
                            volumeSum += Volumes[1][0];
                        }
        
                        Value[0] = volumeSum;
                    }
                }
        
            }
        Please let me know if I can assist any further.

        Comment


          #5
          Originally posted by NinjaTrader_ChrisL View Post
          Hi HiddenPhilosopher, thanks for your reply. Thats happening because you're not updating the plot on the primary series. The Plot will have an index for every bar of the primary series, not the secondary. I was able to get it to work like so:

          Code:
          public class TestIndicator : Indicator
          {
          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Calculate = Calculate.OnEachTick;
          }
          else if (State == State.Configure)
          {
          AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.Bar, "MyBar");
          AddDataSeries(Data.BarsPeriodType.Tick, 1);
          }
          
          }
          
          double volumeSum = 0;
          
          protected override void OnBarUpdate()
          {
          if(BarsInProgress == 0)
          {
          if(IsFirstTickOfBar)
          {
          volumeSum = Volumes[1][0];
          }
          else
          {
          volumeSum += Volumes[1][0];
          }
          
          Value[0] = volumeSum;
          }
          }
          
          }
          Please let me know if I can assist any further.
          Thank you so much Chris! You solved my problem!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          673 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          379 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          111 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          577 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          582 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X