Averaging based on condition
Forums › ProRealTime English forum › ProBuilder support › Averaging based on condition
- This topic has 8 replies, 4 voices, and was last updated 2 years ago by gfx.
-
-
08/04/2022 at 11:10 AM #198485
Hi all,
I hope you are doing fine. I am stuck with a single problem so I’d rather ask help than building another 1000 lines indicator 🙂
The idea is to create a moving average of n candles ( n is user defined) but only of up candles.
I was thinking of a while loop with a counter, but I can’t even get a first function to work.
I get infinite loop warning when running this …
12345678910runback = 0WHILE i <= triggerBarsIF Close[runback] > Open[runback] THENi = i + 1ENDIFrunback = runback + 1WENDThks in advance for your hints.
gfx
08/04/2022 at 12:04 PM #198486What I did for you is testing with
- More and less bars (possibility of reading beyond the first bar in the chart)
- More than one instrument (Fx vs Index)
- Setting up the loop completely different (so PRT could not pre-process it and have false alarms on e.g. i not increasing for a while)
- Finding the threshold
Regarding the latter, it is always around (triggerbars) 2300 – 2500 that it fails. Under 2300 thus works (for what I tested with).
You could make a Technical Report (via the Help menu – check the checkbox to allow PRT to open and read the code – just this code but with triggerbars initiated at 3000 or so) and tell them that you found a bug.
It could be better to find a workaround because the solution may stay out many months (if ever).1 user thanked author for this post.
08/04/2022 at 12:47 PM #198488Thks. I added
1defparam CALCULATEONLASTBARS = 20and indeed it works. Though…
I tried this …
12345678910defparam CALCULATEONLASTBARS = 5// Initialise TrendFOR x = 1 to 20IF Close[x]> Open[x] THENDRAWARROW(barindex, high)ENDIFNEXTreturnand see attached picture.
First it only executes on 4 last bars … not 5.
Wired enough… if condition is not exectued as expected
08/04/2022 at 12:47 PM #19849008/04/2022 at 8:02 PM #19850408/05/2022 at 1:56 AM #198527hi…
From your original post, I think the reason you may be getting the infinite loop is this:
To exit the loop ‘i’ has to equate up to ‘triggerBars’+1.
Now let’s say halve the candles in your chart are bull candles, ‘i’ can only increment up to halve of what ever you set ‘triggerBars’ to, since it only increments on a bull candle via the if condition. Along with this may be some error checking of historical bars loaded and also it has to end the loop before moving on to next bar. Don’t really fully understand whats going on, but that’s my thinking
If you replace ‘i’ with runback in the ‘while’ condition, ‘runback’ always ends up ‘1’ bigger at the end of each loop iteration. This then terminates the loop at the end of the iteration when ‘triggerBars’ value is hit.
Any way, I was looking at huge problem and your ‘up candle moving average’ kind of brushed against a concept I come up with and was thinking of using to solve it.
While testing the concept I knocked up this code, which I think does something very close to what you were trying to do.
It identify up candle’s and uses them to display a SMA via a lookback value. Two things to note, your up candle may be different from my define, and the SMA doesn’t use the current candle, not tested.
Hold on to nuts, get your head around this!
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354defparam drawOnLastBarOnly = true // comment out to see arrows over up candlesonce cnt = -1 // up candle barindex countlookback = 10//---------------------- indentify up candles - see belowcandle = 0if open < close or (open[1] < close[1] and close[1] < close) then // define up candlecandle = 1endif//---------------------- on up candlesif candle then // if a up candlecnt=cnt+1 // increment the count on a new up candleupBarindex = cnt // store the countaccClose = accClose + close // keep an accumulated total of the up candle close's for sma calculationdrawArrowDown(barindex,high+10)elseupBarindex = -1 // if not up candle fill value with -1 for an identifierendif//---------------------- indentify last closed up candlefor i = 1 to barindex[1] // loop back to find previous upBarindexif upBarindex[i] > -1 then // stop on first non -1x= upBarindex[i]drawtext("previous #x#",0,0)anchor(middle)previous = i // previous lookback distancebreak // terminate loop when foundendifnext//---------------------- indentify lookback up candlefor i = 1 to barindex[1] // loop back to find lookback upBarindexif upBarindex[i] = upBarindex[previous]-(lookback) then // stopy = upBarindex[i]drawtext("lookback #y#",0,-20)anchor(middle)back = i // lookback distancebreak // terminate loop when foundendifnext//---------------------- calculate sma using accumulated total dataupSMA = (accClose[previous] - accClose[back])/lookback//---------------------- compare smasma= average[lookback](close)return UpSma as"upSma", sma as"sma"style(dottedline,1)1 user thanked author for this post.
08/05/2022 at 4:42 AM #198532This one works. 🙂
123456789101112131415161718DefParam CalculateOnLastBars = 10 // This is unimportant as long as it is there and "few" for speed. Minimum is 2.ExamineAmount = 100000TriggerBars = 6000 // The amount of up-bars we want to find.i = 0 // Number of UpBars we counted (keep i the same as in gfx' example).for LoopCounter = 1 to ExamineAmountif Close[LoopCounter] > Open[LoopCounter] then // Change to < for the Down-bars.i = i + 1if i > TriggerBars thenbreak // So we exit here ot ar the end of the loop (ExamineAmount).endifendifnextReturn i2nd attachment shows the down-bars.
Both are equal-ish, which I suppose it possible tonight. The chart comprises of 10K bars but it runs for a while so more bars populated.It will do the calculation at each new bar, so maybe it is not smart to let this run forever.
First it only executes on 4 last bars … not 5.
Yes, something seems wrong there because DrawOnLastBars can not be 1 (error message) and with 2 it draws 1.
1 user thanked author for this post.
08/05/2022 at 10:21 AM #19854408/05/2022 at 10:24 AM #198549 -
AuthorPosts
Find exclusive trading pro-tools on