代码之家  ›  专栏  ›  技术社区  ›  Simon E. pbs

松树脚本:如何找到X天前的价格

  •  0
  • Simon E. pbs  · 技术社区  · 4 年前

    在Pine脚本中,如何根据前几天的某个数字找到价格?我试过这样的方法。。。

    // Find the price 90 days ago
    target = time - 90 * 60 * 60 * 24 * 1000
    valuewhen(time < target, close, 1)
    

    …但是 time < target 似乎永远不会返回真的大概是因为当前酒吧的时间不能同时也在过去。也许 valuewhen() 不是被设计用来处理每一个条上都会变化的动态值吗?

    1 回复  |  直到 4 年前
        1
  •  0
  •   Simon E. pbs    4 年前

    也许有更好的方法,但我目前使用的解决方法是一个带有 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)