Hi PRC Coders,
I have been trying to figure out what the problem in my code is regarding the trading strategies on a 5 minute timeframe and have tried different nuances to solve the error (division by zero parameter). I have both tried it on the demo as well as my live account but it keep on stopping with the same error message. It works fine on the back tests but not demo or live! Can someone perhaps show me the error of my ways in this regard?
1.) The first strategy is a MA cross with a RSI condition, being met not more than 5 bars after the crossover.
2.) The second strategy is also a MA cross over without the bar count but only the RSI condition (Trying to eliminate some code to try and figure out why I get the error).
3.) The third strategy is altogether different with various variables but with the same cross over principal (completely different cross over but same error message)
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Position Management
Possize = 1
Ctime = time >= 10000 and time < 220000
// Define the lengths of the moving averages and RSI indicator
maLength1 = 10
maLength2 = 80
rsiLength = 1
// Calculate the moving averages and RSI value
ma1 = Average[maLength1](close)
ma2 = Average[maLength2](close)
nrsi = rsi[rsiLength](close)
// Identify the cross over using the moving averages
crossover = 0
if ma1 > ma2 and ma1[1] < ma2[1] Then
crossover = barindex // moving average crossover occurred = store the barindex
Endif
// Use the cross over signal and RSI value to determine the entry price
if Ctime and ma1 > ma2 and barindex-crossover <= 5 and crossover>0 Then //crossover occured less than 5 bars ago
if nrsi < 30 Then
Buy Possize contract at market
crossover = 0 //reset and prevent next order with same crossover
Endif
Endif
// Stops and targets
SET STOP pLOSS 180
SET TARGET pPROFIT 250
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Position Management
Possize = 1
// Time management
Ctime = time >= 10000 and time < 220000
// Define the lengths of the moving averages and RSI indicator
maLength1 = 10
maLength2 = 80
rsiLength = 1
// Calculate the moving averages and RSI value
ma1 = Average[maLength1](close)
ma2 = Average[maLength2](close)
nrsi = rsi[rsiLength](close)
// Condition to buy
if Ctime and ma1 > ma2 and nrsi < 30 Then
Buy Possize contract at market
Endif
// Stops and targets
SET STOP pLOSS 180
SET TARGET pPROFIT 250
If a < 30 Then // trendreversalup
Weight = 1
Elsif a > 30 Then // trendreversaldown
Weight = 0
Endif
If b < 20 Then // trendreversalup
Weight = 1
Elsif b > 20 Then // trendreversaldown
Weight = 0
Endif
If c > d Then // directionup
Weight = 1
Elsif c < d Then // directiondown
Weight = 0
Endif
If e > f Then // directionup
Weight = 1
Elsif e < f Then // directiondown
Weight = 0
Endif
If g < -80 Then // trendreversalup
Weight = 1
Elsif g > -20 Then
Weight = 0 // trendreversaldown
Endif
If i < -100 Then // trendreversalup
Weight = 1
Elsif i > 100 Then
Weight = 0 // trendreversaldown
Endif
If j = 1 Then // trendreversalup
Weight = 1
Elsif j = -1 Then
Weight = 0 // trendreversaldown
Endif
If k = 1 Then // trendreversalup
Weight = 1
Elsif k = -1 Then // trendreversaldown
Weight = 0
Endif
If l[6] < l[5] and l[5] < l[4] and l[4] < l[3] and l[3] < l[2] and l[2] < l[1] and l[1] < l Then // directionup
Weight = 1
Elsif l[6] > l[5] and l[5] > l[4] and l[4] > l[3] and l[3] > l[2] and l[2] > l[1] and l[1] > l Then // directiondown
Weight = 0
Endif
If m[6] < m[5] and m[5] < m[4] and l[4] < m[3] and m[3] < m[2] and m[2] < m[1] and m[1] < m Then // directionup
Weight = 1
Elsif m[6] > m[5] and m[5] > m[4] and m[4] > m[3] and m[3] > m[2] and m[2] > m[1] and m[1] > m Then // directiondown
Weight = 0
Endif
TrendDirV2 = Summation[n](Weight)
AvgTrendDirV2 = Average[AvgTrendLenght](TrendDirV2)
nrsi = rsi[rsiLength](close)
//graph TrendDirV2
if Ctime and TrendDirV2 crosses over AvgTrendDirV2 and nrsi < 30 Then
Buy Possize contract at market
Endif
// Stops and targets
SET STOP pLOSS 180
SET TARGET pPROFIT 250
Apologies I see I missed a part of the 3rd strategy, here it is:
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
Possize = 1
Ctime = time >= 10000 and time < 220000
// Define the lengths of the moving averages and RSI indicator
rsiLength = 8
TrendLenght = 2
AvgTrendLenght = 2
n = 16
a = RSI[n](close)//OBOS
b = Stochastic[9,6](close)//OBOS
c = MACDline[20,30,n](close)//DIR
d = MACDSignal[20,30,n](close)//DIR
e = DIplus[n](close)//DIR
f = DIminus[n](close)//DIR
g = Williams[n](close)//OBOS
i = CCI[n](close)//OBOS
j = DivergenceCCI[n,-100,100,n]//OBOS
k = DivergenceRSI[n,30,70,n](close)//OBOV
l = Average[7](CCI[n](close))//DIR
m = Average[14](close)//DIR
Weight = 0
If a < 30 Then // trendreversalup
Weight = 1
Elsif a > 30 Then // trendreversaldown
Weight = 0
Endif
If b < 20 Then // trendreversalup
Weight = 1
Elsif b > 20 Then // trendreversaldown
Weight = 0
Endif
If c > d Then // directionup
Weight = 1
Elsif c < d Then // directiondown
Weight = 0
Endif
If e > f Then // directionup
Weight = 1
Elsif e < f Then // directiondown
Weight = 0
Endif
If g < -80 Then // trendreversalup
Weight = 1
Elsif g > -20 Then
Weight = 0 // trendreversaldown
Endif
If i < -100 Then // trendreversalup
Weight = 1
Elsif i > 100 Then
Weight = 0 // trendreversaldown
Endif
If j = 1 Then // trendreversalup
Weight = 1
Elsif j = -1 Then
Weight = 0 // trendreversaldown
Endif
If k = 1 Then // trendreversalup
Weight = 1
Elsif k = -1 Then // trendreversaldown
Weight = 0
Endif
If l[6] < l[5] and l[5] < l[4] and l[4] < l[3] and l[3] < l[2] and l[2] < l[1] and l[1] < l Then // directionup
Weight = 1
Elsif l[6] > l[5] and l[5] > l[4] and l[4] > l[3] and l[3] > l[2] and l[2] > l[1] and l[1] > l Then // directiondown
Weight = 0
Endif
If m[6] < m[5] and m[5] < m[4] and l[4] < m[3] and m[3] < m[2] and m[2] < m[1] and m[1] < m Then // directionup
Weight = 1
Elsif m[6] > m[5] and m[5] > m[4] and m[4] > m[3] and m[3] > m[2] and m[2] > m[1] and m[1] > m Then // directiondown
Weight = 0
Endif
TrendDirV2 = Summation[n](Weight)
AvgTrendDirV2 = Average[AvgTrendLenght](TrendDirV2)
nrsi = rsi[rsiLength](close)
//graph TrendDirV2
if Ctime and TrendDirV2 crosses over AvgTrendDirV2 and nrsi < 30 Then
Buy Possize contract at market
Endif
// Stops and targets
SET STOP pLOSS 180
SET TARGET pPROFIT 250
I have run them in autotrading on Dax, 5-minute TF. I’ll report any returned message and findings, though it doesn’t seem they have any apparent issues.
Thanks Roberto, I was also surprised it is straight forward…with no divisions in the code?
Hi Roberto,
Also notice that one of the strategies actually opened a trade and when the division by zero error appeared it closed the position prematurely (please see image attachments). Thank you.
Johann,
It is a bit tricky to check, but you mix Length with Lenght. And if you do this one time inconsistently within one code, there you go;
Depending how you apply the “errors”, PRT may not detect you are using an undefined variable and instead uses it with a value of 0. It is your typo of course, but it is PRT failing.
Peter
Thanks PeterSt, well spotted. I was going around in circles and could not figure it out will simplify the code and replace with actual numbers that should also help! The only weird thing about this is how the code with the error was accepted on the back testing …?
If I guessed correctly that your code fails somewhere on an undefined variable (and this is a long shot despite the “typos”) then it just depends in what sequence the code is executed, which depends on the (bar-) data. So this is not entirely odd. And even if it is not about the variables, it still can depend on the data which is always new in Live or Demo, against Backtest which is old. IOW, it always is different.
Btw, one other thing I noticed was the RSI(1). This would not make sense (I think ?) and maybe something can choke on it. This too could be data depended (referring to your code not dying from of bar-1).
Edit: But that RSI(1) was in one of the Strategies only, so I never really thought of that being the culprit.
Perhaps, I will replace the defined variables with recognized terminology or code and the defined numbers with actual numbers and see what happens…? It remains a bit of a head scratcher the code is so simple?
I have simplified strategy number 1, will run it and see what happens: (the insert code button was not available fir this post so I just pasted it in below)
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
Possize = 1
// Time management
Ctime = time >= 10000 and time < 220000
// Identify the cross over piont using the moving averages
crossover = 0
if Average[10](close) > Average[80](close) and Average[10](close)[1] < Average[80](close)[1] Then
crossover = barindex // moving average crossover occurred = store the barindex
Endif
// Use the cross over signal and RSI value to determine the entry price
if Ctime and barindex-crossover <= 5 and crossover > 0 Then //crossover occured less than 5 bars ago
if rsi[1](close) < 30 Then
Buy Possize contract at market
crossover = 0 //reset and prevent next order with same crossover
LE = 0
Endif
Endif
// Stops and targets
SET STOP pLOSS 180
SET TARGET pPROFIT 250
Hi Roberto, strategy 2 just got rejected on Wall Street Cash :
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Position Management
Possize = 1
// Time management
Ctime = time >= 10000 and time < 220000
// Define the lengths of the moving averages and RSI indicator
maLength1 = 10
maLength2 = 80
rsiLength = 1
// Calculate the moving averages and RSI value
ma1 = Average[maLength1](close)
ma2 = Average[maLength2](close)
nrsi = rsi[rsiLength](close)
// Condition to buy
if Ctime and ma1 > ma2 and nrsi < 30 Then
Buy Possize contract at market
Endif
// Stops and targets
SET STOP pLOSS 180
SET TARGET pPROFIT 250
Strategy #1 and #2 both were stopped.
I can’t understand why as I can’t spot any undefined variables, nor can I spot wrong period numbers!
I will run them again letting the entry code be executed only after maLength2 bars, but even this should not be an issue, owing to a default 2K preloaded bars.
This modified Strategy #2 entered a Long trade, Dax 5€ 5min-TF, with no errors reported:
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Position Management
Possize = 1
// Time management
Ctime = time >= 010000 and time < 220000 //010000 instead of 10000 ???
// Define the lengths of the moving averages and RSI indicator
maLength1 = 10
maLength2 = 80
rsiLength = 1
// Calculate the moving averages and RSI value
ma1 = Average[maLength1](close)
ma2 = Average[maLength2](close)
nrsi = rsi[rsiLength](close)
if BarIndex > maLength2 then //wait for enough bars to be elapsed ???
// Condition to buy
if Ctime and ma1 > ma2 and nrsi < 30 Then
Buy Possize contract at market
Endif
endif
// Stops and targets
SET STOP pLOSS 180
SET TARGET pPROFIT 250
Thanks Roberto for trying it is a mystery….!
Thanks I will test modified strategy # 2 on Wall Street and see what happens…