Reputation: 1437
I'm trying to get the proportion of time already passed at the current chart resolution of the current candle. For example, in the hourly timeframe, if the current remaining time on the candle is 45minutes, then I'd like to calculate "0.25" because 15 minutes of the full 60 minutes of the current candle has passed.
I've looked at the documentation https://www.tradingview.com/pine-script-docs/en/v4/essential/Sessions_and_time_functions.html for functions related to time, and can't find a way to do this.
Is there a variable I'm missing? It feels like this should be possible, indeed two of the three variables I would need already exist but the final one doesn't. The following shows how to get the remaining milliseconds of a candle, but no way (that I can find) to divide that by the current timeframe's total milliseconds.
timenow - time
But I want:
(timenow - time) / "timeframe"
Upvotes: 2
Views: 637
Reputation: 1437
I found a way to do it, it's not pretty, but it solves my particular issue;
getTimeframeMillis() =>
switch(timeframe.period)
"1" => 60000
"3" => 180000
"5" => 300000
"15" => 900000
"30" => 1800000
"60" => 3600000
"120" => 7200000
"240" => 14400000
"D" => 86400000
"W" => 604800000
"M" => 2419200000
I then use that like;
remainingPortion = (timenow - time) / getTimeframeMillis()
And I needed to make sure calc_on_every_tick = true
was included in the strategy annotation too so that the value updated every second instead of waiting til end of candle.
Upvotes: 3