Trading Session Template
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © its_zezooo


// CODE FROM ZEN FREE COURSE 
//@version=5
indicator("Trading Session Template", shorttitle = "MySession", overlay = true)

// Import Zen Library
import ZenAndTheArtOfTrading/ZenLibrary/2 as zen

// user inputs //{ 
// Time settings 
var g_time          = "════════════ Session Time Settings ═════════════"
useSess             = input.bool(title="use session window?", defval=true, tooltip = "When turned on, the script will change the background color (script deafult is red) when in-session", group=g_time)
string sessionInput = input.session("0800-1600", "session hours", tooltip = "set the hour range of your target session", group = g_time)
string daysInput    = input.string("1234567","session days", tooltip = "Day range: 1 = Sunday, 7 = Saturday", group = g_time)

// ATR settings
var g_ATR           = "═════════════ ATR filter Settings ══════════════" 
ATRswitch           = input.bool(title="ATR based SL/TP?",defval=false, tooltip = "when turned on, ATR based SL & TP according to user inputs ATR settings will be displayed  ", group = g_ATR)
ATR_length          = input.int(title = "ATR length", defval = 14, group = g_ATR)
multiplier          = input.float(title="ATR SL Multiplier", defval = 1, tooltip = "this number will be multiplied by the ATR of the previous 14 candles to set an ATR based SL", group = g_ATR)
rr                  = input.float(title="R:R - using ATR", defval = 1, tooltip = "this number will be multiplied by the multiplier to set a TP based on SL", group = g_ATR)

// Candles
var g_candles       = "════════════ Candle Patterns Settings ════════════" 
switchDoji          = input.bool(title = "Doji Candles?", defval = false, tooltip = "when turned on will detect Doji candles", group = g_candles)
switchEC            = input.bool(title="EC Candles pattern?",defval=false, tooltip = "when turned on will detect Engulfing candles", group = g_candles)
switchHammer_Stars  = input.bool(title="Hammer/Star Candles?",defval=true , tooltip = "when turned on will detect Hammer/Star candles", group = g_candles)
fib_input           = input.float(title="Hammer & Star fib", defval = 0.333, minval = 0, maxval = 1,step = 0.1, tooltip = "thow large is the body of the candle in relation to the distance between its high and low", group = g_candles)
lookback            = input.int(title = "Lookback period", defval = 10, tooltip = "lookback for highest high / Lowest Low", group = g_candles)
atrMinFilterSize    = input.float(title=">= ATR Filter", defval=0.0, minval=0.0, step= 0.1 ,group=g_candles, tooltip="Minimum size of entry candle compared to ATR")
atrMaxFilterSize    = input.float(title="<= ATR Filter", defval=3.0, minval=0.0, step= 0.1, group=g_candles, tooltip="Maximum size of entry candle compared to ATR")
//}

//atr function
atr=ta.atr(ATR_length)

// Engulfing calndles 
bullEC = zen.isBullishEC() and switchEC
bearEC = zen.isBearishEC() and switchEC

// Hammer and star
isHammer  = zen.isHammer(fib_input) and switchHammer_Stars
isStar    =zen.isStar(fib_input) and switchHammer_Stars

// doji
doji = zen.isDoji() and switchDoji

// highest/lowest over lookback 
highest_high = ta.highest(high, lookback)
lowest_low= ta.lowest(low, lookback)

isHighest = high == highest_high? true:false
isLowest = low == lowest_low? true:false

// bar size in aTR
// Check ATR filter
atrMinFilter = high - low >= (atrMinFilterSize * atr) or atrMinFilterSize == 0.0
atrMaxFilter = high - low <= (atrMaxFilterSize * atr) or atrMaxFilterSize == 0.0
atrFilter = atrMinFilter and atrMaxFilter and not na(atr)

// time 
sessionString = sessionInput + ":" + daysInput
inSession = not na(time(timeframe.period, sessionString))

// buy and sell conditions 
buy     = (isHammer or bullEC or doji) and isLowest and atrFilter and barstate.isconfirmed and inSession
sell    = (isStar or bearEC or doji) and isHighest and atrFilter and barstate.isconfirmed and inSession
bgcolor(inSession and useSess? color.new(color.red,90):na)

// TP and SL for longs
longSL = ta.lowest(low, lookback) - (atr*multiplier)
longDistance = close - longSL   
longTP = close + (longDistance*rr)

/// TP and SL for longs
shortSL= ta.highest(high, lookback)+ (atr*multiplier)
shortDistance = shortSL-close
shortTP= close - (shortDistance*rr)

// plot function buy&sell signal 
plotshape(buy, title="buy signal", style = shape.labelup, location = location.belowbar, color=color.lime, size = size.normal)
plotshape(sell, title="sell signal", style = shape.labeldown, location = location.abovebar, color=color.red, size = size.normal)
// plot long SL and TP
plot(buy and ATRswitch?longSL:na, "Long SL", color=color.red, style=plot.style_linebr, linewidth = 3)
plot(buy and ATRswitch?longTP:na, "Long TP", color=color.lime, style=plot.style_linebr, linewidth = 3)
// plot short SL and TP
plot(sell and ATRswitch?shortSL:na, "Long SL", color=color.red, style=plot.style_linebr, linewidth = 3)
plot(sell and ATRswitch?shortTP:na, "Long TP", color=color.lime, style=plot.style_linebr, linewidth = 3)

// Alerts //{
if switchDoji
    if doji and isLowest and atrFilter and barstate.isconfirmed and inSession
        alert("A DOJI was detected at a LOW")
    if doji and isHighest and atrFilter and barstate.isconfirmed and inSession
        alert("A DOJI was detected at a HIGH")


if switchEC
    if bullEC and isLowest and atrFilter and barstate.isconfirmed and inSession
        alert("A BULLISH ENGULFING Candle was detected at a LOW")
    if bearEC and isHighest and atrFilter and barstate.isconfirmed and inSession
        alert("A BEARISH ENGULFING Candle was detected at a HIGH")

if switchHammer_Stars
    if isHammer and isLowest and atrFilter and barstate.isconfirmed and inSession
        alert("A HAMMER was detected at a LOW")
    if isStar and isHighest and atrFilter and barstate.isconfirmed and inSession
        alert("A STAR candle was detected at a HIGH")



