Trading Session Indicator

This indicator is intended to assist in the backtesting process. It can be used to highlight time zones in which you are either asleep or away from your charts, or intraday trading sessions that you want to trade.

It also has alert functionality, so it can be used to alert you when a new trading session is beginning.

How It Works

This is one of the simplest scripts I’ve created – all it does is identify when the current bar falls within the time zone you specify, and then it paints the background of the chart the color you specify.

It can also be used to change the color of the bars themselves, but that feature is optional.


Timezone To Highlight:
This is… you guessed it, the timezone you want to highlight! The first box defines the starting time, the second box defines the ending time.

Paint Background:
This tells the script whether or not to color the background of your chart.

Color Candles Within Zone:
This tells the script whether or not to color the candles themselves.

Alert When Session Starts:
This tells the script whether or not to alert you when a new session starts. You must still set the alert in the TradingView alerts menu.

Alert When Session Starts At:
This specifies the time in which to alert you of a new trading session.

Alert When Session Ends:
This tells the script whether or not to alert you when a trading session ends. You must still set the alert in the TradingView alerts menu.

Alert When Session Ends At:
This tells the script the exact time to alert you of the trading session ending.

Hide On 4HR And Above:
This will hide the highlighted color when you load a timeframe on the 4HR chart or higher (Daily, Weekly etc).

Hide On Other Timeframe:
This allows you to set a custom timeframe to hide the highlighted color on.

Other Timeframe To Hide:
This is the timeframe you want to hide the color on (only applies if Hide On Other Timeframe setting is checked).

Pine Script Basics Course Pine Script Mastery Course

Source Code

// Created by Matthew J. Slabosz
// Last Updated: 3rd April, 2021
// @version=4
study(title="Trading Session Indicator", shorttitle="TSI", overlay=true)

// Get user input
timezone = input(title="Timezone To Highlight", type=input.session, defval="0830-1830")
alertSessionStart = input(title="Alert When Session Starts?", type=input.bool, defval=true)
alertWhenSessionStarts = input(title="Alert When Session Starts At", type=input.session, defval="0830-0830")
alertSessionEnd = input(title="Alert When Session Ends?", type=input.bool, defval=true)
alertWhenSessionEnds = input(title="Alert When Session Ends At", type=input.session, defval="1830-1830")
paintBg = input(title="Paint Background?", type=input.bool, defval=true)
blankInsideCandles = input(title="Color Candles Within Zone?", type=input.bool, defval=false)
hideOn240AndAbove = input(title="Hide on 4HR And Above?", type=input.bool, defval=false)
hideTimeframeCB = input(title="Hide On Other Timeframe?", type=input.bool, defval=false)
hideTimeframe = input(title="Other Timeframe To Hide", type=input.string, defval="60")

// Determine whether or not this timeframe is to be ignored
hide = hideTimeframeCB and timeframe.period == hideTimeframe or 
   hideOn240AndAbove and 
   (timeframe.period == "240" or 
   timeframe.period == "D" or 
   timeframe.period == "W" or 
   timeframe.period == "M")

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

// Color the background of each relevant session and/or bar
bgcolor(color=InSession(timezone) and paintBg and not hide ? : na, title="Not In Session", transp=75)
barcolor(color=InSession(timezone) and blankInsideCandles and not hide ? color.white : na)

// Send out an alert if this candle meets our conditions
alertcondition((InSession(alertWhenSessionStarts) and alertSessionStart) or (InSession(alertWhenSessionEnds) and alertSessionEnd), title="Session Alert!", message="Session signal for {{ticker}}")

Last Updated: 16th October, 2020

Free Premium Charts!

4.7 3 votes
Article Rating
Notify of
Inline Feedbacks
View all comments