Auf GitHub ansehen

Delta MFI Strategy

Strategy based on comparing fast and slow Money Flow Index (MFI) values. It goes long when the fast MFI rises above the slow MFI while the slow MFI is above the signal level. It goes short when the fast MFI falls below the slow MFI while the slow MFI is below 100 minus the signal level.

Details

  • Entry Criteria:
    • Buy when slow MFI > Level and fast MFI > slow MFI
    • Sell when slow MFI < 100 - Level and fast MFI < slow MFI
  • Long/Short: Both
  • Exit Criteria: Opposite signal
  • Stops: No
  • Default Values:
    • FastPeriod = 14
    • SlowPeriod = 50
    • Level = 50
    • CandleType = 4-hour candles
  • Filters:
    • Category: Indicator
    • Direction: Both
    • Indicators: Money Flow Index
    • Stops: No
    • Complexity: Basic
    • Timeframe: H4
    • Seasonality: No
    • Neural networks: No
    • Divergence: No
    • Risk level: Medium
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();
		}
	}
}