NIK 1m Hull-SAR
Hello everyone
Thank you very much for this good basic strategy. I ask for your indulgence and understanding, because I am still a beginner, do not speak English and work with a translator!
I do a test and optimization with Nikkei, with good results (see appendix). It doesn’t run live for very long and keeps getting error messages (see appendix). During the American trading hours it runs for 4 to 5 hours and a profit trade has already been achieved, but I need your help. How to get the algorithm stable, without error message and shutdown. On Nasdaq, things are going better but not perfect either, with 2 to 3 errors a week (always live).
– Error 1: Division by zero – what protections are there? That’s why I use MTF 5min over 7min to 22min total runs on 1min.
– Error 2: Historical data is not sufficient?
Ask for your help. I think the algorithm has potential. What is your opinion on this?
One more general question at the end. Can algorithms, after hundreds of hours of invested work, win in the long run? When will the marketplace come? ( I have at the moment 10 Algo live, of which 5 for rent )
Once again, pay tribute to your work!
Here are the .itf and pictures.
Thank you for your comment and help.
// Definition of code parameters
DEFPARAM CumulateOrders = true // Cumulating positions deactivated
DEFPARAM preloadbars = 10000
//Money Management NAS
MM = 0 // = 0 for optimization
if MM = 0 then
positionsize=0.5
ENDIF
if MM = 1 then
ONCE startpositionsize = 0.5
ONCE factor = 6 // 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 = 200 // IG first tier margin limit
ONCE maxpositionsize = 2000 // IG tier 2 margin limit
ONCE minpositionsize = .5 // enter minimum position allowed
IF StrategyProfit <> StrategyProfit[1] THEN
positionsize = startpositionsize + Strategyprofit/(factor*margin)
ENDIF
IF StrategyProfit <> StrategyProfit[1] THEN
IF startpositionsize + Strategyprofit/(factor*margin) > tier1 then
positionsize = (((startpositionsize + (Strategyprofit/(factor*margin))-tier1)*(factor*margin))/(factor*margin2)) + tier1 //incorporating tier 2 margin
ENDIF
IF StrategyProfit <> StrategyProfit[1] THEN
if startpositionsize + Strategyprofit/(factor*margin) < minpositionsize THEN
positionsize = minpositionsize //keeps positionsize from going below allowed minimum
ENDIF
IF (((startpositionsize + (Strategyprofit/(factor*margin))-tier1)*(factor*margin))/(factor*margin2)) + tier1 > maxpositionsize then
positionsize = maxpositionsize// keeps positionsize from going above IG tier 2 margin limit
ENDIF
ENDIF
ENDIF
ENDIF
Ctime = time >=103000 and time <211400
TIMEFRAME(22 minutes)
Period= 80
inner = 2*weightedaverage[round( Period/2)](typicalprice)-weightedaverage[Period](typicalprice)
HULLa = weightedaverage[round(sqrt(Period))](inner)
c1 = HULLa > HULLa[1] or HULLb > HULLa
c2 = HULLa < HULLa[1] or HULLb < HULLa
ST1 = SAR[0.02,0.015,0.025]
c1a = (close > ST1)
c2a = (close < ST1)
TIMEFRAME(7 minutes)
Periodb= 17
innerb = 2*weightedaverage[round( Periodb/2)](typicalprice)-weightedaverage[Periodb](typicalprice)
HULLb = weightedaverage[round(sqrt(Periodb))](innerb)
c3 = HULLb > HULLb[1]
c4 = HULLb < HULLb[1]
c3b = HULLb > HULLb[1] and HULLb[1] < HULLb[2]
c4b = HULLb < HULLb[1] and HULLb[1] > HULLb[2]
ST2 = SAR[0.015,0.015,0.02]
c3a = (close > ST2)
c4a = (close < ST2)
//Stochastic RSI | indicator
lengthRSI = 3 //RSI period
lengthStoch = 3 //Stochastic period
smoothK = 6 //Smooth signal of stochastic RSI
smoothD = 4 //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)
c3c = K>D
c4c = K<D
TIMEFRAME(5 minutes)
Periodc= 4
innerc = 2*weightedaverage[round( Periodc/2)](typicalprice)-weightedaverage[Periodc](typicalprice)
HULLc = weightedaverage[round(sqrt(Periodc))](innerc)
c5 = HULLc > HULLc[1] and HULLc[1] < HULLc[2]
c6 = HULLc < HULLc[1] and HULLc[1] > HULLc[2]
c5b = HULLc > HULLc[1]
c6b = HULLc < HULLc[1]
ST3 = SAR[0.005,0.015,0.015]
c5a = (close > ST3)
c6a = (close < ST3)
TIMEFRAME(default)
Once MaxPositionsAllowed = 5*positionsize
// Conditions to enter long positions
IF not longonmarket and Ctime and c1 and c1a AND C3a and c3b and c3c AND C5a and c5b THEN
BUY positionsize CONTRACT AT MARKET
elsif longonmarket and Ctime and c1 and c1a and c3 and c3a and c5 and COUNTOFLONGSHARES < MaxPositionsAllowed then
BUY positionsize CONTRACT AT MARKET
SET STOP %LOSS 1.6
SET TARGET %PROFIT 1.4
ENDIF
// Conditions to enter short positions
IF not shortonmarket and Ctime and c2 and c2a AND C4a and c4b and c4c AND C6a and c6b THEN
sellshort positionsize CONTRACT AT MARKET
elsif shortonmarket and Ctime and c2 and c2a and c4 and c4a and c6 and COUNTOFSHORTSHARES < MaxPositionsAllowed then
sellshort positionsize CONTRACT AT MARKET
SET STOP %LOSS 1.2
SET TARGET %PROFIT 1.7
ENDIF
// %trailing stop function incl. cumulative positions
once trailingstoptype = 1
if trailingstoptype then
//====================
once trailingpercentlong = 0.47 // %
once trailingpercentshort = 0.56 // %
once accelerator = 0.095 // 1 = default; always > 0 (i.e. 0.5-3)
once accelerator2 = 0.31 // 1 = default; always > 0 (i.e. 0.5-3)
once ts2sensitivity = 0 // [0]close;[1]high/low;[2]low;high
//====================
once steppercentlong = (trailingpercentlong/10)*accelerator
once steppercentshort = (trailingpercentshort/10)*accelerator2
if onmarket then
trailingstartlong = positionprice[1]*(trailingpercentlong/100)
trailingstartshort = positionprice[1]*(trailingpercentshort/100)
trailingsteplong = positionprice[1]*(steppercentlong/100)
trailingstepshort = positionprice[1]*(steppercentshort/100)
endif
if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
newsl = 0
mypositionprice = 0
endif
positioncount = abs(countofposition)
if newsl > 0 then
if positioncount > positioncount[1] then
if longonmarket then
newsl = max(newsl,positionprice * newsl / mypositionprice)
else
newsl = min(newsl,positionprice * newsl / mypositionprice)
endif
endif
endif
if ts2sensitivity=1 then
ts2sensitivitylong=high
ts2sensitivityshort=low
elsif ts2sensitivity=2 then
ts2sensitivitylong=low
ts2sensitivityshort=high
else
ts2sensitivitylong=close
ts2sensitivityshort=close
endif
if longonmarket then
if newsl=0 and ts2sensitivitylong-positionprice>=trailingstartlong*pipsize then
newsl = positionprice+trailingsteplong*pipsize
endif
if newsl>0 and ts2sensitivitylong-newsl>=trailingsteplong*pipsize then
newsl = newsl+trailingsteplong*pipsize
endif
endif
if shortonmarket then
if newsl=0 and positionprice-ts2sensitivityshort>=trailingstartshort*pipsize then
newsl = positionprice-trailingstepshort*pipsize
endif
if newsl>0 and newsl-ts2sensitivityshort>=trailingstepshort*pipsize then
newsl = newsl-trailingstepshort*pipsize
endif
endif
if barindex-tradeindex>1 then
if longonmarket then
if newsl>0 then
sell at newsl stop
endif
if newsl>0 then
if low crosses under newsl then
sell at market
endif
endif
endif
if shortonmarket then
if newsl>0 then
exitshort at newsl stop
endif
if newsl>0 then
if high crosses over newsl then
exitshort at market
endif
endif
endif
endif
mypositionprice = positionprice
endif