Reputation: 5
How can we calculate the duration of each candle (not Session) in UNIX? For example, 86400000 for daily time frame.
I used the following code and it works well for the crypto currency market which is active 24 hours a day and seven days a week, but in the stock market it returns the session time:
interval := na(interval) ? time_close - time : interval
Upvotes: 0
Views: 148
Reputation: 6905
You don't need to calculate this on every bar. You could just use this:
var int interval = time_close - time
It works in both crypto and stock markets, and returns the duration of a bar (in milliseconds) for the current timeframe.
Edit 1
This will work better I think
var float interval = na
if bar_index == 1
interval := time - time[1]
Edit 2
//@version=4
study("Time Offset Calculation Framework - PineCoders FAQ", "", true, max_lines_count = 10)
// ———————————————————— Functions.
// ————— Converts current chart resolution into a float minutes value.
f_resInMinutes() =>
_resInMinutes = timeframe.multiplier * (
timeframe.isseconds ? 1. / 60 :
timeframe.isminutes ? 1. :
timeframe.isdaily ? 60. * 24 :
timeframe.isweekly ? 60. * 24 * 7 :
timeframe.ismonthly ? 60. * 24 * 30.4375 : na)
var float resInMinutes = f_resInMinutes()
var float resInSeconds = resInMinutes * 60
var float resInMilliSeconds = resInSeconds * 1000
// Plot chart interval in minutes in Data Window.
plotchar(resInMinutes, "resInMinutes", "", location.top, size = size.tiny)
plotchar(resInSeconds, "resInSeconds", "", location.top, size = size.tiny)
plotchar(resInMilliSeconds, "resInMilliSeconds", "", location.top, size = size.tiny)
Source: Time Offset Calculation Framework - PineCoders FAQ
Upvotes: 1