EMA Cloud Intraday Strategy
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rwestbrookjr

//@version=5
strategy("EMA Cloud Intraday Strategy", overlay=true, margin_long=100, margin_short=100, process_orders_on_close=true)

i_trdQty = input.int(100, "Trade Quantity", minval = 1)
pbTrig = input.float(1.00, "Pull Back Trigger", minval = 0.01)

session = time(timeframe.period, "0930-1555:23456")
validSession=(na(session) ? 0 : 1)


fastLen = input(title = "Fast EMA Length", defval = 9)
slowLen = input(title = "Slow EMA Length", defval = 20)

fastEMA = ta.ema(close, fastLen)
slowEMA = ta.ema(close, slowLen)

fema = plot(fastEMA, title = "FastEMA", color = color.green, linewidth = 1, style = plot.style_line)
sema = plot(slowEMA, title = "SlowEMA", color = color.red, linewidth = 1, style = plot.style_line)

Bull = fastEMA > slowEMA
Bear = fastEMA < slowEMA

fill(fema, sema, color = fastEMA > slowEMA ? color.new(#417505, 50) : color.new(#890101, 50), title = "Cloud")

longCondition = close > fastEMA and close[1] > fastEMA and validSession == 1
if (longCondition)
    strategy.entry("Long_Entry", strategy.long, qty=i_trdQty)
longExit = (close < fastEMA and close[1] < fastEMA) or close < (close[1] - pbTrig) or validSession == 0
if (longExit)
    strategy.close("Long_Entry")
    //strategy.exit("exit", "My Long Entry Id", trail_points=1.5, trail_offset=0)

shortCondition = close < fastEMA and close[1] < fastEMA and validSession == 1
if (shortCondition)
    strategy.entry("Short_Entry", strategy.short, qty=i_trdQty)
shortExit = (close > fastEMA and close[1] > fastEMA) or close > (close[1] + pbTrig) or validSession == 0
if (shortExit)
    strategy.close("Short_Entry")
    //strategy.exit("exit", "My Short Entry Id", trail_points=1.5, trail_offset=0)
    
// Close all trades at end of trading day
barOpenTime = time 

tt=timestamp(year(time), month(time), dayofmonth(time), hour(time), 2)
    
if ( hour(time) == 15 and minute(time) > 50  )  
    strategy.close("Short_Entry", comment="SCM")
    
if ( hour(time) == 15 and minute(time) > 50 )   
    strategy.close("Long_Entry", comment="SCM")


if validSession == 0// and strategy.opentrades != 0
    //if strategy.opentrades > 0
    strategy.close_all(comment="SC")
    //if strategy.opentrades < 0
        //strategy.close("Short_Entry", comment="SC")
