OPEN-SOURCE SCRIPT

3-Day Breakout Strategy with Trend Change Exit

153
//version=5
strategy("3-Day Breakout Strategy with Trend Change Exit", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

// === Calculate 3-day high/low (excluding current bar) ===
high3 = ta.highest(high[1], 3)
low3 = ta.lowest(low[1], 3)

// === Entry conditions ===
longEntry = close > high3
shortEntry = close < low3

// === Track position state ===
isLong = strategy.position_size > 0
isShort = strategy.position_size < 0
wasLong = nz(strategy.position_size[1] > 0)
wasShort = nz(strategy.position_size[1] < 0)

// === Exit conditions ===
// Exit on trend reversal (new signal)
longExit = shortEntry // Exit long position when a short signal occurs
shortExit = longEntry // Exit short position when a long signal occurs

// === Execute entries ===
buySignal = longEntry and not isLong and not isShort
sellSignal = shortEntry and not isLong and not isShort

if (buySignal)
strategy.entry("Long", strategy.long)
if (sellSignal)
strategy.entry("Short", strategy.short)

// === Execute exits on opposite signal (trend change) ===
if (isLong and longExit)
strategy.close("Long")
if (isShort and shortExit)
strategy.close("Short")

// === Exit markers (on actual exit bar only) ===
exitLongSignal = wasLong and not isLong
exitShortSignal = wasShort and not isShort

// === Plot entry signals only on the entry bar ===
plotshape(buySignal, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(sellSignal, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")

// === Plot exit signals only on the exit bar ===
plotshape(exitLongSignal, title="Exit Long", location=location.abovebar, color=color.orange, style=shape.labeldown, text="EXIT")
plotshape(exitShortSignal, title="Exit Short", location=location.belowbar, color=color.orange, style=shape.labelup, text="EXIT")

Declinazione di responsabilità

Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.