GitHub で見る

Follow Your Heart戦略

概要

この戦略は、MetaTraderの「Follow Your Heart」エキスパートアドバイザーのStockSharp移植版です。直近の複数のロウソク足を分析し、始値、終値、高値、安値の相対的な変化を合計します。すべての変化が閾値を上回り、合計値が正のときにロングポジションを建てます。逆の条件でショートポジションを建てます。同時に存在できるポジションは1つのみです。

ポジションは口座通貨で測定された利益・損失制限と、ポイント単位のテイクプロフィット/ストップロスで保護されます。オプションの取引セッションにより、指定された時間内のみシグナルが許可されます。

パラメーター

  • Bars – 価格変化を累積するために使用するロウソク足の数。デフォルト: 6。
  • Level – 始値と終値の変化の閾値。デフォルト: 2.3。
  • ProfitBuy – ロングポジションを決済するための金銭的利益目標。デフォルト: 75。
  • ProfitSell – ショートポジションを決済するための金銭的利益目標。デフォルト: 56。
  • LossBuy – ロングポジションを決済するための金銭的損失閾値。デフォルト: -54。
  • LossSell – ショートポジションを決済するための金銭的損失閾値。デフォルト: -51。
  • TakeProfit – ポイント単位のテイクプロフィット。デフォルト: 550。
  • StopLoss – ポイント単位のストップロス。デフォルト: 550。
  • TradingHoursOn – セッションフィルタリングを有効にする。デフォルト: true。
  • OpenHourBuy / CloseHourBuy – 買いシグナルの許容時間。デフォルト: 6 / 12。
  • OpenHourSell / CloseHourSell – 売りシグナルの許容時間。デフォルト: 4 / 10。
  • CandleType – ロウソク足の時間軸。デフォルト: 1分。

戦略ロジック

  1. 完成した各ロウソク足について、前のロウソク足と比較した始値、終値、高値、安値の相対変化を計算し、移動合計を更新します。
  2. ポジションが存在しない場合:
    • 合計が正で、始値と終値の変化が共にLevelを上回り、終値の変化が買いセッション中に始値の変化より大きいときに買い
    • 合計が負で、始値と終値の変化が共に-Levelを下回り、終値の変化が売りセッション中に始値の変化より小さいときに売り
  3. ポジションが存在する場合、利益または損失が設定された金銭的制限を超えるか、価格がTakeProfit/StopLossポイント動いたときに決済します。

注意事項

  • 成行注文のみ使用されます。
  • オリジナルコードの資金管理は簡略化されており、ポジション量は戦略の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>
/// Momentum strategy based on EMA crossover (converted from OHLC sum).
/// </summary>
public class FollowYourHeartStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;

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

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

	public FollowYourHeartStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe", "General");

		_fastPeriod = Param(nameof(FastPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("Fast EMA", "Fast EMA period", "Indicators");

		_slowPeriod = Param(nameof(SlowPeriod), 30)
			.SetGreaterThanZero()
			.SetDisplay("Slow EMA", "Slow EMA period", "Indicators");
	}

	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 fast = new ExponentialMovingAverage { Length = FastPeriod };
		var slow = new ExponentialMovingAverage { Length = SlowPeriod };

		SubscribeCandles(CandleType).Bind(fast, slow, ProcessCandle).Start();
	}

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

		if (!_hasPrev)
		{
			_prevFast = fastVal;
			_prevSlow = slowVal;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevFast <= _prevSlow && fastVal > slowVal;
		var crossDown = _prevFast >= _prevSlow && fastVal < slowVal;

		if (crossUp && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (crossDown && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevFast = fastVal;
		_prevSlow = slowVal;
	}
}