如何在PineScript中判断K线收盘价时间是否为UTC上午8点
Hey there! Let's walk through how to solve this for your strategy—super straightforward once you know the right functions to use.
First: Is time_close dynamic?
Great question! The short answer is yes. time_close updates for every single bar, returning the Unix timestamp of that specific bar's close time (in UTC by default). It's exactly what you need for per-bar time checks.
Extracting the Time of Day from the Unix Timestamp
Pine Script has built-in functions to pull hour, minute, and second values from a timestamp—and crucially, you can specify the timezone to ensure you're working with UTC, not your chart's local timezone.
Here's how to get each component:
- Use
hour(time_close, "UTC")to get the hour (0-23) in UTC - Use
minute(time_close, "UTC")to get the minute (0-59) in UTC - Use
second(time_close, "UTC")to get the second (0-59) in UTC
Putting It All Together: Check for UTC 8 AM Close
To verify a bar closes exactly at 8:00:00 UTC, you'll check that the UTC hour is 8, and both minutes and seconds are 0. Here's a complete example script to demonstrate:
//@version=5 strategy("UTC 8 AM Close Check", overlay=true, default_qty_type=strategy.cash, default_qty_value=1000) // Define our condition: bar closes at exactly 8:00:00 UTC isUTC8AMClose = hour(time_close, "UTC") == 8 and minute(time_close, "UTC") == 0 and second(time_close, "UTC") == 0 // Visualize the bars that meet the condition (green label below the bar) plotshape(isUTC8AMClose, title="UTC 8 AM Close", location=location.belowbar, color=color.new(color.green, 0), style=shape.labelup, text="UTC 8AM", textcolor=color.white, size=size.small) // Example strategy action: Enter a long position when the condition is met if (isUTC8AMClose) strategy.entry("Long Position", strategy.long)
A quick note on timezones
It's critical to add the "UTC" parameter to the hour(), minute(), and second() functions. If you skip it, Pine Script will use your chart's configured timezone instead, which could lead to incorrect checks if your chart isn't set to UTC.
Adjusting for different bar periods
If you're working with larger bar periods (like 1-hour or daily), the close time will naturally land on whole hours (or specific daily times), so checking just the hour might be enough—but including minute and second checks makes your condition robust for any bar period (even 1-minute bars).
内容的提问来源于stack exchange,提问作者dorien

