Detecting Key Trading Session Levels in Pine

This lesson demonstrates how to detect the highest high, lowest low and the closing price of a given trading session (based on a time window).

Check out my other free lessons!

Source Code

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ZenAndTheArtOfTrading / PineScriptMastery
// @version=5
indicator("Session High & Low", overlay=true)

// Get input
timeSession     = input.session(title="Session To Monitor", defval="0800-1500")
timeZone        = input.string(title="Timezone", defval="GMT")
debugPaint      = input.bool(title="Color Session", defval=false)

// InSession() determines if a price bar falls inside the specified session. GMT is the timezone to apply.
InSession(sess) => na(time(timeframe.period, sess + ":1234567", "GMT")) == false

// Save today's session high/low/close
var float todayHigh = high
var float todayLow = low
var float todayClose = close

// Save yesterday's session high/low/close
var float yesterdayHigh = na
var float yesterdayLow = na
var float yesterdayClose = na

// Detect end of session (used to reset today's high/low/close tracker)
var bool firstBarOfNewSession = false

// Check if in session - if so, update today's high/low
if InSession(timeSession)
    if firstBarOfNewSession
        todayHigh := high
        todayLow := low
        firstBarOfNewSession := false
    if high > todayHigh
        todayHigh := high
    if low < todayLow
        todayLow := low
else
    // Save market high/low/close, reset vars
    if not firstBarOfNewSession
        yesterdayHigh := todayHigh
        yesterdayLow := todayLow
        yesterdayClose := close[1]
        firstBarOfNewSession := true

// Draw levels (yesterdayHigh != yesterdayHigh[1] just creates a 'break' in the line when a new session starts, looks better on chart)
plot(yesterdayHigh != yesterdayHigh[1] ? na : yesterdayHigh, color=color.red, style=plot.style_linebr)
plot(yesterdayLow != yesterdayLow[1] ? na : yesterdayLow, color=color.blue, style=plot.style_linebr)
plot(yesterdayClose != yesterdayClose[1] ? na : yesterdayClose, color=color.white, style=plot.style_linebr)

// Highlight session (debug)
bgcolor(debugPaint and InSession(timeSession) ? color.new(color.green, 90) : na)