Seasonality Adjusted Momentum
Seasonality Adjusted Momentum 策略基于相关指标构建。
测试表明年均收益约为 172%,该策略在外汇市场表现最佳。
当指标在指定周期的数据上确认条件时触发信号,适合积极交易者。
止损依赖ATR倍数及其他参数,可根据需要调整默认值以平衡风险和收益。
详细信息
- 入场条件: see implementation for indicator conditions.
- 多空: Both directions.
- 出场条件: opposite signal or stop logic.
- 止损: Yes, using indicator-based calculations.
- 默认值:
MomentumPeriod = 14SeasonalityThreshold = 0.5mStopLossPercent = 2.0mCandleType = TimeSpan.FromMinutes(5).TimeFrame()
- 过滤器:
- 分类: Trend following
- 方向: Both
- 指标: Seasonality, Adjusted
- 止损: Yes
- 复杂度: Intermediate
- 时间框架: Daily
- 季节性: Yes
- 神经网络: No
- 背离: No
- 风险级别: Medium
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Momentum strategy that allows longs or shorts only when the current month historically supports that seasonal bias.
/// </summary>
public class SeasonalityAdjustedMomentumStrategy : Strategy
{
private readonly StrategyParam<int> _momentumPeriod;
private readonly StrategyParam<decimal> _seasonalityThreshold;
private readonly StrategyParam<decimal> _stopLossPercent;
private readonly StrategyParam<int> _cooldownBars;
private readonly StrategyParam<DataType> _candleType;
private readonly Dictionary<int, decimal> _seasonalStrengthByMonth = [];
private Momentum _momentum;
private SimpleMovingAverage _momentumAverage;
private int _cooldown;
/// <summary>
/// Period for the momentum indicator.
/// </summary>
public int MomentumPeriod
{
get => _momentumPeriod.Value;
set => _momentumPeriod.Value = value;
}
/// <summary>
/// Minimum absolute seasonality strength required to allow directional entries.
/// </summary>
public decimal SeasonalityThreshold
{
get => _seasonalityThreshold.Value;
set => _seasonalityThreshold.Value = value;
}
/// <summary>
/// Stop loss percentage.
/// </summary>
public decimal StopLossPercent
{
get => _stopLossPercent.Value;
set => _stopLossPercent.Value = value;
}
/// <summary>
/// Bars to wait after each order.
/// </summary>
public int CooldownBars
{
get => _cooldownBars.Value;
set => _cooldownBars.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes strategy parameters.
/// </summary>
public SeasonalityAdjustedMomentumStrategy()
{
_momentumPeriod = Param(nameof(MomentumPeriod), 14)
.SetRange(3, 100)
.SetDisplay("Momentum Period", "Period for the momentum indicator", "Indicators");
_seasonalityThreshold = Param(nameof(SeasonalityThreshold), 0.2m)
.SetRange(0m, 1m)
.SetDisplay("Seasonality Threshold", "Minimum absolute seasonality strength required for entries", "Signals");
_stopLossPercent = Param(nameof(StopLossPercent), 2m)
.SetRange(0.5m, 10m)
.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk");
_cooldownBars = Param(nameof(CooldownBars), 120)
.SetRange(1, 500)
.SetDisplay("Cooldown Bars", "Bars to wait after each order", "Risk");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles for the strategy", "General");
InitializeSeasonalityData();
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
if (Security != null)
yield return (Security, CandleType);
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_momentum = null;
_momentumAverage = null;
_cooldown = 0;
_seasonalStrengthByMonth.Clear();
InitializeSeasonalityData();
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
if (Security == null)
throw new InvalidOperationException("Security is not specified.");
_momentum = new Momentum { Length = MomentumPeriod };
_momentumAverage = new SimpleMovingAverage { Length = MomentumPeriod };
_cooldown = 0;
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(_momentum, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, _momentum);
DrawIndicator(area, _momentumAverage);
DrawOwnTrades(area);
}
StartProtection(new Unit(0, UnitTypes.Absolute), new Unit(StopLossPercent, UnitTypes.Percent), false);
}
private void ProcessCandle(ICandleMessage candle, decimal momentumValue)
{
if (candle.State != CandleStates.Finished)
return;
var momentumAvgValue = _momentumAverage.Process(momentumValue, candle.OpenTime, true).ToDecimal();
if (!_momentum.IsFormed || !_momentumAverage.IsFormed)
return;
if (ProcessState != ProcessStates.Started)
return;
if (_cooldown > 0)
{
_cooldown--;
return;
}
var seasonalStrength = GetSeasonalStrength(candle.OpenTime.Month);
var allowLong = seasonalStrength >= SeasonalityThreshold;
var allowShort = seasonalStrength <= -SeasonalityThreshold;
var bullishMomentum = momentumValue > momentumAvgValue;
var bearishMomentum = momentumValue < momentumAvgValue;
if (Position > 0)
{
if (!allowLong || bearishMomentum)
{
SellMarket(Math.Abs(Position));
_cooldown = CooldownBars;
}
return;
}
if (Position < 0)
{
if (!allowShort || bullishMomentum)
{
BuyMarket(Math.Abs(Position));
_cooldown = CooldownBars;
}
return;
}
if (allowLong && bullishMomentum)
{
BuyMarket();
_cooldown = CooldownBars;
}
else if (allowShort && bearishMomentum)
{
SellMarket();
_cooldown = CooldownBars;
}
}
private decimal GetSeasonalStrength(int month)
=> _seasonalStrengthByMonth.TryGetValue(month, out var strength) ? strength : 0m;
private void InitializeSeasonalityData()
{
_seasonalStrengthByMonth[1] = 0.8m;
_seasonalStrengthByMonth[2] = 0.2m;
_seasonalStrengthByMonth[3] = 0.5m;
_seasonalStrengthByMonth[4] = 0.7m;
_seasonalStrengthByMonth[5] = 0.3m;
_seasonalStrengthByMonth[6] = -0.2m;
_seasonalStrengthByMonth[7] = -0.3m;
_seasonalStrengthByMonth[8] = -0.4m;
_seasonalStrengthByMonth[9] = -0.7m;
_seasonalStrengthByMonth[10] = 0.4m;
_seasonalStrengthByMonth[11] = 0.6m;
_seasonalStrengthByMonth[12] = 0.9m;
}
}
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, Decimal
from StockSharp.Messages import DataType, CandleStates, Unit, UnitTypes
from StockSharp.Algo.Indicators import Momentum, SimpleMovingAverage
from StockSharp.Algo.Strategies import Strategy
from indicator_extensions import *
class seasonality_adjusted_momentum_strategy(Strategy):
"""
Momentum strategy that allows longs or shorts only when the current month
historically supports that seasonal bias.
"""
def __init__(self):
super(seasonality_adjusted_momentum_strategy, self).__init__()
self._momentum_period = self.Param("MomentumPeriod", 14) \
.SetDisplay("Momentum Period", "Period for the momentum indicator", "Indicators")
self._seasonality_threshold = self.Param("SeasonalityThreshold", 0.2) \
.SetDisplay("Seasonality Threshold", "Minimum absolute seasonality strength required for entries", "Signals")
self._stop_loss_percent = self.Param("StopLossPercent", 2.0) \
.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk")
self._cooldown_bars = self.Param("CooldownBars", 120) \
.SetDisplay("Cooldown Bars", "Bars to wait after each order", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Type of candles for the strategy", "General")
self._seasonal_strength_by_month = {}
self._momentum = None
self._momentum_average = None
self._cooldown = 0
self._initialize_seasonality_data()
@property
def candle_type(self):
return self._candle_type.Value
def _initialize_seasonality_data(self):
self._seasonal_strength_by_month[1] = 0.8
self._seasonal_strength_by_month[2] = 0.2
self._seasonal_strength_by_month[3] = 0.5
self._seasonal_strength_by_month[4] = 0.7
self._seasonal_strength_by_month[5] = 0.3
self._seasonal_strength_by_month[6] = -0.2
self._seasonal_strength_by_month[7] = -0.3
self._seasonal_strength_by_month[8] = -0.4
self._seasonal_strength_by_month[9] = -0.7
self._seasonal_strength_by_month[10] = 0.4
self._seasonal_strength_by_month[11] = 0.6
self._seasonal_strength_by_month[12] = 0.9
def OnReseted(self):
super(seasonality_adjusted_momentum_strategy, self).OnReseted()
self._momentum = None
self._momentum_average = None
self._cooldown = 0
self._seasonal_strength_by_month.clear()
self._initialize_seasonality_data()
def OnStarted2(self, time):
super(seasonality_adjusted_momentum_strategy, self).OnStarted2(time)
mp = int(self._momentum_period.Value)
self._momentum = Momentum()
self._momentum.Length = mp
self._momentum_average = SimpleMovingAverage()
self._momentum_average.Length = mp
self._cooldown = 0
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self._momentum, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, self._momentum)
self.DrawIndicator(area, self._momentum_average)
self.DrawOwnTrades(area)
self.StartProtection(Unit(0, UnitTypes.Absolute), Unit(self._stop_loss_percent.Value, UnitTypes.Percent), False)
def _process_candle(self, candle, momentum_value):
if candle.State != CandleStates.Finished:
return
mv = float(momentum_value)
momentum_avg_val = float(process_float(self._momentum_average, Decimal(mv), candle.OpenTime, True))
if not self._momentum.IsFormed or not self._momentum_average.IsFormed:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
if self._cooldown > 0:
self._cooldown -= 1
return
seasonal_strength = self._seasonal_strength_by_month.get(candle.OpenTime.Month, 0.0)
st = float(self._seasonality_threshold.Value)
allow_long = seasonal_strength >= st
allow_short = seasonal_strength <= -st
bullish_momentum = mv > momentum_avg_val
bearish_momentum = mv < momentum_avg_val
cd = int(self._cooldown_bars.Value)
if self.Position > 0:
if not allow_long or bearish_momentum:
self.SellMarket(Math.Abs(self.Position))
self._cooldown = cd
return
if self.Position < 0:
if not allow_short or bullish_momentum:
self.BuyMarket(Math.Abs(self.Position))
self._cooldown = cd
return
if allow_long and bullish_momentum:
self.BuyMarket()
self._cooldown = cd
elif allow_short and bearish_momentum:
self.SellMarket()
self._cooldown = cd
def CreateClone(self):
return seasonality_adjusted_momentum_strategy()