Kaufen-und-Halten-Strategie
Diese Strategie eröffnet eine einzelne Long-Position zum angegebenen Startdatum und hält sie bis zum Enddatum, wodurch ein einfacher Kaufen-und-Halten-Ansatz umgesetzt wird.
Details
- Einstiegskriterien:
- Wenn die Kerzenzeit am oder nach dem Startdatum liegt, kauft die Strategie einmalig.
- Long/Short: Nur Long.
- Ausstiegskriterien:
- Wenn die Kerzenzeit das Enddatum erreicht oder überschreitet, wird die Position geschlossen.
- Stops: Keine.
- Standardwerte:
- Startdatum = 2018-01-01.
- Enddatum = 2069-12-31.
- Filter:
- Kategorie: Buy and Hold.
- Richtung: Long.
- Indikatoren: Keine.
- Stops: Nein.
- Komplexität: Niedrig.
- Zeitrahmen: Beliebig.
- Saisonalität: Nein.
- Neuronale Netze: Nein.
- Divergenz: Nein.
- Risikolevel: Hoch.
namespace StockSharp.Samples.Strategies;
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
/// <summary>
/// Buys once at the start date and holds the long until the end date.
/// </summary>
public class BuyAndHoldStrategy : Strategy
{
private readonly StrategyParam<DateTimeOffset> _startDate;
private readonly StrategyParam<DateTimeOffset> _endDate;
private readonly StrategyParam<DataType> _candleType;
private bool _entrySubmitted;
private bool _exitSubmitted;
public DateTimeOffset StartDate { get => _startDate.Value; set => _startDate.Value = value; }
public DateTimeOffset EndDate { get => _endDate.Value; set => _endDate.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public BuyAndHoldStrategy()
{
_startDate = Param(nameof(StartDate), new DateTimeOffset(2018, 1, 1, 0, 0, 0, TimeSpan.Zero))
.SetDisplay("Start Date", "Buy once on or after this date", "General");
_endDate = Param(nameof(EndDate), new DateTimeOffset(2069, 12, 31, 0, 0, 0, TimeSpan.Zero))
.SetDisplay("End Date", "Close the long on or after this date", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_entrySubmitted = _exitSubmitted = false;
}
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 || !IsFormedAndOnlineAndAllowTrading())
return;
if (candle.OpenTime >= EndDate.UtcDateTime)
{
if (!_exitSubmitted && Position > 0)
{
SellMarket(Position);
_exitSubmitted = true;
}
}
else if (!_entrySubmitted && candle.OpenTime >= StartDate.UtcDateTime && Position == 0)
{
BuyMarket();
// Remember the submitted entry even while its fill is still pending.
_entrySubmitted = true;
}
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Strategies")
from System import DateTimeOffset, TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Strategies import Strategy
class buy_and_hold_strategy(Strategy):
"""Buys once at the start date and holds the long until the end date."""
def __init__(self):
super(buy_and_hold_strategy, self).__init__()
self._start_date = self.Param("StartDate", DateTimeOffset(2018, 1, 1, 0, 0, 0, TimeSpan.Zero)) \
.SetDisplay("Start Date", "Buy once on or after this date", "General")
self._end_date = self.Param("EndDate", DateTimeOffset(2069, 12, 31, 0, 0, 0, TimeSpan.Zero)) \
.SetDisplay("End Date", "Close the long on or after this date", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(1))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._entry_submitted = False
self._exit_submitted = False
@property
def candle_type(self):
return self._candle_type.Value
def GetWorkingSecurities(self):
return [(self.Security, self.candle_type)]
def OnReseted(self):
super(buy_and_hold_strategy, self).OnReseted()
self._entry_submitted = False
self._exit_submitted = False
def OnStarted2(self, time):
super(buy_and_hold_strategy, self).OnStarted2(time)
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 or not self.IsFormedAndOnlineAndAllowTrading():
return
if candle.OpenTime >= self._end_date.Value.UtcDateTime:
if not self._exit_submitted and self.Position > 0:
self.SellMarket(self.Position)
self._exit_submitted = True
elif (not self._entry_submitted and candle.OpenTime >= self._start_date.Value.UtcDateTime
and self.Position == 0):
self.BuyMarket()
# Remember the submitted entry even while its fill is still pending.
self._entry_submitted = True
def CreateClone(self):
return buy_and_hold_strategy()