OPEN-SOURCE SCRIPT

XAUUSD Strategy with Optimized Averaging

//version=5
strategy("XAUUSD Strategy with Optimized Averaging", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=1)

// Optimized Input Variables
fast_length = input.int(10, title="Fast EMA Length", minval=5, maxval=50, step=1) // Optimized for XAUUSD trends
slow_length = input.int(20, title="Slow EMA Length", minval=10, maxval=100, step=1)
rsi_length = input.int(14, title="RSI Length", minval=7, maxval=50, step=1)
rsi_overbought = input.int(75, title="RSI Overbought Level", minval=60, maxval=90, step=1)
rsi_oversold = input.int(25, title="RSI Oversold Level", minval=10, maxval=40, step=1)
macd_fast = input.int(12, title="MACD Fast Length", minval=5, maxval=20, step=1)
macd_slow = input.int(26, title="MACD Slow Length", minval=15, maxval=40, step=1)
macd_signal = input.int(9, title="MACD Signal Length", minval=5, maxval=15, step=1)
initial_stop_loss_percent = input.float(2.0, title="Initial Stop Loss %", minval=0.5, maxval=5.0, step=0.1) / 100 // Slightly wider stop-loss
initial_take_profit_percent = input.float(4.0, title="Initial Take Profit %", minval=1.0, maxval=10.0, step=0.1) / 100 // Better risk-reward ratio
averaging_distance = input.float(1.0, title="Averaging Distance %", minval=0.5, maxval=5.0, step=0.1) / 100
max_averaging_layers = input.int(3, title="Max Averaging Layers", minval=1, maxval=5)

// Indicators
fast_ma = ta.ema(close, fast_length)
slow_ma = ta.ema(close, slow_length)
rsi = ta.rsi(close, rsi_length)
[macd_line, signal_line, _] = ta.macd(close, macd_fast, macd_slow, macd_signal)
macd_histogram = macd_line - signal_line

// Plotting indicators
plot(fast_ma, color=color.green, title="Fast EMA")
plot(slow_ma, color=color.red, title="Slow EMA")
hline(0, "MACD Zero Line", color=color.gray)
plot(macd_histogram, color=color.blue, style=plot.style_histogram, title="MACD Histogram")

// Trade conditions
long_condition = ta.crossover(fast_ma, slow_ma) and rsi < rsi_oversold and macd_line > signal_line
short_condition = ta.crossunder(fast_ma, slow_ma) and rsi > rsi_overbought and macd_line < signal_line

// Position management
var int layer_count = 0
var float avg_price = na

// Manage long entries
if (long_condition and strategy.position_size == 0)
strategy.entry("Long", strategy.long)
layer_count := 1
avg_price := strategy.position_avg_price

// Manage short entries
if (short_condition and strategy.position_size == 0)
strategy.entry("Short", strategy.short)
layer_count := 1
avg_price := strategy.position_avg_price

// Averaging logic for long trades
if (strategy.position_size > 0 and layer_count < max_averaging_layers)
if (close < avg_price * (1 - averaging_distance))
strategy.entry("Long Add", strategy.long)
layer_count += 1
avg_price := strategy.position_avg_price

// Averaging logic for short trades
if (strategy.position_size < 0 and layer_count < max_averaging_layers)
if (close > avg_price * (1 + averaging_distance))
strategy.entry("Short Add", strategy.short)
layer_count += 1
avg_price := strategy.position_avg_price

// Dynamic stop loss and take profit for long positions
long_stop_loss = avg_price * (1 - initial_stop_loss_percent * layer_count)
long_take_profit = avg_price * (1 + initial_take_profit_percent * layer_count)

// Dynamic stop loss and take profit for short positions
short_stop_loss = avg_price * (1 + initial_stop_loss_percent * layer_count)
short_take_profit = avg_price * (1 - initial_take_profit_percent * layer_count)

// Exit logic
if (strategy.position_size > 0)
strategy.exit("Long Exit", from_entry="Long", stop=long_stop_loss, limit=long_take_profit)

if (strategy.position_size < 0)
strategy.exit("Short Exit", from_entry="Short", stop=short_stop_loss, limit=short_take_profit)

// Alerts for trade signals
if (long_condition)
alert("Buy signal for XAUUSD with averaging enabled!", alert.freq_once_per_bar)

if (short_condition)
alert("Sell signal for XAUUSD with averaging enabled!", alert.freq_once_per_bar)
Pine utilities

오픈 소스 스크립트

진정한 TradingView 정신에 따라, 이 스크립트의 저자는 트레이더들이 이해하고 검증할 수 있도록 오픈 소스로 공개했습니다. 저자에게 박수를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 출판물에서 이 코드를 재사용하는 것은 하우스 룰에 의해 관리됩니다. 님은 즐겨찾기로 이 스크립트를 차트에서 쓸 수 있습니다.

차트에 이 스크립트를 사용하시겠습니까?

면책사항