GitHub で見る

Robust EA テンプレート戦略

MQLのRobust EAテンプレートを実装した戦略です。 エントリーシグナルの生成にCommodity Channel Index (CCI)とRelative Strength Index (RSI)を使用し、固定のテイクプロフィットとストップロスを適用します。

ロジック

  • CCIが-200..-150または-100..-50の範囲にあり、RSIが0から25の間のとき買い。
  • CCIが50から150の間にあり、RSIが80から100の間のとき売り。
  • ストップロスとテイクプロフィットはpipsで定義され、価格ポイントに変換される。

パラメーター

  • Candle Type – ローソク足データシリーズ。
  • CCI Period – CCIインジケーターの期間。
  • RSI Period – RSIインジケーターの期間。
  • Take Profit (pips) – 利益目標までの距離。
  • Stop Loss (pips) – ストップロスまでの距離。
  • Volume – 注文数量。
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>
/// Strategy using CCI and RSI indicators to generate trading signals.
/// </summary>
public class RobustEaTemplateStrategy : Strategy
{
	private readonly StrategyParam<int> _cciPeriod;
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<decimal> _takeProfitPct;
	private readonly StrategyParam<decimal> _stopLossPct;
	private readonly StrategyParam<DataType> _candleType;

	public int CciPeriod
	{
		get => _cciPeriod.Value;
		set => _cciPeriod.Value = value;
	}

	public int RsiPeriod
	{
		get => _rsiPeriod.Value;
		set => _rsiPeriod.Value = value;
	}

	public decimal TakeProfitPct
	{
		get => _takeProfitPct.Value;
		set => _takeProfitPct.Value = value;
	}

	public decimal StopLossPct
	{
		get => _stopLossPct.Value;
		set => _stopLossPct.Value = value;
	}

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

	public RobustEaTemplateStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(8).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");

		_cciPeriod = Param(nameof(CciPeriod), 14)
			.SetDisplay("CCI Period", "Commodity Channel Index period", "Indicators");

		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetDisplay("RSI Period", "Relative Strength Index period", "Indicators");

		_takeProfitPct = Param(nameof(TakeProfitPct), 3m)
			.SetDisplay("Take Profit %", "Take profit percentage", "Risk");

		_stopLossPct = Param(nameof(StopLossPct), 2m)
			.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk");
	}

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

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

		var cci = new CommodityChannelIndex { Length = CciPeriod };
		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription.BindEx(cci, rsi, (candle, cciVal, rsiVal) =>
		{
			if (candle.State != CandleStates.Finished)
				return;

			if (!cciVal.IsFormed || !rsiVal.IsFormed)
				return;

			if (!IsFormedAndOnlineAndAllowTrading())
				return;

			var cciValue = cciVal.ToDecimal();
			var rsiValue = rsiVal.ToDecimal();

			// Wider signal conditions
			var longSignal = cciValue < -50m && rsiValue < 40m;
			var shortSignal = cciValue > 50m && rsiValue > 60m;

			if (longSignal && Position <= 0)
			{
				if (Position < 0) BuyMarket();
				BuyMarket();
			}
			else if (shortSignal && Position >= 0)
			{
				if (Position > 0) SellMarket();
				SellMarket();
			}
		}).Start();

		StartProtection(
			takeProfit: new Unit(TakeProfitPct, UnitTypes.Percent),
			stopLoss: new Unit(StopLossPct, UnitTypes.Percent),
			useMarketOrders: true);

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