ProRealCode - Trading & Coding with ProRealTime™
Ok.. I’ve had a similar issue on another instrument but had some values as low as 3 in the smoothK/smoothD variables. I increased them above 5 and optimised again, haven’t had an issue again yet but it does not fire as often as say the DJI algo. I’d be curious if you increase those to a min of 5 if you continue getting the same error.. I was suspecting that area to be my lines of code failing.. as obviously this can only occur in the code where a division is carried out where the denominator is getting quite small in comparison to the numerator… unless I have missed something elsewhere.
Brent but realized I had to scrap it because of constant zero division error.
Didn’t experience this before. Thanks for sharing.
@christofferrydberg, in any case, I suspect more on this few lines,
MinRSIa = lowest[lengthStocha](myrsia)
MaxRSIa = highest[lengthStocha](myrsia)
StochRSIa = (myRSIa-MinRSIa) / (MaxRSIa-MinRSIa)
I noticed you set the lengthStocha to be 3. In case the market very low volatility, and RSI doesn’t move for 3 bars, then you might result a zero for MaxRSIa-MinRSIa
Maybe you can protect it by something like this,
myRSIa = RSI[lengthRSIa](close)
MinRSIa = lowest[lengthStocha](myrsia)
MaxRSIa = highest[lengthStocha](myrsia)
IF MaxRSIa = MinRSIa THEN
c23 = 0
c24 = 0
ELSE
StochRSIa = (myRSIa-MinRSIa) / (MaxRSIa-MinRSIa)
Ka = average[smoothKa](stochrsia)*100
Da = average[smoothDa](Ka)
c23 = Ka>Da
c24 = Ka<Da
ENDIF
If you really interested to confirm this issue, then you can check the time stamp when the issue is reported to you. Put the indicator on chart, then pay attention to the time stamp so it narrow down your debugging range.
If not above issue, you can still do the same for other division function, by doing a graph of the division.
My version of the system (little changed from the V4a I copied from here) took a long on 1/10/2020 at 12:00:01 and was stopped out for a big loss on 2/10/2020 at 05:55:58. Today I ran the backtest to look at something else and discovered that the backtest does not take this trade and I cannot see why. Even if I copy the code from the live system into a new system and run it it doesn’t take the trade no matter how much I change the spread value (can’t think what else could account for it). Anyone else had this happen?
Hi Autostrategist, yes, unfortunately I experienced the same (currently running version DJ 5m 4.7.2a).
Live entry on 1/10/20 12:00:00 and stop loss hit 2/10/20 05:55:56 (UK times). Doesn’t show up on my backtest.
I’ve been running a version of this since 29 March 2020 and usually real results are very similar to backtest (although 2-3 June and 20 and 24 Aug I found were less good replication). So not just you, but not sure why it happened! Very grateful though for the sharing of this code, as still in profit over that period, despite this 🙂
I added a rounding up for the order sizes, to full numbers, just in case someone has problems orders smaller than 1.
///Definition of code parameters
DEFPARAM CumulateOrders = false// Cumulating positions deactivated
DEFPARAM preloadbars = 5000
//Money Management DOW
MM = 1 // = 0 for optimization
if MM = 0 then
positionsize=1
ENDIF
if MM = 1 then
ONCE startpositionsize = 1
ONCE factor = 5 // factor of 10 means margin will increase/decrease @ 10% of strategy profit; factor 20 = 5% etc
ONCE margin = (close*.005) // tier 1 margin value of 1 contract in instrument currency; change decimal according to available leverage
ONCE margin2 = (close*.01)// tier 2 margin value of 1 contract in instrument currency; change decimal according to available leverage
ONCE tier1 = 55 // DOW €1 IG first tier margin limit
ONCE maxpositionsize = 550 // DOW €1 IG tier 2 margin limit
ONCE minpositionsize = 1 // enter minimum position allowed
IF Not OnMarket THEN
positionsize = round (startpositionsize + Strategyprofit/(factor*margin))//rounding up
ENDIF
IF Not OnMarket THEN
IF startpositionsize + Strategyprofit/(factor*margin) > tier1 then
positionsize = round((((startpositionsize + (Strategyprofit/(factor*margin))-tier1)*(factor*margin))/(factor*margin2)) + tier1) //incorporating tier 2 margin
ENDIF
IF Not OnMarket THEN
if startpositionsize + Strategyprofit/(factor*margin) < minpositionsize THEN
positionsize = round (minpositionsize) //keeps positionsize from going below allowed minimum
ENDIF
IF (((startpositionsize + (Strategyprofit/(factor*margin))-tier1)*(factor*margin))/(factor*margin2)) + tier1 > maxpositionsize then
positionsize = round (maxpositionsize)// keeps positionsize from going above IG tier 2 margin limit
ENDIF
ENDIF
ENDIF
ENDIF
TIMEFRAME(2 hours,updateonclose)
Period= 495
inner = 2*weightedaverage[round( Period/2)](typicalprice)-weightedaverage[Period](typicalprice)
HULLa = weightedaverage[round(sqrt(Period))](inner)
c1 = HULLa > HULLa[1]
c2 = HULLa < HULLa[1]
indicator1 = SuperTrend[8,6]
c3 = (close > indicator1)
c4 = (close < indicator1)
ma = average[60,3](close)
c11 = ma > ma[1]
c12 = ma < ma[1]
//Stochastic RSI | indicator
lengthRSI = 15 //RSI period
lengthStoch = 9 //Stochastic period
smoothK = 10 //Smooth signal of stochastic RSI
smoothD = 5 //Smooth signal of smoothed stochastic RSI
myRSI = RSI[lengthRSI](close)
MinRSI = lowest[lengthStoch](myrsi)
MaxRSI = highest[lengthStoch](myrsi)
StochRSI = (myRSI-MinRSI) / (MaxRSI-MinRSI)
K = average[smoothK](stochrsi)*100
D = average[smoothD](K)
c13 = K>D
c14 = K<D
TIMEFRAME(30 minutes,updateonclose)
indicator5 = Average[2](typicalPrice)
indicator6 = Average[7](typicalPrice)
c15 = (indicator5 > indicator6)
c16 = (indicator5 < indicator6)
TIMEFRAME(15 minutes,updateonclose)
indicator2 = Average[4](typicalPrice)
indicator3 = Average[8](typicalPrice)
c7 = (indicator2 > indicator3)
c8 = (indicator2 < indicator3)
Periodc= 23
innerc = 2*weightedaverage[round( Periodc/2)](typicalprice)-weightedaverage[Periodc](typicalprice)
HULLc = weightedaverage[round(sqrt(Periodc))](innerc)
c9 = HULLc > HULLc[1]
c10 = HULLc < HULLc[1]
TIMEFRAME(10 minutes)
indicator1a = SuperTrend[2,7]
c19 = (close > indicator1a)
c20 = (close < indicator1a)
TIMEFRAME(5 minutes)
//Stochastic RSI | indicator
lengthRSIa = 3 //RSI period
lengthStocha = 6 //Stochastic period
smoothKa = 9 //Smooth signal of stochastic RSI
smoothDa = 3 //Smooth signal of smoothed stochastic RSI
myRSIa = RSI[lengthRSIa](close)
MinRSIa = lowest[lengthStocha](myrsia)
MaxRSIa = highest[lengthStocha](myrsia)
StochRSIa = (myRSIa-MinRSIa) / (MaxRSIa-MinRSIa)
Ka = average[smoothKa](stochrsia)*100
Da = average[smoothDa](Ka)
c23 = Ka>Da
c24 = Ka<Da
ma3 = average[15,3](close)
c21 = ma3 > ma3[1]
c22 = ma3 < ma3[1]
Periodb= 15
innerb = 2*weightedaverage[round( Periodb/2)](typicalprice)-weightedaverage[Periodb](typicalprice)
HULLb = weightedaverage[round(sqrt(Periodb))](innerb)
c5 = HULLb > HULLb[1]and HULLb[1]<HULLb[2]
c6 = HULLb < HULLb[1]and HULLb[1]>HULLb[2]
// Conditions to enter long positions
IF dhigh(0)-high<250 and c1 AND C3 AND C5 and c7 and c9 and c11 and c13 and c15 and c19 and c21 and c23 THEN
BUY positionsize CONTRACT AT MARKET
SET STOP %LOSS 1.5
SET TARGET %PROFIT 2.4
ENDIF
// Conditions to enter short positions
IF low-dlow(0)<700 and c2 AND C4 AND C6 and c8 and c10 and c12 and c14 and c16 and c20 and c22 and c24 THEN
SELLSHORT positionsize CONTRACT AT MARKET
SET STOP %LOSS 1.5
SET TARGET %PROFIT 2.2
ENDIF
//================== exit in profit
if longonmarket and C6 and c8 and close>positionprice then
sell at market
endif
If shortonmarket and C5 and c7 and close<positionprice then
exitshort at market
endif
//==============exit at loss
if longonmarket AND c2 and c6 and close<positionprice then
sell at market
endif
If shortonmarket and c1 and c5 and close>positionprice then
exitshort at market
endif
//%trailing stop function
trailingPercent = .26
stepPercent = .014
if onmarket then
trailingstart = tradeprice(1)*(trailingpercent/100) //trailing will start @trailingstart points profit
trailingstep = tradeprice(1)*(stepPercent/100) //% step to move the stoploss
endif
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart THEN
newSL = tradeprice(1)+trailingstep
ENDIF
//next moves
IF newSL>0 AND close-newSL>trailingstep THEN
newSL = newSL+trailingstep
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart THEN
newSL = tradeprice(1)-trailingstep
ENDIF
//next moves
IF newSL>0 AND newSL-close>trailingstep THEN
newSL = newSL-trailingstep
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//************************************************************************
IF longonmarket and barindex-tradeindex>1800 and close<positionprice then
sell at market
endif
IF shortonmarket and barindex-tradeindex>610 and close>positionprice then
exitshort at market
endif
//=============================================
if longonmarket and abs(open-close)<1 and high[1]>high and close>positionprice and high-close>10then
sell at market
endif
if shortonmarket and abs(open-close)<1 and low[1]>low and close-low>13 and close<positionprice then
exitshort at market
endif
//===================================
myrsiM5=rsi[14](close)
//
if myrsiM5<30 and barindex-tradeindex>1 and longonmarket and close>positionprice then
sell at market
endif
if myrsiM5>70 and barindex-tradeindex>1 and shortonmarket and close<positionprice then
exitshort at market
endif
// --------- US DAY LIGHT SAVINGS MONTHS ---------------- //
mar = month = 3 // MONTH START
nov = month = 11 // MONTH END
IF (month > 3 AND month < 11) OR (mar AND day>14) OR (mar AND day-dayofweek>7) OR (nov AND day<=dayofweek AND day<7) THEN
USDLS=010000
ELSE
USDLS=0
ENDIF
once openStrongLong = 0
once openStrongShort = 0
if (time <= 223000 - USDLS and time >= 050000 - USDLS) then
openStrongLong = 0
openStrongShort = 0
endif
//detect strong direction for market open
once rangeOK = 40
once tradeMin = 1500
IF (time >= 223500 - USDLS) AND (time <= 223500 + tradeMin - USDLS) AND ABS(close - open) > rangeOK THEN
IF close > open and close > open[1] THEN
openStrongLong = 1
openStrongShort = 0
ENDIF
IF close < open and close < open[1] THEN
openStrongLong = 0
openStrongShort = 1
ENDIF
ENDIF
once bollperiod = 20
once bollMAType = 1
once s = 2
bollMA = average[bollperiod, bollMAType](close)
STDDEV = STD[bollperiod]
bollUP = bollMA + s * STDDEV
bollDOWN = bollMA - s * STDDEV
IF bollUP = bollDOWN THEN
bollPercent = 50
ELSE
bollPercent = 100 * (close - bollDOWN) / (bollUP - bollDOWN)
ENDIF
once trendPeriod = 70
once trendPeriodResume = 30
once trendGap = 3
once trendResumeGap = 6
if not onmarket then
fullySupported = 0
fullyResisteded = 0
endif
//Market supported in the wrong direction
IF shortonmarket AND fullySupported = 0 AND summation[trendPeriod](bollPercent > 50) >= trendPeriod - trendGap THEN
fullySupported = 1
ENDIF
//Market pull back but continue to be supported
IF shortonmarket AND fullySupported = 1 AND bollPercent[trendPeriodResume + 1] < 0 AND summation[trendPeriodResume](bollPercent > 50) >= trendPeriodResume - trendResumeGap THEN
exitshort at market
ENDIF
//Market resisted in wrong direction
IF longonmarket AND fullyResisteded = 0 AND summation[trendPeriod](bollPercent < 50) >= trendPeriod - trendGap THEN
fullyResisteded = 1
ENDIF
//Market pull back but continue to be resisted
IF longonmarket AND fullyResisteded = 1 AND bollPercent[trendPeriodResume + 1] > 100 AND summation[trendPeriodResume](bollPercent < 50) >= trendPeriodResume - trendResumeGap THEN
sell at market
ENDIF
//
//Started real wrong direction
once strongTrend = 60
once strongPeriod = 8
once strongTrendGap = 2
IF shortonmarket and openStrongLong and barindex - tradeindex < 12 and summation[strongPeriod](bollPercent > strongTrend) = strongPeriod - strongTrendGap then
exitshort at market
ENDIF
IF longonmarket and openStrongShort and barindex - tradeindex < 12 and summation[strongPeriod](bollPercent < 100 - strongTrend) = strongPeriod - strongTrendGap then
sell at market
ENDIF
I have tried to run Mother of dragons v4.5 live for a while. The only Changes I have made is my own optimization of the values. Time after time the algo is stopped because “division by zero”.
Is there a general way to eliminate that or how do a newbie in PRT coding proceed to fix this?
hi, which version do you suggest to me to trade in real ? I’m interested on NQ, DJ and DAX.
Thanks
@OboeOpt Might be stating the obvious but a division by zero error occurs when the variable to the right of the division operator i.e. ‘/’ is zero. Take this line from MOD code as an example (myRSIa–MinRSIa) / (MaxRSIa–MinRSIa), if ever the values to the right of the ‘/’ were to equal zero (not saying they can) then the error would occur. I have been running MOD and a DAX version for months without seeing this error. I have seen the error in the past with other systems and it is usually caused by a situation in the market that had not been accounted for like when the DJI was paused on limit up or down or a market is unusually quiet and fails to make one or more bars for a while etc. The only way to track it down is to work through all the possible divisions and see which one is causing the error.
Normally I can figure out why a trade was taken even though I wasn’t expecting it e.g. a difference in the spread from live Versus backtest or insufficient backtest data for the moving average but in this case I couldn’t work it out so I raised the issue with PRT, see below:
To my amazement I got this response:
We have identified the problem you reported about your backtest and our technical team is currently working on a correction, which will be implemented as soon as possible. Please note that depending on the complexity of the correction needed, it may take up to several weeks for the problem to be corrected.
That errant trade cost me several hundred pounds but I don’t expect they will be compensating me 🙁 .
Mother of Dragons made in one week its 2 worst losses since Feb 2018. In both cases, it first closed one long position (in profit) and took again a long position a few minutes later.
Then this position would stay opened several hours / days until it hits stoploss. I was wondering whether it would be great to prevent the robot to open a new position if another one has been closed a few minutes ago.
What are your thoughts about that ?
You can use something like the below, I got this from one of the PRC gurus. It will force the strategy to wait N bars. However, the reality is that the returns are such that you sometimes just take the rough with the smooth, or you risk missing out on the next big move.
//Wait
ONCE Count = 0
ONCE MinCount = 5
IF (Not OnMarket AND OnMarket[1]) OR (StrategyProfit <> StrategyProfit[1]) THEN //check thare was a trade open the previous bar and not any longer....
Count = 1 //... to start counting periods (bars)
ELSE
Count = Count + 1 //increment Count at each new period (bar)
ENDIF
IF OnMarket THEN
Count = 0
ENDIF
You can use something like the below, I got this from one of the PRC gurus. It will force the strategy to wait N bars. However, the reality is that the returns are such that you sometimes just take the rough with the smooth, or you risk missing out on the next big move.
1234567891011 //WaitONCE Count = 0ONCE MinCount = 5IF (Not OnMarket AND OnMarket[1]) OR (StrategyProfit <> StrategyProfit[1]) THEN //check thare was a trade open the previous bar and not any longer….Count = 1 //… to start counting periods (bars)ELSECount = Count + 1 //increment Count at each new period (bar)ENDIFIF OnMarket THENCount = 0ENDIF
Thank you for your answer. I will look into it.
But I agree, with this code implemented, it could miss the next big move…
It’s worth noting, with elections in the US soon it’s worth de-risking for the next few weeks.
Hola he puesto lo que le dices a la Madre de Dragones y da un error en mincount. Can you help me please? I’m trying to learn, thank you very much
Mother of Dragons trading strategy…
This topic contains 522 replies,
has 50 voices, and was last updated by LaurentBZH35
4 years, 10 months ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 01/21/2020 |
| Status: | Active |
| Attachments: | 195 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.