INVITE-ONLY SCRIPT
Atualizado

SuperTrend AI Strategy with EMA Crossover and Trend Following

68
//version=5
strategy("SuperTrend AI Strategy with EMA Crossover and Trend Following", overlay=true)

// === Inputs ===
length = input.int(10, 'ATR Length')
minMult = input.int(1, 'Min Factor')
maxMult = input.int(5, 'Max Factor')
step = input.float(0.5, 'Factor Step')
perfAlpha = input.float(10, 'Performance Memory')

// EMA Settings
shortEmaLength = input.int(9, "Short EMA Length")
longEmaLength = input.int(21, "Long EMA Length")

// === ATR Calculation ===
atr = ta.atr(length)

// === Supertrend Optimization Setup ===
type supertrend
float upper
float lower
float output
float perf
float factor
int trend

var factors = array.new<float>()
var supertrends = array.new<supertrend>()

if bar_index == 0
for i = 0 to int((maxMult - minMult) / step)
factor = minMult + i * step
array.push(factors, factor)
array.push(supertrends, supertrend.new(hl2, hl2, na, 0, factor, 0))

// === Calculate all Supertrends ===
for i = 0 to array.size(factors) - 1
factor = array.get(factors, i)
st = array.get(supertrends, i)

up = hl2 + atr * factor
dn = hl2 - atr * factor

trend = close > st.upper ? 1 : close < st.lower ? 0 : st.trend
upper = close[1] < st.upper ? math.min(up, st.upper) : up
lower = close[1] > st.lower ? math.max(dn, st.lower) : dn
output = trend == 1 ? lower : upper
diff = nz(math.sign(close[1] - st.output))
perf = st.perf + 2 / (perfAlpha + 1) * (nz(close - close[1]) * diff - st.perf)

// Update values
st.upper := upper
st.lower := lower
st.output := output
st.trend := trend
st.perf := perf

array.set(supertrends, i, st)

// === Choose Best Performing Supertrend ===
var float bestPerf = na
var float bestOutput = na
var int bestTrend = na

for i = 0 to array.size(supertrends) - 1
st = array.get(supertrends, i)
if na(bestPerf) or st.perf > bestPerf
bestPerf := st.perf
bestOutput := st.output
bestTrend := st.trend

// === EMA Crossover Logic ===
shortEma = ta.ema(close, shortEmaLength) // Green EMA (9)
longEma = ta.ema(close, longEmaLength) // Red EMA (21)

// Check if 9 EMA crosses above 21 EMA (bullish crossover)
emaCrossOver = ta.crossover(shortEma, longEma)
// Check if 9 EMA crosses below 21 EMA (bearish crossover)
emaCrossUnder = ta.crossunder(shortEma, longEma)

// === Strategy Logic ===
var int lastTrend = na
var bool inLong = na
var bool inShort = na

// Buy Condition: Green EMA crosses Red EMA and SuperTrend is Green
if emaCrossOver and bestTrend == 1
strategy.close("Short")
strategy.entry("Long", strategy.long)
inLong := true

// Sell Condition: Red EMA crosses Green EMA and SuperTrend is Red
if emaCrossUnder and bestTrend == 0
strategy.close("Long")
strategy.entry("Short", strategy.short)
inShort := true

// Close Buy when SuperTrend turns Red
if inLong and bestTrend == 0
strategy.close("Long")
inLong := false

// Close Sell when SuperTrend turns Green
if inShort and bestTrend == 1
strategy.close("Short")
inShort := false

// === Plot Supertrend and EMAs ===
plot(bestOutput, title="Supertrend", color=bestTrend == 1 ? color.teal : color.red, linewidth=2)
plot(shortEma, title="9 EMA (Green)", color=color.green, linewidth=2)
plot(longEma, title="21 EMA (Red)", color=color.red, linewidth=2)
Notas de Lançamento
asd

Aviso legal

As informações e publicações não devem ser e não constituem conselhos ou recomendações financeiras, de investimento, de negociação ou de qualquer outro tipo, fornecidas ou endossadas pela TradingView. Leia mais em Termos de uso.