Стратегия Moving Average Crossover
Покупает, когда короткая SMA пересекает длинную SMA снизу вверх, и продаёт при обратном пересечении. Позиции переворачиваются при противоположных сигналах.
Детали
- Условия входа:
- Лонг при пересечении короткой SMA выше длинной.
- Шорт при пересечении короткой SMA ниже длинной.
- Длинные/Короткие: обе стороны.
- Условия выхода: переворот при противоположном пересечении.
- Стопы: нет.
- Значения по умолчанию:
ShortLength= 9LongLength= 21CandleType= TimeSpan.FromMinutes(1)
- Фильтры:
- Категория: Crossover
- Направление: Обе стороны
- Индикаторы: SMA
- Стопы: Нет
- Сложность: Базовая
- Таймфрейм: Внутридневной
- Сезонность: Нет
- Нейросети: Нет
- Дивергенция: Нет
- Риск: Средний
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.
/// Goes long when the short SMA crosses above the long SMA and short when it crosses below,
/// reversing the position on the opposite crossover.
/// </summary>
public class MovingAverageCrossoverStrategy : Strategy
{
private readonly StrategyParam<int> _shortLength;
private readonly StrategyParam<int> _longLength;
private readonly StrategyParam<DataType> _candleType;
private decimal? _prevShort;
private decimal? _prevLong;
/// <summary>
/// Short SMA length.
/// </summary>
public int ShortLength
{
get => _shortLength.Value;
set => _shortLength.Value = value;
}
/// <summary>
/// Long SMA length.
/// </summary>
public int LongLength
{
get => _longLength.Value;
set => _longLength.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public MovingAverageCrossoverStrategy()
{
_shortLength = Param(nameof(ShortLength), 9)
.SetGreaterThanZero()
.SetDisplay("Short Length", "Short SMA length", "Indicators");
_longLength = Param(nameof(LongLength), 21)
.SetGreaterThanZero()
.SetDisplay("Long Length", "Long SMA length", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevShort = null;
_prevLong = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevShort = null;
_prevLong = null;
var shortSma = new SimpleMovingAverage { Length = ShortLength };
var longSma = new SimpleMovingAverage { Length = LongLength };
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(shortSma, longSma, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, shortSma);
DrawIndicator(area, longSma);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue shortValue, IIndicatorValue longValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!shortValue.IsFormed || !longValue.IsFormed)
return;
var shortMa = shortValue.GetValue<decimal>();
var longMa = longValue.GetValue<decimal>();
var prevShort = _prevShort;
var prevLong = _prevLong;
_prevShort = shortMa;
_prevLong = longMa;
if (prevShort is not decimal ps || prevLong is not decimal pl)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var crossUp = ps <= pl && shortMa > longMa;
var crossDown = ps >= pl && shortMa < longMa;
if (crossUp && Position <= 0)
BuyMarket(Volume + Math.Abs(Position));
else if (crossDown && Position >= 0)
SellMarket(Volume + Math.Abs(Position));
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Decimal
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import SimpleMovingAverage
from StockSharp.Algo.Strategies import Strategy
class moving_average_crossover_strategy(Strategy):
"""
Moving average crossover strategy.
Goes long when the short SMA crosses above the long SMA and short when it crosses below,
reversing the position on the opposite crossover.
"""
def __init__(self):
super(moving_average_crossover_strategy, self).__init__()
self._short_length = self.Param("ShortLength", 9).SetGreaterThanZero().SetDisplay("Short Length", "Short SMA length", "Indicators")
self._long_length = self.Param("LongLength", 21).SetGreaterThanZero().SetDisplay("Long Length", "Long SMA length", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(1))).SetDisplay("Candle Type", "Type of candles to use", "General")
self._reset_state()
@property
def candle_type(self):
return self._candle_type.Value
def _reset_state(self):
self._prev_short = None
self._prev_long = None
def OnReseted(self):
super(moving_average_crossover_strategy, self).OnReseted()
self._reset_state()
def OnStarted2(self, time):
super(moving_average_crossover_strategy, self).OnStarted2(time)
self._reset_state()
short_sma = SimpleMovingAverage()
short_sma.Length = self._short_length.Value
long_sma = SimpleMovingAverage()
long_sma.Length = self._long_length.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(short_sma, long_sma, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, short_sma)
self.DrawIndicator(area, long_sma)
self.DrawOwnTrades(area)
def _process_candle(self, candle, short_value, long_value):
if candle.State != CandleStates.Finished:
return
if not short_value.IsFormed or not long_value.IsFormed:
return
short_ma = short_value.GetValue[Decimal](None)
long_ma = long_value.GetValue[Decimal](None)
prev_short = self._prev_short
prev_long = self._prev_long
self._prev_short = short_ma
self._prev_long = long_ma
if prev_short is None or prev_long is None:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
cross_up = prev_short <= prev_long and short_ma > long_ma
cross_down = prev_short >= prev_long and short_ma < long_ma
if cross_up and self.Position <= 0:
self.BuyMarket(self.Volume + abs(self.Position))
elif cross_down and self.Position >= 0:
self.SellMarket(self.Volume + abs(self.Position))
def CreateClone(self):
return moving_average_crossover_strategy()