如何在日内围绕定义的交易时段(最高价和最低价)绘制一个方框

How to draw a box intraday around a defined session, high and low

我正在编写这个 pine 脚本,我想在其中绘制一个从 08:00 到 09:00 以及从 其中的高低。我一直在为盒子起草坐标。

我的问题是我不知道如何将柱形作为整数 我定义的会话。现在,firstBar 和 lastBar 是 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")

来源:

[

[https://www.tradingcode.net/tradingview/daily-high-low-boxes/](https://www.tradingcode.net/tradingview/daily-high-low-boxes/)

我尝试使用特定的时间戳来定义特定的柱, 但这只是不显示任何框

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)

更新:2022 年 3 月 28 日

我进一步转换代码并尝试将日期更改为自定义会话,但现在框不显示,有人有想法吗?

//@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

在综合了很多东西之后,我想出了这个可行的解决方案, 它不漂亮,但可以完成工作。注意:代码中有些时候要修改不是所有的输入都有效!

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