Стратегия Reduce Risks
Описание
Reduce Risks — адаптация советника MetaTrader «Reduce_risks.mq5» для платформы StockSharp. Стратегия анализирует минутные свечи для поиска точки входа и использует подтверждающие фильтры на 15-минутном и часовом таймфреймах. Алгоритм ориентирован на валютные пары с маленьким спредом (EURUSD, USDCHF, USDJPY) и стремится входить в тренд только при низкой волатильности и подтверждённой структуре движения.
Таймфреймы и инструменты
- Основной таймфрейм: свечи M1.
- Подтверждение: свечи M15.
- Фильтр тренда: свечи H1.
- Рекомендуемые инструменты: EURUSD, USDCHF, USDJPY либо инструменты с аналогичным размером пункта.
Используемые данные
- SMA(5), SMA(8), SMA(13), SMA(60) по типичной цене на M1.
- SMA(4), SMA(5), SMA(8) по типичной цене на M15.
- SMA(24) по типичной цене на H1.
- Свечные характеристики (длина тела, тени, диапазон) для M1 и M15.
- Отслеживание максимальной/минимальной цены после входа для имитации оригинального трейлинг-алгоритма.
Правила входа
Покупка
- Последние три свечи на M1 и M15 должны иметь диапазон меньше 20 и 30 пунктов соответственно; ширина канала последних трёх свечей M15 не превышает 30 пунктов.
- Предыдущая свеча M1 активнее, но не более чем в 3 раза, и текущая цена пробивает максимумы предыдущих свечей M1 и M15.
- Иерархия скользящих средних указывает на восходящий тренд: SMA5 > SMA8 > SMA13, SMA60 растёт, цена закрытия выше всех четырёх SMA.
- На M15 SMA4 находится выше SMA8 и растёт; текущая цена выше SMA4(M15) и SMA24(H1).
- Подтверждение «начала волны»: значение SMA8 (M1) попадало внутрь диапазона любой из трёх предыдущих свечей M1, SMA5 (M15) находится внутри диапазона предыдущей свечи M15.
- Свечной фильтр: предыдущие свечи M1 и M15 имеют бычьи тела больше половины диапазона, формируют более высокие максимумы, откат не превышает 25 % диапазона, присутствуют тени.
- Все условия должны выполняться одновременно, открытых позиций нет — отправляется рыночная заявка Buy.
Продажа
Условия симметричны: диапазоны свечей ограничены, цена пробивает локальные минимумы, скользящие средние выстроены нисходящим образом (SMA5 < SMA8 < SMA13, SMA60 снижается), цена ниже всех SMA. Предыдущие свечи подтверждают нисходящую структуру (большие медвежьи тела, пониженные минимумы, маленькие откаты, наличие теней). При выполнении условий открывается рыночная заявка Sell.
Правила выхода
- Стоп-лосс и тейк-профит в пунктах устанавливаются автоматически через механизм StartProtection.
- Дополнительные условия закрытия повторяют оригинальную логику:
- Закрытие длинной позиции, если текущая свеча M1 упала минимум на 10 пунктов от цены открытия или после минуты от входа появилась мощная медвежья свеча.
- Фиксация прибыли при приросте от входа ≥10 пунктов либо при откате ≥20 пунктов от максимума после входа (при условии, что максимум был выше цены входа).
- Закрытие при просадке ≥20 пунктов или при достижении порогового уровня просадки по счёту.
- Для коротких позиций условия зеркальные.
Управление рисками
- Стратегия блокирует новые входы, если текущая стоимость портфеля ≤
InitialDeposit * (1 - RiskPercent/100), и принудительно закрывает открытые позиции при повторном достижении порога.
- Проверки соединения и торговых разрешений из оригинального советника не требуются, так как StockSharp управляет ими автоматически.
Параметры
| Параметр |
Описание |
Значение по умолчанию |
StopLossPips |
Размер стоп-лосса в пунктах. |
30 |
TakeProfitPips |
Размер тейк-профита в пунктах. |
60 |
InitialDeposit |
Базовый депозит для расчёта лимита просадки. |
10000 |
RiskPercent |
Максимальная допустимая просадка от базового депозита. |
5 |
M1CandleType |
Тип свечей для M1. |
Таймфрейм 1 минута |
M15CandleType |
Тип свечей для M15. |
Таймфрейм 15 минут |
H1CandleType |
Тип свечей для H1. |
Таймфрейм 1 час |
Примечания
- При работе с инструментами с другим размером пункта корректируйте параметры в пунктах.
- В проекте присутствует только C# версия стратегии; Python-реализация и соответствующая папка не создаются согласно заданию.
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;
using StockSharp.Algo;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Trend-following strategy converted from the "Reduce risks" MQL5 expert.
/// Uses SMA hierarchy (short/medium/long) for trend detection with risk control exits.
/// Enters on confirmed SMA crossover, exits on reverse cross or stop/take profit.
/// </summary>
public class ReduceRisksStrategy : Strategy
{
private readonly StrategyParam<int> _stopLossPips;
private readonly StrategyParam<int> _takeProfitPips;
private readonly StrategyParam<decimal> _initialDeposit;
private readonly StrategyParam<decimal> _riskPercent;
private readonly StrategyParam<DataType> _candleType;
private SimpleMovingAverage _smaShort;
private SimpleMovingAverage _smaMedium;
private SimpleMovingAverage _smaLong;
private decimal? _smaShortCurr;
private decimal? _smaShortPrev;
private decimal? _smaMediumCurr;
private decimal? _smaMediumPrev;
private decimal? _smaLongCurr;
private decimal? _smaLongPrev;
private decimal _riskThreshold;
private int _riskExceededCounter;
private int _barsSinceEntry;
private decimal _entryPrice;
private int _barsShortAboveMedium;
private int _barsShortBelowMedium;
private bool _enteredLong;
private bool _enteredShort;
/// <summary>
/// Stop loss distance expressed in pips.
/// </summary>
public int StopLossPips
{
get => _stopLossPips.Value;
set => _stopLossPips.Value = value;
}
/// <summary>
/// Take profit distance expressed in pips.
/// </summary>
public int TakeProfitPips
{
get => _takeProfitPips.Value;
set => _takeProfitPips.Value = value;
}
/// <summary>
/// Reference initial deposit used for equity based risk limitation.
/// </summary>
public decimal InitialDeposit
{
get => _initialDeposit.Value;
set => _initialDeposit.Value = value;
}
/// <summary>
/// Percentage of the initial deposit allowed to be lost before new entries are blocked.
/// </summary>
public decimal RiskPercent
{
get => _riskPercent.Value;
set => _riskPercent.Value = value;
}
/// <summary>
/// Candle timeframe for trading.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes strategy parameters.
/// </summary>
public ReduceRisksStrategy()
{
_stopLossPips = Param(nameof(StopLossPips), 30)
.SetNotNegative()
.SetDisplay("Stop Loss", "Protective stop distance in pips", "Risk");
_takeProfitPips = Param(nameof(TakeProfitPips), 60)
.SetNotNegative()
.SetDisplay("Take Profit", "Target distance in pips", "Risk");
_initialDeposit = Param(nameof(InitialDeposit), 1000000m)
.SetGreaterThanZero()
.SetDisplay("Initial Deposit", "Reference equity for drawdown protection", "Risk");
_riskPercent = Param(nameof(RiskPercent), 5m)
.SetRange(0m, 100m)
.SetDisplay("Risk Percent", "Maximum loss allowed relative to the initial deposit", "Risk");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Timeframe", "Trading timeframe", "Timeframes");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return new[] { (Security, CandleType) };
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_smaShort = null;
_smaMedium = null;
_smaLong = null;
_smaShortCurr = null;
_smaShortPrev = null;
_smaMediumCurr = null;
_smaMediumPrev = null;
_smaLongCurr = null;
_smaLongPrev = null;
_riskThreshold = 0m;
_riskExceededCounter = 0;
_barsSinceEntry = 0;
_entryPrice = 0m;
_barsShortAboveMedium = 0;
_barsShortBelowMedium = 0;
_enteredLong = false;
_enteredShort = false;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_riskThreshold = InitialDeposit * (100m - RiskPercent) / 100m;
// SMA periods: ~2h / ~6h / ~12h on 5-min candles
_smaShort = new SimpleMovingAverage { Length = 24 };
_smaMedium = new SimpleMovingAverage { Length = 72 };
_smaLong = new SimpleMovingAverage { Length = 144 };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(ProcessCandle).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, _smaShort);
DrawIndicator(area, _smaMedium);
DrawIndicator(area, _smaLong);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished)
return;
if (_smaShort is null || _smaMedium is null || _smaLong is null)
return;
var typical = (candle.HighPrice + candle.LowPrice + candle.ClosePrice) / 3m;
UpdateSma(_smaShort, typical, candle.OpenTime, ref _smaShortCurr, ref _smaShortPrev);
UpdateSma(_smaMedium, typical, candle.OpenTime, ref _smaMediumCurr, ref _smaMediumPrev);
UpdateSma(_smaLong, typical, candle.OpenTime, ref _smaLongCurr, ref _smaLongPrev);
if (_smaShortCurr is not decimal smaS ||
_smaMediumCurr is not decimal smaM ||
_smaLongCurr is not decimal smaL)
return;
// Track consecutive bars of SMA position
if (smaS > smaM)
{
_barsShortAboveMedium++;
_barsShortBelowMedium = 0;
}
else
{
_barsShortBelowMedium++;
_barsShortAboveMedium = 0;
}
// Risk check
var equity = Portfolio?.CurrentValue ?? InitialDeposit;
var riskExceeded = equity <= _riskThreshold && InitialDeposit > 0m;
if (riskExceeded)
{
if (_riskExceededCounter < 15)
{
LogWarning("Entry blocked. Risk limit of {0}% reached (equity={1:0.##}).", RiskPercent, equity);
_riskExceededCounter++;
}
}
else
{
_riskExceededCounter = 0;
}
// When SMA crosses in opposite direction, allow new entry of that type
if (_barsShortBelowMedium >= 72)
_enteredLong = false;
if (_barsShortAboveMedium >= 72)
_enteredShort = false;
if (Position == 0 && !riskExceeded)
{
// LONG: short crosses above medium, not already entered on this cross
if (_barsShortAboveMedium == 1 && candle.ClosePrice > smaS && !_enteredLong)
{
BuyMarket();
_barsSinceEntry = 0;
_enteredLong = true;
}
// SHORT: short crosses below medium, not already entered on this cross
else if (_barsShortBelowMedium == 1 && candle.ClosePrice < smaS && !_enteredShort)
{
SellMarket();
_barsSinceEntry = 0;
_enteredShort = true;
}
}
else if (Position != 0)
{
_barsSinceEntry++;
if (Position > 0)
{
var entryPrice = _entryPrice;
// Exit on reverse cross after min hold
var reverseCross = _barsShortBelowMedium >= 3 && _barsSinceEntry >= 30;
// Stop loss: 4%
var stopLoss = entryPrice > 0 && candle.ClosePrice < entryPrice * 0.96m;
// Take profit: 6%
var takeProfit = entryPrice > 0 && candle.ClosePrice > entryPrice * 1.06m;
if (reverseCross || stopLoss || takeProfit || riskExceeded)
{
SellMarket(Position.Abs());
}
}
else if (Position < 0)
{
var entryPrice = _entryPrice;
// Exit on reverse cross after min hold
var reverseCross = _barsShortAboveMedium >= 3 && _barsSinceEntry >= 30;
// Stop loss: 4%
var stopLoss = entryPrice > 0 && candle.ClosePrice > entryPrice * 1.04m;
// Take profit: 6%
var takeProfit = entryPrice > 0 && candle.ClosePrice < entryPrice * 0.94m;
if (reverseCross || stopLoss || takeProfit || riskExceeded)
{
BuyMarket(Position.Abs());
}
}
}
if (Position == 0)
{
_entryPrice = 0m;
_barsSinceEntry = 0;
}
}
protected override void OnOwnTradeReceived(MyTrade trade)
{
base.OnOwnTradeReceived(trade);
if (trade?.Trade == null) return;
if (Position != 0 && _entryPrice == 0m)
_entryPrice = trade.Trade.Price;
}
private void UpdateSma(SimpleMovingAverage sma, decimal input, DateTimeOffset time, ref decimal? curr, ref decimal? prev)
{
var indicatorValue = sma.Process(new DecimalIndicatorValue(sma, input, time.UtcDateTime) { IsFinal = true });
if (!sma.IsFormed || indicatorValue is not DecimalIndicatorValue decimalValue)
return;
prev = curr;
curr = decimalValue.Value;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Math
from System.Collections.Generic import List
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Strategies import Strategy
class SimpleSMA(object):
"""Manual Simple Moving Average calculator."""
def __init__(self, length):
self._length = length
self._buffer = []
self._sum = 0.0
@property
def IsFormed(self):
return len(self._buffer) >= self._length
def Process(self, value):
self._buffer.append(value)
self._sum += value
if len(self._buffer) > self._length:
self._sum -= self._buffer.pop(0)
if self.IsFormed:
return self._sum / self._length
return None
class reduce_risks_strategy(Strategy):
"""Trend-following strategy using SMA hierarchy (short/medium/long) for trend detection with risk control exits."""
def __init__(self):
super(reduce_risks_strategy, self).__init__()
self._stop_loss_pips = self.Param("StopLossPips", 30)
self._take_profit_pips = self.Param("TakeProfitPips", 60)
self._initial_deposit = self.Param("InitialDeposit", 1000000.0)
self._risk_percent = self.Param("RiskPercent", 5.0)
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5)))
self._sma_short = None
self._sma_medium = None
self._sma_long = None
self._sma_short_curr = None
self._sma_short_prev = None
self._sma_medium_curr = None
self._sma_medium_prev = None
self._sma_long_curr = None
self._sma_long_prev = None
self._risk_threshold = 0.0
self._risk_exceeded_counter = 0
self._bars_since_entry = 0
self._entry_price = 0.0
self._bars_short_above_medium = 0
self._bars_short_below_medium = 0
self._entered_long = False
self._entered_short = False
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
@property
def StopLossPips(self):
return self._stop_loss_pips.Value
@property
def TakeProfitPips(self):
return self._take_profit_pips.Value
@property
def InitialDeposit(self):
return self._initial_deposit.Value
@property
def RiskPercent(self):
return self._risk_percent.Value
def OnStarted2(self, time):
super(reduce_risks_strategy, self).OnStarted2(time)
self._risk_threshold = float(self.InitialDeposit) * (100.0 - float(self.RiskPercent)) / 100.0
self._sma_short = SimpleSMA(24)
self._sma_medium = SimpleSMA(72)
self._sma_long = SimpleSMA(144)
self._sma_short_curr = None
self._sma_short_prev = None
self._sma_medium_curr = None
self._sma_medium_prev = None
self._sma_long_curr = None
self._sma_long_prev = None
self._risk_exceeded_counter = 0
self._bars_since_entry = 0
self._entry_price = 0.0
self._bars_short_above_medium = 0
self._bars_short_below_medium = 0
self._entered_long = False
self._entered_short = False
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def _process_candle(self, candle):
if candle.State != CandleStates.Finished:
return
if self._sma_short is None or self._sma_medium is None or self._sma_long is None:
return
h = float(candle.HighPrice)
l = float(candle.LowPrice)
c = float(candle.ClosePrice)
typical = (h + l + c) / 3.0
# Update SMA short
val_s = self._sma_short.Process(typical)
if val_s is not None:
self._sma_short_prev = self._sma_short_curr
self._sma_short_curr = val_s
# Update SMA medium
val_m = self._sma_medium.Process(typical)
if val_m is not None:
self._sma_medium_prev = self._sma_medium_curr
self._sma_medium_curr = val_m
# Update SMA long
val_l = self._sma_long.Process(typical)
if val_l is not None:
self._sma_long_prev = self._sma_long_curr
self._sma_long_curr = val_l
sma_s = self._sma_short_curr
sma_m = self._sma_medium_curr
sma_l = self._sma_long_curr
if sma_s is None or sma_m is None or sma_l is None:
return
# Track consecutive bars of SMA position
if sma_s > sma_m:
self._bars_short_above_medium += 1
self._bars_short_below_medium = 0
else:
self._bars_short_below_medium += 1
self._bars_short_above_medium = 0
# Risk check
pf = self.Portfolio
equity = float(pf.CurrentValue) if pf is not None and pf.CurrentValue is not None else float(self.InitialDeposit)
initial_dep = float(self.InitialDeposit)
risk_exceeded = equity <= self._risk_threshold and initial_dep > 0
if risk_exceeded:
if self._risk_exceeded_counter < 15:
self._risk_exceeded_counter += 1
else:
self._risk_exceeded_counter = 0
# When SMA crosses in opposite direction, allow new entry of that type
if self._bars_short_below_medium >= 72:
self._entered_long = False
if self._bars_short_above_medium >= 72:
self._entered_short = False
pos = float(self.Position)
if pos == 0 and not risk_exceeded:
# LONG: short crosses above medium, not already entered on this cross
if self._bars_short_above_medium == 1 and c > sma_s and not self._entered_long:
self.BuyMarket()
self._bars_since_entry = 0
self._entered_long = True
# SHORT: short crosses below medium, not already entered on this cross
elif self._bars_short_below_medium == 1 and c < sma_s and not self._entered_short:
self.SellMarket()
self._bars_since_entry = 0
self._entered_short = True
elif pos != 0:
self._bars_since_entry += 1
if pos > 0:
entry_price = self._entry_price
# Exit on reverse cross after min hold
reverse_cross = self._bars_short_below_medium >= 3 and self._bars_since_entry >= 30
# Stop loss: 4%
stop_loss = entry_price > 0 and c < entry_price * 0.96
# Take profit: 6%
take_profit = entry_price > 0 and c > entry_price * 1.06
if reverse_cross or stop_loss or take_profit or risk_exceeded:
self.SellMarket(Math.Abs(self.Position))
elif pos < 0:
entry_price = self._entry_price
# Exit on reverse cross after min hold
reverse_cross = self._bars_short_above_medium >= 3 and self._bars_since_entry >= 30
# Stop loss: 4%
stop_loss = entry_price > 0 and c > entry_price * 1.04
# Take profit: 6%
take_profit = entry_price > 0 and c < entry_price * 0.94
if reverse_cross or stop_loss or take_profit or risk_exceeded:
self.BuyMarket(Math.Abs(self.Position))
if float(self.Position) == 0:
self._entry_price = 0.0
self._bars_since_entry = 0
def OnOwnTradeReceived(self, trade):
super(reduce_risks_strategy, self).OnOwnTradeReceived(trade)
if trade is None or trade.Trade is None:
return
if float(self.Position) != 0 and self._entry_price == 0.0:
self._entry_price = float(trade.Trade.Price)
def OnReseted(self):
super(reduce_risks_strategy, self).OnReseted()
self._sma_short = None
self._sma_medium = None
self._sma_long = None
self._sma_short_curr = None
self._sma_short_prev = None
self._sma_medium_curr = None
self._sma_medium_prev = None
self._sma_long_curr = None
self._sma_long_prev = None
self._risk_threshold = 0.0
self._risk_exceeded_counter = 0
self._bars_since_entry = 0
self._entry_price = 0.0
self._bars_short_above_medium = 0
self._bars_short_below_medium = 0
self._entered_long = False
self._entered_short = False
def CreateClone(self):
return reduce_risks_strategy()