ProRealCode - Trading & Coding with ProRealTime™
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.
///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
//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.Thank you for your answer. I will look into it. But I agree, with this code implemented, it could miss 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 ENDIF1234567891011//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
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
Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.
Mother of Dragons trading strategy…
This topic contains 522 replies,
has 50 voices, and was last updated by LaurentBZH35
5 years, 5 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.