GitHub で見る
FiveMinuteRsiCci 戦略
FiveMinuteRsiCciStrategy は、MetaTrader 4 Expert Advisor 5Mins Rsi Cci EA.mq4 の StockSharp ポートです。元のスクリプトは、RSI のしきい値クロスと平滑化/EMA 移動平均フィルター、および 2 つの CCI インジケーターの極性を組み合わせることにより、5 分間のローソク足を取引します。 C# バージョンは、データ サブスクリプション、インジケーター バインディング、リスク管理に StockSharp の高レベルの API を使用しながら、同じ意思決定ロジックを維持します。
取引ロジック
- 設定されたローソク足タイプ (デフォルトでは 5 分の時間枠) をサブスクライブし、5 つのインジケーターをリアルタイムで更新します: RSI、始値の平滑化された MA、始値の EMA、さらに典型的な価格から計算された高速 CCI と低速 CCI。
- 完了した各ローソク足は、オープンなポジションがなく、現在の買値/売値スプレッドが
MaxSpreadPoints (価格単位に換算) を下回っている場合にのみ評価されます。
- 長い信号には次のものが必要です。
- EMA を超える平滑化された MA、
- 以前のローソク足と現在のローソク足の間の
BullishRsiLevel を通って上向きに交差する RSI、
- 両方の CCI 値がゼロより大きい。
- 短い信号には逆の条件が必要です(EMA 未満の平滑化された MA、RSI が
BearishRsiLevel を下向きに交差、両方の CCI がゼロ未満)。
- 注文量は、EA の動的なポジション サイズを再現します。
LotCoefficient × sqrt(Equity / EquityDivisor) は商品の量ステップに四捨五入され、VolumeMin/VolumeMax によって制限されます。
- 保護ロジックは
StartProtection によって処理され、MetaTrader ポイントから絶対価格オフセットに変換されたストップロス、テイクプロフィット、トレーリングストップの距離が関連付けられます。
パラメーター
| パラメータ |
デフォルト |
説明 |
CandleType |
TimeSpan.FromMinutes(5).TimeFrame() |
インジケーターの更新とシグナル評価に使用されるタイムフレーム。 |
RsiPeriod |
14 |
RSI の計算で使用されるローソク足の数。 |
FastSmmaPeriod |
2 |
始値に適用される高速平滑移動平均の期間。 |
SlowEmaPeriod |
6 |
低速 EMA の期間がオープン価格に適用されます。 |
FastCciPeriod |
34 |
通常価格 (H+L+C)/3 から計算された高速 CCI の期間。 |
SlowCciPeriod |
175 |
通常の価格から計算された低速の期間 CCI。 |
BullishRsiLevel |
55 |
ロングエントリーを準備するには、RSI のしきい値を上方に超える必要があります。 |
BearishRsiLevel |
45 |
ショートエントリーを準備するには、RSI のしきい値を下方に超える必要があります。 |
StopLossPoints |
60 |
MetaTrader ポイント単位のストップロス距離 (絶対価格に変換)。無効にするには、0 に設定します。 |
TakeProfitPoints |
0 |
MetaTrader ポイント単位のテイクプロフィット距離。ゼロは、元の EA の動作を維持します (TP なし)。 |
TrailingStopPoints |
20 |
トレーリングストップ距離 (MetaTrader ポイント)。ゼロは末尾を無効にします。 |
LotCoefficient |
0.01 |
動的位置サイジング式で使用される基本係数。 |
EquityDivisor |
10 |
株式ベースのサイジングの平方根内の除数 (sqrt(Equity / EquityDivisor))。 |
MaxSpreadPoints |
18 |
最大許容スプレッド (MetaTrader ポイント単位)。スプレッドが狭くなるまで注文はスキップされます。 |
注意事項
- 拡散フィルタはレベル 1 データに依存します。最良の買値/売値が利用できない場合、戦略は新しいポジションをオープンする前に待機します。
- ポイントから価格への変換は、
PriceStep と商品精度 (5/3 小数商品はステップに 10 を乗算) によって自動的にスケーリングされ、MetaTrader の Point 値を反映します。
- ストップとトレーリングは、StockSharp の市場エグジットを備えた組み込み保護エンジンを通じて管理され、トレーリングストップ更新のための EA の成行注文の使用と一致します。
namespace StockSharp.Samples.Strategies;
using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Messages;
/// <summary>
/// Five Minute RSI CCI strategy: RSI momentum with CCI trend confirmation.
/// Buys when RSI above level and CCI positive, sells when RSI below level and CCI negative.
/// </summary>
public class FiveMinuteRsiCciStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _rsiPeriod;
private readonly StrategyParam<int> _cciPeriod;
private readonly StrategyParam<decimal> _bullishLevel;
private readonly StrategyParam<decimal> _bearishLevel;
private bool _wasBullish;
private bool _hasPrevSignal;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
public int CciPeriod { get => _cciPeriod.Value; set => _cciPeriod.Value = value; }
public decimal BullishLevel { get => _bullishLevel.Value; set => _bullishLevel.Value = value; }
public decimal BearishLevel { get => _bearishLevel.Value; set => _bearishLevel.Value = value; }
public FiveMinuteRsiCciStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(30).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
_rsiPeriod = Param(nameof(RsiPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("RSI Period", "RSI period", "Indicators");
_cciPeriod = Param(nameof(CciPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("CCI Period", "CCI period", "Indicators");
_bullishLevel = Param(nameof(BullishLevel), 55m)
.SetDisplay("Bullish RSI Level", "RSI above this for buy", "Signals");
_bearishLevel = Param(nameof(BearishLevel), 45m)
.SetDisplay("Bearish RSI Level", "RSI below this for sell", "Signals");
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_wasBullish = false;
_hasPrevSignal = false;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_hasPrevSignal = false;
var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
var cci = new CommodityChannelIndex { Length = CciPeriod };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(rsi, cci, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, decimal rsiValue, decimal cciValue)
{
if (candle.State != CandleStates.Finished) return;
var isBullish = rsiValue > BullishLevel && cciValue > 0;
if (_hasPrevSignal && isBullish != _wasBullish)
{
if (isBullish && Position <= 0)
BuyMarket();
else if (!isBullish && rsiValue < BearishLevel && cciValue < 0 && Position >= 0)
SellMarket();
}
_wasBullish = isBullish;
_hasPrevSignal = true;
}
}
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 RelativeStrengthIndex, CommodityChannelIndex
from StockSharp.Algo.Strategies import Strategy
class five_minute_rsi_cci_strategy(Strategy):
def __init__(self):
super(five_minute_rsi_cci_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(30)))
self._rsi_period = self.Param("RsiPeriod", 14)
self._cci_period = self.Param("CciPeriod", 14)
self._bullish_level = self.Param("BullishLevel", 55.0)
self._bearish_level = self.Param("BearishLevel", 45.0)
self._was_bullish = False
self._has_prev_signal = False
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
@property
def RsiPeriod(self):
return self._rsi_period.Value
@RsiPeriod.setter
def RsiPeriod(self, value):
self._rsi_period.Value = value
@property
def CciPeriod(self):
return self._cci_period.Value
@CciPeriod.setter
def CciPeriod(self, value):
self._cci_period.Value = value
@property
def BullishLevel(self):
return self._bullish_level.Value
@BullishLevel.setter
def BullishLevel(self, value):
self._bullish_level.Value = value
@property
def BearishLevel(self):
return self._bearish_level.Value
@BearishLevel.setter
def BearishLevel(self, value):
self._bearish_level.Value = value
def OnReseted(self):
super(five_minute_rsi_cci_strategy, self).OnReseted()
self._was_bullish = False
self._has_prev_signal = False
def OnStarted2(self, time):
super(five_minute_rsi_cci_strategy, self).OnStarted2(time)
self._has_prev_signal = False
rsi = RelativeStrengthIndex()
rsi.Length = self.RsiPeriod
cci = CommodityChannelIndex()
cci.Length = self.CciPeriod
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(rsi, cci, self._process_candle).Start()
def _process_candle(self, candle, rsi_value, cci_value):
if candle.State != CandleStates.Finished:
return
is_bullish = float(rsi_value) > self.BullishLevel and float(cci_value) > 0
if self._has_prev_signal and is_bullish != self._was_bullish:
if is_bullish and self.Position <= 0:
self.BuyMarket()
elif not is_bullish and float(rsi_value) < self.BearishLevel and float(cci_value) < 0 and self.Position >= 0:
self.SellMarket()
self._was_bullish = is_bullish
self._has_prev_signal = True
def CreateClone(self):
return five_minute_rsi_cci_strategy()