GitHub で見る

Heiken Ashi ヒゲなし戦略

この戦略はヒゲのないHeiken Ashiローソク足に対して逆張りで取引します。前のローソク足よりも実体が大きく下ヒゲのない強気のHeiken Ashiローソク足はショートエントリーを引き起こします。実体が長く上ヒゲのない弱気のローソク足はロングポジションを開きます。対応するヒゲのない反対色のローソク足が形成されるとポジションが決済されます。

詳細

  • エントリー条件: ショートには下ヒゲなしで前のローソク足より実体が大きい強気HA;ロングには上ヒゲなしで前のローソク足より実体が大きい弱気HA
  • ロング/ショート: ロングとショート
  • エグジット条件: ヒゲなしの反対色HAローソク足
  • ストップ: いいえ
  • デフォルト値:
    • CandleType = 15分足ローソク足
  • フィルター:
    • カテゴリ: パターン
    • 方向: リバーサル
    • インジケーター: Heikin-Ashi
    • ストップ: いいえ
    • 複雑さ: 基本
    • 時間軸: イントラデイ
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
using System;
using System.Collections.Generic;

using Ecng.Common;

using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Strategy trading Heiken Ashi candle color changes.
/// Buys when HA turns bullish, sells when HA turns bearish.
/// </summary>
public class HeikenAshiNoWickStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevHaOpen;
	private decimal _prevHaClose;
	private bool _prevIsBull;
	private bool _hasPrev;

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

	public HeikenAshiNoWickStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevHaOpen = 0;
		_prevHaClose = 0;
		_prevIsBull = false;
		_hasPrev = false;
	}

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

		SubscribeCandles(CandleType)
			.Bind(ProcessCandle)
			.Start();
	}

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

		decimal haOpen;
		decimal haClose;

		if (_prevHaOpen == 0 && _prevHaClose == 0)
		{
			haOpen = (candle.OpenPrice + candle.ClosePrice) / 2m;
			haClose = (candle.OpenPrice + candle.HighPrice + candle.LowPrice + candle.ClosePrice) / 4m;
		}
		else
		{
			haOpen = (_prevHaOpen + _prevHaClose) / 2m;
			haClose = (candle.OpenPrice + candle.HighPrice + candle.LowPrice + candle.ClosePrice) / 4m;
		}

		var isBull = haClose > haOpen;

		if (_hasPrev)
		{
			// Buy on bearish -> bullish transition
			if (isBull && !_prevIsBull && Position <= 0)
			{
				if (Position < 0) BuyMarket();
				BuyMarket();
			}
			// Sell on bullish -> bearish transition
			else if (!isBull && _prevIsBull && Position >= 0)
			{
				if (Position > 0) SellMarket();
				SellMarket();
			}
		}

		_prevHaOpen = haOpen;
		_prevHaClose = haClose;
		_prevIsBull = isBull;
		_hasPrev = true;
	}
}