Die Random Bias Trader-Strategie emuliert den Expertenberater „Random Trader“ von MetaTrader unter Verwendung des High-Level-API von StockSharp.
Bei jeder abgeschlossenen Kerze wirft die Strategie eine virtuelle Münze und eröffnet eine Position in diese Richtung, wenn kein Handel aktiv ist.
Stop-Loss- und Take-Profit-Level werden entweder von ATR(10) oder von einem festen Pip-Abstand abgeleitet und anhand des Chancen-Risiko-Verhältnisses dimensioniert.
Die Positionsgröße wird aus dem konfigurierten Risikoprozentsatz berechnet und automatisch durch die Volumenlimits des Instruments begrenzt.
Ein optionaler Breakeven-Trigger kann den Stop-Loss auf den Einstiegspreis verschieben, sobald ein bestimmter Pip-Gewinn erreicht ist.
Einzelheiten
Daten: Ein Kerzenabonnement, definiert durch CandleType.
Eintrittskriterien:
Long: Keine offene Position, Münzwurf ergibt Long. Der Einstiegspreis entspricht dem letzten Schlusskurs.
Short: Keine offene Position, Münzwurf ergibt Short. Der Einstiegspreis entspricht dem letzten Schlusskurs.
Ausstiegskriterien:
Stop-Loss: Die Distanz entspricht LossPipDistance × Pip-Größe oder LossAtrMultiplier × ATR(10), abhängig von LossType.
Take-Profit: Stoppdistanz multipliziert mit RewardRiskRatio.
Breakeven: Wenn aktiviert, wird der Stopp nach BreakevenDistancePips Gewinn zum Einstieg verschoben.
Stops: Dynamischer Stop-Loss und Take-Profit pro Trade, Breakeven-Stopp optional.
Standardwerte:
CandleType = 1 Minute Zeitrahmen
RewardRiskRatio = 2,0
LossType = Pip
LossAtrMultiplier = 5,0
LossPipDistance = 20 Pips
RiskPercentPerTrade = 1 %
UseBreakeven = Aktiviert
BreakevenDistancePips = 10 Pips
UseMaxMargin = Aktiviert
Filter:
Kategorie: Randomisiert trendneutral
Richtung: Beide, pro Wurf bestimmt
Indikatoren: ATR(10) (optional)
Komplexität: Anfänger
Risikostufe: Mittel, abhängig von der Stoppgröße
Notizen
Wenn das risikobasierte Volumen zu klein wird, greift die Strategie optional auf das maximal handelbare Volumen zurück.
Stop- und Zielniveaus werden vor der Auftragserteilung auf die Preisstufe des Instruments gerundet.
Die Breakeven-Logik hält jeweils nur eine Position offen und spiegelt die ursprüngliche MetaTrader-Logik wider.
using System;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Random trade generator with ATR-based risk management.
/// Opens a random long or short position on each candle when flat,
/// with stop loss and take profit based on ATR.
/// </summary>
public class RandomBiasTraderStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<decimal> _rewardRiskRatio;
private readonly StrategyParam<decimal> _atrMultiplier;
private readonly StrategyParam<int> _atrPeriod;
private Random _random;
private decimal _entryPrice;
private int _direction; // 1=long, -1=short, 0=flat
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public decimal RewardRiskRatio
{
get => _rewardRiskRatio.Value;
set => _rewardRiskRatio.Value = value;
}
public decimal AtrMultiplier
{
get => _atrMultiplier.Value;
set => _atrMultiplier.Value = value;
}
public int AtrPeriod
{
get => _atrPeriod.Value;
set => _atrPeriod.Value = value;
}
public RandomBiasTraderStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(60).TimeFrame())
.SetDisplay("Candle Type", "Candle type", "Data");
_rewardRiskRatio = Param(nameof(RewardRiskRatio), 3m)
.SetDisplay("Reward/Risk", "Reward to risk ratio", "Risk");
_atrMultiplier = Param(nameof(AtrMultiplier), 3m)
.SetDisplay("ATR Multiplier", "ATR multiplier for stop distance", "Risk");
_atrPeriod = Param(nameof(AtrPeriod), 14)
.SetDisplay("ATR Period", "ATR indicator period", "Risk");
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_random = new Random(42);
var atr = new AverageTrueRange { Length = AtrPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(atr, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal atrValue)
{
if (candle.State != CandleStates.Finished)
return;
if (atrValue <= 0)
return;
var stopDistance = atrValue * AtrMultiplier;
var takeDistance = stopDistance * RewardRiskRatio;
var close = candle.ClosePrice;
// Check exit for existing position
if (_direction > 0)
{
if (close >= _entryPrice + takeDistance || close <= _entryPrice - stopDistance)
{
SellMarket();
_direction = 0;
}
return;
}
else if (_direction < 0)
{
if (close <= _entryPrice - takeDistance || close >= _entryPrice + stopDistance)
{
BuyMarket();
_direction = 0;
}
return;
}
// Open new random position
if (_random.Next(4) != 0)
return;
if (_random.Next(2) == 0)
{
BuyMarket();
_entryPrice = close;
_direction = 1;
}
else
{
SellMarket();
_entryPrice = close;
_direction = -1;
}
}
/// <inheritdoc />
protected override void OnReseted()
{
_random = null;
_entryPrice = 0;
_direction = 0;
base.OnReseted();
}
}
import clr
import random
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 AverageTrueRange
from StockSharp.Algo.Strategies import Strategy
class random_bias_trader_strategy(Strategy):
def __init__(self):
super(random_bias_trader_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(60)))
self._reward_risk_ratio = self.Param("RewardRiskRatio", 3.0)
self._atr_multiplier = self.Param("AtrMultiplier", 3.0)
self._atr_period = self.Param("AtrPeriod", 14)
self._rng = None
self._entry_price = 0.0
self._direction = 0
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
@property
def RewardRiskRatio(self):
return self._reward_risk_ratio.Value
@RewardRiskRatio.setter
def RewardRiskRatio(self, value):
self._reward_risk_ratio.Value = value
@property
def AtrMultiplier(self):
return self._atr_multiplier.Value
@AtrMultiplier.setter
def AtrMultiplier(self, value):
self._atr_multiplier.Value = value
@property
def AtrPeriod(self):
return self._atr_period.Value
@AtrPeriod.setter
def AtrPeriod(self, value):
self._atr_period.Value = value
def OnReseted(self):
super(random_bias_trader_strategy, self).OnReseted()
self._rng = None
self._entry_price = 0.0
self._direction = 0
def OnStarted2(self, time):
super(random_bias_trader_strategy, self).OnStarted2(time)
self._rng = random.Random(42)
self._entry_price = 0.0
self._direction = 0
atr = AverageTrueRange()
atr.Length = self.AtrPeriod
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(atr, self._process_candle).Start()
def _process_candle(self, candle, atr_value):
if candle.State != CandleStates.Finished:
return
atr_val = float(atr_value)
if atr_val <= 0:
return
close = float(candle.ClosePrice)
atr_mult = float(self.AtrMultiplier)
rr_ratio = float(self.RewardRiskRatio)
stop_distance = atr_val * atr_mult
take_distance = stop_distance * rr_ratio
# Check exit for existing position
if self._direction > 0:
if close >= self._entry_price + take_distance or close <= self._entry_price - stop_distance:
self.SellMarket()
self._direction = 0
return
elif self._direction < 0:
if close <= self._entry_price - take_distance or close >= self._entry_price + stop_distance:
self.BuyMarket()
self._direction = 0
return
# Open new random position
if self._rng.randint(0, 3) != 0:
return
if self._rng.randint(0, 1) == 0:
self.BuyMarket()
self._entry_price = close
self._direction = 1
else:
self.SellMarket()
self._entry_price = close
self._direction = -1
def CreateClone(self):
return random_bias_trader_strategy()