在 GitHub 上查看

MACFibo策略

本策略实现MACFibo系统。当5周期EMA与20周期SMA发生交叉后,算法测量交叉柱收盘价(点A)到最近极值(点B)的距离,并构建斐波那契扩展水平。随后按市价开仓,止盈和止损来自这些水平。当快速EMA与中间SMA反向交叉且头寸亏损时,可选择提前平仓。

详情

  • 入场条件:
    • 多头: 5 EMA上穿20 SMA。点B为下跌段的最低点。
    • 空头: 5 EMA下穿20 SMA。点B为上涨段的最高点。
  • 出场条件:
    • 在161.8%斐波那契水平或最小止盈距离处获利了结。
    • 在38.2%斐波那契水平或最大止损距离处止损。
    • 如果5 EMA与8 SMA反向交叉且头寸亏损,可提前平仓。
  • 过滤器:
    • 仅在设定的起始和结束小时之间交易。
    • 可禁用周一或周五交易。
  • 参数:
    • FastLength – 快速EMA周期。
    • MidLength – 用于保护性退出的中间SMA周期。
    • SlowLength – 用于趋势判断的慢速SMA周期。
    • MinTakeProfit – 最小止盈距离。
    • MaxStopLoss – 最大止损距离。
    • StartHour / EndHour – 允许的交易时间段。
    • FridayTrade / MondayTrade – 是否在这些日子交易。
    • CloseAtFastMid – 在fast/mid交叉时关闭亏损头寸。
    • CandleType – 计算所用的K线类型。
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 Fibonacci-inspired targets.
/// </summary>
public class MacfiboStrategy : 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 MacfiboStrategy()
	{
		_fastLength = Param(nameof(FastLength), 10)
			.SetGreaterThanZero()
			.SetDisplay("Fast EMA", "Fast EMA period", "Indicators");

		_slowLength = Param(nameof(SlowLength), 20)
			.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 fast = new ExponentialMovingAverage { Length = FastLength };
		var slow = new ExponentialMovingAverage { Length = SlowLength };

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

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

		if (!_hasPrev)
		{
			_prevFast = fastValue;
			_prevSlow = slowValue;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevFast <= _prevSlow && fastValue > slowValue;
		var crossDown = _prevFast >= _prevSlow && fastValue < slowValue;

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

		_prevFast = fastValue;
		_prevSlow = slowValue;
	}
}