GitHub で見る

エキスパート ADC PL ストック戦略

概要

The Expert ADC PL Stoch Strategy is a candlestick pattern strategy converted from the original MQL5 expert advisor Expert_ADC_PL_Stoch. It looks for bullish Piercing Line and bearish Dark Cloud Cover formations on finished candles and confirms the signals with the %D line of a Stochastic Oscillator. The method is trend-following when the market retraces into an established move and requires the oscillator to be in extreme zones before opening positions.ポジションの終了は、極端な領域からの Stochastic クロスオーバーに基づいており、ソース システムの投票ベースの終了ロジックを反映しています。

取引ロジック

  1. Subscribe to a configurable candle type (default: 1-hour time frame).
  2. 終了したローソク足ごとに、ローソク足パターンの評価に必要な最後のローソク足と最近の Stochastic %D 値を維持します。
  3. Long Entry
    • 前のローソク足のペアは、ピアス ライン パターンを形成する必要があります。
      • Candle at bar t-1 is bullish with a body greater than the average body size.
      • Candle at bar t-2 is bearish with a body greater than the average.
      • 強気のローソク足は弱気の安値を下回り、弱気の実体の内側に戻りますが、終値平均によれば全体の傾向は下降傾向にあります。
    • バー t-1 の Stochastic %D 値は、ロングエントリーしきい値 (デフォルト 30) を下回る必要があります。
  4. ショートエントリー
    • 前のローソク足のペアは、暗雲カバー パターンを形成する必要があります。
      • Candle at bar t-2 is bullish with a large body.
      • Candle at bar t-1 opens above the previous high and closes back within the bullish body.
      • 弱気のローソク足の中間価格は終値の移動平均を上回っており、反転前の上昇トレンドを示しています。
    • バー t-1 の Stochastic %D は、ショートエントリーしきい値 (デフォルト 70) を超えている必要があります。
  5. Exit Conditions
    • ロングポジションは、バー t-1 の Stochastic %D がバー t-2 と比較して上限 (80) または下限 (20) のしきい値を下回ったときにクローズされます。
    • ショート ポジションは、バー t-1 の Stochastic %D がバー t-2 と比較して下限 (20) または上限 (80) のしきい値を超えたときにクローズされます。
  6. すべての計算は完成したキャンドルに対して実行されます。イントラバー処理は使用されません。

パラメーター

名前 説明 デフォルト
CandleType パターン検出に使用されるローソク足の時間枠。 1時間
StochasticLength Stochastic オシレーターの基本長。 47
StochasticKPeriod %K ラインのスムージング長。 9
StochasticDPeriod %D ラインのスムージング長。 13
StochasticSlow Additional slowing factor applied to the oscillator. 3
AverageBodyPeriod Number of candles used to measure the reference body size and close average. 5
LongEntryThreshold ロングトレードを開始する前に許可される最大 %D 値。 30
ShortEntryThreshold ショートトレードを開始する前に必要な最小 %D 値。 70
ExitLowerThreshold Lower boundary used for exit crossovers. 20
ExitUpperThreshold 出口クロスオーバーに使用される上限境界。 80

リスク管理

  • この戦略は、基本戦略ボリューム (デフォルトでは 1 契約/ロット) を使用して成行注文を送信します。
  • 自動保護命令は設定されていません。必要に応じて、外部リスク管理または StartProtection を追加できます。
  • Only one position is managed at a time;反対の信号は、新しいポジションを開く前にアクティブなポジションを閉じます。

注意事項

  • 平均ローソク本体と近似平均は、MQL5 の投票ロジックを厳密に再現するために、過去のローソク足から計算されます。
  • Stochastic 値は、元のエキスパートアドバイザーで使用されたのと同じオフセットを評価するために、完成したバーごとに保存されます。
  • Trades are opened and closed only when the strategy is fully formed and trading is allowed by the base class checks.
namespace StockSharp.Samples.Strategies;

using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Messages;

/// <summary>
/// Expert ADC PL Stoch strategy: Dark Cloud Cover and Piercing Line patterns
/// with Stochastic oscillator confirmation for entries and exits.
/// </summary>
public class ExpertAdcPlStochStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _stochPeriod;
	private readonly StrategyParam<decimal> _longThreshold;
	private readonly StrategyParam<decimal> _shortThreshold;
	private readonly StrategyParam<int> _signalCooldownCandles;

	private readonly List<ICandleMessage> _candles = new();
	private decimal _prevSignal;
	private int _candlesSinceTrade;

	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
	public int StochPeriod { get => _stochPeriod.Value; set => _stochPeriod.Value = value; }
	public decimal LongThreshold { get => _longThreshold.Value; set => _longThreshold.Value = value; }
	public decimal ShortThreshold { get => _shortThreshold.Value; set => _shortThreshold.Value = value; }
	public int SignalCooldownCandles { get => _signalCooldownCandles.Value; set => _signalCooldownCandles.Value = value; }

	public ExpertAdcPlStochStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Candle timeframe", "General");
		_stochPeriod = Param(nameof(StochPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("Stoch Period", "Stochastic period", "Indicators");
		_longThreshold = Param(nameof(LongThreshold), 30m)
			.SetDisplay("Long Threshold", "Stochastic below this for long", "Signals");
		_shortThreshold = Param(nameof(ShortThreshold), 70m)
			.SetDisplay("Short Threshold", "Stochastic above this for short", "Signals");
		_signalCooldownCandles = Param(nameof(SignalCooldownCandles), 6)
			.SetGreaterThanZero()
			.SetDisplay("Signal Cooldown", "Bars to wait between trades", "Trading");
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_candles.Clear();
		_prevSignal = 0m;
		_candlesSinceTrade = SignalCooldownCandles;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		_candles.Clear();
		_candlesSinceTrade = SignalCooldownCandles;
		var stoch = new StochasticOscillator { K = { Length = StochPeriod }, D = { Length = 3 } };
		var subscription = SubscribeCandles(CandleType);
		subscription.BindEx(stoch, ProcessCandle).Start();

		StartProtection(
			takeProfit: new Unit(2, UnitTypes.Percent),
			stopLoss: new Unit(1, UnitTypes.Percent)
		);
	}

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

		if (_candlesSinceTrade < SignalCooldownCandles)
			_candlesSinceTrade++;

		var stochTyped = stochValue as StochasticOscillatorValue;
		if (stochTyped?.K is not decimal kValue) return;

		_candles.Add(candle);
		if (_candles.Count > 10)
			_candles.RemoveAt(0);

		if (_candles.Count >= 2)
		{
			var curr = _candles[^1];
			var prev = _candles[^2];

			// Piercing Line: bearish prev + bullish curr that closes above prev midpoint
			var isPiercing = prev.OpenPrice > prev.ClosePrice
				&& curr.ClosePrice > curr.OpenPrice
				&& curr.OpenPrice < prev.LowPrice
				&& curr.ClosePrice > (prev.OpenPrice + prev.ClosePrice) / 2m;

			// Dark Cloud Cover: bullish prev + bearish curr that closes below prev midpoint
			var isDarkCloud = prev.ClosePrice > prev.OpenPrice
				&& curr.OpenPrice > curr.ClosePrice
				&& curr.OpenPrice > prev.HighPrice
				&& curr.ClosePrice < (prev.OpenPrice + prev.ClosePrice) / 2m;

			if (isPiercing && kValue < LongThreshold && Position == 0 && _candlesSinceTrade >= SignalCooldownCandles)
			{
				BuyMarket();
				_candlesSinceTrade = 0;
			}
			else if (isDarkCloud && kValue > ShortThreshold && Position == 0 && _candlesSinceTrade >= SignalCooldownCandles)
			{
				SellMarket();
				_candlesSinceTrade = 0;
			}
		}

		_prevSignal = kValue;
	}
}