GitHub で見る

リジェクション・キャンドル戦略

リジェクション・キャンドルは、価格があるレベルを探っても、そこを超えて維持できなかったときに形成され、長いヒゲと小さな実体を残します。 このようなローソク足は、ある方向へ動こうとした試みが市場に断固として拒否されたことを示します。

テストでは年平均リターンが約49%であることが示されています。暗号資産市場で最も良いパフォーマンスを発揮します。

この戦略は、ローソク足が引けた後、ヒゲの反対方向にエントリーし、価格がレンジ内に戻るリバーサルを期待します。

ストップは拒絶された高値または安値の外側に置かれてリスクを抑制し、モメンタムが現れなければトレードを終了します。

詳細

  • エントリー条件: パターン一致
  • ロング/ショート: 両方
  • エグジット条件: ストップロスまたは逆シグナル
  • ストップ: はい、パーセントベース
  • デフォルト値:
    • CandleType = 15 minute
    • WickRatio = 1.5
    • StopLossPercent = 2 リジェクション足は前の足の安値(高値)を突き抜け、反対方向で引け、ヒゲが実体の WickRatio 倍より長い足です。ストップは否定された安値・高値から StopLossPercent 外側に置かれ、終値がそれを越えるとポジションを閉じます。
  • フィルター:
    • カテゴリ: パターン
    • 方向: 両方
    • インジケーター: 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>
/// Rejection Candle strategy.
/// A bullish rejection probes below the previous candle's low, closes up, and has a lower wick longer than WickRatio bodies;
/// a bearish rejection mirrors it above the previous high. While flat the strategy trades against the wick.
/// The stop lies StopLossPercent beyond the rejected low or high, and a close beyond it closes the position.
/// </summary>
public class RejectionCandleStrategy : Strategy
{
	private readonly StrategyParam<decimal> _wickRatio;
	private readonly StrategyParam<decimal> _stopLossPercent;
	private readonly StrategyParam<DataType> _candleType;

	private ICandleMessage _prevCandle;
	private decimal _stopPrice;

	/// <summary>
	/// How many bodies long the rejecting wick must be.
	/// </summary>
	public decimal WickRatio
	{
		get => _wickRatio.Value;
		set => _wickRatio.Value = value;
	}

	/// <summary>
	/// Distance of the stop beyond the rejected extreme, in percent.
	/// </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 RejectionCandleStrategy()
	{
		_wickRatio = Param(nameof(WickRatio), 1.5m)
			.SetGreaterThanZero()
			.SetDisplay("Wick Ratio", "How many bodies long the rejecting wick must be", "Pattern");

		_stopLossPercent = Param(nameof(StopLossPercent), 2m)
			.SetNotNegative()
			.SetDisplay("Stop Loss %", "Distance of the stop beyond the rejected extreme, in percent", "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();
		_prevCandle = null;
		_stopPrice = default;
	}

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

		_prevCandle = null;
		_stopPrice = default;

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

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

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

		var previous = _prevCandle;
		_prevCandle = candle;

		if (previous == null || !IsFormedAndOnlineAndAllowTrading())
			return;

		var close = candle.ClosePrice;

		if (Position > 0)
		{
			if (close <= _stopPrice)
				SellMarket(Position);

			return;
		}

		if (Position < 0)
		{
			if (close >= _stopPrice)
				BuyMarket(-Position);

			return;
		}

		var body = Math.Abs(close - candle.OpenPrice);
		var upperWick = candle.HighPrice - Math.Max(candle.OpenPrice, close);
		var lowerWick = Math.Min(candle.OpenPrice, close) - candle.LowPrice;

		if (candle.LowPrice < previous.LowPrice && close > candle.OpenPrice && lowerWick > body * WickRatio)
		{
			BuyMarket(Volume);
			_stopPrice = candle.LowPrice * (1 - StopLossPercent / 100m);
		}
		else if (candle.HighPrice > previous.HighPrice && close < candle.OpenPrice && upperWick > body * WickRatio)
		{
			SellMarket(Volume);
			_stopPrice = candle.HighPrice * (1 + StopLossPercent / 100m);
		}
	}
}