OPEN-SOURCE SCRIPT

My script

35
//version=5
indicator("XAUUSD High Win-Rate Strategy", shorttitle="XAUUSD HWR", overlay=true)

// ============================================================================
// XAUUSD HIGH WIN-RATE STRATEGY INDICATOR
// Based on Dual-Phase Momentum Filter System
// Designed for M1, M5 timeframes with H1/H4 bias confirmation
// ============================================================================

// Input Parameters
ma_length = input.int(55, title="MA Channel Length", minval=1, maxval=200)
atr_length = input.int(14, title="ATR Length", minval=1, maxval=50)
atr_multiplier = input.float(2.5, title="ATR Stop Loss Multiplier", minval=1.0, maxval=5.0, step=0.1)
rr_ratio = input.float(1.5, title="Risk:Reward Ratio", minval=1.0, maxval=3.0, step=0.1)
htf_bias = input.timeframe("60", title="Higher Timeframe for Bias")
show_levels = input.bool(true, title="Show Entry/Exit Levels")
show_signals = input.bool(true, title="Show Buy/Sell Signals")
show_channel = input.bool(true, title="Show MA Channel")

// ============================================================================
// CORE CALCULATIONS
// ============================================================================

// Moving Average Channel (55-period High/Low)
ma_high = ta.sma(high, ma_length)
ma_low = ta.sma(low, ma_length)

// Heiken Ashi Calculations
ha_close = (open + high + low + close) / 4
var float ha_open = na
ha_open := na(ha_open[1]) ? (open + close) / 2 : (ha_open[1] + ha_close[1]) / 2
ha_high = math.max(high, math.max(ha_open, ha_close))
ha_low = math.min(low, math.min(ha_open, ha_close))

// Higher Timeframe Bias (200 SMA)
htf_sma200 = request.security(syminfo.tickerid, htf_bias, ta.sma(close, 200), lookahead=barmerge.lookahead_off)

// ATR for Stop Loss Calculation
atr = ta.atr(atr_length)

// RSI for Additional Confirmation
rsi = ta.rsi(close, 14)

// ============================================================================
// SIGNAL LOGIC
// ============================================================================

// Channel Filter - No trades when price is within the channel
in_channel = close > ma_low and close < ma_high
outside_channel = not in_channel

// Heiken Ashi Color
ha_bullish = ha_close > ha_open
ha_bearish = ha_close < ha_open

// Higher Timeframe Bias
htf_bullish = close > htf_sma200
htf_bearish = close < htf_sma200

// Entry Conditions
long_condition = outside_channel and close > ma_high and ha_bullish and htf_bullish
short_condition = outside_channel and close < ma_low and ha_bearish and htf_bearish

// Entry Signals (only on bar close to avoid repainting)
long_signal = long_condition and not long_condition[1]
short_signal = short_condition and not short_condition[1]

// ============================================================================
// TRADE LEVELS CALCULATION
// ============================================================================

var float entry_price = na
var float stop_loss = na
var float take_profit = na
var string trade_direction = na

// Calculate levels for new signals
if long_signal
entry_price := close
stop_loss := close - (atr * atr_multiplier)
take_profit := close + ((close - stop_loss) * rr_ratio)
trade_direction := "LONG"

if short_signal
entry_price := close
stop_loss := close + (atr * atr_multiplier)
take_profit := close - ((stop_loss - close) * rr_ratio)
trade_direction := "SHORT"

// ============================================================================
// VISUAL ELEMENTS
// ============================================================================

// MA Channel - Store plot IDs for fill function
ma_high_plot = plot(show_channel ? ma_high : na, color=color.blue, linewidth=1, title="MA High")
ma_low_plot = plot(show_channel ? ma_low : na, color=color.red, linewidth=1, title="MA Low")

// Fill the channel
fill(ma_high_plot, ma_low_plot, color=color.new(color.gray, 90), title="MA Channel")

// Higher Timeframe SMA200
plot(htf_sma200, color=color.purple, linewidth=2, title="HTF SMA200")

// Entry Signals
plotshape(show_signals and long_signal, title="Long Signal", location=location.belowbar,
style=shape.triangleup, size=size.normal, color=color.green)
plotshape(show_signals and short_signal, title="Short Signal", location=location.abovebar,
style=shape.triangledown, size=size.normal, color=color.red)

// Trade Levels
entry_plot = plot(show_levels and not na(entry_price) ? entry_price : na,
color=color.yellow, linewidth=2, style=line.style_dashed, title="Entry Price")
stop_plot = plot(show_levels and not na(stop_loss) ? stop_loss : na,
color=color.red, linewidth=2, style=line.style_dotted, title="Stop Loss")
target_plot = plot(show_levels and not na(take_profit) ? take_profit : na,
color=color.green, linewidth=2, style=line.style_dotted, title="Take Profit")

// ============================================================================
// ALERTS
// ============================================================================

// Alert Conditions
alertcondition(long_signal, title="Long Entry Signal",
message="XAUUSD Long Entry: Price={{close}}, SL=" + str.tostring(stop_loss) + ", TP=" + str.tostring(take_profit))
alertcondition(short_signal, title="Short Entry Signal",
message="XAUUSD Short Entry: Price={{close}}, SL=" + str.tostring(stop_loss) + ", TP=" + str.tostring(take_profit))

// ============================================================================
// INFORMATION TABLE
// ============================================================================

if barstate.islast and show_levels
var table info_table = table.new(position.top_right, 2, 8, bgcolor=color.white, border_width=1)

table.cell(info_table, 0, 0, "XAUUSD Strategy Info", text_color=color.black, text_size=size.normal)
table.cell(info_table, 1, 0, "", text_color=color.black)

table.cell(info_table, 0, 1, "Current Price:", text_color=color.black)
table.cell(info_table, 1, 1, str.tostring(close, "#.##"), text_color=color.black)

table.cell(info_table, 0, 2, "ATR (14):", text_color=color.black)
table.cell(info_table, 1, 2, str.tostring(atr, "#.##"), text_color=color.black)

table.cell(info_table, 0, 3, "RSI (14):", text_color=color.black)
table.cell(info_table, 1, 3, str.tostring(rsi, "#.##"), text_color=color.black)

table.cell(info_table, 0, 4, "HTF Bias:", text_color=color.black)
table.cell(info_table, 1, 4, htf_bullish ? "BULLISH" : "BEARISH",
text_color=htf_bullish ? color.green : color.red)

table.cell(info_table, 0, 5, "In Channel:", text_color=color.black)
table.cell(info_table, 1, 5, in_channel ? "YES" : "NO",
text_color=in_channel ? color.red : color.green)

if not na(trade_direction)
table.cell(info_table, 0, 6, "Last Signal:", text_color=color.black)
table.cell(info_table, 1, 6, trade_direction,
text_color=trade_direction == "LONG" ? color.green : color.red)

table.cell(info_table, 0, 7, "Risk/Reward:", text_color=color.black)
table.cell(info_table, 1, 7, "1:" + str.tostring(rr_ratio, "#.#"), text_color=color.black)

// ============================================================================
// BACKGROUND COLORING
// ============================================================================

// Color background based on trend and channel status
bg_color = color.new(color.white, 100)
if htf_bullish and not in_channel
bg_color := color.new(color.green, 95)
else if htf_bearish and not in_channel
bg_color := color.new(color.red, 95)
else if in_channel
bg_color := color.new(color.yellow, 95)

bgcolor(bg_color, title="Background Trend Color")

// ============================================================================
// STRATEGY NOTES
// ============================================================================

// This indicator implements the Dual-Phase Momentum Filter System:
// 1. MA Channel Filter: Avoids trades when price is between 55-period high/low MAs
// 2. Heiken Ashi Confirmation: Requires momentum alignment for entries
// 3. Higher Timeframe Bias: Uses 200 SMA on higher timeframe for direction
// 4. ATR-based Risk Management: Dynamic stop losses based on volatility
// 5. Fixed Risk:Reward Ratio: Consistent 1:1.5 profit targets
//
// Usage Instructions:
// 1. Apply to M1 or M5 timeframe for optimal signals
// 2. Set higher timeframe to H1 or H4 for bias confirmation
// 3. Wait for signals outside the MA channel
// 4. Enter trades only when all conditions align
// 5. Use provided stop loss and take profit levels
// 6. Risk no more than 0.5% of account per trade
//
// Best Trading Sessions: Asian and New York
// Avoid: Low liquidity periods and major news events

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.