在 GitHub 上查看

斐波那契平均均线交叉策略

概述

该策略将 MetaTrader 的 EA_Fibo_Avg_001a 转换为 StockSharp 框架。 策略使用两条平滑移动平均线,慢线长度等于基础周期加上一个斐波那契偏移。 当快线向上穿越慢线时做多,反向穿越时做空。 开仓后使用止损、止盈和跟踪止损管理头寸,可选的资金管理根据账户规模计算下单量。

参数

  • CandleType – K线类型。
  • FiboNumPeriod – 加到慢均线上的额外长度。
  • MaPeriod – 移动平均线的基础周期。
  • TrailingStop – 跟踪止损距离(价格步长)。
  • TakeProfit – 止盈距离(价格步长)。
  • StopLoss – 止损距离(价格步长)。
  • UseMoneyManagement – 是否启用简单资金管理。
  • PercentMm – 启用资金管理时使用的账户百分比。
  • LotSize – 未启用资金管理时的默认下单量。

逻辑

  1. 订阅K线并计算两条平滑移动平均线。
  2. 快线向上穿越慢线时买入,向下穿越时卖出。
  3. 建仓后设置止损、止盈和跟踪止损。
  4. 随价格有利变动更新跟踪止损,当保护水平被触发或出现反向交叉时平仓。
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>
/// Strategy based on crossover of two smoothed moving averages with Fibonacci offset.
/// </summary>
public class FiboAvg001aStrategy : Strategy
{
	private readonly StrategyParam<int> _fiboNumPeriod;
	private readonly StrategyParam<int> _maPeriod;
	private readonly StrategyParam<DataType> _candleType;

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

	public int FiboNumPeriod { get => _fiboNumPeriod.Value; set => _fiboNumPeriod.Value = value; }
	public int MaPeriod { get => _maPeriod.Value; set => _maPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public FiboAvg001aStrategy()
	{
		_fiboNumPeriod = Param(nameof(FiboNumPeriod), 11)
			.SetGreaterThanZero()
			.SetDisplay("Fibo Period", "Additional length for slow MA", "Indicators");

		_maPeriod = Param(nameof(MaPeriod), 21)
			.SetGreaterThanZero()
			.SetDisplay("MA Period", "Base moving average 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 fastMa = new SmoothedMovingAverage { Length = MaPeriod };
		var slowMa = new SmoothedMovingAverage { Length = MaPeriod + FiboNumPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(fastMa, slowMa, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevFast = fast;
			_prevSlow = slow;
			_hasPrev = true;
			return;
		}

		// Fast crosses above slow -> buy
		if (_prevFast <= _prevSlow && fast > slow)
		{
			if (Position < 0)
				BuyMarket();
			if (Position <= 0)
				BuyMarket();
		}
		// Fast crosses below slow -> sell
		else if (_prevFast >= _prevSlow && fast < slow)
		{
			if (Position > 0)
				SellMarket();
			if (Position >= 0)
				SellMarket();
		}

		_prevFast = fast;
		_prevSlow = slow;
	}
}