カスタマイズ可能なBTC季節性戦略
この戦略はBitcoinのイントラデイ季節性を利用し、ユーザーが定義したUTC時刻でエントリーとエグジットを行う。 エントリー時刻にロングポジションを開き、エグジット時刻に決済する。
詳細
- エントリー条件: 時刻がユーザー定義のエントリー時刻と一致する
- ロング/ショート: ロングのみ
- エグジット条件: 時刻がユーザー定義のエグジット時刻と一致する
- ストップ: いいえ
- デフォルト値:
CandleType= 1分EntryHour= 21ExitHour= 23
- フィルター:
- カテゴリ: 季節性
- 方向: ロングのみ
- インジケーター: なし
- ストップ: いいえ
- 複雑さ: 初心者
- 時間軸: イントラデイ
- 季節性: はい
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 低
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Customizable BTC seasonality strategy.
/// Opens a long during the EntryHour UTC hour and closes it during the ExitHour UTC hour.
/// </summary>
public class CustomizableBtcSeasonalityStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _entryHour;
private readonly StrategyParam<int> _exitHour;
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// UTC hour when the long is opened.
/// </summary>
public int EntryHour
{
get => _entryHour.Value;
set => _entryHour.Value = value;
}
/// <summary>
/// UTC hour when the long is closed.
/// </summary>
public int ExitHour
{
get => _exitHour.Value;
set => _exitHour.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public CustomizableBtcSeasonalityStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
_entryHour = Param(nameof(EntryHour), 21)
.SetRange(0, 23)
.SetDisplay("Entry Hour", "UTC hour when the long is opened", "Time");
_exitHour = Param(nameof(ExitHour), 23)
.SetRange(0, 23)
.SetDisplay("Exit Hour", "UTC hour when the long is closed", "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 hour = candle.OpenTime.ToUniversalTime().Hour;
if (hour == ExitHour && Position > 0)
SellMarket(Position);
else if (hour == EntryHour && Position == 0)
BuyMarket();
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Strategies import Strategy
class customizable_btc_seasonality_strategy(Strategy):
"""
Customizable BTC seasonality strategy.
Opens a long during the EntryHour UTC hour and closes it during the ExitHour UTC hour.
"""
def __init__(self):
super(customizable_btc_seasonality_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(1))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._entry_hour = self.Param("EntryHour", 21) \
.SetRange(0, 23) \
.SetDisplay("Entry Hour", "UTC hour when the long is opened", "Time")
self._exit_hour = self.Param("ExitHour", 23) \
.SetRange(0, 23) \
.SetDisplay("Exit Hour", "UTC hour when the long is closed", "Time")
@property
def candle_type(self):
return self._candle_type.Value
def GetWorkingSecurities(self):
return [(self.Security, self.candle_type)]
def OnStarted2(self, time):
super(customizable_btc_seasonality_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
hour = candle.OpenTime.ToUniversalTime().Hour
if hour == self._exit_hour.Value and self.Position > 0:
self.SellMarket(self.Position)
elif hour == self._entry_hour.Value and self.Position == 0:
self.BuyMarket()
def CreateClone(self):
return customizable_btc_seasonality_strategy()