首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查bar是否是TradingView松树脚本最后一次会话的问题

检查bar是否是TradingView松树脚本最后一次会话的问题
EN

Stack Overflow用户
提问于 2022-07-09 05:49:38
回答 1查看 209关注 0票数 0

我想检测自定义会话的最后一条。我正在使用这里的代码:https://www.tradingcode.net/tradingview/last-session-bar/

总的来说,代码运行良好,但在某些情况下,它返回意外值。

下面的代码应该在每个15:45 (EDT)栏上绘制红色背景,因为它是美国纽约证券交易所会议(09:30-16:00 UTC-4)的最后一条。

代码语言:javascript
复制
//@version=5
indicator("last session bar",overlay = true)

// IsLastBarSession() returns 'true' when the current bar is the last
// of the specified session, adjusted for the given time zone (optional).
// Returns 'false' for other bars in the session, bars after the 
// session, and when the time frame is day or higher.
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

// Give the last bar of the 09:30 - 16:30 UTC-4 session a red background
bgcolor(IsLastBarSession("0930-1600", "UTC-4") ? 
     color.new(color.red, 80) : na)

在大多数情况下,最后一条是正确的检测。

但是有时候它会比它应该提前90分钟检测到最后一根蜡烛(尽管图上有14:30-15:45的蜡烛)。为什么?

COMEX:GC1!

日期:7月5日

周期: 15m (与30m、1h、2h相同)

链接到图表上的示例

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-09 07:13:46

代码是检查前一天的关闭时间,并使用它在当前天找到关闭,但自从前一天蜡烛关闭在14:15,它使用它的情节相同,我已经更正如下

代码语言:javascript
复制
//@version=5
indicator("last session bar",overlay = true,max_labels_count=500)

// IsLastBarSession() returns 'true' when the current bar is the last
// of the specified session, adjusted for the given time zone (optional).
// Returns 'false' for other bars in the session, bars after the 
// session, and when the time frame is day or higher.
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))

    not inSess and inSess[1]
    
// Give the last bar of the 09:30 - 16:30 UTC-4 session a red background
bgcolor(IsLastBarSession("0930-1600", "UTC-4") ? 
     color.new(color.red, 80) : na,-1)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72919268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档