Sessioned EMA - Frozen EMA in post market hours
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MalibuKenny

//@version=5
indicator("v1.0 Sessioned EMA", overlay = true, max_labels_count = 100)

group0 = 'Session Volatility (Realtime)============'
startDate = input.time(title='Start Date', defval=timestamp('6 Oct 1000 17:00 +0000'), tooltip='Date & time to begin analysis', group=group0)
endDate = input.time(title='End Date', defval=timestamp('1 Jan 2099 12:00 +0000'), tooltip='Date & time to stop analysis', group=group0)
ema9_sessioned = input.bool(title = "ema9_sessioned", defval = false)
timeSession = input.string(defval = '0930-1600', group =  group0)
show_ema_final_difference = input.bool(title = "show_ema_final_difference", defval = false)


// This function returns true if the current bar falls within the given time session (:1234567 is to include all weekdays)
inSession(sess) =>  //custom function input sess
    na(time(timeframe.period, sess + ':1234567', 'GMT+8')) == false and time >= startDate and time <= endDate

// Declaring Variable

// Check if a new session has begun (not in ==> in)
var withinSession = false
if not inSession(timeSession)[1] and inSession(timeSession)
    withinSession := true
if  inSession(timeSession)[1] and not inSession(timeSession)
    withinSession := false
//Change the background color for the in sessioin part (colorBG is a check box)
bgcolor(inSession(timeSession) ? color.new(color.blue, 90) : na)


pine_ema(source, length) =>
    alpha = 2 / (length + 1)
    sum = 0.0
    sum := na(sum[1]) ? 
		 ta.sma(source, length) : 
		 alpha * source + (1 - alpha) * nz(sum[1])

// EMA = (Close - EMA(previous day)) x multiplier + EMA(previous day)
smoothing = 2
var ema9 = 0.0
var ema9b = 0.0
var ema25 = 0.0
var ema55 = 0.0
var ema100 = 0.0
var ema160 = 0.0


if inSession(timeSession)
    ema9b:= pine_ema(ohlc4, 9)
    ema25:= pine_ema(ohlc4, 25)
    ema55:= pine_ema(ohlc4, 55)
    ema100:= pine_ema(ohlc4, 100)
    ema160:= pine_ema(ohlc4, 160)
else
    ema9b:= ema9b[1]
    ema25:= ema25[1]
    ema55:= ema55[1]
    ema100:= ema100[1]
    ema160:= ema160[1]


ema3=ta.ema(ohlc4,3)
if ema9_sessioned
    ema9:=ema9b
else    
    ema9:=ta.ema(ohlc4,9)

plot(ema3, linewidth = 1, color = color.new(color.black,0))
plot(ema9, linewidth = 3, color = color.new(color.fuchsia,30))
plot(ema25, linewidth = 2, color = color.new(color.blue,0))
plot(ema55, linewidth = 2, color = color.new(color.green,0))
plot(ema100, linewidth = 2, color = color.new(color.orange,0))
plot(ema160, linewidth = 2, color = color.new(color.black,0))

max_ema= math.max(ema25, ema55, ema100, ema160)
min_ema= math.min(ema25, ema55, ema100, ema160)
delta_ema = max_ema -min_ema
label_ema_diff = label.new(x=bar_index, y = max_ema, text = str.tostring(delta_ema, "#"), color = color.new(color.blue,70))
if withinSession
    label.delete(label_ema_diff)
if delta_ema == delta_ema[1]
    label.delete(label_ema_diff[1])
