GitHub で見る

RSI MA トレンド戦略

この戦略は相対力指数(RSI)と移動平均トレンドフィルターを組み合わせたものです。 高速移動平均が低速移動平均を上回っている間にRSIが指定した買いレベルを下回ったとき、ロングポジションを建てます。 高速移動平均が低速移動平均を下回っている間にRSIが指定した売りレベルを上回ったとき、ショートポジションを建てます。

パラメーター

  • RSI Period – RSIインジケーターの長さ。
  • RSI Buy Level – ロングポジションを建てるRSI値の下限。
  • RSI Sell Level – ショートポジションを建てるRSI値の上限。
  • Fast MA Period – 高速移動平均の期間。
  • Slow MA Period – 低速移動平均の期間。
  • Candle Type – 計算に使用するローソク足シリーズ。

ロジック

  1. 選択したローソク足シリーズを購読します。
  2. 各確定ローソク足に対してRSI、高速MAおよび低速MAを計算します。
  3. 高速MAが低速MAを上回ったときを上昇トレンド、下回ったときを下降トレンドとして検出します。
  4. RSI < 買いレベルかつトレンドが上昇のときにロングエントリー、ショートポジションがあれば決済します。
  5. RSI > 売りレベルかつトレンドが下降のときにショートエントリー、ロングポジションがあれば決済します。

注意事項

  • この戦略はエントリーに成行注文を使用します。
  • 取引シグナルは確定したローソク足のみで処理されます。
  • パラメーターはユーザーインターフェースで最適化できます。
using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Strategy combining RSI with moving average trend filter.
/// Buys when RSI is below the buy level and fast MA is above slow MA.
/// Sells when RSI is above the sell level and fast MA is below slow MA.
/// </summary>
public class RsiMaTrendStrategy : Strategy
{
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<decimal> _rsiBuyLevel;
	private readonly StrategyParam<decimal> _rsiSellLevel;
	private readonly StrategyParam<int> _fastMaPeriod;
	private readonly StrategyParam<int> _slowMaPeriod;
	private readonly StrategyParam<DataType> _candleType;

	/// <summary>
	/// RSI period length.
	/// </summary>
	public int RsiPeriod
	{
		get => _rsiPeriod.Value;
		set => _rsiPeriod.Value = value;
	}

	/// <summary>
	/// RSI level to trigger buy.
	/// </summary>
	public decimal RsiBuyLevel
	{
		get => _rsiBuyLevel.Value;
		set => _rsiBuyLevel.Value = value;
	}

	/// <summary>
	/// RSI level to trigger sell.
	/// </summary>
	public decimal RsiSellLevel
	{
		get => _rsiSellLevel.Value;
		set => _rsiSellLevel.Value = value;
	}

	/// <summary>
	/// Fast moving average period.
	/// </summary>
	public int FastMaPeriod
	{
		get => _fastMaPeriod.Value;
		set => _fastMaPeriod.Value = value;
	}

	/// <summary>
	/// Slow moving average period.
	/// </summary>
	public int SlowMaPeriod
	{
		get => _slowMaPeriod.Value;
		set => _slowMaPeriod.Value = value;
	}

	/// <summary>
	/// Candle type used for calculations.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Constructor.
	/// </summary>
	public RsiMaTrendStrategy()
	{
		_rsiPeriod = Param(nameof(RsiPeriod), 21)
			.SetGreaterThanZero()
			.SetDisplay("RSI Period", "Length of RSI indicator", "Indicators")
			
			.SetOptimize(10, 30, 5);

		_rsiBuyLevel = Param(nameof(RsiBuyLevel), 45m)
			.SetDisplay("RSI Buy Level", "Value below which long is opened", "Indicators")
			
			.SetOptimize(20m, 40m, 5m);

		_rsiSellLevel = Param(nameof(RsiSellLevel), 55m)
			.SetDisplay("RSI Sell Level", "Value above which short is opened", "Indicators")
			
			.SetOptimize(60m, 80m, 5m);

		_fastMaPeriod = Param(nameof(FastMaPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("Fast MA Period", "Length of fast moving average", "Indicators")
			
			.SetOptimize(20, 80, 10);

		_slowMaPeriod = Param(nameof(SlowMaPeriod), 50)
			.SetGreaterThanZero()
			.SetDisplay("Slow MA Period", "Length of slow moving average", "Indicators")
			
			.SetOptimize(100, 300, 20);

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

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

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

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

		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
		var fastMa = new ExponentialMovingAverage { Length = FastMaPeriod };
		var slowMa = new ExponentialMovingAverage { Length = SlowMaPeriod };

		var subscription = SubscribeCandles(CandleType);

		subscription
			.Bind(rsi, fastMa, slowMa, ProcessCandle)
			.Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, rsi);
			DrawIndicator(area, fastMa);
			DrawIndicator(area, slowMa);
			DrawOwnTrades(area);
		}
	}

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var isUpTrend = fastMaValue > slowMaValue;

		if (rsiValue < RsiBuyLevel && isUpTrend && Position <= 0)
		{
			if (Position < 0)
				BuyMarket();
			BuyMarket();
		}
		else if (rsiValue > RsiSellLevel && !isUpTrend && Position >= 0)
		{
			if (Position > 0)
				SellMarket();
			SellMarket();
		}
	}
}