GitHub で見る

強気の陽線包み足パターン戦略

このセットアップは、ロウソクが直前の陰線バーを完全に包んだときの急激な強気リバーサルを狙います。このような形成は短期的な下落を終わらせることが多く、新たな上昇モメンタムを示唆します。オプションの下降トレンドフィルターは連続した赤いロウソクを数え、売り手の枯渇を確認します。

テストでは年平均リターン約76%を示しています。外国為替市場で最もよく機能します。

ライブ運用中、アルゴリズムは各入力ロウソクを監視し前のバーを追跡します。新しいロウソクが始値より高く引け、そのボディが前のバーを包む場合、ロングエントリーがトリガーされます。ストップはリスクを制限するためパターンの安値のすぐ下に置かれます。

ストップが発動するか、別のシグナルが手動決済を示唆するまでトレードは開いたままです。前の下降バーによる確認がセットアップを強化するため、戦略は弱いリバーサルを追うことを避けます。

詳細

  • エントリー条件: 強気ロウソクが直前の弱気バーを包む、オプションで下降トレンド存在。
  • ロング/ショート: ロングのみ。
  • エグジット条件: ストップロスまたは裁量。
  • ストップ: はい、パターンの安値の下。
  • デフォルト値:
    • CandleType = 15 minute
    • StopLossPercent = 1
    • RequireDowntrend = true
    • DowntrendBars = 3
  • フィルター:
    • カテゴリ: パターン
    • 方向: ロングのみ
    • インジケーター: Candlestick
    • ストップ: はい
    • 複雑さ: 中級
    • 時間軸: イントラデイ
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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>
/// Bullish Engulfing strategy.
/// Enters long on bullish engulfing pattern below SMA.
/// Enters short on bearish engulfing pattern above SMA.
/// Exits via SMA crossover.
/// </summary>
public class EngulfingBullishStrategy : Strategy
{
	private readonly StrategyParam<int> _maPeriod;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _cooldownBars;

	private ICandleMessage _previousCandle;
	private int _cooldown;

	/// <summary>
	/// MA Period.
	/// </summary>
	public int MAPeriod
	{
		get => _maPeriod.Value;
		set => _maPeriod.Value = value;
	}

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

	/// <summary>
	/// Cooldown bars.
	/// </summary>
	public int CooldownBars
	{
		get => _cooldownBars.Value;
		set => _cooldownBars.Value = value;
	}

	/// <summary>
	/// Constructor.
	/// </summary>
	public EngulfingBullishStrategy()
	{
		_maPeriod = Param(nameof(MAPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("MA Period", "Period for SMA", "Indicators");

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

		_cooldownBars = Param(nameof(CooldownBars), 500)
			.SetRange(1, 1000)
			.SetDisplay("Cooldown Bars", "Bars to wait between trades", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_previousCandle = null;
		_cooldown = default;
	}

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

		_previousCandle = null;
		_cooldown = 0;

		var sma = new SimpleMovingAverage { Length = MAPeriod };

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

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (_cooldown > 0)
		{
			_cooldown--;
			_previousCandle = candle;
			return;
		}

		if (_previousCandle != null)
		{
			var isPrevBearish = _previousCandle.ClosePrice < _previousCandle.OpenPrice;
			var isPrevBullish = _previousCandle.ClosePrice > _previousCandle.OpenPrice;
			var isCurrBullish = candle.ClosePrice > candle.OpenPrice;
			var isCurrBearish = candle.ClosePrice < candle.OpenPrice;

			var bullishEngulfing = isPrevBearish && isCurrBullish &&
				candle.ClosePrice > _previousCandle.OpenPrice &&
				candle.OpenPrice < _previousCandle.ClosePrice;

			var bearishEngulfing = isPrevBullish && isCurrBearish &&
				candle.ClosePrice < _previousCandle.OpenPrice &&
				candle.OpenPrice > _previousCandle.ClosePrice;

			if (Position == 0 && bullishEngulfing && candle.ClosePrice < smaValue)
			{
				BuyMarket();
				_cooldown = CooldownBars;
			}
			else if (Position == 0 && bearishEngulfing && candle.ClosePrice > smaValue)
			{
				SellMarket();
				_cooldown = CooldownBars;
			}
			else if (Position > 0 && candle.ClosePrice < smaValue)
			{
				SellMarket();
				_cooldown = CooldownBars;
			}
			else if (Position < 0 && candle.ClosePrice > smaValue)
			{
				BuyMarket();
				_cooldown = CooldownBars;
			}
		}

		_previousCandle = candle;
	}
}