Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unable to plot previous bar values

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

    Unable to plot previous bar values

    Hi,

    I'm trying to plot values from previous bars within OnBarUpdate() however nothing is being plotted when I access bar values from previous bars:

    Code:
    Value[0] = High[1] - Low[1];
    Accessing the current bar works fine:
    Code:
    Value[0] = High[0] - Low[0];
    Below is a basic example indicator trying to plot the previous bar's range.
    Should this not plot the previous bars range? Or am I missing something?

    Code:
    public class PrevRange : Indicator
    {
    	protected override void OnStateChange()
    	{
    		if (State == State.SetDefaults)
    		{
    			Description									= @"Enter the description for your new custom Indicator here.";
    			Name										= "PrevRange";
    			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.Orange, 2), PlotStyle.Bar, "PrevRangePlot");
    		}
    		else if (State == State.Configure)
    		{
    		}
    	}
    
    
    	protected override void OnBarUpdate()
    	{
    		[B]Value[0] = High[1] - Low[1];[/B]
    	}
    
    	#region Properties
    
    	[Browsable(false)]
    	[XmlIgnore]
    	public Series<double> PrevRangePlot
    	{
    		get { return Values[0]; }
    	}
    	#endregion
    }
    Thanks,
    Chris

    #2
    Originally posted by Chris.M View Post
    Hi,

    I'm trying to plot values from previous bars within OnBarUpdate() however nothing is being plotted when I access bar values from previous bars:

    Code:
    Value[0] = High[1] - Low[1];
    Accessing the current bar works fine:
    Code:
    Value[0] = High[0] - Low[0];
    Below is a basic example indicator trying to plot the previous bar's range.
    Should this not plot the previous bars range? Or am I missing something?

    Code:
    public class PrevRange : Indicator
    {
    	protected override void OnStateChange()
    	{
    		if (State == State.SetDefaults)
    		{
    			Description									= @"Enter the description for your new custom Indicator here.";
    			Name										= "PrevRange";
    			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.Orange, 2), PlotStyle.Bar, "PrevRangePlot");
    		}
    		else if (State == State.Configure)
    		{
    		}
    	}
    
    
    	protected override void OnBarUpdate()
    	{
    		[B]Value[0] = High[1] - Low[1];[/B]
    	}
    
    	#region Properties
    
    	[Browsable(false)]
    	[XmlIgnore]
    	public Series<double> PrevRangePlot
    	{
    		get { return Values[0]; }
    	}
    	#endregion
    }
    Thanks,
    Chris
    You cannot access High[1] until there are at least 2 bars on the chart. You want to use an escape condition:
    Code:
    if (CurrentBar < 1) return;
    before your code accessing the bars.

    If you look in your log, you will see an error message: "You are accessing an index with a value that is invalid since its out of range. i.e. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart." If you search the forum on that error message, you will find many answers along the same lines that I have given above, some more explanatory than others.

    The log is always the first place you should look for error messages when things do not go as you expect.

    Comment


      #3
      Indeed the error was present in the log and I was missing the escape condition.

      Thank you very much koganam.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      558 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      324 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      545 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      547 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X