GitHub で見る

Tendency EMA + RSI戦略

この戦略は、より遅いトレンドEMAとRSIフィルターの上に高速/中速EMAクロスオーバーを 重ねます。ロングトレードは、高速EMAが中速EMAを上抜けし、両方がゆっくりとしたトレンド ラインを上回り、ローソク足が強気で終値を付けることを必要とします。ショートトレードは これらのルールを反映します。RSIの極値でポジションを閉じ、オプションの「X本後に閉じる」 機能により、価格が期待される方向に素早く動いた場合に利益を確定させます。

このアプローチは、主要なトレンドに沿ったプルバックエントリーのみに参加し、モメンタムが 過度に伸びた時にRSIを使ってエグジットすることを目指します。EMAクロスオーバーがタイム リーなシグナルを提供し、各セッションで複数のセットアップが発生するイントラデイチャートで 最も効果的です。

詳細

  • エントリー条件:
    • 高速EMAが中速EMAを上抜け、両方が低速EMAを上回り、強気のローソク足。
    • 高速EMAが中速EMAを下抜け、両方が低速EMAを下回り、弱気のローソク足。
  • ロング/ショート: ロング有効、ショートはオプション。
  • エグジット条件:
    • RSI > 70でロングを閉じる;RSI < 30でショートを閉じる。
    • オプション:取引が利益を出している場合、X本後に閉じる。
  • ストップ: 組み込みはなし。
  • デフォルト値:
    • RSIの長さ = 14。
    • EMA A/B/Cの長さ = 9/21/50。
    • X本後に閉じる = オフ、X = 5。
  • フィルター:
    • カテゴリ: トレンド + モメンタム
    • 方向: 両方(デフォルトはロング)
    • インジケーター: EMA、RSI
    • ストップ: いいえ
    • 複雑さ: 中程度
    • 時間軸: 短期
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
namespace StockSharp.Samples.Strategies;

using System;
using System.Collections.Generic;

using Ecng.Common;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

/// <summary>
/// Tendency EMA + RSI Strategy.
/// Uses EMA crossover with RSI and trend filter.
/// Buys when fast EMA crosses above medium EMA while above slow EMA.
/// Exits when RSI becomes overbought/oversold.
/// </summary>
public class TendencyEmaRsiStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _rsiLength;
	private readonly StrategyParam<int> _emaALength;
	private readonly StrategyParam<int> _emaBLength;
	private readonly StrategyParam<int> _emaCLength;
	private readonly StrategyParam<int> _cooldownBars;

	private RelativeStrengthIndex _rsi;
	private ExponentialMovingAverage _emaA;
	private ExponentialMovingAverage _emaB;
	private ExponentialMovingAverage _emaC;

	private decimal _prevEmaA;
	private decimal _prevEmaB;
	private int _cooldownRemaining;

	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	public int RsiLength
	{
		get => _rsiLength.Value;
		set => _rsiLength.Value = value;
	}

	public int EmaALength
	{
		get => _emaALength.Value;
		set => _emaALength.Value = value;
	}

	public int EmaBLength
	{
		get => _emaBLength.Value;
		set => _emaBLength.Value = value;
	}

	public int EmaCLength
	{
		get => _emaCLength.Value;
		set => _emaCLength.Value = value;
	}

	public int CooldownBars
	{
		get => _cooldownBars.Value;
		set => _cooldownBars.Value = value;
	}

	public TendencyEmaRsiStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(30).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");

		_rsiLength = Param(nameof(RsiLength), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Length", "RSI calculation length", "RSI");

		_emaALength = Param(nameof(EmaALength), 10)
			.SetGreaterThanZero()
			.SetDisplay("EMA A Length", "Fast EMA length", "Moving Averages");

		_emaBLength = Param(nameof(EmaBLength), 20)
			.SetGreaterThanZero()
			.SetDisplay("EMA B Length", "Medium EMA length", "Moving Averages");

		_emaCLength = Param(nameof(EmaCLength), 100)
			.SetGreaterThanZero()
			.SetDisplay("EMA C Length", "Slow/Trend EMA length", "Moving Averages");

		_cooldownBars = Param(nameof(CooldownBars), 10)
			.SetDisplay("Cooldown Bars", "Bars to wait between trades", "Risk");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_rsi = null;
		_emaA = null;
		_emaB = null;
		_emaC = null;
		_prevEmaA = 0;
		_prevEmaB = 0;
		_cooldownRemaining = 0;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		_rsi = new RelativeStrengthIndex { Length = RsiLength };
		_emaA = new ExponentialMovingAverage { Length = EmaALength };
		_emaB = new ExponentialMovingAverage { Length = EmaBLength };
		_emaC = new ExponentialMovingAverage { Length = EmaCLength };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(_rsi, _emaA, _emaB, _emaC, OnProcess)
			.Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, _emaA);
			DrawIndicator(area, _emaB);
			DrawIndicator(area, _emaC);
			DrawOwnTrades(area);
		}
	}

	private void OnProcess(ICandleMessage candle, decimal rsiVal, decimal emaA, decimal emaB, decimal emaC)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!_rsi.IsFormed || !_emaA.IsFormed || !_emaB.IsFormed || !_emaC.IsFormed)
		{
			_prevEmaA = emaA;
			_prevEmaB = emaB;
			return;
		}

		if (!IsFormedAndOnlineAndAllowTrading())
		{
			_prevEmaA = emaA;
			_prevEmaB = emaB;
			return;
		}

		if (_cooldownRemaining > 0)
		{
			_cooldownRemaining--;
			_prevEmaA = emaA;
			_prevEmaB = emaB;
			return;
		}

		if (_prevEmaA == 0 || _prevEmaB == 0)
		{
			_prevEmaA = emaA;
			_prevEmaB = emaB;
			return;
		}

		// EMA crossovers
		var emaCrossUp = emaA > emaB && _prevEmaA <= _prevEmaB;
		var emaCrossDown = emaA < emaB && _prevEmaA >= _prevEmaB;

		// Buy: EMA A crosses above EMA B + EMA A > EMA C (uptrend) + bullish candle
		if (emaCrossUp && emaA > emaC && candle.ClosePrice > candle.OpenPrice && Position <= 0)
		{
			if (Position < 0)
				BuyMarket(Math.Abs(Position));
			BuyMarket(Volume);
			_cooldownRemaining = CooldownBars;
		}
		// Sell: EMA A crosses below EMA B + EMA A < EMA C (downtrend) + bearish candle
		else if (emaCrossDown && emaA < emaC && candle.ClosePrice < candle.OpenPrice && Position >= 0)
		{
			if (Position > 0)
				SellMarket(Math.Abs(Position));
			SellMarket(Volume);
			_cooldownRemaining = CooldownBars;
		}
		// Exit long: RSI overbought
		else if (Position > 0 && rsiVal > 70)
		{
			SellMarket(Math.Abs(Position));
			_cooldownRemaining = CooldownBars;
		}
		// Exit short: RSI oversold
		else if (Position < 0 && rsiVal < 30)
		{
			BuyMarket(Math.Abs(Position));
			_cooldownRemaining = CooldownBars;
		}

		_prevEmaA = emaA;
		_prevEmaB = emaB;
	}
}