Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 CortexZenUSA, Today, 12:53 AM
        0 responses
        1 view
        0 likes
        Last Post CortexZenUSA  
        Started by CortexZenUSA, Today, 12:46 AM
        0 responses
        1 view
        0 likes
        Last Post CortexZenUSA  
        Started by usazencortex, Today, 12:43 AM
        0 responses
        5 views
        0 likes
        Last Post usazencortex  
        Started by sidlercom80, 10-28-2023, 08:49 AM
        168 responses
        2,265 views
        0 likes
        Last Post sidlercom80  
        Started by Barry Milan, Yesterday, 10:35 PM
        3 responses
        12 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Working...
        X