Search code examples
pine-scriptpine-script-v5tradingtradingview-apitrend

Plotting a shape once when price is above the Kumo Cloud


Trying to plot an arrow up shape once whenever price is above the cloud. This plotted shape shouldn't be repeated until we have another price above cloud event.

Code so far:

//@version=5
indicator(title='My Ichimoku Cloud', shorttitle='Ichimoku', overlay=true)
conversionPeriods = input.int(9, minval=1, title='Conversion Line Periods')
basePeriods = input.int(26, minval=1, title='Base Line Periods')
laggingSpan2Periods = input.int(52, minval=1, title='Lagging Span 2 Periods')
displacement = input.int(26, minval=1, title='Displacement')

donchian(len) =>
    math.avg(ta.lowest(len), ta.highest(len))

conversionLine = donchian(conversionPeriods)
baseLine = donchian(basePeriods)
leadLine1 = math.avg(conversionLine, baseLine)
leadLine2 = donchian(laggingSpan2Periods)
plot(conversionLine, color=color.new(#00e5ff, 0), title='Conversion Line')
plot(baseLine, color=color.new(color.orange, 0), title='Base Line')
plot(close, offset=-displacement + 1, color=color.new(#459915, 0), title='Lagging Span', display=display.none)
p1 = plot(leadLine1, offset=displacement - 1, color=color.new(color.green, 0), title='Lead 1', display=display.none)
p2 = plot(leadLine2, offset=displacement - 1, color=color.new(color.red, 0), title='Lead 2', display=display.none)
fill(p1, p2, color=leadLine1 > leadLine2 ? color.new(color.green, 90) : color.new(color.red, 90))

topOfCloudLine = float(na)
priceAboveCloud = bool(na)
if leadLine1 > leadLine2
    topOfCloudLine := leadLine1
else
    if leadLine1 < leadLine2
        topOfCloudLine := leadLine2

if close > topOfCloudLine
    priceAboveCloud := true

plotshape(priceAboveCloud, style=shape.arrowup, location=location.abovebar, color=color.new(color.green, 0), size=size.tiny, offset = displacement + 1)

Current Output: enter image description here


Solution

  • You are looking for ta.crossover() and ta.crossunder() functions. Those will return true only once there is a cross. So you can do something like this:

    price_crossed_top = ta.crossover(close, topOfCloudLine)
    

    And then use price_crossed_top as a condition to draw your shape.