【问题标题】:How to reset Pinescript valuewhen() after conditions are met?满足条件后如何重置 Pinescript valuewhen()?
【发布时间】:2022-01-03 21:29:34
【问题描述】:

我正在尝试创建一个脚本,当 SMMA 交叉发生并且之后绘制威廉姆斯分形时突出显示。

我正在使用 valuewhen() 来捕获交叉。当分形打印并且 RSI 高于/低于 50 时,我使用存储的 valuewhen() 来验证是否满足交叉条件并可以突出显示该背景。

问题是,我不知道在满足条件后如何重置 valuewhen()。

提供的屏幕截图显示了运行红色短裤和蓝色长头的脚本。白色背景条是发生交叉的地方,我只希望它旁边的红色条突出显示(在 Fractal 打印之后),它表示一个条目。

这是我第一次写 Pinescript,对于任何菜鸟的错误,我们深表歉意。

// @version=5
indicator(title = "3 SMMA Cross Strategy", shorttitle="3SMMA", overlay=true)

// smma
len1 = input.int(60, minval=1, title="Length")
src1 = input(close, title="Source")
_sma1 = ta.sma(src1, len1)
smma_21 = 0.0
smma_21 := na(smma_21[1]) ? _sma1 : (smma_21[1] * (len1 - 1) + src1) / len1

len2 = input.int(100, minval=1, title="Length")
src2 = input(close, title="Source")
_sma2 = ta.sma(src2, len2)
smma_50 = 0.0
smma_50 := na(smma_50[1]) ? _sma2 : (smma_50[1] * (len2 - 1) + src2) / len2

len3 = input.int(200, minval=1, title="Length")
src3 = input(close, title="Source")
_sma3 = ta.sma(src3, len3)
smma_200 = 0.0
smma_200 := na(smma_200[1]) ? _sma3 : (smma_200[1] * (len3 - 1) + src3) / len3

smma_cross_21_200 = ta.cross(smma_21,smma_200)
smma_cross_50_200 = ta.cross(smma_50,smma_200)

// rsi 
_rsi = ta.rsi(close, 14)

// williams fractals
n = input.int(title="Periods", defval=2, minval=2)

dnFractal = (high[n-2] < high[n]) and (high[n-1] < high[n]) and (high[n+1] < high[n]) and (high[n+2] < high[n])
upFractal = (low[n-2] > low[n]) and (low[n-1] > low[n]) and (low[n+1] > low[n]) and (low[n+2] > low[n])

// begin conditionals
check_shorts = ta.valuewhen((smma_cross_21_200 or smma_cross_50_200) and ((smma_200 > smma_50 and smma_21) and ( smma_50 > smma_21 )), close, 1)
check_longs = ta.valuewhen((smma_cross_21_200 or smma_cross_50_200) and ((smma_200 < smma_50 and smma_21) and ( smma_50 < smma_21 )), close, 1)

TRANSP = 90

bgcolor( dnFractal and check_shorts and _rsi < 50 ? color.new(color.red, TRANSP) : na )
bgcolor( upFractal and check_longs and _rsi < 50 ? color.new(color.blue, TRANSP) : na )

【问题讨论】:

    标签: pine-script trading


    【解决方案1】:
    _when(_cond, _update, _reset, _default)=>    // {
        varip _output                     = _default
        _output := _reset ? _default : _cond ? _update : _output
        _output 
    

    应该这样做..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      • 1970-01-01
      相关资源
      最近更新 更多