Session High and Low Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © benjaminbarch

//@version=5
indicator("Session High and Low Indicator", shorttitle="H/L", overlay=true)

// --- WHAT THIS INDICATOR DOES - DESCRIPTION --- {

// Looks at the ATR for the last 2 candles and compares it with the ATR for the last 14 
// If the ATR(2) / ATR(14) is under our threshold, then calculate H/L
// Plots a Green line for the Pre-Market High
// Plots a Red line for the Pre-Market Low
// Quick spikes are ignored in the PM High/Low Calculation
// AlertConditions for when the Price Close breaks above or below the pre-market channel

// -- END DESCRIPTION }

// --- INPUTS --- {
//Let user adjust how tight to peaks and valleys to mark pre-market high and low
thresholdAtrPercent = input.float(title="ATR threshold %", defval=300, step=10, minval=1, maxval=10000, tooltip="Compares ATR(2) with ATR(14), Threshold must be under this number. The larger the number, the tighter to price action the PM Lines stick to. A lower number in this setting will allow quick price ation to blow over")
PreMarketSession = input.session(title="Premarket Time", defval="0400-0930")

// --- END INPUTS }

// --- VARIABLES AND DECLARATIONS --- {
//establish new function to simplify things
isInSession(_sess) => not na(time(timeframe.period, _sess))

//Set variables
var float PMcalculatedHigh = na // The ending results of the calculated session. only plotted outside of calculated session. In other words this is the variable to ise for alerting during regular trading hour alerts only
var float PMcalculatedLow = na
atr2 = ta.atr(2)
atr14 = ta.atr(14)
BullFlag = false
BearFlag = false
CurrentAtrPercent = 0.0

// --- END VARIABLES }

// --- GET PRE-MARKET H/L --- {
CurrentAtrPercent := (atr2 / atr14) * 100
if isInSession(PreMarketSession) and (not isInSession(PreMarketSession)[1]) // If this is the first bar of pre-market
    PMcalculatedHigh := na //reset variables
    PMcalculatedLow := na
if isInSession(PreMarketSession)
    if (na(PMcalculatedHigh) and CurrentAtrPercent <= thresholdAtrPercent) or (CurrentAtrPercent <= thresholdAtrPercent and close > PMcalculatedHigh)//If we don't have a Calculated PM High yet or our current high is higher, update the calcuated highest
        PMcalculatedHigh := close > open ? close : open //store the higher of either the open or the close
    if (na(PMcalculatedLow) and CurrentAtrPercent <= thresholdAtrPercent) or (CurrentAtrPercent <= thresholdAtrPercent and close < PMcalculatedLow)
        PMcalculatedLow := close < open ? close : open
else // We are not In PM Session, so check if we have broken the channel
    BullFlag := (close > PMcalculatedHigh) ? true : false
    BearFlag := (close < PMcalculatedLow) ? true : false
// --- END GET PRE-MARKET H/L}

// --- PLOTS AND ALERTS --- {
//plot the calculated Pre-Market High/Low Lines
plot(PMcalculatedHigh, title="Calculated PM High Line", color=color.new(color.green, 50), style=plot.style_linebr)
plot(PMcalculatedLow, title="Calculated PM Low Line", color=color.new(color.red, 50), style=plot.style_linebr)


// Alerts for Breaking out of channel
alertcondition(BullFlag, title="Bull Signal", message="Price breaking above pre-market channel")
alertcondition(BearFlag, title="Bear Signal", message="Price breaking below pre-market channel")
// --- END PLOTS }