节后疲软策略
节后疲软是指在主要假日结束后,由于成交清淡,价格往往缓慢下行。 很多交易者尚未回归,逆势波动因此更易获得动量。 本策略在节假日后的第一天卖空,并在市场参与度恢复后迅速回补。 采用小幅止损,避免低流动性期间产生过大亏损。
测试表明年均收益约为 112%,该策略在外汇市场表现最佳。
细节
- 入场条件:日历效应触发
- 多/空:均可
- 退出条件:止损或反向信号
- 止损:是,按百分比
- 默认值:
CandleType= 15分钟StopLoss= 2%
- 过滤器:
- 类别:季节性
- 方向:双向
- 指标:季节性
- 止损:有
- 复杂度:中等
- 时间框架:日内
- 季节性:是
- 神经网络:否
- 背离:否
- 风险等级:中等
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>
/// Implementation of Post-Holiday Weakness trading strategy.
/// Sells short on Monday (post-weekend weakness) if below MA, covers Wednesday.
/// Buys on Wednesday if above MA, exits Friday.
/// </summary>
public class PostHolidayWeaknessStrategy : Strategy
{
private readonly StrategyParam<int> _maPeriod;
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _cooldownBars;
private SimpleMovingAverage _ma;
private int _cooldown;
private DayOfWeek _prevDayOfWeek;
private bool _enteredThisDay;
/// <summary>
/// Moving average period.
/// </summary>
public int MaPeriod
{
get => _maPeriod.Value;
set => _maPeriod.Value = value;
}
/// <summary>
/// Candle type for strategy.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Cooldown bars between trades.
/// </summary>
public int CooldownBars
{
get => _cooldownBars.Value;
set => _cooldownBars.Value = value;
}
/// <summary>
/// Initializes a new instance of the <see cref="PostHolidayWeaknessStrategy"/>.
/// </summary>
public PostHolidayWeaknessStrategy()
{
_maPeriod = Param(nameof(MaPeriod), 20)
.SetGreaterThanZero()
.SetDisplay("MA Period", "Moving average period", "Strategy");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles for strategy", "Strategy");
_cooldownBars = Param(nameof(CooldownBars), 30)
.SetDisplay("Cooldown Bars", "Bars between trades", "General")
.SetRange(5, 500);
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_ma = default;
_cooldown = 0;
_prevDayOfWeek = DayOfWeek.Sunday;
_enteredThisDay = false;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_ma = new SimpleMovingAverage { Length = MaPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(_ma, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, _ma);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal maValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var close = candle.ClosePrice;
var dayOfWeek = candle.OpenTime.DayOfWeek;
if (dayOfWeek != _prevDayOfWeek)
_enteredThisDay = false;
if (_cooldown > 0)
{
_cooldown--;
_prevDayOfWeek = dayOfWeek;
return;
}
// Monday: post-weekend weakness - short if below MA
if (dayOfWeek == DayOfWeek.Monday && !_enteredThisDay && Position == 0 && close < maValue)
{
SellMarket();
_cooldown = CooldownBars;
_enteredThisDay = true;
}
// Wednesday: cover short
else if (dayOfWeek == DayOfWeek.Wednesday && Position < 0 && !_enteredThisDay)
{
BuyMarket();
_cooldown = CooldownBars;
_enteredThisDay = true;
}
// Wednesday: buy if above MA
else if (dayOfWeek == DayOfWeek.Wednesday && !_enteredThisDay && Position == 0 && close > maValue)
{
BuyMarket();
_cooldown = CooldownBars;
_enteredThisDay = true;
}
// Friday: exit long
else if (dayOfWeek == DayOfWeek.Friday && Position > 0 && !_enteredThisDay)
{
SellMarket();
_cooldown = CooldownBars;
_enteredThisDay = true;
}
_prevDayOfWeek = dayOfWeek;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, DayOfWeek
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import SimpleMovingAverage
from StockSharp.Algo.Strategies import Strategy
class post_holiday_weakness_strategy(Strategy):
"""
Post-Holiday Weakness trading strategy.
Sells short on Monday (post-weekend weakness) if below MA, covers Wednesday.
Buys on Wednesday if above MA, exits Friday.
"""
def __init__(self):
super(post_holiday_weakness_strategy, self).__init__()
self._ma_period = self.Param("MaPeriod", 20).SetDisplay("MA Period", "Moving average period", "Strategy")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))).SetDisplay("Candle Type", "Type of candles for strategy", "Strategy")
self._cooldown_bars = self.Param("CooldownBars", 30).SetDisplay("Cooldown Bars", "Bars between trades", "General")
self._cooldown = 0
self._prev_day_of_week = DayOfWeek.Sunday
self._entered_this_day = False
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(post_holiday_weakness_strategy, self).OnReseted()
self._cooldown = 0
self._prev_day_of_week = DayOfWeek.Sunday
self._entered_this_day = False
def OnStarted2(self, time):
super(post_holiday_weakness_strategy, self).OnStarted2(time)
self._cooldown = 0
self._prev_day_of_week = DayOfWeek.Sunday
self._entered_this_day = False
sma = SimpleMovingAverage()
sma.Length = self._ma_period.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(sma, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, sma)
self.DrawOwnTrades(area)
def _process_candle(self, candle, ma_val):
if candle.State != CandleStates.Finished:
return
close = float(candle.ClosePrice)
ma = float(ma_val)
day_of_week = candle.OpenTime.DayOfWeek
cd = self._cooldown_bars.Value
if day_of_week != self._prev_day_of_week:
self._entered_this_day = False
if self._cooldown > 0:
self._cooldown -= 1
self._prev_day_of_week = day_of_week
return
# Monday: post-weekend weakness - short if below MA
if day_of_week == DayOfWeek.Monday and not self._entered_this_day and self.Position == 0 and close < ma:
self.SellMarket()
self._cooldown = cd
self._entered_this_day = True
# Wednesday: cover short
elif day_of_week == DayOfWeek.Wednesday and self.Position < 0 and not self._entered_this_day:
self.BuyMarket()
self._cooldown = cd
self._entered_this_day = True
# Wednesday: buy if above MA
elif day_of_week == DayOfWeek.Wednesday and not self._entered_this_day and self.Position == 0 and close > ma:
self.BuyMarket()
self._cooldown = cd
self._entered_this_day = True
# Friday: exit long
elif day_of_week == DayOfWeek.Friday and self.Position > 0 and not self._entered_this_day:
self.SellMarket()
self._cooldown = cd
self._entered_this_day = True
self._prev_day_of_week = day_of_week
def CreateClone(self):
return post_holiday_weakness_strategy()