GitHub で見る

Eugene ローソク足パターン戦略

「Eugene」が説明するローソク足パターンを取引する戦略です。アルゴリズムは直近4本のローソク足を分析し、インサイドバーと特殊な「バード」フォーメーションを確認し、ブレイクアウトレベルを計算します。追加の確認条件が満たされた場合、前のローソク足の極値のブレイクアウトでポジションを開きます。オプションのストップロスとテイクプロフィットレベルは価格ステップで表されます。

詳細

  • エントリー条件:
    • ロング: 現在の高値が前の高値を上回り、前の安値がさらに前の高値を下回り、現在の安値が前の安値を上回り、さらにジグレベルまたは時間フィルターによる確認。
    • ショート: 現在の安値が前の安値を下回り、前の高値がさらに前の安値を上回り、現在の高値が前の高値を下回り、さらにジグレベルまたは時間フィルターによる確認。
  • ロング/ショート: 両方
  • エグジット条件:
    • ロング: 反対シグナルが現れるかストップロス/テイクプロフィットに達したときに売る。
    • ショート: 反対シグナルが現れるかストップロス/テイクプロフィットに達したときに買う。
  • ストップ: 価格ステップでの固定距離
  • デフォルト値:
    • Volume = 1m
    • StopLossPoints = 0
    • TakeProfitPoints = 0
    • InvertSignals = false
    • CandleType = TimeSpan.FromMinutes(1).TimeFrame()
  • フィルター:
    • カテゴリ: パターン
    • 方向: 両方
    • インジケーター: なし
    • ストップ: オプション
    • 複雑さ: 中級
    • 時間軸: 短期
    • 季節性: イントラデイ (時間 >= 8 フィルター)
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
using System;
using System.Collections.Generic;

using Ecng.Common;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Candle pattern strategy with breakout confirmation.
/// </summary>
public class EugeneCandlePatternStrategy : Strategy
{
	private readonly StrategyParam<int> _sl;
	private readonly StrategyParam<int> _tp;
	private readonly StrategyParam<bool> _inv;
	private readonly StrategyParam<DataType> _cType;
	private readonly StrategyParam<int> _cooldownBars;
	private readonly StrategyParam<decimal> _minBodyPercent;

	private readonly ICandleMessage[] _recent = new ICandleMessage[4];
	private decimal _stop;
	private decimal _take;
	private int _cooldownRemaining;

	public int StopLossPoints { get => _sl.Value; set => _sl.Value = value; }
	public int TakeProfitPoints { get => _tp.Value; set => _tp.Value = value; }
	public bool InvertSignals { get => _inv.Value; set => _inv.Value = value; }
	public DataType CandleType { get => _cType.Value; set => _cType.Value = value; }
	public int CooldownBars { get => _cooldownBars.Value; set => _cooldownBars.Value = value; }
	public decimal MinBodyPercent { get => _minBodyPercent.Value; set => _minBodyPercent.Value = value; }

	public EugeneCandlePatternStrategy()
	{
		_sl = Param(nameof(StopLossPoints), 500).SetDisplay("Stop Loss (points)", "Stop loss in price steps, 0 - disabled", "Risk");
		_tp = Param(nameof(TakeProfitPoints), 800).SetDisplay("Take Profit (points)", "Take profit in price steps, 0 - disabled", "Risk");
		_inv = Param(nameof(InvertSignals), false).SetDisplay("Invert Signals", "Swap buy and sell signals", "General");
		_cType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame()).SetDisplay("Candle Type", "Type of candles", "General");
		_cooldownBars = Param(nameof(CooldownBars), 4).SetDisplay("Cooldown Bars", "Completed candles to wait after a position change", "Trading");
		_minBodyPercent = Param(nameof(MinBodyPercent), 0.0015m).SetDisplay("Minimum Body %", "Minimum candle body size relative to close price", "Filters");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		Array.Clear(_recent, 0, _recent.Length);
		_stop = 0m;
		_take = 0m;
		_cooldownRemaining = 0;
	}

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

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

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

		StartProtection(null, null);
	}

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

		if (_cooldownRemaining > 0)
			_cooldownRemaining--;

		CheckStops(candle);
		_recent[3] = _recent[2];
		_recent[2] = _recent[1];
		_recent[1] = _recent[0];
		_recent[0] = candle;

		if (_recent[3] is null)
			return;

		ComputeSignals(out var openBuy, out var openSell, out var closeBuy, out var closeSell);
		if (InvertSignals)
		{
			(openBuy, openSell) = (openSell, openBuy);
			(closeBuy, closeSell) = (closeSell, closeBuy);
		}

		if (Position > 0 && closeBuy)
			ClosePosition();
		else if (Position < 0 && closeSell)
			ClosePosition();

		if (_cooldownRemaining > 0)
			return;

		if (Position <= 0 && openBuy && !openSell)
		{
			if (Position < 0)
				BuyMarket();

			BuyMarket();
			SetStops(candle.ClosePrice, true);
			_cooldownRemaining = CooldownBars;
		}
		else if (Position >= 0 && openSell && !openBuy)
		{
			if (Position > 0)
				SellMarket();

			SellMarket();
			SetStops(candle.ClosePrice, false);
			_cooldownRemaining = CooldownBars;
		}
	}

	private void ComputeSignals(out bool openBuy, out bool openSell, out bool closeBuy, out bool closeSell)
	{
		var current = _recent[0];
		var prev = _recent[1];
		var prev2 = _recent[2];
		openBuy = false;
		openSell = false;
		closeBuy = false;
		closeSell = false;

		var prevBody = Math.Abs(prev.ClosePrice - prev.OpenPrice);
		var currentBody = Math.Abs(current.ClosePrice - current.OpenPrice);
		var prevBodyPercent = prev.ClosePrice != 0m ? prevBody / prev.ClosePrice : 0m;
		var currentBodyPercent = current.ClosePrice != 0m ? currentBody / current.ClosePrice : 0m;
		var bullishSetup = prev.ClosePrice < prev.OpenPrice && prev.LowPrice > prev2.LowPrice;
		var bearishSetup = prev.ClosePrice > prev.OpenPrice && prev.HighPrice < prev2.HighPrice;
		var bullishBreakout = current.ClosePrice > prev.HighPrice && current.ClosePrice > current.OpenPrice;
		var bearishBreakout = current.ClosePrice < prev.LowPrice && current.ClosePrice < current.OpenPrice;

		openBuy = bullishSetup && bullishBreakout && prevBodyPercent >= MinBodyPercent && currentBodyPercent >= MinBodyPercent;
		openSell = bearishSetup && bearishBreakout && prevBodyPercent >= MinBodyPercent && currentBodyPercent >= MinBodyPercent;
		closeBuy = current.ClosePrice < prev.LowPrice;
		closeSell = current.ClosePrice > prev.HighPrice;
	}

	private void SetStops(decimal price, bool longPos)
	{
		var step = Security.PriceStep ?? 1m;
		if (longPos)
		{
			_stop = StopLossPoints > 0 ? price - step * StopLossPoints : 0m;
			_take = TakeProfitPoints > 0 ? price + step * TakeProfitPoints : 0m;
		}
		else
		{
			_stop = StopLossPoints > 0 ? price + step * StopLossPoints : 0m;
			_take = TakeProfitPoints > 0 ? price - step * TakeProfitPoints : 0m;
		}
	}

	private void ClosePosition()
	{
		if (Position > 0)
			SellMarket();
		else if (Position < 0)
			BuyMarket();

		_stop = 0m;
		_take = 0m;
		_cooldownRemaining = CooldownBars;
	}

	private void CheckStops(ICandleMessage candle)
	{
		if (Position > 0)
		{
			if ((_stop != 0m && candle.LowPrice <= _stop) || (_take != 0m && candle.HighPrice >= _take))
				ClosePosition();
		}
		else if (Position < 0)
		{
			if ((_stop != 0m && candle.HighPrice >= _stop) || (_take != 0m && candle.LowPrice <= _take))
				ClosePosition();
		}
	}
}