GitHub で見る

Charles EMA RSI 戦略

この戦略はCharlesエキスパートアドバイザーをエミュレートし、指数移動平均 (EMA) とRSIフィルター、トレーリングストップを組み合わせています。両方向で取引し、ポジションを動的に保護します。

システムは選択した時間軸で高速と低速のEMAを監視します。高速EMAが低速EMAを上抜けし、RSIが55を超えると、戦略はロングポジションに入ります。逆に、高速EMAが低速EMAを下抜けし、RSIが45を下回ると、ショートポジションに入ります。エントリー後、トレーリングストップが価格を追って利益を確定しながら、固定のテイクプロフィットとストップロスが組み込みのポジション保護によって管理されます。

詳細

  • エントリー条件:
    • ロング: 高速EMA低速EMAを上抜け、かつRSI > 55
    • ショート: 高速EMA低速EMAを下抜け、かつRSI < 45
  • ロング/ショート: 両方。
  • エグジット条件:
    • トレーリングストップ。
    • ストップロスまたはテイクプロフィット。
  • ストップ: トレーリング付き組み込み保護を使用。
  • デフォルト値:
    • FastPeriod = 18
    • SlowPeriod = 60
    • RsiPeriod = 14
    • TakeProfit = 0.02
    • StopLoss = 0.008
    • TrailStart = 0.006
    • TrailOffset = 0.003
  • フィルター:
    • カテゴリ: トレンドフォロー
    • 方向: 両方
    • インジケーター: 複数
    • ストップ: はい
    • 複雑さ: 中級
    • 時間軸: デフォルト1時間
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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>
/// Charles strategy: EMA crossover with RSI filter.
/// </summary>
public class CharlesStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private bool _wasFastBelowSlow;
	private bool _isInitialized;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public CharlesStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 18)
			.SetGreaterThanZero()
			.SetDisplay("Fast EMA", "Period of the fast EMA", "Indicators");

		_slowPeriod = Param(nameof(SlowPeriod), 60)
			.SetGreaterThanZero()
			.SetDisplay("Slow EMA", "Period of the slow EMA", "Indicators");

		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Period", "RSI calculation length", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Time frame for candles", "General");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType)];

	protected override void OnReseted()
	{
		base.OnReseted();
		_wasFastBelowSlow = false;
		_isInitialized = false;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var fast = new ExponentialMovingAverage { Length = FastPeriod };
		var slow = new ExponentialMovingAverage { Length = SlowPeriod };
		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };

		SubscribeCandles(CandleType)
			.Bind(fast, slow, rsi, ProcessCandle)
			.Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue, decimal rsiValue)
	{
		if (candle.State != CandleStates.Finished) return;

		if (!_isInitialized)
		{
			_wasFastBelowSlow = fastValue < slowValue;
			_isInitialized = true;
			return;
		}

		var isFastBelowSlow = fastValue < slowValue;

		// Long signal: fast crosses above slow + RSI > 55
		if (_wasFastBelowSlow && !isFastBelowSlow && rsiValue > 55 && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		// Short signal: fast crosses below slow + RSI < 45
		else if (!_wasFastBelowSlow && isFastBelowSlow && rsiValue < 45 && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_wasFastBelowSlow = isFastBelowSlow;
	}
}