TradingView
PineCoders
14 de Dic. de 2020 10:38

Time Offset Calculation Framework - PineCoders FAQ 

GOLD / U.S. DOLLARICE

Descripción

█ OVERVIEW

Calculating time-based offsets is necessary when coders need to draw lines or labels into the future because using `xloc = xloc.bar_time` in `label.new()` or `line.new()` is then mandatory.
This script provides a function to help with those calculations:

f_timeFrom(_from, _qty, _units)


The function calculates a negative (into the past) or positive (into the future) offset from the current bar's starting or closing time, or from the current time of day.
The offset can be expressed in units of chart resolution, or in seconds, minutes, hours, days, months or years.


█ HOW TO USE THE FRAMEWORK

1. You will need to include the supplied `f_resInMinutes()` function in your script in order to use `f_timeFrom()`.
  It is used to calculate offsets using chart units when `f_timeFrom(_, _, "chart")` is used.
2. Whether you use `f_timeFrom()` for labels or lines, remember to use `xloc = xloc.bar_time`, as the default is `xloc = xloc.bar_index`.
3. Use `f_timeFrom()` for the `x` argument in `label.new()`, or for `x1` or `x2` in `line.new()`.
  It can of course also be used in the relevant `label.set_*()` or `line.set_*()` functions.

Examples
// Label 3 days into the future from current bar's time. label.new(f_timeFrom("bar", 3, "days"), high, "time + 3 days", xloc.bar_time) // Label 2 hours into the future from current time label.new(f_timeFrom("now", 2, "hours"), high, "timenow + 3 hours", xloc.bar_time) // Label at bar's time plus 4 units of the chart's resolution. label.new(f_timeFrom("bar", 4, "chart"), high, "time + 3 chart units", xloc.bar_time)


The parameters are:
f_timeFrom(_from, _qty, _units) => // _from : starting time from where the offset is calculated: "bar" to start from the bar's starting time, "close" to start from the bar's closing time, "now" to start from the current time. // _qty : the +/- qty of _units of offset required. A "series float" can be used but it will be cast to a "series int". // _units : string containing one of the seven allowed time units: "chart" (chart's resolution), "seconds", "minutes", "hours", "days", "months", "years".



█ LIMITATIONS

While this function makes it easier for coders to calculate time offsets using a variety of methods, it does not solve the inherent problematic that offsets do not calculate accurately when bars are missing between the start and end times of the offset. There is currently no way to circumvent this challenge in Pine.

Missing bars will occur on holidays, during no-trade periods (including normal periods where markets are closed) and when there are irregularities in data feeds. Charts at seconds resolutions, for example, will often miss bars when there are no trades to update the feed. On hourly charts of non 24x7 markets, periods when the markets are closed will also cause irregularities, as will holidays on day charts.

Other irregularities can occur because of how the offsets are calculated. A calculation of a one second offset from the bar's time will end one bar further on daily charts, for example. `f_timeFrom()` is no panacea; it simply makes offsets easier to calculate, however imprecise they are.


█ HOW TO USE THIS SCRIPT

The script's Inputs allow you to specify an offset, its units and starting time, and control the frequency of bars where lines are drawn.

Use the Inputs to play around with the parameters; you will quickly notice the irregularities mentioned above and be able to judge the usefulness of time-based offsets on the type of chart you use.



Look first. Then leap.

Notas de prensa

Updated comments.
Comentarios
seoco
Thx for sharing
Pratik_4Clover
Very useful, thanks for sharing ^^
fikira
Thank you very much!
slimer0
If I wanted to get a function like f_bar_index_to_time(x),
I should use those parameters below, right?

f_timeFrom("bar", x - last_bar_index, "chart")
FadiD86
How would you add an offset to an indicator? For example, I want to add an offset period to the rate of change indicator. That is not available in the indicator settings.
franzb0
Thank you very much, best for 24x7 time markets, right?
PineCoders
@Francesco_Marzolo, Yes.
PineCoders
The functions in this script can be used to calculate time delays for any use case. As an example, this is a FAQ entry showing how to implement a time delay between the executions of orders in a strategy: pinecoders.com/faq_and_code/
idrisbengali
excellent work P... thank you
everget
Line 66: _unit == "econd"
Más