在 GitHub 上查看

Heiken Ashi Waves Strategy

该策略将Heikin-Ashi蜡烛与双移动平均波过滤相结合。快速SMA(2)上穿或下穿慢速SMA(30)提示波段变化,并由当前Heikin-Ashi蜡烛方向确认。

细节

  • 入场条件:
    • 多头: 看涨 Heikin-Ashi 蜡烛且快速SMA上穿慢速SMA
    • 空头: 看跌 Heikin-Ashi 蜡烛且快速SMA下穿慢速SMA
  • 多/空: 两者
  • 出场条件:
    • 相反交叉
    • 移动止损
  • 止损: 通过 StopLoss 以点为单位的跟踪止损
  • 默认值:
    • FastLength = 2
    • SlowLength = 30
    • StopLoss = new Unit(20, UnitTypes.Point)
    • UseTrailing = true
    • CandleType = TimeSpan.FromHours(1).TimeFrame()
  • 筛选:
    • 类别: 趋势跟随
    • 方向: 双向
    • 指标: Heikin Ashi, SMA
    • 止损: 是
    • 复杂度: 初级
    • 时间框架: 日内
    • 季节性: 否
    • 神经网络: 否
    • 背离: 否
    • 风险等级: 中等
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>
/// Heiken Ashi Waves strategy combining Heikin-Ashi candles and moving averages.
/// Opens long positions when a bullish Heikin-Ashi candle aligns with a fast SMA crossing above a slow SMA.
/// Opens short positions on bearish candles when the fast SMA crosses below the slow SMA.
/// </summary>
public class HeikenAshiWavesStrategy : Strategy
{
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<Unit> _stopLoss;
	private readonly StrategyParam<bool> _useTrailing;

	private decimal _prevFast;
	private decimal _prevSlow;
	private decimal _prevHaOpen;
	private decimal _prevHaClose;
	private bool _isInitialized;

	/// <summary>
	/// Fast SMA period.
	/// </summary>
	public int FastLength
	{
		get => _fastLength.Value;
		set => _fastLength.Value = value;
	}

	/// <summary>
	/// Slow SMA period.
	/// </summary>
	public int SlowLength
	{
		get => _slowLength.Value;
		set => _slowLength.Value = value;
	}

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

	/// <summary>
	/// Trailing stop loss distance.
	/// </summary>
	public Unit StopLoss
	{
		get => _stopLoss.Value;
		set => _stopLoss.Value = value;
	}

	/// <summary>
	/// Enables trailing stop behavior.
	/// </summary>
	public bool UseTrailing
	{
		get => _useTrailing.Value;
		set => _useTrailing.Value = value;
	}

	/// <summary>
	/// Initializes a new instance of <see cref="HeikenAshiWavesStrategy"/>.
	/// </summary>
	public HeikenAshiWavesStrategy()
	{
		_fastLength = Param(nameof(FastLength), 2)
		.SetGreaterThanZero()
		.SetDisplay("Fast SMA", "Period of the fast moving average", "Parameters")
		
		.SetOptimize(1, 10, 1);

		_slowLength = Param(nameof(SlowLength), 30)
		.SetGreaterThanZero()
		.SetDisplay("Slow SMA", "Period of the slow moving average", "Parameters")
		
		.SetOptimize(20, 60, 5);

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
		.SetDisplay("Candle Type", "Type of candles for calculations", "General");

		_stopLoss = Param(nameof(StopLoss), new Unit(20, UnitTypes.Absolute))
		.SetDisplay("Stop Loss", "Trailing stop distance in points", "Risk Management")
		
		.SetOptimize(10m, 50m, 10m);

		_useTrailing = Param(nameof(UseTrailing), true)
		.SetDisplay("Use Trailing", "Enable trailing stop protection", "Risk Management");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = _prevSlow = 0m;
		_prevHaOpen = _prevHaClose = 0m;
		_isInitialized = false;
	}

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

		StartProtection(
			takeProfit: null,
			stopLoss: StopLoss,
			isStopTrailing: UseTrailing,
			useMarketOrders: true
		);

		var fastSma = new SMA { Length = FastLength };
		var slowSma = new SMA { Length = SlowLength };

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

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
		return;

		decimal haOpen;
		decimal haClose;

		if (!_isInitialized)
		{
		haOpen = (candle.OpenPrice + candle.ClosePrice) / 2m;
		haClose = (candle.OpenPrice + candle.HighPrice + candle.LowPrice + candle.ClosePrice) / 4m;
		_prevHaOpen = haOpen;
		_prevHaClose = haClose;
		_prevFast = fastValue;
		_prevSlow = slowValue;
		_isInitialized = true;
		return;
		}

		haOpen = (_prevHaOpen + _prevHaClose) / 2m;
		haClose = (candle.OpenPrice + candle.HighPrice + candle.LowPrice + candle.ClosePrice) / 4m;

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

		if (isBullish && crossUp && Position <= 0)
		{
		BuyMarket(Volume + Math.Abs(Position));
		}
		else if (!isBullish && crossDown && Position >= 0)
		{
		SellMarket(Volume + Math.Abs(Position));
		}

		_prevFast = fastValue;
		_prevSlow = slowValue;
		_prevHaOpen = haOpen;
		_prevHaClose = haClose;
	}
}