OPEN-SOURCE SCRIPT
Color Test

You sent
//version=5
indicator("EZ Buy/Sell with Auto SL & TP (final typed version)", overlay=true)
// --- Signals (50/200 MA) ---
fastMA = ta.sma(close, 50)
slowMA = ta.sma(close, 200)
buySignal = barstate.isconfirmed and ta.crossover(fastMA, slowMA)
sellSignal = barstate.isconfirmed and ta.crossunder(fastMA, slowMA)
// --- Risk settings ---
method = input.string("ATR", "Stop Method", options=["ATR","Percent"])
atrLen = input.int(14, "ATR Length", minval=1)
atrMult = input.float(1.5, "Stop = ATR x", step=0.1, minval=0.1)
pctStop = input.float(2.0, "Stop = % (if Percent)", step=0.1, minval=0.1)
tpR1 = input.float(1.0, "TP1 (R multiples)", step=0.25, minval=0.25)
tpR2 = input.float(2.0, "TP2 (R multiples)", step=0.25, minval=0.25)
moveToBE = input.bool(true, "Move Stop to Breakeven after TP1?")
// --- Compute risk unit R ---
atr = ta.atr(atrLen)
riskR = method == "ATR" ? atr * atrMult : (close * (pctStop / 100.0))
// --- Candle color (typed to avoid NA assignment issue) ---
color candleColor = na
candleColor := buySignal ? color.new(color.green, 0) :
sellSignal ? color.new(color.red, 0) :
color.na
barcolor(candleColor)
// --- Labels ---
if buySignal
label.new(bar_index, low, "BUY", style=label.style_label_up, color=color.new(color.green, 0), textcolor=color.white, size=size.large)
if sellSignal
label.new(bar_index, high, "SELL", style=label.style_label_down, color=color.new(color.red, 0), textcolor=color.white, size=size.large)
// --- Bias background (last signal) ---
var int bias = 0 // 1=long, -1=short
if buySignal
bias := 1
else if sellSignal
bias := -1
bgcolor(bias == 1 ? color.new(color.green, 88) : bias == -1 ? color.new(color.red, 88) : na)
// --- State & levels (explicitly typed) ---
var bool inLong = false
var bool inShort = false
var float entryPrice = na
var float longSL = na, longTP1 = na, longTP2 = na
var float shortSL = na, shortTP1 = na, shortTP2 = na
var bool longTP1Hit = false, shortTP1Hit = false
newLong = buySignal
newShort = sellSignal
// --- Long entry ---
if newLong
inLong := true
inShort := false
entryPrice := close
longTP1Hit := false
float r = method == "ATR" ? atr * atrMult : (entryPrice * (pctStop / 100.0))
longSL := entryPrice - r
longTP1 := entryPrice + tpR1 * r
longTP2 := entryPrice + tpR2 * r
// clear short levels
shortSL := na
shortTP1 := na
shortTP2 := na
shortTP1Hit := false
// --- Short entry ---
if newShort
inShort := true
inLong := false
entryPrice := close
shortTP1Hit := false
float r2 = method == "ATR" ? atr * atrMult : (entryPrice * (pctStop / 100.0))
shortSL := entryPrice + r2
shortTP1 := entryPrice - tpR1 * r2
shortTP2 := entryPrice - tpR2 * r2
// clear longs
longSL := na
longTP1 := na
longTP2 := na
longTP1Hit := false
// --- Move stop to BE after TP1 ---
if inLong and not na(longSL)
if not longTP1Hit and ta.crossover(close, longTP1)
longTP1Hit := true
if moveToBE and longTP1Hit
longSL := math.max(longSL, entryPrice)
if inShort and not na(shortSL)
if not shortTP1Hit and ta.crossunder(close, shortTP1)
shortTP1Hit := true
if moveToBE and shortTP1Hit
shortSL := math.min(shortSL, entryPrice)
// --- Exit detections ---
bool longStopHit = inLong and not na(longSL) and ta.crossunder(close, longSL)
bool longTP1HitNow = inLong and not na(longTP1) and ta.crossover(close, longTP1)
bool longTP2Hit = inLong and not na(longTP2) and ta.crossover(close, longTP2)
bool shortStopHit = inShort and not na(shortSL) and ta.crossover(close, shortSL)
bool shortTP1HitNow = inShort and not na(shortTP1) and ta.crossunder(close, shortTP1)
bool shortTP2Hit = inShort and not na(shortTP2) and ta.crossunder(close, shortTP2)
// --- Auto-flat after SL or TP2 ---
if longStopHit or longTP2Hit
inLong := false
if shortStopHit or shortTP2Hit
inShort := false
// --- Draw levels ---
plot(inLong ? longSL : na, title="Long SL", color=color.new(color.red, 0), linewidth=2, style=plot.style_linebr)
plot(inLong ? longTP1 : na, title="Long TP1", color=color.new(color.green, 0), style=plot.style_linebr)
plot(inLong ? longTP2 : na, title="Long TP2", color=color.new(color.green, 0), style=plot.style_linebr)
plot(inShort ? shortSL : na, title="Short SL", color=color.new(color.red, 0), linewidth=2, style=plot.style_linebr)
plot(inShort ? shortTP1 : na, title="Short TP1", color=color.new(color.green, 0), style=plot.style_linebr)
plot(inShort ? shortTP2 : na, title="Short TP2", color=color.new(color.green, 0), style=plot.style_linebr)
// --- Alerts ---
alertcondition(buySignal, title="BUY Signal", message="BUY: Candle green. SL/TP drawn.")
alertcondition(sellSignal, title="SELL Signal", message="SELL: Candle red. SL/TP drawn.")
alertcondition(longStopHit, title="Long STOP Hit", message="Long STOP hit.")
alertcondition(longTP1HitNow, title="Long TP1 Hit", message="Long TP1 reached.")
alertcondition(longTP2Hit, title="Long TP2 Hit", message="Long TP2 reached.")
alertcondition(shortStopHit, title="Short STOP Hit", message="Short STOP hit.")
alertcondition(shortTP1HitNow, title="Short TP1 Hit", message="Short TP1 reached.")
alertcondition(shortTP2Hit, title="Short TP2 Hit", message="Short TP2 reached.")
// --- Optional MA context ---
plot(fastMA, color=color.new(color.green, 0), title="MA 50")
plot(slowMA, color=color.new(color.red, 0), title="MA 200")
Script de código abierto
Siguiendo fielmente el espíritu de TradingView, el creador de este script lo ha publicado en código abierto, permitiendo que otros traders puedan revisar y verificar su funcionalidad. ¡Enhorabuena al autor! Puede utilizarlo de forma gratuita, pero tenga en cuenta que la publicación de este código está sujeta a nuestras Normas internas.
Exención de responsabilidad
La información y las publicaciones que ofrecemos, no implican ni constituyen un asesoramiento financiero, ni de inversión, trading o cualquier otro tipo de consejo o recomendación emitida o respaldada por TradingView. Puede obtener información adicional en las Condiciones de uso.
Script de código abierto
Siguiendo fielmente el espíritu de TradingView, el creador de este script lo ha publicado en código abierto, permitiendo que otros traders puedan revisar y verificar su funcionalidad. ¡Enhorabuena al autor! Puede utilizarlo de forma gratuita, pero tenga en cuenta que la publicación de este código está sujeta a nuestras Normas internas.
Exención de responsabilidad
La información y las publicaciones que ofrecemos, no implican ni constituyen un asesoramiento financiero, ni de inversión, trading o cualquier otro tipo de consejo o recomendación emitida o respaldada por TradingView. Puede obtener información adicional en las Condiciones de uso.