Average Body Size of last X Candles
Forums › ProRealTime English forum › ProBuilder support › Average Body Size of last X Candles
- This topic has 10 replies, 4 voices, and was last updated 6 years ago by Vonasi.
-
-
06/11/2018 at 12:29 PM #72872
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)?
12345FOR i = 1 TO Bars DOavbody=avbody+abs(open[i]-close[i])NEXTavbody=avbody/BarsReturn avbody as "Average Body Size"Thanks
06/11/2018 at 12:56 PM #72876You 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:
1234Bars = 10a = average[Bars](range)return a06/11/2018 at 1:01 PM #7287706/11/2018 at 1:23 PM #72880Nicolas – he wants it based on body size not range so he would need:
1234Bars = 10a = average[Bars](abs(close-open))return aBut he also does not want to include the current forming bar in the calculation which this would do.
06/11/2018 at 1:27 PM #72881You 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:
12345678910111213141516171819202122232425Period = 24IF BarIndex > Period thenFOR i = 1 TO Period DOavbody=avbody+abs(open[i]-close[i])NEXTavbody=avbody/PeriodENDIFFlag = 0IF abs(open-close) > avbody and close > open thenFlag = close - openR = 0G = 128ENDIFIF abs(open-close) > avbody and close < open thenFlag = close - openR = 128G = 0ENDIFReturn Flag coloured(R,G,0) Style(histogram, 2) as " Over Average Body Size"06/11/2018 at 1:33 PM #72882123Bars = 10a = 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)
1 user thanked author for this post.
06/11/2018 at 1:48 PM #72884Thanks 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:
12345678910111213141516171819Period = 24avbody = average[period](abs(close-open))Flag = 0IF abs(open-close) > avbody[1] and close > open thenFlag = close - openR = 0G = 128ENDIFIF abs(open-close) > avbody[1] and close < open thenFlag = close - openR = 128G = 0ENDIFReturn Flag coloured(R,G,0) Style(histogram, 2) as " Over Average Body Size"06/11/2018 at 2:07 PM #72889In 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:
1234567891011121314151617avbody = average[period](abs(close-open))Flag = 0IF abs(open-close) > avbody[1] and close > open thenFlag = ((close - open)/avbody[1])*100R = 0G = 128ENDIFIF abs(open-close) > avbody[1] and close < open thenFlag = 0-(((open - close)/(avbody[1]))*100)R = 128G = 0ENDIFReturn Flag coloured(R,G,0) Style(histogram, 2) as " Over Average Body Size"06/11/2018 at 2:55 PM #72899Thank you all,
you are right, the answer was simple than i thought!
06/12/2018 at 9:51 AM #72968Since 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.
1234567891011121314151617181920212223242526MinPeriod = 10MaxPeriod = 500Qty = 0R = 0G = 128For numcandles = MinPeriod to MaxPeriodBody = abs(close - open)LargestBody = (Body = highest[NumCandles](Body))Rnge = abs(High - Low)LargestRange = (Rnge = highest[NumCandles](Rnge))IF Largestrange and Largestbody thenQty = numcandlesENDIFNEXTIF close < open thenQty = 0 - QtyR = 128G = 0endifReturn Qty coloured(R,G,0) Style(Histogram,2) as "Big Range and Body"2 users thanked author for this post.
06/12/2018 at 10:01 AM #72970Another 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:
1234567891011121314151617181920212223242526MinPeriod = 10MaxPeriod = 200Qty = 0R = 0G = 128For numcandles = MinPeriod to MaxPeriodBody = abs(close - open)SmallestBody = (Body = lowest[NumCandles](Body))Rnge = abs(High - Low)SmallestRange = (Rnge = lowest[NumCandles](Rnge))IF Smallestrange and Smallestbody thenQty = numcandlesENDIFNEXTIF close < open thenQty = 0 - QtyR = 128G = 0endifReturn Qty coloured(R,G,0) Style(Histogram,2) as "Small Range and Body" -
AuthorPosts
Find exclusive trading pro-tools on