GitHub で見る

EMA SAR Power戦略

このイントラデイ戦略は、高速・低速の指数移動平均線とParabolic SAR、Bulls/Bears Powerインジケーターを組み合わせます。活発な市場時間帯のみ取引し、ポジションを建てる前に十分な余剰証拠金を必要とします。

高速EMAが低速EMAを下回り、Parabolic SARがローソク足の高値の上にあり、Bears Powerが負の値を保ちながら上昇しているときにショートポジションを建てます。高速EMAが低速EMAを上回り、Parabolic SARがローソク足の安値の下にあり、Bulls Powerが下降しながらも正の値を維持しているときにロングポジションを建てます。各取引には広いストップロスと近いテイクプロフィットが設定されます。

動的証拠金フィルター

取引前に、戦略はポートフォリオの余剰証拠金を確認します。その値に応じて、必要な最低証拠金が段階的に増加します:600 → 1000 → 1300 → 1500 → 1800 → 2000 → 2500。余剰証拠金が現在のしきい値を下回ると取引はスキップされます。

詳細

  • エントリー条件:
    • ショート: EMA3 < EMA34 && SAR > High && BearsPower < 0 && BearsPower > BearsPower[1].
    • ロング: EMA3 > EMA34 && SAR < Low && BullsPower > 0 && BullsPower < BullsPower[1].
  • ロング/ショート: 両方向。
  • ストップ/ターゲット: ストップロス2000ポイント、テイクプロフィット400ポイント。
  • 時間フィルター: ブローカー時間の09:00から16:59の間のみ取引。
  • インジケーター:
    • 中央価格に基づく指数移動平均線(3、34)。
    • Parabolic SAR(ステップ0.02、最大0.2)。
    • Bulls Power(13)とBears Power(13)。
  • デフォルト数量: 30契約。
  • 時間軸: 15分ローソク足。
  • フィルター:
    • カテゴリ: トレンドフォロー
    • 方向: 両方
    • インジケーター: 複数
    • ストップ: はい
    • 複雑さ: 中程度
    • 時間軸: イントラデイ
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 高
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>
/// EMA crossover strategy with Parabolic SAR confirmation.
/// Buys when fast EMA above slow EMA and SAR below price.
/// Sells when fast EMA below slow EMA and SAR above price.
/// </summary>
public class EmaSarPowerStrategy : Strategy
{
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _hasPrev;

	public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
	public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public EmaSarPowerStrategy()
	{
		_fastLength = Param(nameof(FastLength), 3)
			.SetGreaterThanZero()
			.SetDisplay("Fast EMA", "Fast EMA period", "Indicators");

		_slowLength = Param(nameof(SlowLength), 34)
			.SetGreaterThanZero()
			.SetDisplay("Slow EMA", "Slow EMA period", "Indicators");

		_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();
		_prevFast = 0;
		_prevSlow = 0;
		_hasPrev = false;
	}

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

		var fastEma = new ExponentialMovingAverage { Length = FastLength };
		var slowEma = new ExponentialMovingAverage { Length = SlowLength };
		var sar = new ParabolicSar();

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(fastEma, slowEma, sar, ProcessCandle)
			.Start();

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

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

		if (!_hasPrev)
		{
			_prevFast = fast;
			_prevSlow = slow;
			_hasPrev = true;
			return;
		}

		// Buy: EMA crossover up + SAR below price
		if (_prevFast <= _prevSlow && fast > slow && sar < candle.LowPrice)
		{
			if (Position <= 0)
				BuyMarket();
		}
		// Sell: EMA crossover down + SAR above price
		else if (_prevFast >= _prevSlow && fast < slow && sar > candle.HighPrice)
		{
			if (Position >= 0)
				SellMarket();
		}

		_prevFast = fast;
		_prevSlow = slow;
	}
}