CCI Hook Reversal 戦略
CCI Hook Reversal戦略は、Commodity Channel Indexが極端な値からフックして離れるときにトリガーとして使用します。インジケーターが+100を超えるか-100を下回った後、モメンタムが失速するにつれて急速に戻ることがよくあります。
テストでは平均年間リターンが約169%であることが示されています。暗号資産市場で最もパフォーマンスが優れています。
CCIが売られすぎから上向きに転じ、価格がまだ小幅の新安値を印しているときにロング取引が発生します。CCIが買われすぎから戻し始め、価格が新高値をつけているときにショートが開始されます。
各取引には小さな固定ストップが設定され、CCIが反対方向にフックするかストップに達したときに終了します。
詳細
- エントリー条件: インジケーターシグナル
- ロング/ショート: 両方
- エグジット条件: ストップロスまたは逆シグナル
- ストップ: はい、パーセントベース
- デフォルト値:
CandleType= 15分CciPeriod= 20OversoldLevel= -100OverboughtLevel= 100StopLossPercent= 2
- フィルター:
- カテゴリ: リバーサル
- 方向: 両方
- インジケーター: CCI
- ストップ: はい
- 複雑さ: 中級
- 時間軸: イントラデイ
- 季節性: いいえ
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 中
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>
/// CCI Hook Reversal strategy.
/// A long opens when CCI was below the oversold level on the previous candle and turns up while the low makes a new low
/// against the previous candle; a short opens when it was above the overbought level and turns down while the high makes a new high.
/// The position closes when CCI turns the other way, reversing on the opposite signal, and a percent stop limits the loss.
/// </summary>
public class CciHookReversalStrategy : Strategy
{
private readonly StrategyParam<int> _cciPeriod;
private readonly StrategyParam<int> _oversoldLevel;
private readonly StrategyParam<int> _overboughtLevel;
private readonly StrategyParam<decimal> _stopLossPercent;
private readonly StrategyParam<DataType> _candleType;
private decimal? _prevValue;
private decimal _prevHigh;
private decimal _prevLow;
/// <summary>
/// Period for CCI.
/// </summary>
public int CciPeriod
{
get => _cciPeriod.Value;
set => _cciPeriod.Value = value;
}
/// <summary>
/// CCI level of the oversold zone.
/// </summary>
public int OversoldLevel
{
get => _oversoldLevel.Value;
set => _oversoldLevel.Value = value;
}
/// <summary>
/// CCI level of the overbought zone.
/// </summary>
public int OverboughtLevel
{
get => _overboughtLevel.Value;
set => _overboughtLevel.Value = value;
}
/// <summary>
/// Stop-loss percentage.
/// </summary>
public decimal StopLossPercent
{
get => _stopLossPercent.Value;
set => _stopLossPercent.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public CciHookReversalStrategy()
{
_cciPeriod = Param(nameof(CciPeriod), 20)
.SetGreaterThanZero()
.SetDisplay("CCI Period", "Period for CCI", "Indicators");
_oversoldLevel = Param(nameof(OversoldLevel), -100)
.SetDisplay("Oversold Level", "CCI level of the oversold zone", "Levels");
_overboughtLevel = Param(nameof(OverboughtLevel), 100)
.SetDisplay("Overbought Level", "CCI level of the overbought zone", "Levels");
_stopLossPercent = Param(nameof(StopLossPercent), 2m)
.SetNotNegative()
.SetDisplay("Stop Loss %", "Stop loss percentage from entry price", "Risk");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevValue = null;
_prevHigh = default;
_prevLow = default;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevValue = null;
_prevHigh = default;
_prevLow = default;
var oscillator = new CommodityChannelIndex { Length = CciPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(oscillator, ProcessCandle)
.Start();
StartProtection(new Unit(), new Unit(StopLossPercent, UnitTypes.Percent), useMarketOrders: true, isLocalStop: true);
// The stop has to see prices between candles, not only at their close.
foreach (var field in new[] { Level1Fields.BestBidPrice, Level1Fields.BestAskPrice })
{
var quotes = new Subscription(DataType.Level1, Security);
quotes.MarketData.BuildField = field;
SubscribeLevel1(quotes).Bind(ObserveProtectionQuote).Start();
}
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, oscillator);
DrawOwnTrades(area);
}
}
private void ObserveProtectionQuote(Level1ChangeMessage quote)
{
// The high-level handler activates native protection before this callback, also between signal bars.
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue value)
{
if (candle.State != CandleStates.Finished || !value.IsFormed || value.IsEmpty)
return;
var current = value.GetValue<decimal>();
var previous = _prevValue;
var prevHigh = _prevHigh;
var prevLow = _prevLow;
_prevValue = current;
_prevHigh = candle.HighPrice;
_prevLow = candle.LowPrice;
if (previous is not decimal last || !IsFormedAndOnlineAndAllowTrading())
return;
var hooksUp = current > last;
var hooksDown = current < last;
var longSignal = last < OversoldLevel && hooksUp && candle.LowPrice < prevLow;
var shortSignal = last > OverboughtLevel && hooksDown && candle.HighPrice > prevHigh;
if (Position > 0)
{
if (hooksDown)
SellMarket(shortSignal ? Volume + Position : Position);
}
else if (Position < 0)
{
if (hooksUp)
BuyMarket(longSignal ? Volume - Position : -Position);
}
else if (longSignal)
{
BuyMarket(Volume);
}
else if (shortSignal)
{
SellMarket(Volume);
}
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
clr.AddReference("StockSharp.BusinessEntities")
from System import TimeSpan, Decimal
from StockSharp.Messages import DataType, CandleStates, Unit, UnitTypes, Level1Fields
from StockSharp.BusinessEntities import Subscription
from StockSharp.Algo.Indicators import CommodityChannelIndex
from StockSharp.Algo.Strategies import Strategy
class cci_hook_reversal_strategy(Strategy):
"""
CCI Hook Reversal strategy.
A long opens when CCI was below the oversold level on the previous candle and turns up while the low makes a new low
against the previous candle; a short opens when it was above the overbought level and turns down while the high makes a new high.
The position closes when CCI turns the other way, reversing on the opposite signal, and a percent stop limits the loss.
"""
def __init__(self):
super(cci_hook_reversal_strategy, self).__init__()
self._cci_period = self.Param("CciPeriod", 20).SetGreaterThanZero().SetDisplay("CCI Period", "Period for CCI", "Indicators")
self._oversold_level = self.Param("OversoldLevel", -100).SetDisplay("Oversold Level", "CCI level of the oversold zone", "Levels")
self._overbought_level = self.Param("OverboughtLevel", 100).SetDisplay("Overbought Level", "CCI level of the overbought zone", "Levels")
self._stop_loss_percent = self.Param("StopLossPercent", 2.0).SetNotNegative().SetDisplay("Stop Loss %", "Stop loss percentage from entry price", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(15))).SetDisplay("Candle Type", "Type of candles to use", "General")
self._prev_value = None
self._prev_high = Decimal(0)
self._prev_low = Decimal(0)
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(cci_hook_reversal_strategy, self).OnReseted()
self._prev_value = None
self._prev_high = Decimal(0)
self._prev_low = Decimal(0)
def OnStarted2(self, time):
super(cci_hook_reversal_strategy, self).OnStarted2(time)
self._prev_value = None
self._prev_high = Decimal(0)
self._prev_low = Decimal(0)
oscillator = CommodityChannelIndex()
oscillator.Length = self._cci_period.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(oscillator, self._process_candle).Start()
self.StartProtection(Unit(), Unit(Decimal(self._stop_loss_percent.Value), UnitTypes.Percent), useMarketOrders=True, isLocalStop=True)
# The stop has to see prices between candles, not only at their close.
for field in (Level1Fields.BestBidPrice, Level1Fields.BestAskPrice):
quotes = Subscription(DataType.Level1, self.Security)
quotes.MarketData.BuildField = field
self.SubscribeLevel1(quotes).Bind(self._observe_protection_quote).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, oscillator)
self.DrawOwnTrades(area)
def _observe_protection_quote(self, quote):
# The high-level handler activates native protection before the callback, including between bars.
pass
def _process_candle(self, candle, value):
if candle.State != CandleStates.Finished or not value.IsFormed or value.IsEmpty:
return
current = value.GetValue[Decimal](None)
previous = self._prev_value
prev_high = self._prev_high
prev_low = self._prev_low
self._prev_value = current
self._prev_high = candle.HighPrice
self._prev_low = candle.LowPrice
if previous is None or not self.IsFormedAndOnlineAndAllowTrading():
return
hooks_up = current > previous
hooks_down = current < previous
long_signal = previous < Decimal(self._oversold_level.Value) and hooks_up and candle.LowPrice < prev_low
short_signal = previous > Decimal(self._overbought_level.Value) and hooks_down and candle.HighPrice > prev_high
if self.Position > 0:
if hooks_down:
self.SellMarket(self.Volume + self.Position if short_signal else self.Position)
elif self.Position < 0:
if hooks_up:
self.BuyMarket(self.Volume - self.Position if long_signal else -self.Position)
elif long_signal:
self.BuyMarket(self.Volume)
elif short_signal:
self.SellMarket(self.Volume)
def CreateClone(self):
return cci_hook_reversal_strategy()