Mateosの時間帯分析 LE
指定されたイントラデイの時間枠内でロングポジションを建て、その後同日中に決済します。
この戦略は時間帯効果を探索するのに役立ちます。
詳細
- エントリー条件:
From-Thruの日付範囲内で時刻がStartTimeに達する。 - ロング/ショート: ロングのみ。
- エグジット条件: 時刻が
EndTime(20:00前)に達する。 - ストップ: いいえ。
- デフォルト値:
CandleType= TimeSpan.FromMinutes(1)StartTime= 09:30EndTime= 16:00From= 2017-04-21Thru= 2099-12-01
- フィルター:
- カテゴリ: 時間ベース
- 方向: ロングのみ
- インジケーター: なし
- ストップ: いいえ
- 複雑さ: 基本
- 時間軸: イントラデイ
- 季節性: いいえ
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 低
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Mateo's Time of Day Analysis LE strategy.
/// Between the From and Thru dates a long position is opened once the candle time reaches StartTime and closed once it reaches
/// EndTime. Times are the candle open time in UTC.
/// </summary>
public class MateosTimeOfDayAnalysisLeStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<TimeSpan> _startTime;
private readonly StrategyParam<TimeSpan> _endTime;
private readonly StrategyParam<DateTimeOffset> _from;
private readonly StrategyParam<DateTimeOffset> _thru;
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Time of day the long position opens.
/// </summary>
public TimeSpan StartTime
{
get => _startTime.Value;
set => _startTime.Value = value;
}
/// <summary>
/// Time of day the long position closes.
/// </summary>
public TimeSpan EndTime
{
get => _endTime.Value;
set => _endTime.Value = value;
}
/// <summary>
/// First date entries are allowed.
/// </summary>
public DateTimeOffset From
{
get => _from.Value;
set => _from.Value = value;
}
/// <summary>
/// Last date entries are allowed.
/// </summary>
public DateTimeOffset Thru
{
get => _thru.Value;
set => _thru.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public MateosTimeOfDayAnalysisLeStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
_startTime = Param(nameof(StartTime), new TimeSpan(9, 30, 0))
.SetDisplay("Start Time", "Time of day the long position opens", "Time");
_endTime = Param(nameof(EndTime), new TimeSpan(16, 0, 0))
.SetDisplay("End Time", "Time of day the long position closes", "Time");
_from = Param(nameof(From), new DateTimeOffset(2017, 4, 21, 0, 0, 0, TimeSpan.Zero))
.SetDisplay("From", "First date entries are allowed", "Time");
_thru = Param(nameof(Thru), new DateTimeOffset(2099, 12, 1, 0, 0, 0, TimeSpan.Zero))
.SetDisplay("Thru", "Last date entries are allowed", "Time");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <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;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var openTime = candle.OpenTime;
var timeOfDay = openTime.TimeOfDay;
var inSession = timeOfDay >= StartTime && timeOfDay < EndTime;
var inDateRange = openTime >= From.UtcDateTime && openTime <= Thru.UtcDateTime;
if (inSession && inDateRange && Position <= 0)
BuyMarket(Volume + Math.Abs(Position));
else if (!inSession && Position > 0)
SellMarket(Position);
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, DateTimeOffset
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Strategies import Strategy
class mateos_time_of_day_analysis_le_strategy(Strategy):
"""
Mateo's Time of Day Analysis LE strategy.
Between the From and Thru dates a long position is opened once the candle time reaches StartTime and closed once it reaches
EndTime. Times are the candle open time in UTC.
"""
def __init__(self):
super(mateos_time_of_day_analysis_le_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(1))).SetDisplay("Candle Type", "Type of candles to use", "General")
self._start_time = self.Param("StartTime", TimeSpan(9, 30, 0)).SetDisplay("Start Time", "Time of day the long position opens", "Time")
self._end_time = self.Param("EndTime", TimeSpan(16, 0, 0)).SetDisplay("End Time", "Time of day the long position closes", "Time")
self._from = self.Param("From", DateTimeOffset(2017, 4, 21, 0, 0, 0, TimeSpan.Zero)).SetDisplay("From", "First date entries are allowed", "Time")
self._thru = self.Param("Thru", DateTimeOffset(2099, 12, 1, 0, 0, 0, TimeSpan.Zero)).SetDisplay("Thru", "Last date entries are allowed", "Time")
@property
def candle_type(self):
return self._candle_type.Value
def OnStarted2(self, time):
super(mateos_time_of_day_analysis_le_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:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
open_time = candle.OpenTime
time_of_day = open_time.TimeOfDay
in_session = time_of_day >= self._start_time.Value and time_of_day < self._end_time.Value
in_date_range = open_time >= self._from.Value.UtcDateTime and open_time <= self._thru.Value.UtcDateTime
if in_session and in_date_range and self.Position <= 0:
self.BuyMarket(self.Volume + abs(self.Position))
elif not in_session and self.Position > 0:
self.SellMarket(self.Position)
def CreateClone(self):
return mateos_time_of_day_analysis_le_strategy()