Auf GitHub ansehen

Delta MFI-Strategie

Strategie basierend auf dem Vergleich von schnellen und langsamen Money Flow Index (MFI) Werten. Sie geht long, wenn der schnelle MFI über den langsamen MFI steigt, während der langsame MFI über dem Signalniveau liegt. Sie geht short, wenn der schnelle MFI unter den langsamen MFI fällt, während der langsame MFI unter 100 minus dem Signalniveau liegt.

Details

  • Einstiegskriterien:
    • Kaufen wenn slow MFI > Level und fast MFI > slow MFI
    • Verkaufen wenn slow MFI < 100 - Level und fast MFI < slow MFI
  • Long/Short: Beide
  • Ausstiegskriterien: Gegenteiliges Signal
  • Stops: Nein
  • Standardwerte:
    • FastPeriod = 14
    • SlowPeriod = 50
    • Level = 50
    • CandleType = 4-Stunden-Kerzen
  • Filter:
    • Kategorie: Indikator
    • Richtung: Beide
    • Indikatoren: Money Flow Index
    • Stops: Nein
    • Komplexität: Grundlegend
    • Zeitrahmen: H4
    • Saisonalität: Nein
    • Neuronale Netze: Nein
    • Divergenz: Nein
    • Risikolevel: Mittel
using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Strategy based on the difference between fast and slow Money Flow Index (MFI).
/// Buys when the fast MFI is above the slow MFI and the slow MFI is above the signal level.
/// Sells when the fast MFI is below the slow MFI and the slow MFI is below 100 minus the signal level.
/// </summary>
public class DeltaMfiStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<int> _level;
	private readonly StrategyParam<DataType> _candleType;

	/// <summary>
	/// Fast MFI period length.
	/// </summary>
	public int FastPeriod
	{
		get => _fastPeriod.Value;
		set => _fastPeriod.Value = value;
	}

	/// <summary>
	/// Slow MFI period length.
	/// </summary>
	public int SlowPeriod
	{
		get => _slowPeriod.Value;
		set => _slowPeriod.Value = value;
	}

	/// <summary>
	/// MFI level used to confirm signals.
	/// </summary>
	public int Level
	{
		get => _level.Value;
		set => _level.Value = value;
	}

	/// <summary>
	/// The type of candles used for calculations.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Constructor.
	/// </summary>
	public DeltaMfiStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("Fast MFI Period", "Period for fast Money Flow Index", "Parameters")
			
			.SetOptimize(5, 30, 5);

		_slowPeriod = Param(nameof(SlowPeriod), 50)
			.SetGreaterThanZero()
			.SetDisplay("Slow MFI Period", "Period for slow Money Flow Index", "Parameters")
			
			.SetOptimize(20, 100, 10);

		_level = Param(nameof(Level), 50)
			.SetGreaterThanZero()
			.SetDisplay("Signal Level", "MFI level to confirm signals", "Parameters")
			
			.SetOptimize(30, 70, 5);

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

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

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

		// Enable position protection once
		StartProtection(null, null);

		var fastMfi = new MoneyFlowIndex { Length = FastPeriod };
		var slowMfi = new MoneyFlowIndex { Length = SlowPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(fastMfi, slowMfi, ProcessCandle).Start();

		// Draw indicators if a chart is available
		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, fastMfi);
			DrawIndicator(area, slowMfi);
		}
	}

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

		// Check strategy readiness and connection state
		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		// Long signal: slow MFI above level and fast MFI above slow MFI
		if (slowValue > Level && fastValue > slowValue && Position <= 0)
		{
			BuyMarket();
			return;
		}

		// Short signal: slow MFI below (100 - level) and fast MFI below slow MFI
		if (slowValue < (100 - Level) && fastValue < slowValue && Position >= 0)
		{
			SellMarket();
		}
	}
}