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
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
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"
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.
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"
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)
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"
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"
Thank you all,
you are right, the answer was simple than i thought!
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"
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"