I would like to figure out how to code entering a trade at the open and exiting at the close on a daily candle.
here is my attempt at the code which opens and closes simultaneously, rather than the end of day (:
any suggestions greatly appreciated
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
Position = 10
starttime = 090000
finishtime = 230000
ema = ExponentialAverage[10](close)
// Conditions to enter long positions
c1 = (ema > ema[1])
c2 = (currenttime = starttime)
IF c1 and c2 THEN
BUY position CONTRACT AT MARKET
ENDIF
// Conditions to exit long positions
c3 = (currenttime = finishtime)
IF c3 then
sell position contract at market
endif
// Conditions to enter short positions
c4 = (ema < ema[1])
c5 = (currenttime = starttime)
IF c4 and c5 THEN
SELLSHORT position CONTRACT AT MARKET
ENDIF
// Conditions to exit short positions
c6 = (currenttime = finishtime)
if c6 then
exitshort position contract at market
endif
// Stops and targets
SET STOP loss 1*(averagetruerange[14](close))
Hi Chris,
What instrument are you referring to, and what time zone are is your platform set to ?
Your code refers to “currenttime” – should this be just “time” ?
In any case – This sample code should point you in the right direction, using XJO as it appears your a fellow Aussie 😉
Use c2 for entries and c3 for exits
DayOpenTime = 100100 //1min after market open
DayCloseTime = 155900 //1min before market close
c2 = time <= DayOpenTime
c3 = time >= DayCloseTime
many thanks Grizzly
I have set the time zone as Sydney GMT+10 (screenshot attached)
I am looking to test on Australia 200 & AUD/USD on daily candles timeframes, which would include after hours trading to pick up any moves during the europe & US sessions. I’ll adjust the times to 000100 to enter and 115900 to exit – I’ll do some testing & see how it goes.
cheers
Chris
When I applied the code to Sydney GMT+10 there were no entries & when I changed that to GMT+1 the entry conditions seemed to work, though not the exit based on time – see attached screenshot. Could it be something around when the time condition is met, it is applied to the next bar and leaves the position open??
Any suggestions?
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
DayOpenTime = 000100 //1min after market open
DayCloseTime = 235900 //1min before market close
Position = 1
ema = ExponentialAverage[10](close)
// Conditions to enter long positions
c1 = (ema > ema[1])
c2 = time <= DayOpenTime
IF c1 and c2 THEN
BUY position CONTRACT AT MARKET
ENDIF
// Conditions to exit long positions
c3 = time >= DayCloseTime
IF c3 then
sell position contract at market
endif
// Conditions to enter short positions
c4 = (ema < ema[1])
c5 = time <= DayOpenTime
IF c4 and c5 THEN
SELLSHORT position CONTRACT AT MARKET
ENDIF
// Conditions to exit short positions
c6 = time >= DayCloseTime
if c6 then
exitshort position contract at market
endif
// Stops and targets
SET STOP loss 1*(averagetruerange[14](close))
When you are changing timezome in the configuration window, you need to save the platform and restart it, otherwise it will not has any effect on the current session.
About the exit at end of day, maybe you can try to add this line at the beginning of the code and see what happens:
defparam flatafter = 235900
Thanks Nicolas – no luck I’m afraid. I’ll experiment with an intraday timeframe and see how that goes. cheers Chris
You’ll get more chance with an intraday timeframe for sure. Since each condition are only tested once per bar, your condition to close trades are not executed, because this time condition will never be met.