【问题标题】:Dynamic date range inside pine script松树脚本内的动态日期范围
【发布时间】:2019-07-28 21:53:28
【问题描述】:

我无法从今天的 pine 脚本中获取回溯日期。我已经定义了从当前时间减去 UNIX 时间戳的函数。但以下代码导致错误为"Timestamp requires integer parameter than series parameter"

getdate() =>
    tt = timenow - 1549238400
    yr = year(tt)
    mt = month(tt)
    dt = dayofmonth(tt)
    timestamp(yr[0], mt[0], dt[0], 0 ,0)

任何帮助将不胜感激。

【问题讨论】:

    标签: finance pine-script


    【解决方案1】:
    • 也许您可以使用round 函数将其转换为整数?
    • 另外,在yr 等之后删除[0]...

    【讨论】:

      【解决方案2】:

      似乎是松树的不一致。如果准确性不是那么重要,我建议使用 selfwriten 函数作为时间戳:

      //@version=3
      study("Timestamp")
      MILLISECONDS_IN_DAY = 86400000
      TIMESTAMP_BEGIN_YEAR = 1970
      
      myTimestamp(y, m, d) =>
          years = y - TIMESTAMP_BEGIN_YEAR
          years * MILLISECONDS_IN_DAY * 365.25 + (m - 1) * 30 * MILLISECONDS_IN_DAY + (d - 1) * MILLISECONDS_IN_DAY
      
      
      
      // TEST:
      tmspm = myTimestamp(2019, 3, 5)
      
      y = year(tmspm)
      m = month(tmspm)
      d = dayofmonth(tmspm)
      
      plot(y, color=green)
      plot(m, color=red)
      plot(d, color=maroon)
      

      顺便说一句,timenow 返回一个以毫秒为单位的值,而您试图将其减去一个以秒为单位的值:1549238400

      而且我并不完全理解您的代码的逻辑,因为您要减去两个日期,然后将该差异转换为新日期。对我来说这毫无意义。但也许这只是 stackoverflow 的一个例子,所以没关系

      UPD:您的代码不起作用,因为您将 timenow 减去 1549238400,但 29 天前的毫秒数是 2505600000。 我希望下一个代码会有所帮助:

      //@version=3
      study("My Script")
      
      _MILLISECONDS_IN_DAY = 86400000
      _29_DAYS_MILLIS = 29 * _MILLISECONDS_IN_DAY
      
      reqDate = timenow - _29_DAYS_MILLIS
      reqYear = year(reqDate)
      reqMonth = month(reqDate)
      reqDay = dayofmonth(reqDate)
      
      linePlotted = false
      linePlotted := nz(linePlotted[1], false)
      
      vertLine = na
      col = color(red, 100)
      
      //this puts a line exactlty 29 day ago or nothing if there wasn't a trading day at the date. If you want to put a line 29 days ago or closer, then:
      // if year >= reqYear and month >= reqMonth and dayofmonth >= reqDay and not linePlotted
      if year == reqYear and month == reqMonth and dayofmonth == reqDay and not linePlotted
          linePlotted := true
          vertLine := 1000
          col := color(red, 0)
      
      plot(vertLine, style=histogram, color=col)
      

      请注意,有两种可能的条件取决于您的需要:在 29 天前准确放置一条线(如果当天没有任何条形,则不放置),并且必须在该日期或更接近今天的日期放置一条线

      【讨论】:

      • 感谢@Michel_T 的回答。我正在尝试在特定日期相对于当前日期绘制一条线。为了。例如从今天起 29 天前画线。为此,我有 timenow 函数并从中减去 29 天毫秒。我期待新的价值'会给我第 29 天的时间戳。可能是我做错了什么
      • 我想你的代码有错误。我添加了一个 29 天前放置垂直线的脚本。希望对您有所帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      相关资源
      最近更新 更多