Hello,
I have the code below for a H1 EURGBP system that I’m backtesting but as you can see from the code, I only want PRT to trade within certain times. On backtesting the system keeps ignoring these times with multiple trades at 00;00,00 (midnight) which is not what I expect the code to allow. These errant trades seem to be after the system has not traded the day before (such as after the weekend). Could anyone ascertain why it is not abiding by the if time= commands?
defparam cumulateorders=false
// --- settings
balance = 10000 //balance of the strategy when activated the first time
minlot = 1 //minimum lot size for the current instrument (example: 1 for DAX)
riskpercent = 2 //risk percent per trade
activationtime = 080000 //Close time of the candle to activate the strategy
LimitHour = 140000 //Time to close off strategy
//choose days of week to trade
if dayofweek=1 then //Monday
daytrading=1
endif
if dayofweek=2 then // Tuesday
daytrading=1
endif
if dayofweek=3 then // Wednesday
daytrading=1
endif
if dayofweek=4 then //Thursday
daytrading=1
endif
if dayofweek=5 then // Friday
daytrading=1
endif
if dayofweek=6 or dayofweek=7 then //Sat and Sun
daytrading=0
endif
// ---
// --- indicators
ema = exponentialaverage[150](close)
ema2 = exponentialaverage[50](close)
//set trend using highs and lows
Trend1= highest[1](high)
Trend2= highest[20](high)
Trend3 = trend2-trend1
///
Trend4= lowest[1](low)
Trend5= lowest[20](low)
Trend6 = trend4-trend5
///
EMAabove = ema2>ema
EMAbelow = ema2<ema
atr = averagetruerange[24]
hh = highest[12](high)
ll = lowest[12](low)
Long = emaabove and trend3 >0
Short = emabelow and trend6 >-1
// ---
if intradaybarindex=0 then
alreadytraded = 0
case = 0
levelhi = 0
levello = 0
endif
if onmarket or (onmarket[1] and not onmarket) or (currentprofit<>strategyprofit) then
alreadytraded = 1
endif
if time=activationtime and time<limithour then
// case 1 : If price candle touches MA (even wicks) then look at high or low of signal candle
if trend3<0 or trend6<-1 then
case = 1
levelhi = CALL"#floor and ceil"[high,10.0,1]
levello = CALL"#floor and ceil"[low,10.0,-1]
endif
//case 2 : If price is above the MA then only trade long BUT only above the highest high of the past 24 hrs
if long and case = 0 then
case = 2
levelhi = hh[1]
endif
//case 3 : If price is below the MA then only trade short BUT only below the lowest low of the past 24 hrs
if short and case = 0 then
case = 3
levello = ll[1]
endif
endif
if alreadytraded = 0 then
//money management
if case=1 then
StopLoss = 10
else
StopLoss = 10
endif
Risk = riskpercent/100
//calculate contracts
equity = balance + StrategyProfit
maxrisk = round(equity*Risk)
size = max(minlot,abs(round((maxrisk/StopLoss)/PointValue)*pipsize))
//in all cases put pending orders on market
while case <> 0 and daytrading=1 and time <=LimitHour do
if levelhi>0 then
buy size contract at levelhi stop
endif
if levello>0 then
sellshort size contract at levello stop
endif
wend
endif
//set target and profit
if onmarket then
if case = 1 then
set target profit 1*ATR
set stop ploss StopLoss
endif
if case = 2 or case = 3 then
set target profit 1*ATR
set stop ploss StopLoss
endif
endif
currentprofit = strategyprofit
//debugging
//graph case as "case"
//graph time=activationtime coloured(100,120,133) as "activation time!"
graph time
//graph ema
//graph levelhi coloured(0,200,0) as "level high"
//graph levello coloured(200,0,0) as "level low"
//graph size as "mm"
Hi, had a quick look at this, I don’t think your code is doing what you think it is doing:
if time=activationtime and time<limithour then
This will only perform the tests on the following lines at 080000, I think you meant it to be between the 2 times so time >= activationtime and time<limithour?
while case <> 0 and daytrading=1 and time <=LimitHour do
if levelhi>0 then
buy size contract at levelhi stop
This will execute orders at any time before LimitHour including 000000 if it meets the entry criteria
Thanks autostrategist.
I see what you mean for the first command, so the second element is correct further down under //in all cases put pending orders on market
while case <> 0 and daytrading=1 and time <=LimitHour do
So this part of the code should ensure that trades are not activated beyond/past the limitHour set earlier in code?
Autostrategist
I just tried the above and sadly the system is still taking trades at 00:00.00 (midnight) randomly. Could this be linked to this part of the code?
// ---
if intradaybarindex=0 then
alreadytraded = 0
case = 0
levelhi = 0
levello = 0
endif
if onmarket or (onmarket[1] and not onmarket) or (currentprofit<>strategyprofit) then
alreadytraded = 1
endif
It seems to take the random midnight trades when the system has not traded the day before? (such as criteria not met, or weekends)?
You need to change this line:
while case <> 0 and daytrading=1 and time <=LimitHour do
to be:
while case <> 0 and daytrading=1 and (time >= activationtime and time <=LimitHour) do
Ahh, brilliant. Thanks AS! That should solve it.
Best wishes