松脚本中的动态日期范围

时间:2019-03-07 05:17:02

标签: finance pine-script tradingview-api

我无法从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)

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

似乎是松木的不一致性。如果准确性不是那么重要,我建议对时间戳使用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天前准确地放置一行(如果当天没有任何小节,则不行),并且必须在该日期或更接近今天的位置放置一条直线

答案 1 :(得分:0)

  • 也许您可以只使用round函数将其转换为整数?
  • 另外,在[0]等后面删除yr ...