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 to address a series of bars ?

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

    How to address a series of bars ?

    Hello

    One of the conditions of my custom indicator is that it draws an arrow on the chart every time the high of a candle is lower than the high of at least one of the highs of the last ten candles. Is there a more efficient way to code this than to include every possible situation in the code?

    Currently it looks like this:

    if(High[0] < High[1] || High[0] < High[2] || High[0] < High[3] || etc until High[10]

    I'm looking for the correct version of something like:

    if(High[0] < High[1-10]

    Thanks

    #2
    Hello,

    This is one way you could do it.

    such as the following,

    for (int i = 0; i < 10; i++)
    {
    if (High[0] > High[i])
    {
    //Either store a counter variable and check to make sure this is equal to something to amke sure it did occur in the last 10 bars. This would iterate through all 10 previous values, or whatever you set as the condition for the for loop for how many bars back you want to go.

    Comment


      #3
      Hi Brett

      I just implemented what you suggested in my strategy, but it gets automatically disabled when I hit F5.

      Here's the code snippet:

      for (int i = 0; i < 30; i++)
      {
      if (High[0] < High[i])
      {
      SpaceToExit = true;
      }

      else
      {
      SpaceToExit = false;
      }
      }

      What did I miss?

      Thanks a lot.

      Comment


        #4
        Hello,

        PLease check your log tab in the control center to see the error.

        Most likely its the eror saying you dont have enough bars in the chart.

        Please see this tips page this will most likely do the trick. You need to make sure you have at least 30 bars before running this for loop.



        Let me know if I can be of further assistance.

        Comment


          #5
          Brett

          The log says "You are accessing an index with a value that is invalid since it's out of range, ie accessing a series with a value of 5 when there are only 4 bars on the chart".

          I don't understand this as there are dozens of bars on my chart.

          What am I missing?

          Thanks

          Comment


            #6
            Hello,

            This is the error I was talking about in my previous post.

            Please add the article information in that I linked to in my previous post to resolve this issue.

            OnBarUpdate() runs for every bar in the chart. Starting from Bar 1 to the last bar in the cahrt. What occurs is that when it first starts on Bar 1, its trying to grab a value from 30 bars ago. THis will not work as there is no bar that time. Therefor you get an error.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by zstheorist, Today, 07:52 PM
            0 responses
            3 views
            0 likes
            Last Post zstheorist  
            Started by pmachiraju, 11-01-2023, 04:46 AM
            8 responses
            149 views
            0 likes
            Last Post rehmans
            by rehmans
             
            Started by mattbsea, Today, 05:44 PM
            0 responses
            5 views
            0 likes
            Last Post mattbsea  
            Started by RideMe, 04-07-2024, 04:54 PM
            6 responses
            33 views
            0 likes
            Last Post RideMe
            by RideMe
             
            Started by tkaboris, Today, 05:13 PM
            0 responses
            5 views
            0 likes
            Last Post tkaboris  
            Working...
            X