Bizarre looping problem

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #105184 quote
    GrantxGrantx
    Participant
    Average

    This is bizarre. I basically want to get the average of the last x amount of bars.

    Even when I set the loop to 1 it doesnt calculate properly.

    Pseudo code should be something like this:

    1. Go back 10 bars and add up all the bar ranges
    2. Divide the total amount by the lookbackperiod when the loop has completed

     

    The calculations are completely incorrect. Im stuck as I cant see anything wrong with the code. Line 15 seems to be the problem but why?

    Note: I am running this on the m6e futures which is why I multiply by 10000

    //Grantx
    
    defparam drawonlastbaronly=true
    
    //--- settings
    Loockbackperiod = 10
    //--- end of settings
    
    RngeSum = 0
    largestBar = 0
    FOR i = 0 TO Loockbackperiod DO
    if (Range[i]>largestBar) THEN
    largestBar = Range[i]
    ENDIF
    RngeSum = RngeSum + Range[i]
    NEXT
    //RngeSum = Round((RngeSum / Loockbackperiod)*10000)
    RngeSum = Round(RngeSum*10000)
    largestBar = largestBar*10000
    
    drawtext("                                                                                            ABR(#Loockbackperiod#) = #RngeSum# ¬ LB = #largestBar#",barindex,High,Dialog,Standard,10) coloured(0,0,255)
    
    RETURN
    
    #105185 quote
    GraHalGraHal
    Participant
    Master

    Does below (for Lines 9 and 10) make any difference?

    ONCE RngeSum = 0
    ONCE largestBar = 0
    Grantx thanked this post
    #105186 quote
    robertogozzirobertogozzi
    Moderator
    Legend

    You are summing up 11 bars (0 – 10), not 10.

    will

    LookBackPeriod = 10
    AverageRange   = summation[LookBackPeriod](range) / LookBackPeriod

    do?

    Grantx thanked this post
    #105188 quote
    GrantxGrantx
    Participant
    Average

    Yep. This works. Thank you!

    Also, thanks @GraHal for replying.

    Much appreciated.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Bizarre looping problem


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Grantx @grantx Participant
Summary

This topic contains 3 replies,
has 3 voices, and was last updated by GrantxGrantx
7 years, 1 month ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 08/21/2019
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...