GitHub で見る
ハラミ CCI 確認
概要
ハラミ CCI 確認は、MetaTrader 5 エキスパート アドバイザー Expert_ABH_BH_CCI の高レベルの StockSharp 移植です。オリジナルの EA は、2 ローソク足の強気ハラミと弱気ハラミの反転パターンをトレードします。取引に入る前に、商品チャネル指数(CCI)オシレーターからの確認を要求し、移動平均に対してローソク本体のサイズを測定して、より大きなローソクが本当にレンジを支配していることを確認します。 StockSharp 変換では、同じ確認ロジックが維持され、完了したキャンドルのみが処理され、注文の安全性のためにプラットフォームの組み込み保護モジュールが使用されます。
戦略ロジック
パターン検出
- 平均実体の計算 – 最後の N バー (デフォルトは 5) にわたる絶対的なローソク足の実体の移動平均を維持します。これは、ローソク足のサイズとトレンド参照を平滑化する MetaTrader ヘルパー クラスを反映しています。
- 強気のハラミ – 前のローソク足が強気であること、前のローソク足が平均よりも実体が長い弱気であること、そして強気の実体が弱気の範囲内に留まることを必要とします。前のローソク足の中間点も終値の移動平均を下回る必要があり、下降トレンドが確認されます。
- 弱気のハラミ – 鏡像条件: 前のローソク足は弱気、前のローソク足は強気でロングである必要があり、弱気の実体は強気の範囲内に含まれている必要があり、上昇トレンドを確認するには中間点が終値移動平均を上回っている必要があります。
CCIの確認
- エントリ フィルター – この戦略は、最後に完了したローソク足 (シフト 1) から CCI の値をチェックします。ロングトレードでは、CCI が
-EntryThreshold (デフォルトは 50) 未満である必要がありますが、ショートトレードでは、+EntryThreshold を超える値が必要です。
- 出口バンド – CCI 履歴は、±
ExitBand (デフォルトは 80) の交差を監視します。インジケーターが -ExitBand を通過すると、オープンしているショート ポジションはクローズされます。 +ExitBand を下回ると、既存の長時間露光は終了します。これは、MetaTrader のエキスパートがポジションを平準化するために使用した「投票」を再現します。
貿易管理
- 反転 – 戦略がすでにポジションを保持しているときに反対のハラミのセットアップが確認された場合、既存のエクスポージャーをクローズし、新しい方向を開くのに十分な量が取引されます。
- 保護 –
StartProtection() が有効化されているため、ユーザーは必要に応じて StockSharp UI を通じてストップロスまたはテイクプロフィット設定を追加できます。デフォルトでは、ソース EA との調整を維持するために固定ストップは適用されず、手動の資金管理設定に依存していました。
パラメーター
- 注文量 – すべての市場参入時に送信される基本量。反転が発生した場合、反対のポジションを閉じるために追加のボリュームが自動的に追加されます。
- CCI 期間 – 商品チャネル指数オシレーターの長さ。
- 実体平均 – 実体サイズと終値を平均する際に使用される過去のローソク足の数。
- CCI エントリ – ハラミシグナルを受け入れるために必要な最小絶対値 CCI 。
- CCI 出口バンド – CCI クロスオーバー出口ルールを定義するバンドの大きさ。
- ローソク足タイプ – ローソク足に使用される時間枠 (デフォルト: 1 時間の時間枠)。
追加の注意事項
- すべての計算は、
SubscribeCandles によって提供される完成したローソク足で実行されます。 MetaTrader 実行モデルに一致させるために、バー内シグナルは意図的に無視されます。
- この戦略は、完全なインジケーター バッファーを再作成することなくハラミ ルールを評価するために、ローソク足と CCI 値の短いスライド履歴を保持します。
- このフォルダーには C# 実装のみが提供されます。この変換に対応する Python バージョンはありません。
namespace StockSharp.Samples.Strategies;
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Messages;
/// <summary>
/// Harami CCI Confirmation strategy: Harami pattern with CCI confirmation.
/// </summary>
public class HaramiCciConfirmationStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _cciPeriod;
private readonly StrategyParam<decimal> _entryLevel;
private readonly StrategyParam<int> _signalCooldownCandles;
private readonly List<ICandleMessage> _candles = new();
private int _candlesSinceTrade;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int CciPeriod { get => _cciPeriod.Value; set => _cciPeriod.Value = value; }
public decimal EntryLevel { get => _entryLevel.Value; set => _entryLevel.Value = value; }
public int SignalCooldownCandles { get => _signalCooldownCandles.Value; set => _signalCooldownCandles.Value = value; }
public HaramiCciConfirmationStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(30).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
_cciPeriod = Param(nameof(CciPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("CCI Period", "CCI period", "Indicators");
_entryLevel = Param(nameof(EntryLevel), 0m)
.SetDisplay("Entry Level", "CCI threshold", "Signals");
_signalCooldownCandles = Param(nameof(SignalCooldownCandles), 6)
.SetGreaterThanZero()
.SetDisplay("Signal Cooldown", "Bars to wait between trades", "Trading");
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_candles.Clear();
_candlesSinceTrade = SignalCooldownCandles;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_candles.Clear();
_candlesSinceTrade = SignalCooldownCandles;
var cci = new CommodityChannelIndex { Length = CciPeriod };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(cci, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, decimal cciValue)
{
if (candle.State != CandleStates.Finished) return;
if (_candlesSinceTrade < SignalCooldownCandles)
_candlesSinceTrade++;
_candles.Add(candle);
if (_candles.Count > 5) _candles.RemoveAt(0);
if (_candles.Count >= 2)
{
var curr = _candles[^1];
var prev = _candles[^2];
var bullishHarami = prev.OpenPrice > prev.ClosePrice
&& curr.ClosePrice > curr.OpenPrice
&& curr.OpenPrice > prev.ClosePrice
&& curr.ClosePrice < prev.OpenPrice;
var bearishHarami = prev.ClosePrice > prev.OpenPrice
&& curr.OpenPrice > curr.ClosePrice
&& curr.ClosePrice > prev.OpenPrice
&& curr.OpenPrice < prev.ClosePrice;
if (bullishHarami && cciValue < -EntryLevel && Position <= 0 && _candlesSinceTrade >= SignalCooldownCandles)
{
BuyMarket();
_candlesSinceTrade = 0;
}
else if (bearishHarami && cciValue > EntryLevel && Position >= 0 && _candlesSinceTrade >= SignalCooldownCandles)
{
SellMarket();
_candlesSinceTrade = 0;
}
}
}
}
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 CommodityChannelIndex
from StockSharp.Algo.Strategies import Strategy
class harami_cci_confirmation_strategy(Strategy):
def __init__(self):
super(harami_cci_confirmation_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(30)))
self._cci_period = self.Param("CciPeriod", 14)
self._entry_level = self.Param("EntryLevel", 0.0)
self._signal_cooldown_candles = self.Param("SignalCooldownCandles", 6)
self._candles = []
self._candles_since_trade = 6
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
@property
def CciPeriod(self):
return self._cci_period.Value
@CciPeriod.setter
def CciPeriod(self, value):
self._cci_period.Value = value
@property
def EntryLevel(self):
return self._entry_level.Value
@EntryLevel.setter
def EntryLevel(self, value):
self._entry_level.Value = value
@property
def SignalCooldownCandles(self):
return self._signal_cooldown_candles.Value
@SignalCooldownCandles.setter
def SignalCooldownCandles(self, value):
self._signal_cooldown_candles.Value = value
def OnReseted(self):
super(harami_cci_confirmation_strategy, self).OnReseted()
self._candles.clear()
self._candles_since_trade = self.SignalCooldownCandles
def OnStarted2(self, time):
super(harami_cci_confirmation_strategy, self).OnStarted2(time)
self._candles.clear()
self._candles_since_trade = self.SignalCooldownCandles
cci = CommodityChannelIndex()
cci.Length = self.CciPeriod
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(cci, self._process_candle).Start()
def _process_candle(self, candle, cci_value):
if candle.State != CandleStates.Finished:
return
if self._candles_since_trade < self.SignalCooldownCandles:
self._candles_since_trade += 1
cci_val = float(cci_value)
self._candles.append(candle)
if len(self._candles) > 5:
self._candles.pop(0)
if len(self._candles) >= 2:
curr = self._candles[-1]
prev = self._candles[-2]
bullish_harami = (float(prev.OpenPrice) > float(prev.ClosePrice)
and float(curr.ClosePrice) > float(curr.OpenPrice)
and float(curr.OpenPrice) > float(prev.ClosePrice)
and float(curr.ClosePrice) < float(prev.OpenPrice))
bearish_harami = (float(prev.ClosePrice) > float(prev.OpenPrice)
and float(curr.OpenPrice) > float(curr.ClosePrice)
and float(curr.ClosePrice) > float(prev.OpenPrice)
and float(curr.OpenPrice) < float(prev.ClosePrice))
if bullish_harami and cci_val < -self.EntryLevel and self.Position <= 0 and self._candles_since_trade >= self.SignalCooldownCandles:
self.BuyMarket()
self._candles_since_trade = 0
elif bearish_harami and cci_val > self.EntryLevel and self.Position >= 0 and self._candles_since_trade >= self.SignalCooldownCandles:
self.SellMarket()
self._candles_since_trade = 0
def CreateClone(self):
return harami_cci_confirmation_strategy()