Average Body Size of last X Candles

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #72872 quote
    Alberto Lucchese
    Participant
    Average

    Hello everyone,

    As far as I understand the average[X] function will take the average of the X last bars, including the current one. So i need to create a simple indicator that will provide the average body size of the last N bars. I created the following code but the result is always 1, any thoughts (“Bars” is an indicator variable, an integer)?

    FOR i = 1 TO Bars DO
    avbody=avbody+abs(open[i]-close[i])
    NEXT
    avbody=avbody/Bars
    Return avbody as "Average Body Size"
    

    Thanks

    #72876 quote
    Nicolas
    Keymaster
    Master

    You must define “Bars”, otherwise your loop will not start to count.

    How to make loop in ProRealTime is a part of the advanced training program.

    About your indicator, you’ll get the same result with just:

    Bars = 10
    a = average[Bars](range)
    
    return a
    #72877 quote
    Vonasi
    Moderator
    Master

    Try this:

    bars = 6
    
    IF BarIndex > bars then
    FOR i = 1 TO Bars DO
    avbody=avbody+abs(open[i]-close[i])
    NEXT
    avbody=avbody/Bars
    ENDIF
    
    Return avbody as "Average Body Size"
    #72880 quote
    Vonasi
    Moderator
    Master

    Nicolas – he wants it based on body size not range so he would need:

    Bars = 10
    a = average[Bars](abs(close-open))
     
    return a

    But he also does not want to include the current forming bar in the calculation which this would do.

    #72881 quote
    Vonasi
    Moderator
    Master

    You could also turn the idea into an indicator that shows you when the current bar is larger than the average of the last n bars not including this one and present it as a histogram with red for down bars and green for up bars and bar size based on body size like this:

    Period = 24
    
    IF BarIndex > Period then
    FOR i = 1 TO Period DO
    avbody=avbody+abs(open[i]-close[i])
    NEXT
    avbody=avbody/Period
    ENDIF
    
    Flag = 0
    
    IF abs(open-close) > avbody and close > open then
    Flag = close - open 
    R = 0
    G = 128
    ENDIF
    
    IF abs(open-close) > avbody and close < open then
    Flag = close - open
    R = 128
    G = 0
    ENDIF
    
    
    Return Flag coloured(R,G,0) Style(histogram, 2) as " Over Average Body Size"
    #72882 quote
    robertogozzi
    Moderator
    Master
    Bars = 10
    a = average[Bars](abs(close-open))
    return a[1]

    This should do for n bars BEFORE the current one, nevertheless it’ll be displayed under the CURRENT forming bar! (it is shifted)

    Alberto Lucchese thanked this post
    #72884 quote
    Vonasi
    Moderator
    Master

    Thanks for that Robertogozzi. Sometimes it is difficult to see the really simple answer even when it is staring you in the face!

    That would also mean that my histogram indicator becomes this and has the benefit of drawing much faster:

    Period = 24
    
    avbody = average[period](abs(close-open))
    
    Flag = 0
    
    IF abs(open-close) > avbody[1] and close > open then
    Flag = close - open
    R = 0
    G = 128
    ENDIF
    
    IF abs(open-close) > avbody[1] and close < open then
    Flag = close - open
    R = 128
    G = 0
    ENDIF
    
    Return Flag coloured(R,G,0) Style(histogram, 2) as " Over Average Body Size"
    
    #72889 quote
    Vonasi
    Moderator
    Master

    In fact I think that it is better to display it as a percentage of the last n bars average range as it is more bounded.

    Here’s the adapted code:

    avbody = average[period](abs(close-open))
    
    Flag = 0
    
    IF abs(open-close) > avbody[1] and close > open then
    Flag = ((close - open)/avbody[1])*100
    R = 0
    G = 128
    ENDIF
    
    IF abs(open-close) > avbody[1] and close < open then
    Flag = 0-(((open - close)/(avbody[1]))*100)
    R = 128
    G = 0
    ENDIF
    
    Return Flag coloured(R,G,0) Style(histogram, 2) as " Over Average Body Size"
    
    #72899 quote
    Alberto Lucchese
    Participant
    Average

    Thank you all,

    you are right, the answer was simple than i thought!

    #72968 quote
    Vonasi
    Moderator
    Master

    Since you posted this question I have been playing around with looking for candles that have a larger range than n candles before and larger body size than n candles before as interesting times to be in the market (good volatility) and so I came up with this indicator that counts back to see how long it is since a candle with the current range and size of body was last seen.

    I thought that I would post it here in case it is of any interest to anybody.

    You can set the minimum period that you wish the count back to start from and also the maximum period to count back.

    The result is returned in a histogram with down red candles if the close was lower than the open and green up candles if it was higher than the open. Hover over the candle and the size represents the number of candles since one with a body size and range bigger was last seen.

    MinPeriod = 10
    MaxPeriod = 500
    
    Qty = 0
    R = 0 
    G = 128
    
    For numcandles = MinPeriod to MaxPeriod
    Body = abs(close - open)
    LargestBody = (Body = highest[NumCandles](Body))
    
    Rnge = abs(High - Low)
    LargestRange = (Rnge = highest[NumCandles](Rnge))
    
    IF Largestrange and Largestbody then
    Qty = numcandles
    ENDIF
    NEXT
    
    IF close < open then
    Qty = 0 - Qty
    R = 128
    G = 0
    endif
    
    Return Qty coloured(R,G,0) Style(Histogram,2) as "Big Range and Body"
    robertogozzi and eckaw thanked this post
    #72970 quote
    Vonasi
    Moderator
    Master

    Another alternative is to look for the smallest body size and range in n bars. This is a good way to see if a market is running out of momentum and maybe a turning point.

    Here is the code showing how long it is since a candle this small was last seen:

    MinPeriod = 10
    MaxPeriod = 200
    
    Qty = 0
    R = 0
    G = 128
    
    For numcandles = MinPeriod to MaxPeriod
    Body = abs(close - open)
    SmallestBody = (Body = lowest[NumCandles](Body))
    
    Rnge = abs(High - Low)
    SmallestRange = (Rnge = lowest[NumCandles](Rnge))
    
    IF Smallestrange and Smallestbody then
    Qty = numcandles
    ENDIF
    NEXT
    
    IF close < open then
    Qty = 0 - Qty
    R = 128
    G = 0
    endif
    
    Return Qty coloured(R,G,0) Style(Histogram,2) as "Small Range and Body"
Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.

Average Body Size of last X Candles


ProBuilder: Indicators & Custom Tools

New Reply
Author
Summary

This topic contains 10 replies,
has 4 voices, and was last updated by Vonasi
7 years, 8 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 06/11/2018
Status: Active
Attachments: No files
Logo Logo
Loading...