在 GitHub 上查看

MA MACD BB BackTester

该策略可在三种指标之间选择:简单移动平均线交叉、MACD 交叉或布林带突破。任意时刻仅启用一种模式,并可选择做多或做空方向。

参数

  • CandleType — K线周期。
  • Indicator — 使用的指标(MA、MACD、BB)。
  • Direction — 交易方向(Long 或 Short)。
  • MaLength — 移动平均线周期。
  • FastLength — MACD 快速 EMA 周期。
  • SlowLength — MACD 慢速 EMA 周期。
  • SignalLength — MACD 信号线周期。
  • BbLength — 布林带周期。
  • BbMultiplier — 布林带系数。
  • StartDate — 开始日期。
  • EndDate — 结束日期。
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 using MA crossover with price for entry/exit.
/// </summary>
public class MaMacdBbBackTesterStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _maLength;

	private ExponentialMovingAverage _ma;
	private decimal _prevClose;
	private decimal _prevMa;
	private bool _initialized;
	private int _cooldown;

	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
	public int MaLength { get => _maLength.Value; set => _maLength.Value = value; }

	public MaMacdBbBackTesterStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
			.SetDisplay("Candle Type", "Candles", "General");
		_maLength = Param(nameof(MaLength), 20)
			.SetDisplay("MA Length", "MA period", "Indicators");
	}

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

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

		_prevClose = default;
		_prevMa = default;
		_initialized = false;
		_cooldown = default;
	}

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

		_ma = new ExponentialMovingAverage { Length = MaLength };

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

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

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

		if (!_ma.IsFormed)
			return;

		if (!_initialized)
		{
			_prevClose = candle.ClosePrice;
			_prevMa = maVal;
			_initialized = true;
			return;
		}

		if (_cooldown > 0)
		{
			_cooldown--;
			_prevClose = candle.ClosePrice;
			_prevMa = maVal;
			return;
		}

		var crossUp = _prevClose <= _prevMa && candle.ClosePrice > maVal;
		var crossDown = _prevClose >= _prevMa && candle.ClosePrice < maVal;

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

		_prevClose = candle.ClosePrice;
		_prevMa = maVal;
	}
}