Search code examples
pine-scriptpine-script-v5

Detecting ONLY Bar close Above Ma cross Candle


I use ma crossover in trading ( ma9, ma21), I look at the condle when the crossover occur.

Then I wait for another candle that CLOSE above the high of that candle.

I want to detect ONLY the first bar that CLOSE above the candle in the crossover.

I tried different coding scripts but plot many times (not only the fisrt candle as I hope)

//@version=5
indicator(title="MA Cross", overlay=true, timeframe="", timeframe_gaps=true)

//inputs
shortlen = input.int(9, "Short MA Length", minval=1)
longlen = input.int(21, "Long MA Length", minval=1)

//Calculations
short = ta.sma(close, shortlen)
long = ta.sma(close, longlen)

//Plot
plot(long, color = #43A047, title="Long MA")

Possitive = ta.crossover  (short,long)

plotshape(Possitive, "+", style=shape.triangleup, location=location.belowbar, size=size.small, color=color.green)

// How to identify the candle high and low when the conditions met ?

var float h = na
if Possitive
    h := high

// How to plot only on the first bar that met condition ?

// conditions
buy_signal= ta.crossover(close, h)

// plots
plotshape(buy_signal , title="Buy",text="Buy", style=shape.circle,
 color=color.rgb(67, 224, 138), size= size.tiny, location=location.belowbar)

Another Code

//@version=5
indicator(title="MA Cross", overlay=true, timeframe="", timeframe_gaps=true)

//inputs
shortlen = input.int(9, "Short MA Length", minval=1)
longlen = input.int(21, "Long MA Length", minval=1)

//Calculations
short = ta.sma(close, shortlen)
long = ta.sma(close, longlen)

//Plot
plot(long, color = #43A047, title="Long MA")

Possitive = ta.crossover  (short,long)

plotshape(Possitive, "+", style=shape.triangleup, location=location.belowbar, size=size.small, color=color.green)

// How to identify the candle high and low when the conditions met ?

var float h = na
if Possitive
    h := high

// How to plot only on the first bar that met condition ?

// conditions
buy_signal= close > h

// plots
plotshape(buy_signal , title="Buy",text="Buy", style=shape.circle,
 color=color.rgb(67, 224, 138), size= size.tiny, location=location.belowbar)

I tried using Var but I was not capable to find a solution


Solution

  • You are right using a var to store the high price but you should also have a bool flag to see if you have already foudn the first candle or not.

    //@version=5
    indicator(title="MA Cross", overlay=true, timeframe="", timeframe_gaps=true)
    
    //inputs
    shortlen = input.int(9, "Short MA Length", minval=1)
    longlen = input.int(21, "Long MA Length", minval=1)
    
    //Calculations
    short = ta.sma(close, shortlen)
    long = ta.sma(close, longlen)
    
    //Plot
    plot(long, color = #43A047, title="Long MA")
    plot(short, color = color.red, title="Short MA")
    
    Possitive = ta.crossover  (short,long)
    
    plotshape(Possitive, "+", style=shape.triangleup, location=location.belowbar, size=size.small, color=color.green)
    
    type o_cross
        bool m_is_found = false
        float m_high = na
    
    var cross_obj = o_cross.new()
    
    if (Possitive)
        cross_obj.m_is_found := false   // Reset the flag
        cross_obj.m_high := high    // Store the high price
    
    buy_signal = false
    
    if (not cross_obj.m_is_found)   // If the first bar is not found yet
        if (close > cross_obj.m_high)
            buy_signal := true
            cross_obj.m_is_found := true
    
    // plots
    plot(not cross_obj.m_is_found ? cross_obj.m_high : na, "High", color.white, 1, plot.style_circles)
    plotshape(buy_signal , title="Buy",text="Buy", style=shape.circle, color=color.rgb(67, 224, 138), size= size.tiny, location=location.belowbar)
    

    enter image description here