【问题标题】:Pine scripting: how to find the price X days agoPine 脚本:如何查找 X 天前的价格
【发布时间】:2020-12-27 05:24:25
【问题描述】:
在 Pine Script 中,我如何根据某个天数找到价格?我已经尝试过这样的事情......
// Find the price 90 days ago
target = time - 90 * 60 * 60 * 24 * 1000
valuewhen(time < target, close, 1)
...然而time < target 似乎永远不会返回真值——大概是因为当前柱的时间不能同时在过去。也许valuewhen() 的设计初衷不是用于在每根柱上变化的动态值?
我是否需要使用循环,并扫描过去的每一个栏,直到找到我要查找的日期?
【问题讨论】:
标签:
date
time
pine-script
【解决方案1】:
也许有更好的方法,但我目前使用的解决方法是一个带有for 循环的函数,向后扫描直到找到合适的日期。这是我的功能:
priceXDaysAgo(numDays) =>
targetTimestamp = time - numDays*60*60*24*1000
// Declare a result variable with a "void" value
float result = if false
1
// We'll scan backwards through the preceding bars to find the first bar
// earlier than X days ago (it might be a little greater than X days if
// there was a break in trading: weekend, public holiday, etc.)
for i = 1 to 1000
if time[i] < targetTimestamp
result := close[i]
break
result
然后您可以在脚本中的任何位置调用该函数:
priceXDaysAgo(90)