XAUUSD 10-Minute Strategy
This strategy trades XAUUSD on 10-minute candles using MACD, RSI and Bollinger Bands signals. It opens long positions when bullish conditions appear and short positions when bearish signals trigger. The system applies ATR-based stop-loss and take-profit levels adjusted for a fixed spread.
Details
- Entry Criteria:
- Long: MACD line crosses above signal, RSI below oversold or price below lower Bollinger Band.
- Short: MACD line crosses below signal, RSI above overbought or price above upper Bollinger Band.
- Long/Short: Both.
- Exit Criteria:
- Position closed on opposite signal, stop-loss or take-profit.
- Stops: ATR stop-loss at
3 * ATR, take-profit at5 * ATR. - Default Values:
- MACD fast/slow/signal:
12/26/9. - RSI period:
14, overbought65, oversold35. - Bollinger length
20, width2. - ATR period
14. - Spread
38ticks.
- MACD fast/slow/signal:
- Filters:
- Category: Trend following
- Direction: Both
- Indicators: Multiple
- Stops: Yes
- Complexity: Moderate
- Timeframe: Intraday
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// XAUUSD 10-minute strategy using RSI, dual EMA crossover, and Bollinger bands.
/// Combines momentum (RSI), trend (EMA cross), and volatility (StdDev bands) signals.
/// </summary>
public class Xauusd10MinuteStrategy : Strategy
{
private readonly StrategyParam<int> _rsiLength;
private readonly StrategyParam<decimal> _rsiOverbought;
private readonly StrategyParam<decimal> _rsiOversold;
private readonly StrategyParam<int> _fastEma;
private readonly StrategyParam<int> _slowEma;
private readonly StrategyParam<decimal> _stopMult;
private readonly StrategyParam<decimal> _tpMult;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevFastEma;
private decimal _prevSlowEma;
private decimal _entryPrice;
private decimal _stopPrice;
private decimal _takePrice;
public int RsiLength { get => _rsiLength.Value; set => _rsiLength.Value = value; }
public decimal RsiOverbought { get => _rsiOverbought.Value; set => _rsiOverbought.Value = value; }
public decimal RsiOversold { get => _rsiOversold.Value; set => _rsiOversold.Value = value; }
public int FastEma { get => _fastEma.Value; set => _fastEma.Value = value; }
public int SlowEma { get => _slowEma.Value; set => _slowEma.Value = value; }
public decimal StopMult { get => _stopMult.Value; set => _stopMult.Value = value; }
public decimal TpMult { get => _tpMult.Value; set => _tpMult.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public Xauusd10MinuteStrategy()
{
_rsiLength = Param(nameof(RsiLength), 14)
.SetGreaterThanZero()
.SetDisplay("RSI Length", "RSI period", "Indicators");
_rsiOverbought = Param(nameof(RsiOverbought), 65m)
.SetDisplay("RSI Overbought", "Overbought level", "Indicators");
_rsiOversold = Param(nameof(RsiOversold), 35m)
.SetDisplay("RSI Oversold", "Oversold level", "Indicators");
_fastEma = Param(nameof(FastEma), 12)
.SetGreaterThanZero()
.SetDisplay("Fast EMA", "Fast EMA period", "Indicators");
_slowEma = Param(nameof(SlowEma), 26)
.SetGreaterThanZero()
.SetDisplay("Slow EMA", "Slow EMA period", "Indicators");
_stopMult = Param(nameof(StopMult), 3m)
.SetGreaterThanZero()
.SetDisplay("Stop Mult", "StdDev mult for stop", "Risk");
_tpMult = Param(nameof(TpMult), 5m)
.SetGreaterThanZero()
.SetDisplay("TP Mult", "StdDev mult for TP", "Risk");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(30).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
protected override void OnReseted()
{
base.OnReseted();
_prevFastEma = 0;
_prevSlowEma = 0;
_entryPrice = 0;
_stopPrice = 0;
_takePrice = 0;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var rsi = new RelativeStrengthIndex { Length = RsiLength };
var fastEma = new ExponentialMovingAverage { Length = FastEma };
var slowEma = new ExponentialMovingAverage { Length = SlowEma };
var stdDev = new StandardDeviation { Length = 14 };
_prevFastEma = 0;
_prevSlowEma = 0;
_entryPrice = 0;
_stopPrice = 0;
_takePrice = 0;
var subscription = SubscribeCandles(CandleType);
subscription.Bind(rsi, fastEma, slowEma, stdDev, ProcessCandle).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, fastEma);
DrawIndicator(area, slowEma);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal rsiVal, decimal fastVal, decimal slowVal, decimal stdVal)
{
if (candle.State != CandleStates.Finished)
return;
// TP/SL management
if (Position > 0 && _entryPrice > 0)
{
if (candle.ClosePrice <= _stopPrice || candle.ClosePrice >= _takePrice)
{
SellMarket();
_entryPrice = 0;
}
}
else if (Position < 0 && _entryPrice > 0)
{
if (candle.ClosePrice >= _stopPrice || candle.ClosePrice <= _takePrice)
{
BuyMarket();
_entryPrice = 0;
}
}
if (_prevFastEma == 0 || _prevSlowEma == 0 || stdVal <= 0)
{
_prevFastEma = fastVal;
_prevSlowEma = slowVal;
return;
}
var emaCrossUp = _prevFastEma <= _prevSlowEma && fastVal > slowVal;
var emaCrossDown = _prevFastEma >= _prevSlowEma && fastVal < slowVal;
var buySignal = emaCrossUp;
var sellSignal = emaCrossDown;
if (buySignal && Position <= 0)
{
BuyMarket();
_entryPrice = candle.ClosePrice;
_stopPrice = _entryPrice - StopMult * stdVal;
_takePrice = _entryPrice + TpMult * stdVal;
}
else if (sellSignal && Position >= 0)
{
SellMarket();
_entryPrice = candle.ClosePrice;
_stopPrice = _entryPrice + StopMult * stdVal;
_takePrice = _entryPrice - TpMult * stdVal;
}
_prevFastEma = fastVal;
_prevSlowEma = slowVal;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import ExponentialMovingAverage, RelativeStrengthIndex, StandardDeviation
from StockSharp.Algo.Strategies import Strategy
class xauusd10_minute_strategy(Strategy):
def __init__(self):
super(xauusd10_minute_strategy, self).__init__()
self._rsi_length = self.Param("RsiLength", 14) \
.SetDisplay("RSI Length", "RSI period", "Indicators")
self._rsi_overbought = self.Param("RsiOverbought", 65) \
.SetDisplay("RSI Overbought", "Overbought level", "Indicators")
self._rsi_oversold = self.Param("RsiOversold", 35) \
.SetDisplay("RSI Oversold", "Oversold level", "Indicators")
self._fast_ema = self.Param("FastEma", 12) \
.SetDisplay("Fast EMA", "Fast EMA period", "Indicators")
self._slow_ema = self.Param("SlowEma", 26) \
.SetDisplay("Slow EMA", "Slow EMA period", "Indicators")
self._stop_mult = self.Param("StopMult", 3) \
.SetDisplay("Stop Mult", "StdDev mult for stop", "Risk")
self._tp_mult = self.Param("TpMult", 5.0) \
.SetDisplay("TP Mult", "StdDev mult for TP", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(30))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._prev_fast_ema = 0.0
self._prev_slow_ema = 0.0
self._entry_price = 0.0
self._stop_price = 0.0
self._take_price = 0.0
@property
def rsi_length(self):
return self._rsi_length.Value
@property
def rsi_overbought(self):
return self._rsi_overbought.Value
@property
def rsi_oversold(self):
return self._rsi_oversold.Value
@property
def fast_ema(self):
return self._fast_ema.Value
@property
def slow_ema(self):
return self._slow_ema.Value
@property
def stop_mult(self):
return self._stop_mult.Value
@property
def tp_mult(self):
return self._tp_mult.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(xauusd10_minute_strategy, self).OnReseted()
self._prev_fast_ema = 0.0
self._prev_slow_ema = 0.0
self._entry_price = 0.0
self._stop_price = 0.0
self._take_price = 0.0
def OnStarted2(self, time):
super(xauusd10_minute_strategy, self).OnStarted2(time)
rsi = RelativeStrengthIndex()
rsi.Length = self.rsi_length
fast_ema = ExponentialMovingAverage()
fast_ema.Length = self.fast_ema
slow_ema = ExponentialMovingAverage()
slow_ema.Length = self.slow_ema
std_dev = StandardDeviation()
std_dev.Length = 14
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(rsi, fast_ema, slow_ema, std_dev, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, fast_ema)
self.DrawIndicator(area, slow_ema)
self.DrawOwnTrades(area)
def on_process(self, candle, rsi_val, fast_val, slow_val, std_val):
if candle.State != CandleStates.Finished:
return
# TP/SL management
if self.Position > 0 and self._entry_price > 0:
if candle.ClosePrice <= self._stop_price or candle.ClosePrice >= self._take_price:
self.SellMarket()
self._entry_price = 0
elif self.Position < 0 and self._entry_price > 0:
if candle.ClosePrice >= self._stop_price or candle.ClosePrice <= self._take_price:
self.BuyMarket()
self._entry_price = 0
if self._prev_fast_ema == 0 or self._prev_slow_ema == 0 or std_val <= 0:
self._prev_fast_ema = fast_val
self._prev_slow_ema = slow_val
return
ema_cross_up = self._prev_fast_ema <= self._prev_slow_ema and fast_val > slow_val
ema_cross_down = self._prev_fast_ema >= self._prev_slow_ema and fast_val < slow_val
buy_signal = ema_cross_up
sell_signal = ema_cross_down
if buy_signal and self.Position <= 0:
self.BuyMarket()
self._entry_price = candle.ClosePrice
self._stop_price = self._entry_price - self.stop_mult * std_val
self._take_price = self._entry_price + self.tp_mult * std_val
elif sell_signal and self.Position >= 0:
self.SellMarket()
self._entry_price = candle.ClosePrice
self._stop_price = self._entry_price + self.stop_mult * std_val
self._take_price = self._entry_price - self.tp_mult * std_val
self._prev_fast_ema = fast_val
self._prev_slow_ema = slow_val
def CreateClone(self):
return xauusd10_minute_strategy()