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

Calculate Average of Series Values

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

    Calculate Average of Series Values

    Hi there,

    Is there a way to calculate the rolling average of a certain number of items stored in a custom series?

    For example, let's say I want to store 'High[0] - Low[0]' each OnBarUpdate and maintain the previous 20 values in custom series. How can I do that and then have a variable that calculates the average of the those 20 values stored in the series?

    Thanks in advance for anyone's help.

    Fiddich

    #2
    Hello fiddich,
    Thanks for your post.

    The Range() method would allow you to access the value for High[0]-Low[0] at any bar in your Data Series. If you want to get the average of the 20 most recent Range() values you could simply pass Range as an input to a 20 period SMA.

    Something similar to the following snippet would calculate the average High[0]-Low[0] value for the last 20 bars.

    Code:
    twentyBarRollingAverage = SMA(Range(), 20)[0]
    I am including a link to our help guide documentation on Range(), for your convenience.

    Range()
    https://ninjatrader.com/support/help...-us/?range.htm

    Please let me know if you have any further questions.
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      Hi Josh. Is it also possible to find the bar with the highest range ( High - Low ) within those last 20 bars also? I tried MAX ( Range, 20)[1] and it did not work.
      Thanks a lot.

      Comment


        #4
        Originally posted by Trader17 View Post
        Hi Josh. Is it also possible to find the bar with the highest range ( High - Low ) within those last 20 bars also? I tried MAX ( Range, 20)[1] and it did not work.
        Thanks a lot.
        Code:
        [COLOR=#FF0000][B]if (CurrentBar > 0) [/B][/COLOR]MAX( Range, 20)[1] ...

        Comment


          #5
          That scrip does not compile.

          Comment


            #6
            Hi Trader17,

            I'm not seeing anything that will not compile. Is there anything you are working on that I could help out with?
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              What i did was MAX(Range(),10)[1] and that compiled. Was unsure how to use if ( CurrentBar > 0 ) by koganam. Maybe that caused the error.

              Comment


                #8
                Hi Trader17,

                It might have just been a paste error since that compiles. There is a property called BarsRequiredToPlot which is 20 by default, so this will keep you from getting a barsago error until you go above it, Its good practice to do this anyway or else you will get an error like this:

                Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  Are you referring to BarsRequiredToTrade? That is set to 20. I am using this and it compiles.

                  if (CurrentBars[0] < 20)
                  return;

                  if ( ( Close[0] - Open[0] ) > MAX(Range(),10)[1] )
                  {
                  EnterLong(Convert.ToInt32(DefaultQuantity), "");
                  SetStopLoss("", CalculationMode.Currency, 1000, false);
                  }

                  Will this return the highest range of the last say 10 bars just before the bar that just closed?

                  Thanks a lot.

                  Comment


                    #10
                    Hello Trader17,

                    Thanks for your reply.

                    "Will this return the highest range of the last say 10 bars just before the bar that just closed?" If by this you mean MAX(Range(),10)[1], then it will return the maximum range value of the previous 10 bars starting with the previous bar. Please note that Range() is actually the High - Low of the bar.

                    Note: When you ask such question you can easily answer these yourself through simple print statements and we would encourage you to do this to help you gain/confirm the understanding of what is being produced. Here is a link to our debugging tips which will help get you started with using print statements: https://ninjatrader.com/support/help...script_cod.htm
                    Paul H.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by alifarahani, Today, 09:40 AM
                    6 responses
                    36 views
                    0 likes
                    Last Post alifarahani  
                    Started by Waxavi, Today, 02:10 AM
                    1 response
                    17 views
                    0 likes
                    Last Post NinjaTrader_LuisH  
                    Started by Kaledus, Today, 01:29 PM
                    5 responses
                    14 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Started by Waxavi, Today, 02:00 AM
                    1 response
                    12 views
                    0 likes
                    Last Post NinjaTrader_LuisH  
                    Started by gentlebenthebear, Today, 01:30 AM
                    3 responses
                    17 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Working...
                    X