Search code examples
pine-scripttrading

Take first sign pine script


I've been trying for the build sign in version 2. However this is getting executed in version 4. Can someone help me with this in version 2? Thank you so much

enter image description here

// Determine currently LONG or SHORT
isLong = nz(isLong[1], false)
isShort = nz(isShort[1], false)

// Buy or Sell only if the buy signal is triggered and not already long or short
buySignal = not isLong and buy
sellSignal = not isShort and sell

if (buySignal)
    isLong := true
    isShort := false

if (sellSignal)
    isLong := false
    isShort := true
    
plotshape(series=buySignal, text='Buy', style=shape.labelup, location=location.belowbar, offset=0, color=#009688, textcolor=#ffffff, size=size.small)

plotshape(series=sellSignal, text='Sell', style=shape.labeldown, location=location.abovebar, offset=0, color=#F44336, textcolor=#ffffff, size=size.small)

Solution

  • Upgrade your script to v5 and get rid of isShort, you don't need it.

    //@version=5
    indicator("My script", overlay=true)
    
    _sma = ta.sma(close, 9)
    
    buy = ta.crossover(close, _sma)
    sell = ta.crossunder(close, _sma)
    
    // Determine currently LONG or SHORT
    var isLong = false
    
    // Buy or Sell only if the buy signal is triggered and not already long or short
    buySignal = not isLong and buy
    sellSignal = isLong and sell
    
    if (buySignal)
        isLong := true
    
    if (sellSignal)
        isLong := false
    
    plot(_sma)
    plotshape(series=buySignal, text='Buy', style=shape.labelup, location=location.belowbar, offset=0, color=#009688, textcolor=#ffffff, size=size.small)
    plotshape(series=sellSignal, text='Sell', style=shape.labeldown, location=location.abovebar, offset=0, color=#F44336, textcolor=#ffffff, size=size.small)
    

    enter image description here