Timezone convertion between tickers

浪尽此生 提交于 2020-06-23 14:23:11

问题


I'm attempting to color the background on ticker ES1! for those bars that fall within the trading session of ticker SPX.
We can use the time(resolution, session) function to find out if a bar is in a specified session.

t1 = time(timeframe.period, "0930-1600")
bgcolor(t1 ? color.yellow : na)

This works when it's used on ticker SPX.
You can see that all bars have a yellow background, as expected.
Why? Because SPX trades between 09:30 and 16:00 in the timezone of the SPX ticker, which is America/New_York.

When we switch the ticker to ES1!, the yellow background ALSO starts at 09:30.
The reason for that is that the session parameter in the time(resolution, session) function, is evaluated in the timezone of the current ticker.

SPX trades in timezone America/New_York, which is UTC-4.
ES1! trades in timezone America/Chicago, which is UTC-5.

So, when SPX starts trading in New York at 09:30, it's only 08:30 in Chicago.
Therefore, I need the yellow background on the ES1! chart to start at 08:30.
However, it's not possible to get the time(resolution, session) function to be evaluated in another timezone.

What I need is a time() function that also takes the timezone into account.
Ideally like this:

t1 = time(timeframe.period, "0930-1600", "America/New_York")

This would return a non-na value on ES1! when the current bar falls within the New York trading session.
I know this is not possible with the current time(resolution, session) function.

Does anyone have a solution or workaround for this?


回答1:


You can use security to calculate the time:

t1 = security("SPX", timeframe.period, time(timeframe.period, "0930-1600"))
bgcolor(t1 ? color.yellow : na)

UPDATE

As Bjorn Mistiaen noticed in comments, the gaps parameter must be set to barmerge.gaps_on:

t1 = security("SPX", timeframe.period, time(timeframe.period, "0930-1600"), gaps=barmerge.gaps_on)
bgcolor(t1 ? color.yellow : na)


来源:https://stackoverflow.com/questions/62198423/timezone-convertion-between-tickers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!