GitHub で見る

EMAクロスオーバーシグナル戦略

この戦略は2本の指数移動平均(EMA)のクロスオーバーで取引します。選択したローソク足シリーズから速いEMAと遅いEMAを計算します。速いEMAが遅いEMAを上抜けすると、戦略は既存のショートポジションを決済し、必要に応じてロングポジションを開くことができます。速いEMAが遅いEMAを下抜けすると、ロングポジションを決済し、必要に応じてショートポジションを開くことができます。

リスク管理のため、新しいポジションを開いた後にテイクプロフィットおよびストップロス注文を発注することができます。どちらの距離もティックで指定します。これらの保護注文は新しいエントリーごとにキャンセルされ、再作成されます。

この戦略はロングおよびショートエントリーの有効・無効を切り替える独立したスイッチを提供し、反対のシグナルでロングおよびショートポジションを個別に決済することもできます。すべての計算は完成したローソク足のみを使用します。

パラメーター

  • Fast Period – 速いEMAの長さ。
  • Slow Period – 遅いEMAの長さ。
  • Candle Type – 計算に使用するローソク足の時間軸。
  • Allow Buy Open – 速いEMAが遅いEMAを上抜けしたときにロングを開く。
  • Allow Sell Open – 速いEMAが遅いEMAを下抜けしたときにショートを開く。
  • Allow Buy Close – 速いEMAが遅いEMAを下抜けしたときにロングを閉じる。
  • Allow Sell Close – 速いEMAが遅いEMAを上抜けしたときにショートを閉じる。
  • Take Profit Ticks – エントリー価格からのテイクプロフィット距離(ティック)。
  • Stop Loss Ticks – エントリー価格からのストップロス距離(ティック)。
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>
/// Trades EMA crossovers with optional separate entry and exit permissions for long and short positions.
/// </summary>
public class EmaCrossoverSignalStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private bool _isInitialized;
	private bool _wasFastAboveSlow;

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

	public EmaCrossoverSignalStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 5)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Length of the fast EMA", "EMA");

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

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_isInitialized = default;
		_wasFastAboveSlow = default;
	}

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

		var fastEma = new ExponentialMovingAverage { Length = FastPeriod };
		var slowEma = new ExponentialMovingAverage { Length = SlowPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(fastEma, slowEma, Process)
			.Start();
	}

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

		if (!_isInitialized)
		{
			_wasFastAboveSlow = fastValue > slowValue;
			_isInitialized = true;
			return;
		}

		var isFastAboveSlow = fastValue > slowValue;

		if (_wasFastAboveSlow != isFastAboveSlow)
		{
			if (isFastAboveSlow)
			{
				// Upward crossover - buy signal
				if (Position < 0)
					BuyMarket();
				if (Position <= 0)
					BuyMarket();
			}
			else
			{
				// Downward crossover - sell signal
				if (Position > 0)
					SellMarket();
				if (Position >= 0)
					SellMarket();
			}

			_wasFastAboveSlow = isFastAboveSlow;
		}
	}
}