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

How can I get Highs an Lows of bars on the chart?

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

    How can I get Highs an Lows of bars on the chart?

    Hello Support,

    I'm developing the screept and need Highs and Lows of the bars on the 5 minutes chart.
    I use the following code but it gives incorrect data.

    int bars = 100;
    bool print = false;

    protected override void OnBarUpdate()
    {
    if (print == false)
    {
    for (int index = 0; index < bars; index++)
    {
    Print(index.ToString() + " " + High[index].ToString() + " " + Low[index].ToString());
    }

    print = true;
    }
    }

    The screen with the chart and output window attached. How can I get the correct values?

    Regards.
    Attached Files

    #2
    Hello alex_bbfg,

    Thank you for writing in.

    The reason why you are getting a run-time error is because you are attempting to access non-existent values.

    An indicator will run its logic on every historical bar. On the first bar, a previous bar does not exist. You are trying to check for High[i] and Low[i] for barsAgo values of 1 - 99. A barsAgo value of 1 would be the previous bar. 2 would be the one previous to that. So on and so forth.

    So, when you try to print High[1] on the first bar, you're going to get an error because no previous bar exists.

    You can simply use the code below to print the Highs and Low of each bar as your indicator evaluates over them:
    Code:
    protected override void OnBarUpdate()
    {
         Print(CurrentBar + " " + High[0] + " " + Low[0]);
    }
    A CurrentBar value of 0 would be the first bar on your chart. 1 the second, so on and so forth.

    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Hello ZacharyG,

      Thank you for the reply. Your code works, but I did not get the logic of CurrentBar.

      I would like compare bars' data with each other not from begining to end region but on some spethified region of the chart, So, how can I set the region of the bars fore example from bar 12 to bar 38 and compare bars' highs of that region?

      Regards.

      Comment


        #4
        Hello alex_bbfg,

        Printing out values across a certain period of bars can be done as below, using your example of starting at bar 12 and ending at bar 38:
        Code:
        private int startBar = 12;
        private int endBar = 38;
        
        protected override void OnBarUpdate()
        {
             if (CurrentBar >= startBar && CurrentBar <= endBar)
                  Print("Bar " + CurrentBar + " High: " + High[0] + " Low: " + Low[0]);
        }
        CurrentBar outputs the value of the bar that the script is currently evaluating, starting at 0 (the left-most bar on the chart).
        Zachary G.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Mizzouman1, Today, 07:35 AM
        4 responses
        18 views
        0 likes
        Last Post Mizzouman1  
        Started by philmg, Today, 01:17 PM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_ChristopherJ  
        Started by cre8able, Today, 01:01 PM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by manitshah915, Today, 12:59 PM
        1 response
        5 views
        0 likes
        Last Post NinjaTrader_Erick  
        Started by ursavent, Today, 12:54 PM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X