Skip to content

Instantly share code, notes, and snippets.

@iCodeForBananas
Last active April 18, 2023 15:02
Show Gist options
  • Save iCodeForBananas/c8cc68f348bdf87c3fafe1ef8939b3cf to your computer and use it in GitHub Desktop.
Save iCodeForBananas/c8cc68f348bdf87c3fafe1ef8939b3cf to your computer and use it in GitHub Desktop.
//@version=5
// @MNQ on M5 M15 and M30
strategy("MA Price Cross w/ Trail Points Strategy", overlay=true, initial_capital = 5000, default_qty_value = 1, pyramiding = 0)
maLength = input(30, 'Trigger EMA length', group = 'Filter')
closeTradesEOD = input.bool(true, 'Close trades end of day', group = 'Risk')
// Time Range
timeAllowed = input.session("0830-1430", "Allowed hours", group = 'Time filters')
timeIsAllowed = time(timeframe.period, timeAllowed + ":1234567")
FromMonth=input.int(defval=1,title="FromMonth",minval=1,maxval=12, group = 'Time filters')
FromDay=input.int(defval=1,title="FromDay",minval=1,maxval=31, group = 'Time filters')
FromYear=input.int(defval=2020,title="FromYear",minval=2016, group = 'Time filters')
ToMonth=input.int(defval=1,title="ToMonth",minval=1,maxval=12, group = 'Time filters')
ToDay=input.int(defval=1,title="ToDay",minval=1,maxval=31, group = 'Time filters')
ToYear=input.int(defval=9999,title="ToYear",minval=2017, group = 'Time filters')
start=timestamp(FromYear,FromMonth,FromDay,00,00)
finish=timestamp(ToYear,ToMonth,ToDay,23,59)
window()=>time>=start and time<=finish?true:false
// STEP 2:
// See if this bar's time happened on/after start date
afterStartDate = time >= start and time<=finish?true:false
ma = ta.ema(close, maLength)
plot(ma, "MA", color = color.purple, linewidth = 1)
// Highlight stop loss on prior bar
l = line.new(bar_index, close > ma ? low : high, bar_index + 5, close > ma ? low : high, color=color.red)
line.delete(l[1])
if (ta.crossover(close, ma) and afterStartDate and timeIsAllowed)
strategy.entry("LE", strategy.long, comment="LE")
strategy.exit("LX", "LE", stop = low[1], comment = "LX", comment_profit = "LX profit")
if (ta.crossunder(close, ma) and afterStartDate and timeIsAllowed)
strategy.entry("SE", strategy.short, comment = "SE")
strategy.exit("SX", "SE", stop = high[1], comment = "SX", comment_profit = "SX profit")
if (closeTradesEOD and hour >= 14 and minute >= 30)
strategy.close_all("EOD")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment