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.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()