• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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 mohdhm, Today, 04:09 PM
        0 responses
        1 view
        0 likes
        Last Post mohdhm
        by mohdhm
         
        Started by BGWTEX, Today, 03:30 PM
        1 response
        3 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by marekflys, Today, 03:07 PM
        0 responses
        5 views
        0 likes
        Last Post marekflys  
        Started by GregoryBaskin, Today, 02:51 PM
        1 response
        4 views
        0 likes
        Last Post NinjaTrader_Tyler  
        Started by swcooke, Today, 12:33 PM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X