在 GitHub 上查看

MFI Histogram Strategy

MFI Histogram 策略使用资金流量指数(MFI)通过可配置阈值识别超买和超卖区域。MFI 结合价格与成交量衡量资金流入和流出强度。当指标从下方向上突破上限时,视为买盘增强,策略开多并平掉已有空单;当指标从上向下跌破下限时,开空并平掉多单。止损和止盈以跳动点表示,由内置保护机制管理。

该策略运行在用户选择的K线周期(默认4小时),仅依赖单一指标,无额外过滤器。参数允许优化 MFI 周期、阈值以及风险限制,使系统能适应不同市场及波动环境。

细节

  • 入场条件
    • 做多MFI 自下而上穿越 HighLevel
    • 做空MFI 自上而下穿越 LowLevel
  • 多空方向:双向。
  • 离场条件
    • 反向信号导致反转。
    • 触发止损或止盈。
  • 止损StopLossTakeProfit(跳动点)。
  • 默认参数
    • MFI Period = 14
    • HighLevel = 60
    • LowLevel = 40
    • Candle Type = 4 小时
    • StopLoss = 1000 跳
    • TakeProfit = 2000 跳
  • 过滤器
    • 类型:振荡器
    • 方向:双向
    • 指标:单一
    • 止损:有
    • 复杂度:基础
    • 时间框架:中期
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险级别:中等
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>
/// Money Flow Index histogram strategy.
/// Buys when MFI crosses above the high level.
/// Sells when MFI crosses below the low level.
/// </summary>
public class MfiHistogramStrategy : Strategy
{
	private readonly StrategyParam<int> _mfiPeriod;
	private readonly StrategyParam<decimal> _highLevel;
	private readonly StrategyParam<decimal> _lowLevel;
	private readonly StrategyParam<Unit> _stopLoss;
	private readonly StrategyParam<Unit> _takeProfit;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevMfi;

	/// <summary>
	/// Period for Money Flow Index.
	/// </summary>
	public int MfiPeriod
	{
		get => _mfiPeriod.Value;
		set => _mfiPeriod.Value = value;
	}

	/// <summary>
	/// Overbought level.
	/// </summary>
	public decimal HighLevel
	{
		get => _highLevel.Value;
		set => _highLevel.Value = value;
	}

	/// <summary>
	/// Oversold level.
	/// </summary>
	public decimal LowLevel
	{
		get => _lowLevel.Value;
		set => _lowLevel.Value = value;
	}

	/// <summary>
	/// Stop-loss value in ticks.
	/// </summary>
	public Unit StopLoss
	{
		get => _stopLoss.Value;
		set => _stopLoss.Value = value;
	}

	/// <summary>
	/// Take-profit value in ticks.
	/// </summary>
	public Unit TakeProfit
	{
		get => _takeProfit.Value;
		set => _takeProfit.Value = value;
	}

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

	/// <summary>
	/// Initialize <see cref="MfiHistogramStrategy"/>.
	/// </summary>
	public MfiHistogramStrategy()
	{
		_mfiPeriod = Param(nameof(MfiPeriod), 14)
		.SetGreaterThanZero()
		.SetDisplay("MFI Period", "Period for Money Flow Index", "MFI");

		_highLevel = Param(nameof(HighLevel), 60m)
		.SetDisplay("High Level", "Overbought threshold", "MFI");

		_lowLevel = Param(nameof(LowLevel), 40m)
		.SetDisplay("Low Level", "Oversold threshold", "MFI");

		_stopLoss = Param(nameof(StopLoss), new Unit(1000, UnitTypes.Absolute))
		.SetDisplay("Stop Loss", "Stop-loss in ticks", "Risk");

		_takeProfit = Param(nameof(TakeProfit), new Unit(2000, UnitTypes.Absolute))
		.SetDisplay("Take Profit", "Take-profit in ticks", "Risk");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
	base.OnReseted();

	_prevMfi = 0;
	}

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

	var mfi = new MoneyFlowIndex
	{
		Length = MfiPeriod
	};

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

	var area = CreateChartArea();
	if (area != null)
	{
		DrawCandles(area, subscription);
		DrawOwnTrades(area);

		var area2 = CreateChartArea();
		if (area2 != null)
			DrawIndicator(area2, mfi);
	}
	}

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

	if (!IsFormedAndOnlineAndAllowTrading())
	{
		_prevMfi = mfiValue;
		return;
	}

	// MFI crosses above high level
	if (mfiValue > HighLevel && _prevMfi <= HighLevel)
	{
	if (Position <= 0)
	BuyMarket();
	}
	// MFI crosses below low level
	else if (mfiValue < LowLevel && _prevMfi >= LowLevel)
	{
	if (Position >= 0)
	SellMarket();
	}

	_prevMfi = mfiValue;
	}
}