GitHub で見る
基本 CCI RSI 戦略
基本 CCI RSI 戦略は、取引に入る前にCommodity Channel Index(CCI)とRelative Strength Index(RSI)の両方が2つの連続したクローズドキャンドルでモメンタムを確認するのを待つ元のMetaTrader エキスパートアドバイザーを再現します。StockSharpバージョンはpipベースの資金管理ルールを維持し、それらを価格ステップに自動的に変換し、MQL5でポジション変更で実装されたものと同じトレーリングストップ動作を追加します。
戦略の取引方法
- 各キャンドルのクローズ時(デフォルトで毎時間)に戦略は新しいCCIとRSIの値を受け取ります。
- ロングエントリーは現在と前のクローズドキャンドルの両方でそれぞれの上限しきい値を上回り続けることを両方のインジケーターに要求します。ショートエントリーは最後の2つのキャンドルについて両方の下限しきい値を下回り続けることを要求します。
- シグナルが発生すると、戦略は設定されたボリュームでポジションを開き(反対のエクスポージャーをクローズしながら)、元のスクリプトからのpip距離を使用して固定のストップロスとテイクプロフィット価格を即座に計算します。
- ポジションが開いている間、戦略はキャンドルレンジがストップまたはテイクレベルに触れたかどうかを常に確認し、どちらかが達せられたら市場でエグジットします。
- トレーリングストップはMetaTrader実装を複製します:利益が
TrailingStopPips + TrailingStepPipsを超えると、保護ストップは現在のクローズからTrailingStopPips後ろ(ロングの場合)またはそれより上(ショートの場合)に動かされます。さらなる調整は再度引き締める前に追加のTrailingStepPipsの利益を必要とします。
このフローはStockSharpの高レベルキャンドルサブスクリプションとインジケーターを使用しながら、ロジックをMQL5ソースエキスパートに近い状態に保ちます。
リスク管理
- ストップロス: インストゥルメントの価格ステップに変換された固定pip距離。ゼロに設定すると無効。
- テイクプロフィット: 価格ステップに変換された固定pip距離。ゼロのとき無効。
- トレーリングストップ: エキスパートアドバイザーの
Trailing()関数を模倣するステップバッファーを持つオプションのpip距離。TrailingStopPipsがゼロのとき無効。
- ポジションサイジング: 戦略の
Volumeプロパティを通じて制御;デフォルトのロットは1契約。
パラメーター
| 名前 |
説明 |
StopLossPips |
エントリー価格とストップロス注文の間のpips距離。 |
TakeProfitPips |
エントリー価格とテイクプロフィット目標の間のpips距離。 |
TrailingStopPips |
ストップをトレールし始めるために必要な利益(pips)。 |
TrailingStepPips |
各新しいトレーリング調整の前に必要な追加利益(pips)。 |
CciPeriod |
CCIインジケーターの平均化期間。 |
RsiPeriod |
RSIインジケーターの平均化期間。 |
RsiLevelUp |
ロングトレードを検証するために超えなければならない買われ過ぎレベル。 |
RsiLevelDown |
ショートトレードを検証するために破られなければならない売られ過ぎレベル。 |
CciLevelUp |
強気のモメンタムを確認する上限CCIしきい値。 |
CciLevelDown |
弱気のモメンタムを確認する下限CCIしきい値。 |
CandleType |
キャンドル集計とインジケーター計算に使用される時間軸。 |
デフォルト値
StopLossPips = 125
TakeProfitPips = 60
TrailingStopPips = 5
TrailingStepPips = 5
CciPeriod = 12
RsiPeriod = 15
RsiLevelUp = 75
RsiLevelDown = 30
CciLevelUp = 80
CciLevelDown = -95
CandleType = 1時間キャンドル
追加ノート
- pip距離は自動的にスケールされます:インストゥルメントが3桁または5桁の小数点以下を使用する場合、戦略は価格ステップを10倍にし、MetaTraderの「調整されたポイント」ロジックと一致します。
- エントリーは再描画を避け、エキスパートアドバイザーの元の「新しいバー」条件を反映するためにクローズドキャンドルでのみ評価されます。
- エグジットは常に成行注文を使用し、StockSharpバックテスト環境内で決定論的な動作を提供します。
分類タグ
- カテゴリ: オシレーター確認
- 方向: 両方
- インジケーター: CCI、RSI
- ストップ: 固定とトレーリング(pipベース)
- 複雑さ: 基本
- 時間軸: イントラデイからスイング(デフォルト1時間)
- 季節性: いいえ
- ニューラルネットワーク: いいえ
- ダイバージェンス: いいえ
- リスクレベル: 中
using System;
using System.Collections.Generic;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Basic CCI RSI strategy. Uses CCI zero-line crossover with RSI confirmation.
/// </summary>
public class BasicCciRsiStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _cciPeriod;
private decimal? _prevCci;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int CciPeriod { get => _cciPeriod.Value; set => _cciPeriod.Value = value; }
public BasicCciRsiStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame()).SetDisplay("Candle Type", "Timeframe", "General");
_cciPeriod = Param(nameof(CciPeriod), 20).SetGreaterThanZero().SetDisplay("CCI Period", "CCI lookback", "Indicators");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities() => [(Security, CandleType)];
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevCci = null;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevCci = null;
var cci = new CommodityChannelIndex { Length = CciPeriod };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(cci, ProcessCandle).Start();
var area = CreateChartArea();
if (area != null) { DrawCandles(area, subscription); DrawOwnTrades(area); }
}
private void ProcessCandle(ICandleMessage candle, decimal cciVal)
{
if (candle.State != CandleStates.Finished) return;
if (!IsFormedAndOnlineAndAllowTrading()) { _prevCci = cciVal; return; }
if (_prevCci == null) { _prevCci = cciVal; return; }
if (_prevCci.Value < 0m && cciVal >= 0m && Position <= 0) { if (Position < 0) BuyMarket(); BuyMarket(); }
else if (_prevCci.Value > 0m && cciVal <= 0m && Position >= 0) { if (Position > 0) SellMarket(); SellMarket(); }
_prevCci = cciVal;
}
}
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 basic_cci_rsi_strategy(Strategy):
def __init__(self):
super(basic_cci_rsi_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))) \
.SetDisplay("Candle Type", "Timeframe", "General")
self._cci_period = self.Param("CciPeriod", 20) \
.SetDisplay("CCI Period", "CCI lookback", "Indicators")
self._prev_cci = None
@property
def candle_type(self):
return self._candle_type.Value
@candle_type.setter
def candle_type(self, value):
self._candle_type.Value = value
@property
def cci_period(self):
return self._cci_period.Value
@cci_period.setter
def cci_period(self, value):
self._cci_period.Value = value
def OnReseted(self):
super(basic_cci_rsi_strategy, self).OnReseted()
self._prev_cci = None
def OnStarted2(self, time):
super(basic_cci_rsi_strategy, self).OnStarted2(time)
self._prev_cci = None
cci = CommodityChannelIndex()
cci.Length = self.cci_period
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(cci, self.OnProcess).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def OnProcess(self, candle, cci_val):
if candle.State != CandleStates.Finished:
return
if not self.IsFormedAndOnlineAndAllowTrading():
self._prev_cci = float(cci_val)
return
if self._prev_cci is None:
self._prev_cci = float(cci_val)
return
if self._prev_cci < 0 and cci_val >= 0 and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif self._prev_cci > 0 and cci_val <= 0 and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_cci = float(cci_val)
def CreateClone(self):
return basic_cci_rsi_strategy()