GitHub で見る

EMA Sticker戦略

この戦略は指数移動平均線(EMA)を使用して短期トレンドを追います。終値がEMAを上抜けするとロングポジションを建て、下抜けするとショートポジションを建てます。オプションの固定ストップロスとテイクプロフィットレベルがリスク管理に役立ちます。

詳細

  • エントリー条件:
    • ロング: Close > EMA.
    • ショート: Close < EMA.
  • ロング/ショート: 両方のサイド。
  • エグジット条件:
    • 反対のシグナルまたは設定されたストップレベルに到達。
  • ストップ: あり、価格単位でのオプションのストップロスとテイクプロフィット。
  • デフォルト値:
    • MA period = 5.
    • Stop loss = 0.001.
    • Take profit = 0.001.
  • フィルター:
    • カテゴリ: トレンドフォロー
    • 方向: 両方
    • インジケーター: 単一
    • ストップ: はい
    • 複雑さ: シンプル
    • 時間軸: 短期
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>
/// Simple EMA-based trend following strategy.
/// Buys when price crosses above EMA, sells on opposite cross.
/// </summary>
public class EmaStickerStrategy : Strategy
{
	private readonly StrategyParam<int> _maPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private bool _wasAbove;
	private bool _hasPrev;

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

	public EmaStickerStrategy()
	{
		_maPeriod = Param(nameof(MaPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("EMA Period", "Length of the EMA", "Parameters");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_wasAbove = false;
		_hasPrev = false;
	}

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

		var ema = new ExponentialMovingAverage { Length = MaPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(ema, ProcessCandle)
			.Start();
	}

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

		var isAbove = candle.ClosePrice > emaValue;

		if (!_hasPrev)
		{
			_wasAbove = isAbove;
			_hasPrev = true;
			return;
		}

		// Cross above EMA -> buy
		if (isAbove && !_wasAbove)
		{
			if (Position < 0)
				BuyMarket();
			if (Position <= 0)
				BuyMarket();
		}
		// Cross below EMA -> sell
		else if (!isAbove && _wasAbove)
		{
			if (Position > 0)
				SellMarket();
			if (Position >= 0)
				SellMarket();
		}

		_wasAbove = isAbove;
	}
}