【问题标题】:Seemingly wrong evaluation of simple condition inside a function对函数内部简单条件的看似错误的评估
【发布时间】:2019-08-15 16:58:14
【问题描述】:

我想要一个通常返回整数 100 的函数,但在满足某些条件时会偏离该值,在我的情况下,如果给定的振荡器 (rsi) 低于 20。如果当时的收盘价是接近 100 天的最低价格低于最高价格,回报值将升至 100 以上,如果接近最高价格,则跌破。

但奇怪的是,在大多数情况下(但不是全部),返回的值的行为完全相反。

我已经在以下两个屏幕中绘制了所有相关值(绿色 > 50 表示更接近最高

蓝绿色线错落画面

如您所见,背景颜色为绿色,因此表达式 rsi <= 20 and closeRelativeToHighestLowest() <= 50 在 bgcolor() 函数内被正确计算。

蓝绿色线正确上升的画面

这里的背景也是绿色的,所以在比较的评估中没有什么不同 - 至少在 bgcolor() 函数中。

这是我的代码:

//@version=4
study("My lookback minimum example")

closeRelativeToHighestLowest() =>
    ret = (close - lowest(low,100)) * 100 / (highest(high,100) - lowest(low,100))
    ret

myOptimalLookback(oscillator) => // will increase currentLength if oscillator is low and above optimalLevelForLow (relative to highest/lowest)
    ret = 100
    if oscillator <= 20
        if (closeRelativeToHighestLowest() > 50) //same condition as outside of function
            ret := round(ret[1] * 0.9)
        else
            ret := round(ret[1] * 1.12)
    ret := max(min(ret, 200), 10) // cap ret at 10 and 200


rsi = rsi(close, 10)
var lookback = 100
lookback := myOptimalLookback(rsi)

plot(lookback)
plot(closeRelativeToHighestLowest(), color = color.green)
plot(rsi, color = color.red)
hline(20)
hline(50)
bgcolor(rsi <= 20 and closeRelativeToHighestLowest() > 50 ? color.red : na)
bgcolor(rsi <= 20 and closeRelativeToHighestLowest() <= 50 ? color.green : na)

有什么可以尝试缩小问题范围的想法吗?

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    条件分支的评估在 v4 中发生了变化。与以前的版本相反,所有分支都不再被评估,因此在进入条件分支之前预先评估函数调用更安全(并且通常是必要的)。见Release Notes:

    myOptimalLookback(oscillator) => // will increase currentLength if oscillator is low and above optimalLevelForLow (relative to highest/lowest)
        ret = 100
        c = closeRelativeToHighestLowest()
        if oscillator <= 20
            if (c > 50) //same condition as outside of function
                ret := round(ret[1] * 0.9)
            else
                ret := round(ret[1] * 1.12)
        ret := max(min(ret, 200), 10) // cap ret at 10 and 200
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多