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

发布于 01-17 14:33 字数 4934 浏览 3 评论 0原文

我正在研究这个松树脚本,我想在08:00到09:00绘制一个盒子 内部高和低。我被起草了盒子的坐标。

对我来说问题是我不知道如何将酒吧作为整数 我定义的会话。目前,FirstBar和LastBar是布尔

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

我尝试使用特定的时间戳来定义某些条, 但这只是没有显示任何框

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)

更新:28.03.2022

我进一步更改了代码,并试图将几天更改为自定义会话,但是现在盒子没有显示,有人想到了吗?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一页2025-01-24 14:33:22

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

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文