'How to draw a box intraday around a defined session, high and low
I am working on this pine script where I want to draw a box from 08:00 to 09:00 and from the high and low within it. I am stuck with drafting the coordinates for the box.
The problem for me is that I don't know how to get the bars as integer within the session I defined. Right now, firstBar and lastBar are boolean
//@version=5
indicator("breakout", overlay=true)
usrTimeframe = input.timeframe('15', title="Timeframe", options=['1', '3', '5','15','30'])
usrSession = input.session('0800-0900', title="Session")
// Color timeframe
timeInSession = time(timeframe.period, usrSession + ':23456')
bgcolor(timeInSession ? color.new(color.blue, 90) : na)
//check bars in timeframe
showHi = input(true, 'Show highs')
showLo = input(true, 'Show lows')
srcHi = input(high, 'Source for Highs')
srcLo = input(low, 'Source for Lows')
noPlotOutside = input(true, 'Don\'t plot outside of hours')
var hi = 10e-10
var lo = 10e10
if timeInSession
// We are entering allowed hours; reset hi/lo.
if not timeInSession[1]
hi := srcHi
lo := srcLo
lo
else
// We are in allowed hours; track hi/lo.
hi := math.max(srcHi, hi)
lo := math.min(srcLo, lo)
lo
plot(showHi and not (noPlotOutside and not timeInSession) ? hi : na, 'Highs', color.new(color.blue, 0), 3, plot.style_circles)
plot(showLo and not (noPlotOutside and not timeInSession) ? lo : na, 'Lows', color.new(color.fuchsia, 0), 3, plot.style_circles)
firstBar = na(timeInSession[1]) and not na(timeInSession) or timeInSession[1] < timeInSession
lastBar = na(timeInSession) and not na(timeInSession)
breakoutBox = box.new(left = firstBar, top = hi, right = lastBar, bottom = lo)
plot(close, title="ENDplot")
Sources:
[https://stackoverflow.com/questions/69644349/how-to-get-highest-high-lowest-low-and-close-of-a-session-in-pine-script-tradin](https://stackoverflow.com/questions/69644349/how-to-get-highest-high-lowest-low-and-close-of-a-session-in-pine-script-tradin)
[https://www.tradingcode.net/tradingview/daily-high-low-boxes/](https://www.tradingcode.net/tradingview/daily-high-low-boxes/)
I tried defining certain bars by using specific timestamps, but this just does not show any box
t1 = (timestamp("GMT+1",2022,03,25,08,00,00))
t2 = (timestamp("GMT+1",2022,03,25,09,00,00))
candleHigh = high[t1]
candleLow = low[t2]
breakoutBox = box.new(left = t1, top = hi, right = t2, bottom = lo)
UPDATE: 28.03.2022
I transformed the code further and tried to change days into a custom session, but now the boxes don't show, someone an idea?
//@version=5
strategy("custom session", overlay=true, margin_long=100, margin_short=100)
// Input options
boxBorderSize = input.int(2, title="Box border size", minval=0)
upBoxColor = input.color(color.new(color.green, 85), title="Up box colour")
upBorderColor = input.color(color.green, title="Up border colour")
downBoxColor = input.color(color.new(color.red, 85), title="Down box colour")
downBorderColor = input.color(color.red, title="Down border colour")
joinBoxes = input.bool(false, title="Join boxes")
usrSession = input.session('0800-0929', title="uSession", options=['0800-0929','0800-0930', '0900-1029', '0900-1030'])
// Create variables
var sessionHighPrice = 0.0
var sessionLowPrice = 0.0
var prevDayClose = 0.0 //will later be problem if sessionstart is not daystart!
var box sessionBox = na
// See if a new calendar day started on the intra-day time frame
//newDayStart = dayofmonth != dayofmonth[1] and
// timeframe.isintraday
//see if new session started
newSessionStart = usrSession != usrSession[1] and timeframe.isminutes
inSession = not na(time(timeframe.period, "0800-0929:23456", "GMT+1"))
// If a new session starts, set the high and low to that bar's data. Else
// during the session track the highest high and lowest low.
if newSessionStart and inSession
sessionHighPrice := high
sessionLowPrice := low
prevDayClose := close[1]
else if inSession
sessionHighPrice := math.max(sessionHighPrice, high)
sessionLowPrice := math.min(sessionLowPrice, low)
else
na
// When a new session start, create a new box for that session.
// Else, during the session, update that day's box.
if newSessionStart and inSession
sessionBox := box.new(left=bar_index, top=sessionHighPrice,
right=bar_index + 1, bottom=sessionLowPrice,
border_width=boxBorderSize)
// If we don't want the boxes to join, the previous box shouldn't
// end on the same bar as the new box starts.
if inSession and not joinBoxes
box.set_right(sessionBox[1], bar_index[1])
else if inSession
box.set_top(sessionBox, sessionHighPrice)
box.set_rightbottom(sessionBox, right=bar_index + 1, bottom=sessionLowPrice)
// If the current bar closed higher than yesterday's close, make
// the box green (and paint it red otherwise)
if close > prevDayClose
box.set_bgcolor(sessionBox, upBoxColor)
box.set_border_color(sessionBox, upBorderColor)
else
box.set_bgcolor(sessionBox, downBoxColor)
box.set_border_color(sessionBox, downBorderColor)
else
na
Solution 1:[1]
after bodging a lot of stuff together I came up with this working solution, its not pretty but does the job. Note: Some times have to be modified in the code not all the inputs are effective!
strategy('DAX breakout session', overlay=true, margin_long=100, margin_short=100)
usrSession = input.session('0800-0929', title="Session", options=['0800-0929','0800-0930', '0900-1029', '0900-1030'])
sessionBoxColor = input.color(color.new(color.green, 85), title="Up box colour")
// Color timeframe
timeInSession = time(timeframe.period, usrSession + ':23456')
bgcolor(timeInSession ? color.new(color.blue, 90) : na)
//create box
boxBorderSize = input.int(2, title="Box border size", minval=0)
var dayHighPrice = 0.0
var dayLowPrice = 0.0
var prevDayClose = 0.0
var box sessionBox = na
// See if a new calendar day started on the intra-day time frame
newDayStart = dayofmonth != dayofmonth[1] and
timeframe.isintraday
// If a new day starts, set the high and low to that bar's data. Else
// during the day track the highest high and lowest low.
if newDayStart
dayHighPrice := high
dayLowPrice := low
prevDayClose := close[1]
else
dayHighPrice := math.max(dayHighPrice, high)
dayLowPrice := math.min(dayLowPrice, low)
// When a new day start, create a new box for that day.
// Else, during the day, update that day's box.
if newDayStart
sessionBox := box.new(left=bar_index, top=dayHighPrice,
right=bar_index + 1, bottom=dayLowPrice,
border_width=boxBorderSize, bgcolor=sessionBoxColor)//, extend=extend.right)
//Functions
IsSessionStart(sessionTime, sessionTimeZone=syminfo.timezone) =>
inSess = not na(time(timeframe.period, sessionTime, sessionTimeZone))
inSess and not inSess[1]
IsLastBarSession(sessionTime, sessionTimeZone=syminfo.timezone) =>
var int lastBarHour = na
var int lastBarMinute = na
var int lastBarSecond = na
inSess = not na(time(timeframe.period, sessionTime, sessionTimeZone))
if not inSess and inSess[1]
lastBarHour := hour[1]
lastBarMinute := minute[1]
lastBarSecond := second[1]
hour == lastBarHour and minute == lastBarMinute and second == lastBarSecond
SessionHigh(sessionTime, sessionTimeZone=syminfo.timezone) =>
insideSession = not na(time(timeframe.period, sessionTime, sessionTimeZone))
var float sessionHighPrice = na
if insideSession and not insideSession[1]
sessionHighPrice := high
else if insideSession
sessionHighPrice := math.max(sessionHighPrice, high)
sessionHighPrice
SessionLow(sessionTime, sessionTimeZone=syminfo.timezone) =>
insideSession = not na(time(timeframe.period, sessionTime, sessionTimeZone))
var float sessionLowPrice = na
if insideSession and not insideSession[1]
sessionLowPrice := low
else if insideSession
sessionLowPrice := math.min(sessionLowPrice, low)
sessionLowPrice
//Definitions
isFirstBarOfSession = IsSessionStart("0800-0930")
isSessionLast = IsLastBarSession("0800-0930")
//Set Box Y
if isFirstBarOfSession
// label.new(bar_index, high, 'First Bar')
box.set_left(id=sessionBox, left=bar_index)
else
na
if isSessionLast
// label.new(bar_index, high, 'Last Bar')
box.set_right(id=sessionBox, right=bar_index)
else
na
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | pineConer |
