Search code examples
pine-scriptalgorithmic-tradingtradingtradingview-apipine-script-v5

Excessive signals on exit trade (Indicator)


I have compiled this script (Indicator) and it keeps showing an error. The trade exit should correspond to the "SL" or "LongExit" level. "LongExit" works while "SL" does not issue a single signal per trade but issues the signal every time the price breaks the "SL". I tried several alternatives but none of them managed to eliminate this error.

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

// ------------------------------ ALGO SETTING SSL INDICATOR ---------------------------{

len     = input(title = 'Period', defval = 10)
smaHigh = ta.sma(high, len)
smaLow  = ta.sma(low, len)
Hlv     = int(na)
Hlv := close > smaHigh ? 1 : close < smaLow ? -1 : Hlv[1]
sslDown = Hlv < 0 ? smaHigh : smaLow
sslUp   = Hlv < 0 ? smaLow : smaHigh

plot(sslDown, linewidth = 2, color = color.new(color.red, 0))
plot(sslUp,   linewidth = 2, color = color.new(color.lime, 0))

// }

// -------------- LONG INDICATOR INPUT FOR 3COMMAS -----------------{

LongEntry = ta.crossover(sslUp,  sslDown) and barstate.isconfirmed
plotshape(LongEntry, color = color.yellow)
alertcondition(LongEntry, "BUY LONG", "message")

var int bar     = 0
var int count   = 0

if LongEntry
    bar := bar_index
    bar
count := bar_index - bar + 1

// ------------------ ALGO SETTING LONG EXIT ------------------- {

LongExit = ta.crossunder(sslUp, sslDown) and barstate.isconfirmed
alertcondition(LongExit, "LONG CLOSE REVERSE", "message")

StopLoss = input.float(defval = 0.10, title = "STOP LOSS VALUE", minval = 0.00)
distance = (sslDown * StopLoss) / 100
Value    = (sslDown - distance)

SL              = ta.valuewhen(LongEntry, Value, 0)
plot(SL, color  = color.white)

LongStopLoss    = ta.crossunder(close, SL)
Condition       = (LongExit or LongStopLoss)

alertcondition(LongStopLoss, "CLOSE TRADE", "message")
plotshape(Condition, color = color.blue)

// }

Solution

  • It issues the SL signal every time because you haven't told the script when and why it shouldn't trigger. Why not use the strategy script? Otherwise, you'll have to manually follow orders and manage them with your custom logic, while the built-in backtesting engine could do it instead.

    As a workaround, create a counter of SL break event and reset it on the entry condition, I've also added a pyramiding input to filter the amount of SL signals in a sequence. Red cross on the chart - SL event, white cross - SL event filtered with a counter and pyramiding:

    // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
    // © e2e4mfck
    
    //@version=5
    indicator("indicator() sl")
    
    LongEntry = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
    bgcolor(LongEntry ? color.new(color.red, 80) : na, title = 'EntrySignal / ResetSLCounter')
    LongStopLoss = close < ta.sma(close, 14)
    plotshape(LongStopLoss, color = color.red, title = 'LongStopLoss')
    
    var int LongStopLossCounter = 0
    if LongStopLoss
        LongStopLossCounter += 1
    else if LongEntry
        LongStopLossCounter := 0
    
    pyramid = input.int(1, 'Pyramiding', minval = 1)
    
    plotshape((LongStopLossCounter <= pyramid) and LongStopLoss, color = color.white, size = size.huge, title = 'LongStopLoss filtered')
    

    enter image description here