满足两个条件后绘制形状,但在 100 根柱后满足之前不要重新绘制

Plot shape once two conditions are met but don't repaint until it is met after 100 bars

我想在满足两个单独的条件后绘制一个形状,并且在 100 根柱线后再次满足该条件之前不再重新绘制。到目前为止,我一直在玩以下游戏;

    var bool bull1=na, bool bull2=na, var a_thisBar=1, var a_barCount=bar_index-a_thisBar, var b_thisBar=1, var b_barCount=bar_index-b_thisBar

    a4K:=sma(stoch(close,high,low,8),3)
    a5K:=sma(stoch(close,high,low,32),12)

    if a5K>50
         bull1:=true,a_thisBar:=bar_index
    if bull1 and a_barCount<25 and crossover(a4K,a4D)
         bull2:=true,bull1:=false

   plotshape(bull2,style=shape.arrowup,location=location.belowbar,color=color.blue,size=size.small)

Atm 这工作正常,但可以理解的是,每次满足两个条件时它都会绘制一个形状。我希望它做的是在第一个实例上绘制它,然后在第一个实例后至少 100 个柱满足两个条件之前不再绘制它。

我试过以下变体,但没有成功;

    if a5K>50
        bull1:=true,a_thisBar:=bar_index
    if bull1 and a_barCount<25 and crossover(a4K,a4D) and b_barCount>100
        bull2:=true,bull1:=false,b_thisBar:=bar_index

有一个变量告诉您​​是否可以开始计数(当您的购买条件变为 true)。

在您的买入条件变为 true.

后,使用另一个变量来计算柱数

然后等待您的买入条件再次变为true,并检查您的计数器值。当它是一个有效的买入信号时,重置您的计数器。

下面是一个使用 crossunder(RSI, 50) 事件进行购买的示例。出于演示目的,我的例子中的计数器设置为 15。当交叉发生时,我绘制了 X 标记。

//@version=5
indicator("My script", overlay=true)

var cnt = 0
var startCounting = false

_rsi = ta.rsi(close, 14)
_buy_condition = ta.crossunder(_rsi, 50)

startCounting := _buy_condition ? true : startCounting  // Start counting if the buy condition is true, keep the old value otherwise
cnt := startCounting ? cnt + 1 : cnt    // INcrement the counter if startCounting is true, keep the old value otherwise

canBuy = _buy_condition and cnt > 15
cnt := canBuy ? 0 : cnt     // If there is a buy signal, reset the counter for next buy signal

plotshape(_buy_condition, size=size.small)
plotshape(canBuy, style=shape.triangleup, location=location.belowbar, color=color.green, text="Buy", size=size.small)