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?
// --- 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 settings
DEFPARAM PRELOADBARS = 100
matype = ExponentialAverage[p](close)
ulinreg = 1
rate = rateinp / 100
pricerange = highest[100](close) - lowest[100](close)
chan = pricerange * rate
p = 5
while p<=100 do
//trend
masrc = average[p,matype](close)
if ulinreg then
ma = LinearRegression[linprd](masrc)
else
ma = masrc
endif
hh = highest[prd](ma)
ll = lowest[prd](ma)
diff = abs(hh-ll)
if diff>chan then
if ma>ll+chan then
trend=1
elsif ma<hh-chan then
trend=-1
else
trend=0
endif
else
trend=0
endif
ret = trend*diff/chan
alpha = min(255,(80+abs(ret*10)))
if ret<0 then
r=255
g=0
else
r=0
g=255
endif
$prev[p] = ma
p=p+5
wend
if trend = 1 then
buy 10 contracts at market
endif
if trend = -1 then
sell at market
endif
Is line 55 correct?
prev is not used anywhere else in the strategy and what is the $ doing?
$prev[p] = ma
Hello 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?
Anyway, have you got PRT v11 as Nicolas Trend Following Moving Average only works on V11 onwards!
Yes 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?
There 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)
DEFPARAM PRELOADBARS = 10000
// --- settings
prd = 20 //Period to Check Trend
rateinp = 1 //Trend Channel Rate % minval = 0.1, step = 0.1
ulinreg = 0 // 1=true, Use Linear Regression
linprd = 10 //Linear Regression Period, minval = 2
matype = 1 //ma type 0=sma, 1-ema, etc.
// --- end of settings
matype = ExponentialAverage[p](close)
ulinreg = 1
rate = rateinp / 100
pricerange = highest[100](close) - lowest[100](close)
chan = pricerange * rate
p = 5
count=0
while p<=100 do
//trend
masrc = average[p,matype](close)
if ulinreg then
ma = LinearRegression[linprd](masrc)
else
ma = masrc
endif
hh = highest[prd](ma)
ll = lowest[prd](ma)
diff = abs(hh-ll)
if diff>chan then
if ma>ll+chan then
trend=1
elsif ma<hh-chan then
trend=-1
else
trend=0
endif
else
trend=0
endif
if chan>0 then
ret = trend*diff/chan
endif
alpha = min(255,(80+abs(ret*10)))
if ret<0 then
r=255
g=0
else
r=0
g=255
endif
p=p+5
count=count+1
wend
if trend = 1 then
buy 10 contracts at market
endif
//graph ret
graph count
graphonprice hh
graphonprice ll
graphonprice ma
graphonprice ll+chan
Thank you very much for taking the time to investigate @Nicolas. I hope we can find the solution!
I 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!).
//DEFPARAM PRELOADBARS = 10000
defparam cumulateorders=false
// --- settings
prd = 20 //Period to Check Trend
rateinp = 1 //Trend Channel Rate % minval = 0.1, step = 0.1
ulinreg = 0 // 1=true, Use Linear Regression
linprd = 10 //Linear Regression Period, minval = 2
matype = 1 //ma type 0=sma, 1-ema, etc.
// --- end of settings
//matype = ExponentialAverage[p](close)
ulinreg = 1
rate = rateinp / 100
pricerange = highest[100](close) - lowest[100](close)
chan = pricerange * rate
p = 5
count=0
green=0
red=0
while p<=100 do
//trend
masrc = average[p,matype](close)
if ulinreg then
ma = LinearRegression[linprd](masrc)
else
ma = masrc
endif
hh = highest[prd](ma)
ll = lowest[prd](ma)
diff = abs(hh-ll)
if diff>chan then
if ma>ll+chan then
trend=1
elsif ma<hh-chan then
trend=-1
else
trend=0
endif
else
trend=0
endif
if chan>0 then
ret = trend*diff/chan
endif
alpha = min(255,(80+abs(ret*10)))
if ret<0 then
r=255
g=0
red=red+1
else
r=0
g=255
green=green+1
endif
p=p+5
count=count+1
wend
if green=count then
buy 1 contracts at market
endif
//graph ret
//graph count
//graphonprice hh
//graphonprice ll
//graphonprice ma
//graphonprice ll+chan
Excellent, thank you very much for the help Nicolas!
Sorry 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:
//DEFPARAM PRELOADBARS = 10000
DEFPARAM CUMULATEORDERS = FALSE
// --- settings
prd = 20 //Period to Check Trend
rateinp = 1 //Trend Channel Rate % minval = 0.1, step = 0.1
ulinreg = 0 // 1=true, Use Linear Regression
linprd = 10 //Linear Regression Period, minval = 2
matype = 1 //ma type 0=sma, 1-ema, etc.
// --- end of settings
//matype = ExponentialAverage[p](close)
ulinreg = 1
rate = rateinp / 100
pricerange = highest[100](close) - lowest[100](close)
chan = pricerange * rate
p = 5
count=0
green=0
red=0
while p<=100 do
//trend
masrc = average[p,matype](close)
if ulinreg then
ma = LinearRegression[linprd](masrc)
else
ma = masrc
endif
hh = highest[prd](ma)
ll = lowest[prd](ma)
diff = abs(hh-ll)
if diff>chan then
if ma>ll+chan then
trend=1
elsif ma<hh-chan then
trend=-1
else
trend=0
endif
else
trend=0
endif
if chan>0 then
ret = trend*diff/chan
endif
alpha = min(255,(80+abs(ret*10)))
if ret<0 then
r=255
g=0
red=red+1
else
r=0
g=255
green=green+1
endif
p=p+5
count=count+1
wend
if green crosses over red then
buy 10 contracts at market
endif
if red crosses over green then
sell at market
endif
//graph ret
//graph count
//graphonprice hh
//graphonprice ll
//graphonprice ma
//graphonprice ll+chan
graph green
graph red
should give you valuable informations.
Yes 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?
Settings of the strategy the same as the one of the indicator displayed on the chart? Linear regression use or not, period, ..