Timezone convertion between tickers (part 3)

烈酒焚心 提交于 2020-06-09 05:38:04

问题


As mentioned in the answer to Timezone convertion between tickers by Dennis T,
we can use the security() function to get the time in another timezone.

Now I was trying to get times from 2 different timezones.

Ticker CL1! trades on Nymex in New York, in timezone UTC-4.
Ticker ES1! trades on CME in Chicago, in timezone UTC-5.

We can see that from the symbol info

In order to visualize that time difference, I've created a small script that plots the time in both timezones.

//@version=4
study("Nymex-CME times", shorttitle="Time")

time_cl1 = security("CL1!", timeframe.period, time(timeframe.period), gaps=barmerge.gaps_on) // UTC-4 Nymex
time_es1 = security("ES1!", timeframe.period, time(timeframe.period), gaps=barmerge.gaps_on) // UTC-5 CME

plot(time_cl1, "time_cl1", color.blue)
plot(time_es1, "time_es1", color.red)

Since both tickers are traded in timezones that are 1 hour apart, I expected to see a 1 hour time difference.

To my surprise however, both times are always exactly the same.
I don't understand that.
Shouldn't there be a 1 hour difference?


回答1:


time() is the time in the exchange's time zone and when viewed through security(), will also be dependent on how the bars from each ticker are mapped to each other. Also, those two markets have a one hour delta in their starting/closing time.

Here, we fetch the Unix time but then convert it to hours in the context of the chart's timezone:

//@version=4
study("Nymex-CME times", shorttitle="Time")

time_cl1 = security("CL1!", timeframe.period, timestamp(year, month, dayofmonth, hour, minute, second), gaps=barmerge.gaps_on) // UTC-4 Nymex
time_es1 = security("ES1!", timeframe.period, timestamp(year, month, dayofmonth, hour, minute, second), gaps=barmerge.gaps_on) // UTC-5 CME

hour_cl1 = hour(security("CL1!", timeframe.period, timestamp(year, month, dayofmonth, hour, minute, second), gaps=barmerge.gaps_on)) // UTC-4 Nymex
hour_es1 = hour(security("ES1!", timeframe.period, timestamp(year, month, dayofmonth, hour, minute, second), gaps=barmerge.gaps_on)) // UTC-5 CME

plot(time_cl1, "time_cl1", color.blue)
plot(time_es1, "time_es1", color.red)

plot(hour_cl1, "hour_cl1", color.blue)
plot(hour_es1, "hour_es1", color.red)



来源:https://stackoverflow.com/questions/62249159/timezone-convertion-between-tickers-part-3

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