Averaging down code
Forums › ProRealTime English forum › ProOrder support › Averaging down code
- This topic has 5 replies, 3 voices, and was last updated 4 years ago by robertogozzi.
-
-
02/02/2020 at 2:20 AM #118459
Hi, I have added this averaging down code to the bottom of a strategy and changed cumulateorders to true but I get an error saying that haopen and haclose are undefined.
What else do I need to do?
123456789101112131415161718// averaging downif longonmarket and haopen<haclose and haopen[1]>haclose[1] and haclose<tradeprice thenBUY 1 SHARE AT MARKETendif// monitor the average price of whole orders and close them accordinglyif longonmarket and haclose>positionprice and countofposition>-1 thenSELL AT MARKETendifif shortonmarket and haopen>haclose and haopen[1]<haclose[1] and haclose>tradeprice thenSELLSHORT 1 SHARE AT MARKETendif// monitor the average price of whole orders and close them accordinglyif shortonmarket and haclose<positionprice and countofposition<-1 thenEXITSHORT AT MARKETendif02/02/2020 at 8:47 AM #118462haopen and haclose need to be defined somewhere in the code. I suspect you have borrowed the snippet from someone that was using it in a hiekin ashi strategy where haclose and haopen were the calculated values for the HA candles.
02/02/2020 at 9:46 AM #118467This is the HA setup to be added to your code, at the beginning:
123456789// HA - definizione Heikin-Ashi//once haOpen = openhaClose = (open+close+high+low)/4if barindex > 0 thenhaOpen = (haOpen+haClose[1])/2endifhaLow = min(low,min(haClose,haOpen))haHigh = max(high,max(haClose,haOpen))any reference to unused variable shall be commented out.
02/02/2020 at 12:08 PM #118488Thanks Roberto, but I’m still a bit confused as Heikin-Ashi is new territory for me. I got the snippet here:
https://www.prorealcode.com/blog/trading/averaging-techniques-automated-trading/
Is Heikin-Ashi an essential component to the averaging down technique? or is it a part of the main strategy in Nicolas’s example?
02/02/2020 at 12:55 PM #118492This is the code I’m modifying, as devised by Soulintact. It’s a buy and hold strategy that only sells when in profit and I thought it ‘might’ benefit from averaging down.
Might also crash and burn.
I have tried adding your Heikin-Ashi definition at the top but not sure if I’m doing it right. Does not seem to have the expected effect.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161//-------------------------------------------------------------------------defparam cumulateorders=falseDefparam preloadbars=10000amount = 1 //quantity of shares/contracts to open for each new order//--------------------------------------------------------------------------------////Indicators//Indicator: PRC_OneMoreAverage MACD//Release date: 23.11.2016//Web: https://www.prorealcode.com/prorealtime-indicators/one-more-average-macd///Code translator: Nicolas @ www.prorealcode.com//--parameters//>OMA parametersSensibility = 1Adaptive = 1//>MACD periodsFLength = 24SLength = 52//>signal lineSigLength = 9Sigma = 4Offset = 0.85// HA - definizione Heikin-Ashi//once haOpen = openhaClose = (open+close+high+low)/4if barindex > 0 thenhaOpen = (haOpen+haClose[1])/2endif//haLow = min(low,min(haClose,haOpen))//haHigh = max(high,max(haClose,haOpen))//--------if barindex > 129 thenSpeed = SensibilitySpeed = Max(Speed,-1.5)price = average[1](customclose)tconst=Speed//--Fast moving averageFLength = Max(FLength,1)//adaptive periodaveragePeriod = FLengthif adaptive=1 and averagePeriod > 1 thenminPeriod = averagePeriod/2.0maxPeriod = minPeriod*5.0endPeriod = round(maxPeriod)signal = Abs((price-stored[endPeriod]))noise = 0.00000000001for k=1 to endPeriod donoise=noise+Abs(price-stored[k])averagePeriod = round(((signal/noise)*(maxPeriod-minPeriod))+minPeriod)nextendifalpha = (2.0+tconst)/(1.0+tconst+averagePeriod)e1 = e1 + alpha*(price-e1)e2 = e2 + alpha*(e1-e2)v1 = 1.5 * e1 - 0.5 * e2e3 = e3 + alpha*(v1 -e3)e4 = e4 + alpha*(e3-e4)v2 = 1.5 * e3 - 0.5 * e4e5 = e5 + alpha*(v2 -e5)e6 = e6 + alpha*(e5-e6)Fast = 1.5 * e5 - 0.5 * e6//------------------------------------//--Slow moving averageSLength = Max(SLength,1)//adaptive periodSaveragePeriod = SLengthif adaptive=1 and SaveragePeriod > 1 thenSminPeriod = SaveragePeriod/2.0SmaxPeriod = SminPeriod*5.0SendPeriod = round(maxPeriod)Ssignal = Abs((price-stored[SendPeriod]))Snoise = 0.00000000001for k=1 to SendPeriod doSnoise=Snoise+Abs(price-stored[k])SaveragePeriod = round(((Ssignal/Snoise)*(SmaxPeriod-SminPeriod))+SminPeriod)nextendifSalpha = (2.0+tconst)/(1.0+tconst+SaveragePeriod)Se1 = Se1 + Salpha*(price-Se1)Se2 = Se2 + Salpha*(Se1-Se2)Sv1 = 1.5 * Se1 - 0.5 * Se2Se3 = Se3 + Salpha*(Sv1 -Se3)Se4 = Se4 + Salpha*(Se3-Se4)Sv2 = 1.5 * Se3 - 0.5 * Se4Se5 = Se5 + Salpha*(Sv2 -Se5)Se6 = Se6 + Salpha*(Se5-Se6)Slow = 1.5 * Se5 - 0.5 * Se6//------------------------------------//--Signal moving averageOMAMACD = Slow-FastSigLength = Max(SigLength,1)//---Signal MAn = (Offset * (SigLength - 1))t = SigLength/SigmaSWtdSum = 0SCumWt = 0for k = 0 to SigLength - 1 doSWtd = Exp(-((k-n)*(k-n))/(2*t*t))SWtdSum = SWtdSum + SWtd * OMAMACD[SigLength - 1 - k]SCumWt = SCumWt + SWtdnextSIGMACD = SWtdSum / SCumWt//------------------------------------stored=price//------------------------------------BullSMA=(SIGMACD<OMAMACD)BearSMA=(SIGMACD>OMAMACD)//--------------------------------------------------------------------------------////Conditions when to actif (not longonmarket and BullSMA)thenbuy amount shares at marketendifif (longonmarket and BearSMA and positionperf>0) thensell at marketendifendif// averaging downif longonmarket and haopen<haclose and haopen[1]>haclose[1] and haclose<tradeprice thenBUY 1 SHARE AT MARKETendif// monitor the average price of whole orders and close them accordinglyif longonmarket and haclose>positionprice and countofposition>-1 thenSELL AT MARKETendif//if shortonmarket and haopen>haclose and haopen[1]<haclose[1] and //haclose>tradeprice then//SELLSHORT 1 SHARE AT MARKET//endif// monitor the average price of whole orders and close them accordingly//if shortonmarket and haclose<positionprice and countofposition<-1 then//EXITSHORT AT MARKET//endif02/02/2020 at 1:11 PM #118493Averaging down can be done on both common japanese and HA candlesticks.
Use CLOSE, OPEN, HIGH and LOW when you want to use common candlesticks.
1 user thanked author for this post.
-
AuthorPosts
Find exclusive trading pro-tools on