Start
/
Strategie-Beispiele
Auf GitHub ansehen
EURUSD V2.0-Strategie
Mean-Reversion-System für EURUSD unter Verwendung eines langfristigen einfachen gleitenden Durchschnitts (SMA) und eines Volatilitätsfilters auf Basis des Average True Range (ATR).
Strategielogik
Berechnung eines SMA der Länge MA Length auf dem gewählten Kerzentyp.
Short -Einstieg, wenn der Preis über dem SMA liegt und innerhalb von Buffer Pips zurückkommt, während der ATR unter ATR Threshold liegt.
Long -Einstieg, wenn der Preis unter dem SMA liegt und sich innerhalb von Buffer Pips nähert bei niedrigem ATR.
Die Positionsgröße ergibt sich aus dem Kontostand und dem Risk Factor Z .
Stop-Loss und Take-Profit werden in festen Pip-Abständen vom Einstiegspreis gesetzt.
Nach dem Ausstieg wartet das System, bis der Preis Noise Filter Pips vom Einstiegsniveau entfernt ist, bevor ein neuer Trade erlaubt wird.
Parameter
MA Length – Periode des einfachen gleitenden Durchschnitts (Standard 218).
Buffer (pips) – maximaler Abstand vom SMA für den Einstieg (Standard 0).
Stop Loss (pips) – Stop-Loss-Abstand vom Einstieg (Standard 20).
Take Profit (pips) – Take-Profit-Abstand vom Einstieg (Standard 350).
Noise Filter (pips) – Abstand zur Rückstellung der Handelserlaubnis (Standard 50).
ATR Length – ATR-Berechnungsperiode (Standard 200).
ATR Threshold (pips) – maximaler ATR für neue Positionen (Standard 40).
Max Spread (pips) – maximal erlaubter Spread (Standard 4).
Risk Factor Z – Money-Management-Faktor (Standard 2).
Candle Type – Zeitrahmen der verarbeiteten Kerzen (Standard 15 Minuten).
Diese Strategie verwendet Marktorders für Ein- und Ausstiege.
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// SMA mean-reversion strategy with ATR filter.
/// Buys below SMA, sells above SMA when ATR confirms range conditions.
/// </summary>
public class EurusdV20Strategy : Strategy
{
private readonly StrategyParam<int> _maLength;
private readonly StrategyParam<int> _atrLength;
private readonly StrategyParam<decimal> _takeProfit;
private readonly StrategyParam<decimal> _stopLoss;
private readonly StrategyParam<DataType> _candleType;
private decimal _entryPrice;
public int MaLength { get => _maLength.Value; set => _maLength.Value = value; }
public int AtrLength { get => _atrLength.Value; set => _atrLength.Value = value; }
public decimal TakeProfit { get => _takeProfit.Value; set => _takeProfit.Value = value; }
public decimal StopLoss { get => _stopLoss.Value; set => _stopLoss.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public EurusdV20Strategy()
{
_maLength = Param(nameof(MaLength), 50)
.SetGreaterThanZero()
.SetDisplay("MA Length", "SMA period", "General");
_atrLength = Param(nameof(AtrLength), 14)
.SetGreaterThanZero()
.SetDisplay("ATR Length", "ATR period", "Indicators");
_takeProfit = Param(nameof(TakeProfit), 500m)
.SetDisplay("Take Profit", "Take profit in price units", "Risk");
_stopLoss = Param(nameof(StopLoss), 300m)
.SetDisplay("Stop Loss", "Stop loss in price units", "Risk");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Candle type", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_entryPrice = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var sma = new SimpleMovingAverage { Length = MaLength };
var sub = SubscribeCandles(CandleType);
sub.Bind(sma, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, decimal smaValue)
{
if (candle.State != CandleStates.Finished)
return;
var close = candle.ClosePrice;
// Exit management
if (Position > 0)
{
if (close - _entryPrice >= TakeProfit || _entryPrice - close >= StopLoss)
{
SellMarket();
_entryPrice = 0;
return;
}
}
else if (Position < 0)
{
if (_entryPrice - close >= TakeProfit || close - _entryPrice >= StopLoss)
{
BuyMarket();
_entryPrice = 0;
return;
}
}
if (Position != 0)
return;
// Mean reversion: buy below SMA, sell above SMA
var dist = Math.Abs(close - smaValue);
if (dist < smaValue * 0.002m)
return;
if (close < smaValue)
{
BuyMarket();
_entryPrice = close;
}
else if (close > smaValue)
{
SellMarket();
_entryPrice = close;
}
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Math
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import SimpleMovingAverage
from StockSharp.Algo.Strategies import Strategy
class eurusd_v2_0_strategy(Strategy):
"""
SMA mean-reversion strategy with TP/SL.
Buys below SMA, sells above SMA when distance exceeds threshold.
Exits on take profit or stop loss.
"""
def __init__(self):
super(eurusd_v2_0_strategy, self).__init__()
self._ma_length = self.Param("MaLength", 50) \
.SetDisplay("MA Length", "SMA period", "General")
self._take_profit = self.Param("TakeProfit", 500.0) \
.SetDisplay("Take Profit", "Take profit in price units", "Risk")
self._stop_loss = self.Param("StopLoss", 300.0) \
.SetDisplay("Stop Loss", "Stop loss in price units", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Candle type", "General")
self._entry_price = 0.0
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(eurusd_v2_0_strategy, self).OnReseted()
self._entry_price = 0.0
def OnStarted2(self, time):
super(eurusd_v2_0_strategy, self).OnStarted2(time)
sma = SimpleMovingAverage()
sma.Length = self._ma_length.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(sma, self._process_candle).Start()
def _process_candle(self, candle, sma_val):
if candle.State != CandleStates.Finished:
return
close = float(candle.ClosePrice)
sma_val = float(sma_val)
tp = float(self._take_profit.Value)
sl = float(self._stop_loss.Value)
if self.Position > 0:
if close - self._entry_price >= tp or self._entry_price - close >= sl:
self.SellMarket()
self._entry_price = 0.0
return
elif self.Position < 0:
if self._entry_price - close >= tp or close - self._entry_price >= sl:
self.BuyMarket()
self._entry_price = 0.0
return
if self.Position != 0:
return
dist = abs(close - sma_val)
if dist < sma_val * 0.002:
return
if close < sma_val:
self.BuyMarket()
self._entry_price = close
elif close > sma_val:
self.SellMarket()
self._entry_price = close
def CreateClone(self):
return eurusd_v2_0_strategy()