首页 > 解决方案 > UNIX 中每个烛台的持续时间

问题描述

我们如何计算 UNIX 中每个蜡烛(不是 Session)的持续时间?例如,每日时间范围为 86400000。

我使用了以下代码,它适用于每周 7 天、每天 24 小时活跃的加密货币市场,但在股票市场中它返回会话时间:

interval := na(interval) ? time_close - time : interval

标签: pine-script

解决方案


您不需要在每个柱上都进行计算。你可以用这个:

var int interval = time_close - time

它适用于加密市场和股票市场,并返回当前时间范围内柱的持续时间(以毫秒为单位)。

编辑 1

我认为这会更好

var float interval = na

if bar_index == 1
    interval := time - time[1]

编辑 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)

资料来源:时间偏移计算框架 - PineCoders 常见问题解答


推荐阅读