'How to plot different shapes based on the occurrences of 1 condition over the past x bars (PineScript, TradingView)

Would really appreciate some help please... I'm trying to plot different shapes depending on the occurrence of the same condition, over the past 50 bars

I have a condition and when this condition is true, I overlay plotshape() on the chart; this works fine. Summary of what I'm trying to achieve:

-Is NOT the 3rd instance of the condition/plotshape1 within the last 50 bars? Then continue to plot plotshape1

-Is the 3rd instance of the condition/plotshape1 within the last 50 bars? Then plot plotshape2 not plotshape1

One issue is that it will continuously plot above every candle until there is no longer 3 instances in 50 bars. I understand why its happening, but my attempt to fix makes more problems:

// "condition" is my input var

// running count of instances in last 50 bars
countcondition = math.sum(condition ? 1 : 0, 50)

// identify number of occurrences whether 3 or less
Three = countcondition > 2
NotThree = countcondition <= 2

// My attempt to stop the continuous printing above every candle
var Pos = 0
// save it to a new number when occurs
if Three and Pos <= 0
    Pos := 1

// assign var for plotshape - even if this worked it would plot both shapes on the 3rd occurrence
Plotshape1 = condition   // works as expected
Plotshape2 = Pos == 1 and (Pos != 1)[1]  // not working, plots are incorrect

plotshape(Plotshape1, title="1-2 times", style=shape.triangleup, location=location.belowbar, color=#00E676, size=size.small, text="Plot1")
plotshape(Plotshape2, title="3rd time", style=shape.triangleup, location=location.belowbar, color=#FF5252, size=size.small, text="Plot2")


Solution 1:[1]

Plotshape1 = NotThree
Plotshape2 = Three and not Three[1]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rumpypumpydumpy