将夏令时构建到脚本中以定义交易开放时间 Pinescript

0 dst pine-script

我正在尝试将夏令时纳入我的策略帐户中。该策略取决于交易所的开放时间。(不是加密货币)。

我的代码是:

dst=0

t=time(timeframe.period,"0400-0500:23456")
t1=time(timeframe.period,"0500-0555:23456") 

//This resolves daylight saving back to 2016
dsfallback2020 = timestamp(2019,10,25,08,00)
dsspringfor2020 = timestamp(2020,03,29,08,00)
dsfallback2019 = timestamp(2019,10,27,08,00)
dsspringfor2019 = timestamp(2019,03,31,08,00)
dsfallback2018 = timestamp(2018,10,28,08,00)
dsspringfor2018 = timestamp(2018,03,25,08,00)
dsfallback2017 = timestamp(2017,10,25,08,00)
dsspringfor2017 = timestamp(2017,03,26,08,00)
dsfallback2016 = timestamp(2017,10,30,08,00)
dsspringfor2016 = timestamp(2017,03,27,08,00)

if (t > dsspringfor2020 and t < dsfallback2020)
    dst:=1
else if (t > dsspringfor2019 and t < dsfallback2019)
    dst:=1
else if (t > dsspringfor2018 and t < dsfallback2018)
    dst:=1
else if (t > dsspringfor2018 and t < dsfallback2018)
    dst:=1
else
    na

if dst = 1
    t = time(timeframe.period,"0400-0500:23456")
    t1 = time(timeframe.period,"0500-0555:23456")  
    bgcolor(not na(t) ? color.green : na)
    H_Highbar = security('CAPITALCOM:UK100', '60', high[0]) 
    H_Lowbar = security('CAPITALCOM:UK100', '60', low[0])
    plot(series=not t1 ? na : H_Highbar, title='Session Open Price', color=color.green, linewidth=2, style=plot.style_linebr, transp=0)
    plot(series=not t1 ? na : H_Lowbar, title='Buy Line', color=color.red, linewidth=2, style=plot.style_linebr, transp=0)
else
    t =time(timeframe.period,"0300-0400:23456")
    t1 = time(timeframe.period,"0400-0455:23456")
    bgcolor(not na(t) ? color.green : na)
    H_Highbar = security('CAPITALCOM:UK100', '60', high[0]) 
    H_Lowbar = security('CAPITALCOM:UK100', '60', low[0])
    plot(series=not t1 ? na : H_Highbar, title='Session Open Price', color=color.green, linewidth=2, style=plot.style_linebr, transp=0)
    plot(series=not t1 ? na : H_Lowbar, title='Buy Line', color=color.red, linewidth=2, style=plot.style_linebr, transp=0)
Run Code Online (Sandbox Code Playgroud)

这不起作用。希望你能看到我一直在努力做的事情。希望有人能帮助我。

Rgr*_*ray 5

我相信有一种更简单的方法,使用 hour() 函数。

例如,我们知道在标准时间(非 DST)期间,纽约与 UTC 之间的时差为 5 小时,而在 DST 期间,时差为 4 小时——UTC 中没有 DST。所以:

HourNY = 小时(时间,“美国/纽约”)

HourUTC = 小时(时间,“UTC”)

DST := UTC 时间 - UTC 时间 == 5 ?假:真

在纽约标准时间期间以及 12:00 和 12:59:59 之间,HourNY 将等于 12,HourUTC 将等于 17,差值为 5 (DST = false)。

在 DST 期间以及纽约的 12:00 和 12:59:59 之间,HourNY 将等于 12,HourUTC 将等于 14,差值为 4 (DST = true)。

现在,在纽约 19:00 之后,此方法将不起作用,因为这是 UTC 和 HourUTC 的第二天 - HourUTC 将给出负数。

您可以通过仅在纽约时间 0000-1859 时间段内进行测试来实现此目的。然而,在我看来,一种更简洁的方法是注意到在标准时间期间以及 19:00 和 23:59 HourUTC 之间 - HourUTC 将等于 -19(在 DST 期间它将等于 -20),这样就可以构建进入代码:

DST := HourUTC - HourNY == 5 或 HourUTC - HourNY == -19 ?false :HourUTC - HourNY == 4 或 HourUTC - HourNY == -20 ?正确:不

瞧:)