ギャップフィル戦略
ギャップフィル戦略は、連続する15分のローソク足間の価格ギャップを活用します。
新しいローソク足が前のローソク足の高値より設定可能な閾値以上で始まると、戦略は売り、ギャップが埋まることを期待して前の高値に買い指値を置きます。
ローソク足が前の安値より閾値以上低く始まると、買い、前の安値に売り指値を置きます。
閾値はMinGapSizeの価格ステップに最良気配の現在スプレッドを加えて計算されます。
詳細
- エントリー条件: 現在の始値と前の高値/安値の間のギャップが
MinGapSizeにスプレッドを加えた値を超える。 - ロング/ショート: 両方向。
- エグジット条件: 前のローソク足の極値での指値注文。
- ストップ: なし。
- デフォルト値:
MinGapSize= 1Volume= 0.1CandleType= 15分
- フィルター:
- カテゴリ: ギャップ
- 方向: 両方
- インジケーター: なし
- ストップ: なし
- 複雑さ: 基本
- 時間軸: イントラデイ (15m)
- 季節性: いいえ
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 中
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;
/// <summary>
/// Gap fill strategy using Highest/Lowest channel breakout.
/// </summary>
public class GapFillStrategy : Strategy
{
private readonly StrategyParam<int> _channelPeriod;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevHigh;
private decimal _prevLow;
private bool _hasPrev;
public int ChannelPeriod { get => _channelPeriod.Value; set => _channelPeriod.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public GapFillStrategy()
{
_channelPeriod = Param(nameof(ChannelPeriod), 12)
.SetGreaterThanZero()
.SetDisplay("Channel Period", "Highest/Lowest period", "Parameters");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Candle type", "Data");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_prevHigh = 0;
_prevLow = 0;
_hasPrev = false;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var highest = new Highest { Length = ChannelPeriod };
var lowest = new Lowest { Length = ChannelPeriod };
SubscribeCandles(CandleType)
.Bind(highest, lowest, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal highVal, decimal lowVal)
{
if (candle.State != CandleStates.Finished) return;
if (!_hasPrev)
{
_prevHigh = highVal;
_prevLow = lowVal;
_hasPrev = true;
return;
}
if (candle.ClosePrice > _prevHigh && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
else if (candle.ClosePrice < _prevLow && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
}
_prevHigh = highVal;
_prevLow = lowVal;
}
}
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 Highest, Lowest
from StockSharp.Algo.Strategies import Strategy
class gap_fill_strategy(Strategy):
def __init__(self):
super(gap_fill_strategy, self).__init__()
self._channel_period = self.Param("ChannelPeriod", 12) \
.SetDisplay("Channel Period", "Highest/Lowest period", "Parameters")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Candle type", "Data")
self._prev_high = 0.0
self._prev_low = 0.0
self._has_prev = False
@property
def channel_period(self):
return self._channel_period.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(gap_fill_strategy, self).OnReseted()
self._prev_high = 0.0
self._prev_low = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(gap_fill_strategy, self).OnStarted2(time)
highest = Highest()
highest.Length = self.channel_period
lowest = Lowest()
lowest.Length = self.channel_period
self.SubscribeCandles(self.candle_type) \
.Bind(highest, lowest, self.process_candle) \
.Start()
def process_candle(self, candle, high_val, low_val):
if candle.State != CandleStates.Finished:
return
high_val = float(high_val)
low_val = float(low_val)
if not self._has_prev:
self._prev_high = high_val
self._prev_low = low_val
self._has_prev = True
return
if float(candle.ClosePrice) > self._prev_high and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif float(candle.ClosePrice) < self._prev_low and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_high = high_val
self._prev_low = low_val
def CreateClone(self):
return gap_fill_strategy()