Стратегия MovingUp
Стратегия реализует пересечение скользящих средних с дополнительными параметрами управления рисками.
Длинная позиция открывается при пересечении быстрой средней выше медленной, короткая — при обратном пересечении.
Параметры
- Fast MA (
FastLength): период быстрой скользящей средней.
- Slow MA (
SlowLength): период медленной скользящей средней.
- Use TP (
UseTakeProfit): включение правила фиксации прибыли.
- TP (
TakeProfit): расстояние в цене для фиксации прибыли.
- Use SL (
UseStopLoss): включение правила стоп-лосса.
- SL (
StopLoss): расстояние в цене для стоп-лосса.
- Use TS (
UseTrailingStop): включение трейлинг-стопа.
- TS (
TrailingStop): расстояние трейлинг-стопа в цене.
- Candle (
CandleType): тип свечей для расчёта.
Логика торговли
- Подписка на свечи и расчёт двух SMA индикаторов.
- Определение пересечений быстрый/медленный MA.
- Вход в лонг при пересечении быстрым MA выше медленного при отсутствии позиции.
- Вход в шорт при обратном пересечении при отсутствии короткой позиции.
- На каждой новой свече проверяются правила управления рисками:
- Фиксация прибыли при достижении заданного расстояния.
- Стоп-лосс при движении цены против позиции.
- Трейлинг-стоп защищает прибыль при благоприятном движении.
Оригинальная стратегия MQL
Исходный скрипт MQL4 ma_v_1_3_3.mq4 содержит дополнительные функции, такие как шаговое увеличение объёма и сложное управление позицией. Данная версия на C# концентрируется на базовой логике пересечения скользящих средних и основных правилах управления рисками.
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>
/// Moving average crossover strategy with risk management via StartProtection.
/// </summary>
public class MovingUpStrategy : Strategy
{
private readonly StrategyParam<int> _fastLength;
private readonly StrategyParam<int> _slowLength;
private readonly StrategyParam<decimal> _stopLoss;
private readonly StrategyParam<decimal> _takeProfit;
private readonly StrategyParam<DataType> _candleType;
private bool _isInitialized;
private bool _wasFastBelowSlow;
public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
public decimal StopLoss { get => _stopLoss.Value; set => _stopLoss.Value = value; }
public decimal TakeProfit { get => _takeProfit.Value; set => _takeProfit.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public MovingUpStrategy()
{
_fastLength = Param(nameof(FastLength), 13)
.SetGreaterThanZero()
.SetDisplay("Fast MA", "Fast MA period", "MA");
_slowLength = Param(nameof(SlowLength), 21)
.SetGreaterThanZero()
.SetDisplay("Slow MA", "Slow MA period", "MA");
_stopLoss = Param(nameof(StopLoss), 250m)
.SetGreaterThanZero()
.SetDisplay("SL", "Stop loss distance", "Risk");
_takeProfit = Param(nameof(TakeProfit), 500m)
.SetGreaterThanZero()
.SetDisplay("TP", "Take profit distance", "Risk");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle", "Candle type", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_isInitialized = default;
_wasFastBelowSlow = default;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var fastMa = new ExponentialMovingAverage { Length = FastLength };
var slowMa = new ExponentialMovingAverage { Length = SlowLength };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(fastMa, slowMa, ProcessCandle)
.Start();
StartProtection(
new Unit(StopLoss, UnitTypes.Absolute),
new Unit(TakeProfit, UnitTypes.Absolute));
}
private void ProcessCandle(ICandleMessage candle, decimal fast, decimal slow)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (!_isInitialized)
{
_wasFastBelowSlow = fast < slow;
_isInitialized = true;
return;
}
var isFastBelowSlow = fast < slow;
if (_wasFastBelowSlow != isFastBelowSlow)
{
if (!isFastBelowSlow && Position <= 0)
{
if (Position < 0)
BuyMarket();
BuyMarket();
}
else if (isFastBelowSlow && Position >= 0)
{
if (Position > 0)
SellMarket();
SellMarket();
}
_wasFastBelowSlow = isFastBelowSlow;
}
}
}
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, Unit, UnitTypes
from StockSharp.Algo.Indicators import ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class moving_up_strategy(Strategy):
def __init__(self):
super(moving_up_strategy, self).__init__()
self._fast_length = self.Param("FastLength", 13) \
.SetDisplay("Fast MA", "Fast MA period", "MA")
self._slow_length = self.Param("SlowLength", 21) \
.SetDisplay("Slow MA", "Slow MA period", "MA")
self._stop_loss = self.Param("StopLoss", 250.0) \
.SetDisplay("SL", "Stop loss distance", "Risk")
self._take_profit = self.Param("TakeProfit", 500.0) \
.SetDisplay("TP", "Take profit distance", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))) \
.SetDisplay("Candle", "Candle type", "General")
self._is_initialized = False
self._was_fast_below_slow = False
@property
def FastLength(self):
return self._fast_length.Value
@FastLength.setter
def FastLength(self, value):
self._fast_length.Value = value
@property
def SlowLength(self):
return self._slow_length.Value
@SlowLength.setter
def SlowLength(self, value):
self._slow_length.Value = value
@property
def StopLoss(self):
return self._stop_loss.Value
@StopLoss.setter
def StopLoss(self, value):
self._stop_loss.Value = value
@property
def TakeProfit(self):
return self._take_profit.Value
@TakeProfit.setter
def TakeProfit(self, value):
self._take_profit.Value = value
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
def OnStarted2(self, time):
super(moving_up_strategy, self).OnStarted2(time)
fast_ma = ExponentialMovingAverage()
fast_ma.Length = self.FastLength
slow_ma = ExponentialMovingAverage()
slow_ma.Length = self.SlowLength
self.SubscribeCandles(self.CandleType) \
.Bind(fast_ma, slow_ma, self.ProcessCandle) \
.Start()
self.StartProtection(
stopLoss=Unit(self.StopLoss, UnitTypes.Absolute),
takeProfit=Unit(self.TakeProfit, UnitTypes.Absolute)
)
def ProcessCandle(self, candle, fast_value, slow_value):
if candle.State != CandleStates.Finished:
return
fast = float(fast_value)
slow = float(slow_value)
if not self._is_initialized:
self._was_fast_below_slow = fast < slow
self._is_initialized = True
return
is_fast_below_slow = fast < slow
if self._was_fast_below_slow != is_fast_below_slow:
if not is_fast_below_slow and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif is_fast_below_slow and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._was_fast_below_slow = is_fast_below_slow
def OnReseted(self):
super(moving_up_strategy, self).OnReseted()
self._is_initialized = False
self._was_fast_below_slow = False
def CreateClone(self):
return moving_up_strategy()