首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >松脚本中的动态日期范围

松脚本中的动态日期范围
EN

Stack Overflow用户
提问于 2019-03-07 05:17:02
回答 2查看 3.7K关注 0票数 1

从今天起,我无法在松树脚本中得到回溯。我已经将函数定义为减去当前时间的UNIX时间戳。但是,下面的代码作为"Timestamp requires integer parameter than series parameter"导致了错误

代码语言:javascript
复制
getdate() =>
    tt = timenow - 1549238400
    yr = year(tt)
    mt = month(tt)
    dt = dayofmonth(tt)
    timestamp(yr[0], mt[0], dt[0], 0 ,0)

任何帮助都将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-07 15:02:42

好像是松树的不一致。如果准确性不那么重要,我建议在时间戳中使用自写函数:

代码语言:javascript
复制
//@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

我并不完全理解你的代码的逻辑,因为你要减去两个日期,然后把这个差异转换成一个新的日期。对我来说没有意义。但是也许这只是堆栈溢出的一个例子,所以没关系

UPD:您的代码不能工作,因为您将时间减去1549238400,但是29天前毫秒是2505600000。我希望下一段代码会有所帮助:

代码语言:javascript
复制
//@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
EN

Stack Overflow用户

发布于 2019-09-20 23:19:15

  • 也许您可以使用round函数将其转换为整数?
  • 同时,去除[0]后的yr等。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55036549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档