In the following strategy I want to take entry 10 minutes once the Exchange opens (0925 hrs) and close all open positions 10 minutes before it closes (1520 hrs). Means the condition checking period is 0925-1520 hrs and Universal Exit (closing all open positions) is at 1520 hrs. I have added some codes but its not working. Can anybody please help me out. Thanks in advance.
//@version=5 strategy("EMA crossover strategy", overlay = true, calc_on_every_tick=true, process_orders_on_close = true)
session_time = input.session("0925-1520", "Session") is_in_session = time(timeframe.period, session_time)
ema10Close = ta.ema(close, 10) ema20Close = ta.ema(close, 20)
longEntryCondition = (is_in_session) and ta.crossover(ema10Close, ema20Close)
longCloseCondition = (is_in_session) and ta.crossunder(ema20Close, ema10Close)
if (longEntryCondition)
strategy.entry("Long", strategy.long) if (longCloseCondition) strategy.close("Long")
shortEntryCondition = (is_in_session) and ta.crossunder(ema10Close , ema20Close)
shortCloseCondition = (is_in_session) and ta.crossover(ema10Close, ema20Close)
if (shortEntryCondition)
strategy.entry("Short", strategy.short) if (shortCloseCondition) strategy.close("Short")
plot(ema10Close, color = color.new(color.blue, 0), linewidth = 1) plot(ema20Close, color = color.new(color.red, 0), linewidth = 1)
Not sure if it was because of the post, but the formatting was incorrect. This would have been part of the problem. I corrected this and also added inputs for your EMAs. This will allow you to quickly change them and see how it affects the strategy. This will plot indicators as well now, and will close all open trades outside of the session time.
//@version=5
strategy("EMA crossover strategy", overlay = true, calc_on_every_tick=true, process_orders_on_close = true)
string session_time = input.session("0925-1520", "Session")
string offset = input.string('UTC-0', title='Time Zone',
options=['UTC-11', 'UTC-10', 'UTC-9', 'UTC-8'
, 'UTC-7', 'UTC-6', 'UTC-5', 'UTC-4'
, 'UTC-3', 'UTC-2', 'UTC-1', 'UTC-0'
, 'UTC+1', 'UTC+2', 'UTC+3', 'UTC+4'
, 'UTC+5', 'UTC+6', 'UTC+7', 'UTC+8'
, 'UTC+9', 'UTC+10', 'UTC+11', 'UTC+12'
, 'UTC+13', 'UTC+14'])
is_in_session = time(timeframe.period, session_time, offset)
int fastLen = input.int(10, 'Fast EMA')
int slowLen = input.int(20, 'Slow EMA')
ema10Close = ta.ema(close, fastLen)
ema20Close = ta.ema(close, slowLen)
// Long Conditions
bool longEntryCondition = is_in_session and ta.crossover(ema10Close, ema20Close)
bool longCloseCondition = ta.crossunder(ema20Close, ema10Close)
// Short Conditions
bool shortEntryCondition = is_in_session and ta.crossunder(ema10Close , ema20Close)
bool shortCloseCondition = ta.crossover(ema10Close, ema20Close)
// Strategy
if longEntryCondition
strategy.entry("Long", strategy.long)
else if longCloseCondition
strategy.close("Long")
if shortEntryCondition
strategy.entry("Short", strategy.short)
else if shortCloseCondition
strategy.close("Short")
if not is_in_session
strategy.close_all(comment = 'Trading session ended, all positions closed.')
plot(ema10Close, 'Fast EMA', color = color.new(color.blue, 0), linewidth = 1)
plot(ema20Close, 'Slow EMA', color = color.new(color.red, 0), linewidth = 1)
plotshape(longEntryCondition
, title = 'Long'
, style = shape.triangleup
, size = size.small
, location = location.belowbar
, color = color.new(color.green, 0))
plotshape(shortEntryCondition
, title = 'Short'
, style = shape.triangledown
, size = size.small
, location = location.abovebar
, color = color.new(color.fuchsia, 0))