GitHub で見る

Zone Recovery Formula戦略

概要

Zone Recovery Formula戦略は、MetaTrader 4 の "Zone Recovery Formula" エキスパートアドバイザーを移植したものです。このアルゴリズムは移動平均で決まるトレンド方向に従い、その後、不利な価格変動を軽減するためにゾーンリカバリー手法を適用します。中核となる考え方は、価格が定義済みの回復ゾーンを抜けるまで、徐々に数量を増やしながらロングとショートのサイクルを交互に行い、複数回の反転後でも利益を固定することです。

仕組み

  1. シグナル検出 - 戦略は時間枠ローソク足 (デフォルト 15 分) を購読し、高速と低速の単純移動平均を追跡します。強気クロスはロング回復サイクルを開始し、弱気クロスはショートサイクルを開始します。
  2. 初期注文 - 新しいサイクルが始まると、戦略は基本数量乗数で市場ポジションを開きます。take-profit と回復距離は、pip 設定と商品の tick サイズから計算されます。
  3. ゾーンリカバリー - 価格が設定された回復距離だけオープンポジションに逆行した場合、戦略は方向を反転し、元の数式シーケンスを使って注文サイズを増やします (最大取引数まで)。これにより交互のネットエクスポージャーが作られ、価格が利益目標へ戻ったときに過去の損失を補うことを目指します。
  4. 利益管理 - アルゴリズムは未実現利益を監視します。
    • 金額および割合の take-profit 条件は、すべてのポジションを即座に閉じることができます。
    • 任意の trailing 管理は、事前定義された利益後に利益を捉え、trailing stop 距離で保護します。
  5. サイクルリセット - 利益目標に達するか、trailing 保護がポジションを閉じると、回復サイクルはリセットされ、戦略は次の移動平均シグナルを待ちます。

主要パラメーター

  • TP Money使用 / TP Money - 金額ベースの take-profit を有効化して設定します。
  • TP %使用 / TP Percent - ポートフォリオ残高に基づく割合 take-profit を有効化して設定します。
  • Trailing有効化 / Trailing TP / Trailing SL - trailing 利益捕捉を有効にし、起動水準と保護距離を定義します。
  • TP Pips / Zone Pips - take-profit 目標と回復トリガーゾーンを定義する距離 (pips)。
  • Base Volume / Max Trades - 初期注文サイズと 1 サイクルで許可される回復ステップ数。
  • Fast MA / Slow MA - エントリーシグナルを生成する移動平均。
  • Profit Offset - 元の回復数量数式で使われる任意の調整。

注意事項

  • 戦略はローソク足購読とインジケーターバインディングを使う StockSharp の高レベル API を使用します。
  • ヘッジポジションは、ネットポジション方向を反転し数量をスケールすることでエミュレートされ、StockSharp のネットポジション会計と互換性を保ちます。
  • trailing と take-profit のチェックは、現在のポジション価格から計算される未実現利益に依存します。金額値は商品の tick 価値に合わせて調整してください。
  • ライブ口座へ展開する前に、必ずシミュレーション環境でテストしてください。

ファイル

  • CS/ZoneRecoveryFormulaStrategy.cs - 戦略の C# 実装。
  • README.md - 英語のこのドキュメントファイル。
  • README_ru.md - ロシア語ドキュメント。
  • README_zh.md - 中国語ドキュメント。
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;

public class ZoneRecoveryFormulaStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<int> _stopLossPoints;
	private readonly StrategyParam<int> _takeProfitPoints;

	private ExponentialMovingAverage _fast;
	private ExponentialMovingAverage _slow;

	private decimal _prevFast;
	private decimal _prevSlow;
	private decimal _entryPrice;
	private int _cooldown;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public int StopLossPoints { get => _stopLossPoints.Value; set => _stopLossPoints.Value = value; }
	public int TakeProfitPoints { get => _takeProfitPoints.Value; set => _takeProfitPoints.Value = value; }

	public ZoneRecoveryFormulaStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 14).SetGreaterThanZero().SetDisplay("Fast Period", "Fast EMA period", "Indicator");
		_slowPeriod = Param(nameof(SlowPeriod), 50).SetGreaterThanZero().SetDisplay("Slow Period", "Slow EMA period", "Indicator");
		_stopLossPoints = Param(nameof(StopLossPoints), 200).SetNotNegative().SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk");
		_takeProfitPoints = Param(nameof(TakeProfitPoints), 400).SetNotNegative().SetDisplay("Take Profit", "Take-profit in price steps", "Risk");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		yield return (Security, TimeSpan.FromMinutes(5).TimeFrame());
	}

	protected override void OnReseted()
	{
		base.OnReseted();
		_fast = null; _slow = null;
		_prevFast = 0; _prevSlow = 0; _entryPrice = 0; _cooldown = 0;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		_fast = new ExponentialMovingAverage { Length = FastPeriod };
		_slow = new ExponentialMovingAverage { Length = SlowPeriod };
		var subscription = SubscribeCandles(TimeSpan.FromMinutes(5).TimeFrame());
		subscription.Bind(_fast, _slow, ProcessCandle);
		subscription.Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue)
	{
		if (candle.State != CandleStates.Finished) return;
		if (!_fast.IsFormed || !_slow.IsFormed) { _prevFast = fastValue; _prevSlow = slowValue; return; }
		if (_cooldown > 0) { _cooldown--; _prevFast = fastValue; _prevSlow = slowValue; return; }

		var close = candle.ClosePrice;
		var step = Security?.PriceStep ?? 1m;

		if (Position > 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close <= _entryPrice - StopLossPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
			if (TakeProfitPoints > 0 && close >= _entryPrice + TakeProfitPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
		}
		else if (Position < 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close >= _entryPrice + StopLossPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
			if (TakeProfitPoints > 0 && close <= _entryPrice - TakeProfitPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
		}

		if (_prevFast <= _prevSlow && fastValue > slowValue && Position <= 0)
		{ if (Position < 0) BuyMarket(); BuyMarket(); _entryPrice = close; _cooldown = 100; }
		else if (_prevFast >= _prevSlow && fastValue < slowValue && Position >= 0)
		{ if (Position > 0) SellMarket(); SellMarket(); _entryPrice = close; _cooldown = 100; }

		_prevFast = fastValue; _prevSlow = slowValue;
	}
}