松脚本中的动态日期范围
Dynamic date range inside pine script
我无法在 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)
任何帮助将不胜感激。
好像是pine的不一致。如果准确性不是那么重要,我建议使用 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
returns 一个以毫秒为单位的值,而你试图用一个以秒为单位的值减去它:1549238400
而且我不完全理解您代码的逻辑,因为您要减去两个日期,然后将差值转换为新日期。对我来说,这毫无意义。但也许这只是 Whosebug 的一个例子,所以没关系
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 天前放一条线(如果当天没有任何柱,则什么都不放)并且必须在该日期或更近的日期放一条线到今天
- 也许您可以使用
round
函数将其转换为整数?
- 另外,删除
yr
之后的 [0]
等...
我无法在 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)
任何帮助将不胜感激。
好像是pine的不一致。如果准确性不是那么重要,我建议使用 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
returns 一个以毫秒为单位的值,而你试图用一个以秒为单位的值减去它:1549238400
而且我不完全理解您代码的逻辑,因为您要减去两个日期,然后将差值转换为新日期。对我来说,这毫无意义。但也许这只是 Whosebug 的一个例子,所以没关系
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 天前放一条线(如果当天没有任何柱,则什么都不放)并且必须在该日期或更近的日期放一条线到今天
- 也许您可以使用
round
函数将其转换为整数? - 另外,删除
yr
之后的[0]
等...