PineCoders

Time Offset Calculation Framework - PineCoders FAQ

█ 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.
Phát hành các Ghi chú: Updated comments.
Mã nguồn mở

Với tinh thần của TradingView, tác giả đã xuất bản tập lệnh theo mã nguồn mở, vì thế trader có thể dễ dàng hiểu và tùy chỉnh được. Bạn có thể sử dụng miễn phí, hoặc tùy chỉnh lại mã đã được cấp phép bởi Quy tắc Chung. Bạn có thể sử dụng nó trên biểu đồ.

Bạn muốn sử dụng tập lệnh này trên biểu đồ?
Tools and ideas for all Pine coders: http://www.pinecoders.com
Our Pine FAQ & Code: http://www.pinecoders.com/faq_and_code/
Pine news broadcasts: https://t.me/PineCodersSquawkBox or https://twitter.com/PineCoders

Bình luận

Thx for sharing
+2 Phản hồi
Very useful, thanks for sharing ^^
+1 Phản hồi
Thank you very much!
+1 Phản hồi
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: https://www.pinecoders.com/faq_and_code/#how-can-i-implement-a-time-delay-between-orders
Phản hồi
excellent work P... thank you
Phản hồi
Line 66: _unit == "econd"
Phản hồi
PineCoders everget
@everget, That because we strip the "s" chars from the argument. That way "day" and "days" both work.
+1 Phản hồi
everget PineCoders
@PineCoders, ok, I believe you
+1 Phản hồi
great work, thank for sharing this :D
Phản hồi
Trang chủ Bộ lọc cổ phiếu Bộ lọc Forex Bộ lọc Tiền điện tử Lịch kinh tế Cách thức hoạt động Tính năng Biểu đồ Trả phí Giới thiệu bạn Quy tắc Áp dụng Trung tâm Trợ giúp Giải pháp cho Website & Nhà môi giới Widget Giải pháp biểu đồ Thư viện Biểu đồ Lightweight Blog & Tin tức Twitter
Hồ sơ Tùy chỉnh Hồ sơ Tài khoản và Thanh toán Giới thiệu bạn Xu Ticket Hỗ trợ của tôi Trung tâm Trợ giúp Tin nhắn riêng Trò chuyện Đăng xuất