jtsas
Pine Script Rookie
Pine Script Rookie
Posts: 4
Joined: December 6th, 2020

Session boxes based on high/low for the session?

Hi all, I'm struggling to figure out how to plot a box for a defined FOREX session using the high and low of the session as the upper and lower bound. I currently have the bgcolor changing for each session, but this isn't what I want. Any ideas?

jtsas
Pine Script Rookie
Pine Script Rookie
Posts: 4
Joined: December 6th, 2020

Re: Session boxes based on high/low for the session?

This is as far as I've gotten. I'm not sure how to get it to redraw so it backfills from the highest high and lowest low for the session.

Image

User avatar
Matthew
Site Admin
Site Admin
Posts: 92
Joined: July 1st, 2020
Location: Australia
Contact: Website Facebook Twitter TradingView Profile

Re: Session boxes based on high/low for the session?

Hi jtsas!

Achieving that is kind of tricky and will require the use of "var" variables.

What you need to do is something like this:

Step 1: Detect when your session begins or ends

Code: Select all

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

Step 2: Detect the high / low of the session

Code: Select all

var highOfSession = 0.0
var lowOfSession = 99999999

if InSession(<specified_trading_session>) and (high > highOfSession or na(highOfSession))
    highOfSession := high

if InSession(<specified_trading_session>) and (low < lowOfSession or na(lowOfSession))
    lowOfSession := low

Step 3: Reset high/low when the session ends

Code: Select all

if not InSession(<specified_trading_session>)
    highOfSession := na
    lowOfSession := na

Step 4: Draw/fill background

Code: Select all

highPlot = plot(highOfSession)
lowPlot = plot(lowOfSession)
fill(highPlot, lowPlot, color=color.yellow)

Unfortunately I don't have time to go into more detail, this is just some pseudo-code to get you thinking about potential solutions. This kind of thing is fairly complex and is something we'll cover in the Mentorship Program when it's released :) best of luck with your trading!

Return to “Pine Script Q&A”