Открыть на GitHub

Стратегия разворота по Heikin‑Ashi

Свечи Heikin‑Ashi сглаживают шум и подчёркивают направление тренда. Переход от серии медвежьих HA‑свечей к бычьей, или наоборот, может указывать на смену импульса. Эта стратегия торгует такие смены цвета и использует процентный стоп для защиты.

Тестирование показывает среднегодичную доходность около 145%. Стратегию лучше запускать на крипторынке.

Логика рассчитывает значения Heikin‑Ashi из обычных свечей. Когда закрытие HA поднимается выше открытия после медвежьей серии, открывается длинная позиция. Пересечение вниз после бычьего движения открывает короткую позицию. Стоп размещается на фиксированном проценте от входа.

Метод прост, но эффективен в периоды боковых колебаний, когда классические свечи дают много шума.

Детали

  • Условия входа: свеча Heikin‑Ashi меняет цвет.
  • Длинные/короткие: обе стороны.
  • Условия выхода: стоп‑лосс.
  • Стопы: да, процентные.
  • Значения по умолчанию:
    • CandleType = 15 минут
    • StopLossPercent = 2
  • Фильтры:
    • Категория: разворот
    • Направление: оба
    • Индикаторы: Heikin‑Ashi
    • Стопы: да
    • Сложность: базовая
    • Таймфрейм: внутридневной
    • Сезонность: нет
    • Нейросети: нет
    • Дивергенция: нет
    • Уровень риска: средний
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>
/// Heikin Ashi Reversal strategy.
/// Computes Heikin-Ashi candles from regular candles. A bullish Heikin-Ashi candle after bearish ones turns the position long,
/// a bearish one after bullish ones turns it short; a percent stop limits the loss.
/// </summary>
public class HeikinAshiReversalStrategy : Strategy
{
	private readonly StrategyParam<decimal> _stopLossPercent;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _haOpen;
	private decimal _haClose;
	private bool? _prevBullish;

	/// <summary>
	/// Stop-loss percentage.
	/// </summary>
	public decimal StopLossPercent
	{
		get => _stopLossPercent.Value;
		set => _stopLossPercent.Value = value;
	}

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

	/// <summary>
	/// Constructor.
	/// </summary>
	public HeikinAshiReversalStrategy()
	{
		_stopLossPercent = Param(nameof(StopLossPercent), 2m)
			.SetNotNegative()
			.SetDisplay("Stop Loss %", "Stop loss percentage from entry price", "Risk");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_haOpen = null;
		_haClose = default;
		_prevBullish = null;
	}

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

		_haOpen = null;
		_haClose = default;
		_prevBullish = null;

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

		StartProtection(new Unit(), new Unit(StopLossPercent, UnitTypes.Percent), useMarketOrders: true, isLocalStop: true);

		// The stop has to see prices between candles, not only at their close.
		foreach (var field in new[] { Level1Fields.BestBidPrice, Level1Fields.BestAskPrice })
		{
			var quotes = new Subscription(DataType.Level1, Security);
			quotes.MarketData.BuildField = field;
			SubscribeLevel1(quotes).Bind(ObserveProtectionQuote).Start();
		}

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

	private void ObserveProtectionQuote(Level1ChangeMessage quote)
	{
		// The high-level handler activates native protection before this callback, also between signal bars.
	}

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

		var haClose = (candle.OpenPrice + candle.HighPrice + candle.LowPrice + candle.ClosePrice) / 4;
		var haOpen = _haOpen is decimal prevOpen
			? (prevOpen + _haClose) / 2
			: (candle.OpenPrice + candle.ClosePrice) / 2;

		_haOpen = haOpen;
		_haClose = haClose;

		// A Heikin-Ashi candle without a body keeps the previous color.
		if (haClose == haOpen)
			return;

		var isBullish = haClose > haOpen;
		var wasBullish = _prevBullish;
		_prevBullish = isBullish;

		if (wasBullish is not bool previous || previous == isBullish || !IsFormedAndOnlineAndAllowTrading())
			return;

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