//@version=6
strategy("Ultimate Auto Trading Strategy v6",
shorttitle="UATS v6",
overlay=true,
initial_capital=10000,
default_qty_type=strategy.percent_of_equity,
default_qty_value=5,
commission_type=strategy.commission.percent,
commission_value=0.1,
slippage=2,
max_bars_back=5000)
// ============================================
// 📊 INPUT PARAMETERS
// ============================================
// === Trend Detection ===
group_trend = "🔄 Trend Detection"
fast_ema_len = input.int(9, "Fast EMA Length", minval=1, maxval=50,
group=group_trend)
slow_ema_len = input.int(21, "Slow EMA Length", minval=1, maxval=100,
group=group_trend)
sma_len = input.int(50, "SMA Confirmation Length", minval=1, maxval=200,
group=group_trend)
// === SuperTrend ===
group_st = "⚡ SuperTrend Settings"
st_factor = input.float(3.0, "SuperTrend Factor", minval=0.5, maxval=10.0,
step=0.1, group=group_st)
st_length = input.int(10, "SuperTrend ATR Length", minval=1, maxval=50,
group=group_st)
// === Momentum Filters ===
group_momentum = "📈 Momentum Filters"
rsi_length = input.int(14, "RSI Length", minval=1, maxval=50, group=group_momentum)
rsi_ob = input.int(70, "RSI Overbought", minval=50, maxval=90,
group=group_momentum)
rsi_os = input.int(30, "RSI Oversold", minval=10, maxval=50, group=group_momentum)
// QQE Settings
qqe_rsi_length = input.int(14, "QQE RSI Length", minval=1, maxval=50,
group=group_momentum)
qqe_sf = input.int(5, "QQE Smoothing Factor", minval=1, maxval=20,
group=group_momentum)
qqe_threshold = input.float(4.236, "QQE Threshold", minval=1.0, maxval=10.0,
step=0.001, group=group_momentum)
// MACD Settings
macd_fast = input.int(12, "MACD Fast Length", minval=1, maxval=50,
group=group_momentum)
macd_slow = input.int(26, "MACD Slow Length", minval=1, maxval=100,
group=group_momentum)
macd_signal = input.int(9, "MACD Signal Length", minval=1, maxval=50,
group=group_momentum)
// === Volume Filter ===
group_volume = "📊 Volume Filter"
volume_ma_len = input.int(20, "Volume MA Length", minval=1, maxval=100,
group=group_volume)
volume_spike_mult = input.float(1.5, "Volume Spike Multiplier", minval=1.0,
maxval=5.0, step=0.1, group=group_volume)
// === Risk Management ===
group_risk = " Risk Management"
atr_length = input.int(14, "ATR Length", minval=1, maxval=50, group=group_risk)
stop_atr_mult = input.float(2.0, "Stop Loss ATR Multiplier", minval=0.5,
maxval=5.0, step=0.1, group=group_risk)
// Multi-level Take Profit
tp1_atr_mult = input.float(1.5, "TP1 ATR Multiplier", minval=0.5, maxval=10.0,
step=0.1, group=group_risk)
tp2_atr_mult = input.float(3.0, "TP2 ATR Multiplier", minval=1.0, maxval=15.0,
step=0.1, group=group_risk)
tp3_atr_mult = input.float(5.0, "TP3 ATR Multiplier", minval=2.0, maxval=20.0,
step=0.1, group=group_risk)
tp1_qty = input.int(40, "TP1 Quantity %", minval=10, maxval=100, group=group_risk)
tp2_qty = input.int(30, "TP2 Quantity %", minval=10, maxval=100, group=group_risk)
tp3_qty = input.int(30, "TP3 Quantity %", minval=10, maxval=100, group=group_risk)
// Early Loss Prevention
early_loss_pct = input.float(1.0, "Early Loss Prevention %", minval=0.5,
maxval=3.0, step=0.1, group=group_risk)
// === Higher Timeframe Filter ===
group_htf = "⏰ Higher Timeframe Filter"
use_htf_filter = input.bool(true, "Use HTF Filter", group=group_htf)
htf_timeframe = input.timeframe("15", "HTF Timeframe", group=group_htf)
htf_ema_len = input.int(50, "HTF EMA Length", minval=1, maxval=200,
group=group_htf)
// === Trade Management ===
group_trade = "⚙️ Trade Management"
max_position_size = input.float(5.0, "Max Position Size %", minval=1.0,
maxval=25.0, step=0.5, group=group_trade)
allow_longs = input.bool(true, "Allow Long Trades", group=group_trade)
allow_shorts = input.bool(true, "Allow Short Trades", group=group_trade)
// ============================================
// 📊 TECHNICAL INDICATORS
// ============================================
// === Moving Averages ===
fast_ema = ta.ema(close, fast_ema_len)
slow_ema = ta.ema(close, slow_ema_len)
sma_filter = ta.sma(close, sma_len)
// === SuperTrend ===
atr_st = ta.atr(st_length)
hl2_calc = (high + low) / 2
upper_band = hl2_calc + (st_factor * atr_st)
lower_band = hl2_calc - (st_factor * atr_st)
var float supertrend = na
var bool uptrend = false
if na(supertrend[1])
supertrend := close > upper_band ? lower_band : upper_band
uptrend := close > upper_band
else
supertrend := close > supertrend[1] ? lower_band : upper_band
uptrend := close > supertrend[1]
// === RSI ===
rsi = ta.rsi(close, rsi_length)
// === QQE Calculation ===
rsi_qqe = ta.rsi(close, qqe_rsi_length)
rsi_ma = ta.ema(rsi_qqe, qqe_sf)
atr_rsi = math.abs(rsi_ma[1] - rsi_ma)
ma_atr_rsi = ta.ema(atr_rsi, qqe_sf)
dar = ta.ema(ma_atr_rsi, qqe_sf) * qqe_threshold
var float longband = na
var float shortband = na
var int trend = na
DeltaFastAtrRsi = dar
RSIndex = rsi_ma
newshortband = RSIndex + DeltaFastAtrRsi
newlongband = RSIndex - DeltaFastAtrRsi
longband := na(longband[1]) ? newlongband : (RSIndex[1] > longband[1] and RSIndex >
longband[1] ? math.max(longband[1], newlongband) : newlongband)
shortband := na(shortband[1]) ? newshortband : (RSIndex[1] < shortband[1] and
RSIndex < shortband[1] ? math.min(shortband[1], newshortband) : newshortband)
cross_1 = ta.cross(longband[1], RSIndex)
trend := ta.cross(RSIndex, shortband[1]) ? 1 : cross_1 ? -1 : nz(trend[1], 1)
FastAtrRsiTL = trend == 1 ? longband : shortband
// QQE Signals
qqe_up = trend == 1 and trend[1] == -1
qqe_down = trend == -1 and trend[1] == 1
// === MACD ===
[macd_line, signal_line, hist] = ta.macd(close, macd_fast, macd_slow, macd_signal)
// === Volume Filter ===
volume_ma = ta.sma(volume, volume_ma_len)
volume_spike = volume > volume_ma * volume_spike_mult
// === ATR for Risk Management ===
atr_risk = ta.atr(atr_length)
// === Higher Timeframe Filter ===
htf_ema = use_htf_filter ? request.security(syminfo.tickerid, htf_timeframe,
ta.ema(close, htf_ema_len), lookahead=barmerge.lookahead_off) : close
htf_bullish = use_htf_filter ? close > htf_ema : true
htf_bearish = use_htf_filter ? close < htf_ema : true
// ============================================
// 🎯 ENTRY CONDITIONS
// ============================================
// === Trend Confirmation ===
ema_bullish = ta.crossover(fast_ema, slow_ema) or (fast_ema > slow_ema and close >
fast_ema)
ema_bearish = ta.crossunder(fast_ema, slow_ema) or (fast_ema < slow_ema and close <
fast_ema)
sma_bullish = close > sma_filter
sma_bearish = close < sma_filter
supertrend_bullish = uptrend and not uptrend[1]
supertrend_bearish = not uptrend and uptrend[1]
// === Momentum Confirmation ===
rsi_long_ok = rsi > rsi_os and rsi < rsi_ob
rsi_short_ok = rsi < rsi_ob and rsi > rsi_os
macd_bullish = ta.crossover(macd_line, signal_line) and macd_line < 0
macd_bearish = ta.crossunder(macd_line, signal_line) and macd_line > 0
// === Strong Entry Conditions ===
strong_long = ema_bullish and supertrend_bullish and sma_bullish and htf_bullish
and (qqe_up or macd_bullish) and rsi_long_ok and volume_spike
strong_short = ema_bearish and supertrend_bearish and sma_bearish and htf_bearish
and (qqe_down or macd_bearish) and rsi_short_ok and volume_spike
// === Final Entry Conditions ===
long_condition = strong_long and allow_longs and strategy.position_size == 0
short_condition = strong_short and allow_shorts and strategy.position_size == 0
// ============================================
// 📊 POSITION MANAGEMENT
// ============================================
var float entry_price = na
var float entry_atr = na
var int bars_since_entry = na
// === Entry Logic ===
if long_condition
entry_price := close
entry_atr := atr_risk
bars_since_entry := 0
strategy.entry("Long", strategy.long, alert_message="STRONG_BUY")
if short_condition
entry_price := close
entry_atr := atr_risk
bars_since_entry := 0
strategy.entry("Short", strategy.short, alert_message="STRONG_SELL")
// === Risk Management Levels ===
var float stop_loss = na
var float tp1_level = na
var float tp2_level = na
var float tp3_level = na
if strategy.position_size > 0 // Long position
stop_loss := entry_price - (entry_atr * stop_atr_mult)
tp1_level := entry_price + (entry_atr * tp1_atr_mult)
tp2_level := entry_price + (entry_atr * tp2_atr_mult)
tp3_level := entry_price + (entry_atr * tp3_atr_mult)
else if strategy.position_size < 0 // Short position
stop_loss := entry_price + (entry_atr * stop_atr_mult)
tp1_level := entry_price - (entry_atr * tp1_atr_mult)
tp2_level := entry_price - (entry_atr * tp2_atr_mult)
tp3_level := entry_price - (entry_atr * tp3_atr_mult)
// === Early Loss Prevention ===
if strategy.position_size != 0
bars_since_entry := bars_since_entry + 1
early_loss_long = strategy.position_size > 0 and bars_since_entry == 1 and
close < entry_price * (1 - early_loss_pct / 100)
early_loss_short = strategy.position_size < 0 and bars_since_entry == 1 and
close > entry_price * (1 + early_loss_pct / 100)
if early_loss_long or early_loss_short
strategy.close_all(alert_message="EARLY_EXIT")
// === Multi-Level Take Profits ===
if strategy.position_size > 0
strategy.exit("TP1", "Long", limit=tp1_level, stop=stop_loss,
qty_percent=tp1_qty, alert_message="TP1_LONG")
strategy.exit("TP2", "Long", limit=tp2_level, stop=stop_loss,
qty_percent=tp2_qty, alert_message="TP2_LONG")
strategy.exit("TP3", "Long", limit=tp3_level, stop=stop_loss,
qty_percent=tp3_qty, alert_message="TP3_LONG")
if strategy.position_size < 0
strategy.exit("TP1", "Short", limit=tp1_level, stop=stop_loss,
qty_percent=tp1_qty, alert_message="TP1_SHORT")
strategy.exit("TP2", "Short", limit=tp2_level, stop=stop_loss,
qty_percent=tp2_qty, alert_message="TP2_SHORT")
strategy.exit("TP3", "Short", limit=tp3_level, stop=stop_loss,
qty_percent=tp3_qty, alert_message="TP3_SHORT")
// ============================================
// 🎨 VISUAL ELEMENTS
// ============================================
// === Plot Indicators ===
plot(fast_ema, color=color.new(color.blue, 0), title="Fast EMA", linewidth=1)
plot(slow_ema, color=color.new(color.red, 0), title="Slow EMA", linewidth=1)
plot(sma_filter, color=color.new(color.orange, 0), title="SMA Filter", linewidth=2)
plot(supertrend, color=uptrend ? color.green : color.red, title="SuperTrend",
linewidth=2)
// === HTF EMA ===
plot(use_htf_filter ? htf_ema : na, color=color.new(color.purple, 20), title="HTF
EMA", linewidth=3)
// === Entry Signals ===
plotshape(long_condition, title="Long Entry", location=location.belowbar,
style=shape.labelup, size=size.large, color=color.new(color.green, 0), text="BUY",
textcolor=color.white)
plotshape(short_condition, title="Short Entry", location=location.abovebar,
style=shape.labeldown, size=size.large, color=color.new(color.red, 0), text="SELL",
textcolor=color.white)
// === Risk Levels ===
plot(strategy.position_size > 0 ? stop_loss : na, color=color.new(color.red, 0),
title="Stop Loss", style=plot.style_linebr, linewidth=2)
plot(strategy.position_size > 0 ? tp1_level : na, color=color.new(color.green, 0),
title="TP1", style=plot.style_linebr, linewidth=1)
plot(strategy.position_size > 0 ? tp2_level : na, color=color.new(color.green, 30),
title="TP2", style=plot.style_linebr, linewidth=1)
plot(strategy.position_size > 0 ? tp3_level : na, color=color.new(color.green, 60),
title="TP3", style=plot.style_linebr, linewidth=1)
plot(strategy.position_size < 0 ? stop_loss : na, color=color.new(color.red, 0),
title="Short Stop Loss", style=plot.style_linebr, linewidth=2)
plot(strategy.position_size < 0 ? tp1_level : na, color=color.new(color.green, 0),
title="Short TP1", style=plot.style_linebr, linewidth=1)
plot(strategy.position_size < 0 ? tp2_level : na, color=color.new(color.green, 30),
title="Short TP2", style=plot.style_linebr, linewidth=1)
plot(strategy.position_size < 0 ? tp3_level : na, color=color.new(color.green, 60),
title="Short TP3", style=plot.style_linebr, linewidth=1)
// === Background Color for Market Conditions ===
strong_bullish_bg = ema_bullish and uptrend and sma_bullish and htf_bullish and
volume_spike
strong_bearish_bg = ema_bearish and not uptrend and sma_bearish and htf_bearish and
volume_spike
bgcolor(strong_bullish_bg ? color.new(color.green, 95) : strong_bearish_bg ?
color.new(color.red, 95) : na)
// === Info Table ===
var table info_table = table.new(position.top_right, 2, 8,
bgcolor=color.new(color.white, 80), border_width=1, border_color=color.gray)
if barstate.islast
table.cell(info_table, 0, 0, "Condition", text_color=color.black,
bgcolor=color.new(color.gray, 70))
table.cell(info_table, 1, 0, "Status", text_color=color.black,
bgcolor=color.new(color.gray, 70))
table.cell(info_table, 0, 1, "EMA Trend", text_color=color.black)
table.cell(info_table, 1, 1, fast_ema > slow_ema ? "Bullish" : "Bearish",
text_color=fast_ema > slow_ema ? color.green : color.red)
table.cell(info_table, 0, 2, "SuperTrend", text_color=color.black)
table.cell(info_table, 1, 2, uptrend ? "Bullish" : "Bearish",
text_color=uptrend ? color.green : color.red)
table.cell(info_table, 0, 3, "RSI", text_color=color.black)
table.cell(info_table, 1, 3, str.tostring(math.round(rsi, 1)), text_color=rsi >
70 ? color.red : rsi < 30 ? color.green : color.black)
table.cell(info_table, 0, 4, "Volume", text_color=color.black)
table.cell(info_table, 1, 4, volume_spike ? "High" : "Normal",
text_color=volume_spike ? color.green : color.black)
table.cell(info_table, 0, 5, "HTF Filter", text_color=color.black)
table.cell(info_table, 1, 5, use_htf_filter ? (htf_bullish ? "Bullish" :
"Bearish") : "Disabled", text_color=use_htf_filter ? (htf_bullish ? color.green :
color.red) : color.gray)
table.cell(info_table, 0, 6, "Position", text_color=color.black)
table.cell(info_table, 1, 6, strategy.position_size > 0 ? "Long" :
strategy.position_size < 0 ? "Short" : "None", text_color=strategy.position_size >
0 ? color.green : strategy.position_size < 0 ? color.red : color.black)
table.cell(info_table, 0, 7, "P&L", text_color=color.black)
table.cell(info_table, 1, 7, str.tostring(math.round(strategy.netprofit, 2)),
text_color=strategy.netprofit > 0 ? color.green : strategy.netprofit < 0 ?
color.red : color.black)
// ============================================
// 🚨 ALERTS
// ============================================
// Alert conditions
alertcondition(long_condition, title="Strong Buy Signal", message="{{ticker}} -
STRONG BUY SIGNAL - All conditions met for long entry")
alertcondition(short_condition, title="Strong Sell Signal", message="{{ticker}} -
STRONG SELL SIGNAL - All conditions met for short entry")
// Position alerts
alertcondition(strategy.position_size > 0 and strategy.position_size[1] == 0,
title="Long Position Opened", message="{{ticker}} - LONG POSITION OPENED at
{{close}}")
alertcondition(strategy.position_size < 0 and strategy.position_size[1] == 0,
title="Short Position Opened", message="{{ticker}} - SHORT POSITION OPENED at
{{close}}")
alertcondition(strategy.position_size == 0 and strategy.position_size[1] != 0,
title="Position Closed", message="{{ticker}} - POSITION CLOSED at {{close}}")