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