Стратегия Payday Anomaly
Стратегия открывает длинную позицию в выбранные дни месяца (1‑е, 2‑е, 16‑е и 31‑е) и закрывает позицию на следующий день.
Детали
- Условия входа:
- Лонг: открыть длинную позицию в выбранные дни месяца.
- Лонг/Шорт: только лонг.
- Условия выхода:
- закрыть длинную позицию, когда день не выбран.
- Стопы: нет.
- Значения по умолчанию:
Trade1st= true.Trade2nd= true.Trade16th= true.Trade31st= true.CandleType= TimeSpan.FromMinutes(5).TimeFrame().
- Фильтры:
- Категория: Сезонность
- Направление: Лонг
- Индикаторы: Нет
- Стопы: Нет
- Сложность: базовая
- Таймфрейм: дневной
- Сезонность: да
- Нейросети: нет
- Дивергенция: нет
- Уровень риска: низкий
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;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Payday anomaly strategy.
/// Opens long positions on selected days of the month and closes on other days.
/// </summary>
public class PaydayAnomaly2Strategy : Strategy
{
private readonly StrategyParam<bool> _trade1st;
private readonly StrategyParam<bool> _trade2nd;
private readonly StrategyParam<bool> _trade16th;
private readonly StrategyParam<bool> _trade31st;
private readonly StrategyParam<DataType> _candleType;
private bool _tradeOpened;
/// <summary>
/// Trade on the 1st day of the month.
/// </summary>
public bool Trade1st
{
get => _trade1st.Value;
set => _trade1st.Value = value;
}
/// <summary>
/// Trade on the 2nd day of the month.
/// </summary>
public bool Trade2nd
{
get => _trade2nd.Value;
set => _trade2nd.Value = value;
}
/// <summary>
/// Trade on the 16th day of the month.
/// </summary>
public bool Trade16th
{
get => _trade16th.Value;
set => _trade16th.Value = value;
}
/// <summary>
/// Trade on the 31st day of the month.
/// </summary>
public bool Trade31st
{
get => _trade31st.Value;
set => _trade31st.Value = value;
}
/// <summary>
/// Candle type used for strategy calculations.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes a new instance of the <see cref="PaydayAnomalyStrategy"/> class.
/// </summary>
public PaydayAnomaly2Strategy()
{
_trade1st = Param(nameof(Trade1st), true)
.SetDisplay("Trade 1st", "Trade on the 1st day", "General")
;
_trade2nd = Param(nameof(Trade2nd), true)
.SetDisplay("Trade 2nd", "Trade on the 2nd day", "General")
;
_trade16th = Param(nameof(Trade16th), true)
.SetDisplay("Trade 16th", "Trade on the 16th day", "General")
;
_trade31st = Param(nameof(Trade31st), true)
.SetDisplay("Trade 31st", "Trade on the 31st day", "General")
;
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).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();
_tradeOpened = false;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished)
return;
var day = candle.OpenTime.Day;
var isTargetDay = (day == 1 && Trade1st)
|| (day == 2 && Trade2nd)
|| (day == 16 && Trade16th)
|| (day == 31 && Trade31st);
if (isTargetDay && !_tradeOpened && Position <= 0)
{
BuyMarket();
_tradeOpened = true;
}
else if (!isTargetDay && _tradeOpened && Position > 0)
{
SellMarket();
_tradeOpened = false;
}
}
}
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
from StockSharp.Algo.Strategies import Strategy
class payday_anomaly2_strategy(Strategy):
def __init__(self):
super(payday_anomaly2_strategy, self).__init__()
self._trade_1st = self.Param("Trade1st", True)
self._trade_2nd = self.Param("Trade2nd", True)
self._trade_16th = self.Param("Trade16th", True)
self._trade_31st = self.Param("Trade31st", True)
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5)))
self._trade_opened = False
@property
def candle_type(self):
return self._candle_type.Value
@candle_type.setter
def candle_type(self, value):
self._candle_type.Value = value
def OnReseted(self):
super(payday_anomaly2_strategy, self).OnReseted()
self._trade_opened = False
def OnStarted2(self, time):
super(payday_anomaly2_strategy, self).OnStarted2(time)
self._trade_opened = False
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self.OnProcess).Start()
def OnProcess(self, candle):
if candle.State != CandleStates.Finished:
return
day = candle.OpenTime.Day
is_target_day = (day == 1 and self._trade_1st.Value) \
or (day == 2 and self._trade_2nd.Value) \
or (day == 16 and self._trade_16th.Value) \
or (day == 31 and self._trade_31st.Value)
if is_target_day and not self._trade_opened and self.Position <= 0:
self.BuyMarket()
self._trade_opened = True
elif not is_target_day and self._trade_opened and self.Position > 0:
self.SellMarket()
self._trade_opened = False
def CreateClone(self):
return payday_anomaly2_strategy()