Montags-Eröffnungs-Strategie
Diese Strategie kauft zu Beginn der Woche und schließt die Position am Dienstag innerhalb eines festgelegten Jahreszeitraums.
Details
- Einstiegskriterien:
- Long: Long-Position am Montag eröffnen.
- Long/Short: Nur Long.
- Ausstiegskriterien:
- Long-Position am Dienstag schließen.
- Stops: Nein.
- Standardwerte:
StartYear= 2023.EndYear= 2025.CandleType= TimeSpan.FromMinutes(5).TimeFrame().
- Filter:
- Kategorie: Saisonalität
- Richtung: Long
- Indikatoren: Keine
- Stops: Nein
- Komplexität: Grundlegend
- Zeitrahmen: Täglich
- Saisonalität: Ja
- Neuronale Netze: Nein
- Divergenz: Nein
- Risikolevel: Niedrig
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Monday open strategy.
/// Buys at the beginning of the week on Monday and closes the long position at Tuesday's close,
/// trading only in years from StartYear to EndYear.
/// </summary>
public class MondayOpenStrategy : Strategy
{
private readonly StrategyParam<int> _startYear;
private readonly StrategyParam<int> _endYear;
private readonly StrategyParam<DataType> _candleType;
private DateTime? _lastEntryDate;
/// <summary>
/// First year in which trading is allowed.
/// </summary>
public int StartYear
{
get => _startYear.Value;
set => _startYear.Value = value;
}
/// <summary>
/// Last year in which trading is allowed.
/// </summary>
public int EndYear
{
get => _endYear.Value;
set => _endYear.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public MondayOpenStrategy()
{
_startYear = Param(nameof(StartYear), 2023)
.SetDisplay("Start Year", "First year in which trading is allowed", "General");
_endYear = Param(nameof(EndYear), 2025)
.SetDisplay("End Year", "Last year in which trading is allowed", "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();
_lastEntryDate = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_lastEntryDate = null;
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;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var openTime = candle.OpenTime;
var closeTime = CandleType.Arg is TimeSpan frame ? openTime + frame : openTime;
var day = openTime.DayOfWeek;
if (Position > 0)
{
// Exit on the candle that completes Tuesday, or on the first later candle if that one was missing.
var tuesdayClosed = day == DayOfWeek.Tuesday && closeTime.Date > openTime.Date;
if (tuesdayClosed || (day != DayOfWeek.Monday && day != DayOfWeek.Tuesday))
SellMarket(Position);
return;
}
var year = openTime.Year;
if (year < StartYear || year > EndYear)
return;
if (day == DayOfWeek.Monday && _lastEntryDate != openTime.Date && Position == 0)
{
BuyMarket(Volume);
_lastEntryDate = openTime.Date;
}
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, DayOfWeek
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Strategies import Strategy
class monday_open_strategy(Strategy):
"""
Monday open strategy.
Buys at the beginning of the week on Monday and closes the long position at Tuesday's close,
trading only in years from StartYear to EndYear.
"""
def __init__(self):
super(monday_open_strategy, self).__init__()
self._start_year = self.Param("StartYear", 2023).SetDisplay("Start Year", "First year in which trading is allowed", "General")
self._end_year = self.Param("EndYear", 2025).SetDisplay("End Year", "Last year in which trading is allowed", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))).SetDisplay("Candle Type", "Type of candles to use", "General")
self._last_entry_date = None
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(monday_open_strategy, self).OnReseted()
self._last_entry_date = None
def OnStarted2(self, time):
super(monday_open_strategy, self).OnStarted2(time)
self._last_entry_date = None
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def _process_candle(self, candle):
if candle.State != CandleStates.Finished:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
open_time = candle.OpenTime
frame = self.candle_type.Arg
close_time = open_time + frame if isinstance(frame, TimeSpan) else open_time
day = open_time.DayOfWeek
if self.Position > 0:
# Exit on the candle that completes Tuesday, or on the first later candle if that one was missing.
tuesday_closed = day == DayOfWeek.Tuesday and close_time.Date > open_time.Date
if tuesday_closed or (day != DayOfWeek.Monday and day != DayOfWeek.Tuesday):
self.SellMarket(self.Position)
return
year = open_time.Year
if year < self._start_year.Value or year > self._end_year.Value:
return
if day == DayOfWeek.Monday and self._last_entry_date != open_time.Date and self.Position == 0:
self.BuyMarket(self.Volume)
self._last_entry_date = open_time.Date
def CreateClone(self):
return monday_open_strategy()