Mateo's Time of Day Analysis LE
Открывает длинную позицию в заданном внутридневном окне и закрывает её позже в течение дня.
Эта стратегия помогает исследовать влияние времени суток.
Детали
- Условия входа: Время достигает
StartTimeв пределах диапазонаFrom–Thru. - Long/Short: Только long.
- Условия выхода: Время достигает
EndTime(до 20:00). - Стопы: Нет.
- Значения по умолчанию:
CandleType= TimeSpan.FromMinutes(1)StartTime= 09:30EndTime= 16:00From= 2017-04-21Thru= 2099-12-01
- Фильтры:
- Category: Time-based
- Direction: Long
- Indicators: None
- Stops: No
- Complexity: Basic
- Timeframe: Intraday
- Seasonality: No
- Neural Networks: No
- Divergence: No
- Risk Level: Low
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;
public class MateosTimeOfDayAnalysisLeStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _startHour;
private readonly StrategyParam<int> _endHour;
private DateTime _entryDate;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int StartHour { get => _startHour.Value; set => _startHour.Value = value; }
public int EndHour { get => _endHour.Value; set => _endHour.Value = value; }
public MateosTimeOfDayAnalysisLeStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame());
_startHour = Param(nameof(StartHour), 9);
_endHour = Param(nameof(EndHour), 16);
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_entryDate = default;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_entryDate = default;
var dummyEma1 = new ExponentialMovingAverage { Length = 10 };
var dummyEma2 = new ExponentialMovingAverage { Length = 20 };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(dummyEma1, dummyEma2, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal d1, decimal d2)
{
if (candle.State != CandleStates.Finished)
return;
var hour = candle.ServerTime.Hour;
var date = candle.ServerTime.Date;
if (hour >= StartHour && hour < EndHour)
{
if (Position <= 0 && _entryDate != date)
{
BuyMarket();
_entryDate = date;
}
}
else if (hour >= EndHour || hour < StartHour)
{
if (Position > 0)
{
SellMarket();
_entryDate = default;
}
}
}
}
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.Indicators import ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class mateos_time_of_day_analysis_le_strategy(Strategy):
def __init__(self):
super(mateos_time_of_day_analysis_le_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(15))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._start_hour = self.Param("StartHour", 9) \
.SetDisplay("Start Hour", "Hour to enter", "General")
self._end_hour = self.Param("EndHour", 16) \
.SetDisplay("End Hour", "Hour to exit", "General")
self._entry_date = None
@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(mateos_time_of_day_analysis_le_strategy, self).OnReseted()
self._entry_date = None
def OnStarted2(self, time):
super(mateos_time_of_day_analysis_le_strategy, self).OnStarted2(time)
self._entry_date = None
dummy1 = ExponentialMovingAverage()
dummy1.Length = 10
dummy2 = ExponentialMovingAverage()
dummy2.Length = 20
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(dummy1, dummy2, self.OnProcess).Start()
def OnProcess(self, candle, d1, d2):
if candle.State != CandleStates.Finished:
return
hour = candle.ServerTime.Hour
date = candle.ServerTime.Date
start = self._start_hour.Value
end = self._end_hour.Value
if hour >= start and hour < end:
if self.Position <= 0 and self._entry_date != date:
self.BuyMarket()
self._entry_date = date
elif hour >= end or hour < start:
if self.Position > 0:
self.SellMarket()
self._entry_date = None
def CreateClone(self):
return mateos_time_of_day_analysis_le_strategy()