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の精神に則り、このスクリプトの作者は、トレーダーが理解し検証できるようにオープンソースで公開しています。作者に敬意を表します!無料で使用することができますが、このコードを投稿で再利用するには、ハウスルールに準拠する必要があります。 お気に入りに登録してチャート上でご利用頂けます。

チャートでこのスクリプトを利用したいですか?

免責事項