Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

N Bar High Low

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

    N Bar High Low

    My requirement is to draw the Highest High and Lowest Low of last 3 bars of a 15 minute data series. These lines have to be displayed on a Unirenko chart. My code is as below. But I do not see the lines. Am I doing something wrong ? Appreciate your help.
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class NBar15MinuteHL : Indicator
    {
    #region Private Variables
    private int highestBarsAgo;
    private double highestPrice;
    private int lowestBarsAgo;
    private double lowestPrice;

    #endregion

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Highest High/Lowest Low of last n bars on 15 min chart";
    Name = "NBar15MinuteHL";
    Calculate = Calculate.OnBarClose;
    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;
    NBars = 3;
    AddLine(Brushes.Lime, 1, "High");
    AddLine(Brushes.Red, 1,"Low");

    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 15);
    }
    }

    protected override void OnBarUpdate()
    {
    //Add your custom indicator logic here.

    if (CurrentBar<NBars)
    return;

    highestBarsAgo = HighestBar(High, NBars);
    highestPrice = High[highestBarsAgo];

    if (highestPrice >= High[0])
    {

    //Draw.Line(this, "High"+CurrentBar,false,1,high,0,high,Brush es.Silver,DashStyleHelper.Solid,4);
    Draw.Line(this, "High"+CurrentBar, false,NBars , 1000, 0, 1001, Brushes.LimeGreen, DashStyleHelper.Dot, 2);
    }

    lowestBarsAgo = LowestBar(Low, NBars);
    lowestPrice = Low[lowestBarsAgo];

    if (lowestPrice <= Low[0])
    {

    //Draw.Line(this, "High"+CurrentBar,false,1,high,0,high,Brush es.Silver,DashStyleHelper.Solid,4);
    Draw.Line(this, "Low"+CurrentBar, false,NBars , 1000, 0, 1001, Brushes.Red, DashStyleHelper.Dot, 2);
    }

    }

    #2
    Hello sairamsrini,

    Is the condition that draws the line evaluating as true?

    Below is a public link to a forum post that demonstrates using prints to understand behavior.


    If the condition is evaluating as true based on the output from the prints, are there any errors appearing in the Log tab of the Control Center when running the script?

    Do you see the drawing objects appearing in the Draw Objects window?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,
      Thank you for taking up my request. I get an error: Error on calling 'OnBarUpdate' method on bar 1: Index was outside the bounds of the array.

      -Sairam

      Comment


        #4
        Hello sairamsrini,

        I understand you are getting that message.

        Have you used prints to debug and specify which line of code is causing the error?

        Are you understanding how using an invalid index causes this error?

        What index is causing this error?

        Is the index a valid index for a bars ago value or for an array element based on the condition that calls that index?
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        637 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        366 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        107 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        569 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        572 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X