Backtest Nicolas "TrendFollowingMovingAverage"
Forums › ProRealTime English forum › ProOrder support › Backtest Nicolas "TrendFollowingMovingAverage"
- This topic has 12 replies, 3 voices, and was last updated 3 years ago by Nicolas.
-
-
06/17/2020 at 11:08 AM #136236
Hi everyone. I got interested in Nicolas Trend Following Moving Average and decided to backtest a simple strategy based on his indicator, which can be found here: https://www.prorealcode.com/prorealtime-indicators/trend-following-moving-averages/.
However I seem to not fully understand the code which makes it hard to make a backtest. I would simply like to buy when the entire “cloud” is green and sell when it is red. Alternatively, buy when close crosses over the “cloud” and sell when it crosses under. As it stands, the program doesn´t make any trades whatsoever. Could someone please check my code and see what I´m doing wrong?
Nicolas Trend Follower1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465// --- settings//prd = 20 //Period to Check Trend//rateinp = 1 //Trend Channel Rate % minval = 0.1, step = 0.1//ulinreg = 1 // 1=true, Use Linear Regression//linprd = 10 //Linear Regression Period, minval = 2//matype = 1 //ma type 0=sma, 1-ema, etc.// --- end of settingsDEFPARAM PRELOADBARS = 100matype = ExponentialAverage[p](close)ulinreg = 1rate = rateinp / 100pricerange = highest[100](close) - lowest[100](close)chan = pricerange * ratep = 5while p<=100 do//trendmasrc = average[p,matype](close)if ulinreg thenma = LinearRegression[linprd](masrc)elsema = masrcendifhh = highest[prd](ma)ll = lowest[prd](ma)diff = abs(hh-ll)if diff>chan thenif ma>ll+chan thentrend=1elsif ma<hh-chan thentrend=-1elsetrend=0endifelsetrend=0endifret = trend*diff/chanalpha = min(255,(80+abs(ret*10)))if ret<0 thenr=255g=0elser=0g=255endif$prev[p] = map=p+5wendif trend = 1 thenbuy 10 contracts at marketendifif trend = -1 thensell at marketendif06/17/2020 at 11:33 AM #13623906/17/2020 at 11:50 AM #136241Hello GraHal. If you check the original code from Nicolas indicator you see that before line 59 there is the “drawsegment” line. Since this line is not required in Probacktest I decided to remove it. Perhaps prev[p] should be removed aswell?
06/17/2020 at 11:50 AM #13624206/17/2020 at 12:01 PM #136243Yes I know, I am using V11 for this backtest so it shouldn´t be a problem. I´m simply wondering why it is not taking any trades?
06/17/2020 at 12:56 PM #136253There is something wrong by the calculation in the loop. I’m investigating it and let you know if I can make it working.
The code i’m using is this one: (for debugging purpose)
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374DEFPARAM PRELOADBARS = 10000// --- settingsprd = 20 //Period to Check Trendrateinp = 1 //Trend Channel Rate % minval = 0.1, step = 0.1ulinreg = 0 // 1=true, Use Linear Regressionlinprd = 10 //Linear Regression Period, minval = 2matype = 1 //ma type 0=sma, 1-ema, etc.// --- end of settingsmatype = ExponentialAverage[p](close)ulinreg = 1rate = rateinp / 100pricerange = highest[100](close) - lowest[100](close)chan = pricerange * ratep = 5count=0while p<=100 do//trendmasrc = average[p,matype](close)if ulinreg thenma = LinearRegression[linprd](masrc)elsema = masrcendifhh = highest[prd](ma)ll = lowest[prd](ma)diff = abs(hh-ll)if diff>chan thenif ma>ll+chan thentrend=1elsif ma<hh-chan thentrend=-1elsetrend=0endifelsetrend=0endifif chan>0 thenret = trend*diff/chanendifalpha = min(255,(80+abs(ret*10)))if ret<0 thenr=255g=0elser=0g=255endifp=p+5count=count+1wendif trend = 1 thenbuy 10 contracts at marketendif//graph retgraph countgraphonprice hhgraphonprice llgraphonprice magraphonprice ll+chan1 user thanked author for this post.
06/18/2020 at 8:23 AM #136349Thank you very much for taking the time to investigate @Nicolas. I hope we can find the solution!
06/18/2020 at 9:33 AM #136371I solved the problem, you made a change in the code that result in a wrong calculation of the moving average … anyway, here is the version that buy only (but with no exit either in loss or profit, i let you manage that!).
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980//DEFPARAM PRELOADBARS = 10000defparam cumulateorders=false// --- settingsprd = 20 //Period to Check Trendrateinp = 1 //Trend Channel Rate % minval = 0.1, step = 0.1ulinreg = 0 // 1=true, Use Linear Regressionlinprd = 10 //Linear Regression Period, minval = 2matype = 1 //ma type 0=sma, 1-ema, etc.// --- end of settings//matype = ExponentialAverage[p](close)ulinreg = 1rate = rateinp / 100pricerange = highest[100](close) - lowest[100](close)chan = pricerange * ratep = 5count=0green=0red=0while p<=100 do//trendmasrc = average[p,matype](close)if ulinreg thenma = LinearRegression[linprd](masrc)elsema = masrcendifhh = highest[prd](ma)ll = lowest[prd](ma)diff = abs(hh-ll)if diff>chan thenif ma>ll+chan thentrend=1elsif ma<hh-chan thentrend=-1elsetrend=0endifelsetrend=0endifif chan>0 thenret = trend*diff/chanendifalpha = min(255,(80+abs(ret*10)))if ret<0 thenr=255g=0red=red+1elser=0g=255green=green+1endifp=p+5count=count+1wendif green=count thenbuy 1 contracts at marketendif//graph ret//graph count//graphonprice hh//graphonprice ll//graphonprice ma//graphonprice ll+chan06/18/2020 at 10:33 AM #136383Excellent, thank you very much for the help Nicolas!
06/18/2020 at 11:29 AM #136393Sorry to disturb you again @Nicolas but the system is still not working properly. I tried the “green = count” command and the entrys are almost completely wrong, it even enters trades when the average is red. So I tried writing and indicator with a simple “green crosses over red” command, and it shows exactly and properly where I want to enter (white arrows in the attached image). So I tried writing the same function into the backtest but the entrys are still the same (way to late after the green trend has started and during red periods) as shown by the blue arrows. I´ve tried a bunch of different entry criterias now and none of them seem to enter where they are supposed to, i.e where the white arrows are. Here is the most recent code I´ve tried:
Moving Average Trend12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485//DEFPARAM PRELOADBARS = 10000DEFPARAM CUMULATEORDERS = FALSE// --- settingsprd = 20 //Period to Check Trendrateinp = 1 //Trend Channel Rate % minval = 0.1, step = 0.1ulinreg = 0 // 1=true, Use Linear Regressionlinprd = 10 //Linear Regression Period, minval = 2matype = 1 //ma type 0=sma, 1-ema, etc.// --- end of settings//matype = ExponentialAverage[p](close)ulinreg = 1rate = rateinp / 100pricerange = highest[100](close) - lowest[100](close)chan = pricerange * ratep = 5count=0green=0red=0while p<=100 do//trendmasrc = average[p,matype](close)if ulinreg thenma = LinearRegression[linprd](masrc)elsema = masrcendifhh = highest[prd](ma)ll = lowest[prd](ma)diff = abs(hh-ll)if diff>chan thenif ma>ll+chan thentrend=1elsif ma<hh-chan thentrend=-1elsetrend=0endifelsetrend=0endifif chan>0 thenret = trend*diff/chanendifalpha = min(255,(80+abs(ret*10)))if ret<0 thenr=255g=0red=red+1elser=0g=255green=green+1endifp=p+5count=count+1wendif green crosses over red thenbuy 10 contracts at marketendifif red crosses over green thensell at marketendif//graph ret//graph count//graphonprice hh//graphonprice ll//graphonprice ma//graphonprice ll+chan06/18/2020 at 11:35 AM #13639406/18/2020 at 4:47 PM #136484Yes I can see that the “graph green” and “graph red” correlates with where my system enters and exits a trade. But those graph instructions doesn´t correlate with the actual colours of the moving average at all so it still doesn´t help me any further. Seems to me like the “graph green” and “graph red” instructions display a green trend when the moving average is obviously red and vice versa?
06/19/2020 at 7:37 AM #136533 -
AuthorPosts
Find exclusive trading pro-tools on