0

I am drafting a script based on a donchian channel strategy. For buy/sell signals, I require that every time the price is higher than the upper/lower line, it generates a buy or sell signal. For exit signals, I am using ATR, however I would like the signal to appear only when a buy or sell signal is generated, and not when and only the ATR changes direction. To do this, I would need the script to understand that it should plot an exit signal, only when there was a previous buy or sell signal. I came up with the script below, but I couldn't stop it from repeating the signals. Can anyone help me? Thanks!

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SMTraderTradingCrypto

//@version=5
indicator(title="Donchian Channels Strategy", shorttitle="DC", overlay=true, timeframe="", timeframe_gaps=true)

//INPUTS

length = input.int(20, title = "DC Period", minval = 1)
offset = input.int(0, title = "DC Offset")
keyvalue = input(2, title = "ATR Length")
atrperiod = input(20, title = "ATR Period")
src = input(close, title = "ATR Source")

cns_len = 2

//DC FUNCTION

lower =  ta.lowest(length)
upper =  ta.highest(length)
basis =  math.avg(upper, lower)

//ATR FUNCTION

xATR = ta.atr(atrperiod)
nLoss = keyvalue * xATR

xATRTrailingStop = 0.0
iff_1 = src > nz(xATRTrailingStop[1], 0) ? src - nLoss : src + nLoss
iff_2 = src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0) ? math.min(nz(xATRTrailingStop[1]), src + nLoss) : iff_1
xATRTrailingStop := src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), src - nLoss) : iff_2

pos = 0
iff_3 = src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0) ? -1 : nz(pos[1], 0)
pos := src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0) ? 1 : iff_3

//BUY/SELL FUNCTION

cns_up = 0
cns_up := nz(cns_up[1])

cns_dwn = 0
cns_dwn := nz(cns_dwn[1])

isLong = false
isLong := nz(isLong[1])

isShort = false
isShort := nz(isShort[1])

cns_up := high >= upper ? cns_up + 1 : 0
cns_dwn := low <= lower ? cns_dwn + 1 : 0

buySignal = not isLong and (cns_up >= cns_len)
sellSignal = not isShort and (cns_dwn >= cns_len)

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

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

//EXIT SIGNAL

var lastSignal = 0

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

if (sellSignal)
    isLong := false
    isShort := true
    lastSignal := -1

//EXIT SIGNAL

Atrbuy = ta.crossover(src,xATRTrailingStop) and lastSignal == -1
Atrsell = ta.crossunder(src,xATRTrailingStop) and lastSignal == 1

plotshape(Atrbuy, title="Exit Signal", text="Exit", style=shape.triangleup, location=location.belowbar, color=color.red, size=size.small, offset=-1)
plotshape(Atrsell, title="Exit Signal", text="Exit", style=shape.triangledown, location=location.abovebar, color=color.green, size=size.small, offset=-1)

//COLORS

green = #00FF0A
red = #FF1100

xcolor = pos == -1 ? red : pos == 1 ? green : na

//PLOTS

u = plot(upper, "Upper", color = red, linewidth = 3, offset = offset)
l = plot(lower, "Lower", color = green, linewidth = 3, offset = offset)

plot(xATRTrailingStop, color=xcolor, title = "Trailing Stop")

plotshape(buySignal, title='Long Signal', text='Long', style=shape.labelup, location=location.belowbar, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), size=size.small, offset = -1)
plotshape(sellSignal, title='Short Signal', text='Short', style=shape.labeldown, location=location.abovebar, color=color.new(color.red, 0), textcolor=color.new(color.white, 0), size=size.small, offset = -1)

I have already managed to get an exit signal that is not in the direction I want to appear, but exit signals that are in the same direction keep appearing repeatedly, and I would like it to appear only after the sell or buy signal, and nothing else.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.