GitHub で見る

Aeron Robotグリッド戦略

この戦略は、AeronRobot EAにインスパイアされたグリッドベースのヘッジシステムを実装します。事前に定義された価格間隔で買い注文と売り注文を配置し、新しい注文ごとにポジション量を増加させます。このアプローチは、設定可能なテイクプロフィット、ストップロス、取引制限によりリスクを制御しながら、小さな価格振動を捉えることを目的とします。

戦略はロングとショートの両ポジションで機能します。価格がGapパラメーターで定義されたステップで動くと、LotsFactorで乗算された量の新しい注文が建てられます。価格がTakeProfitポイント戻ったときに利益が確保され、動きがStopLossポイントに達すると損失が切り取られます。Hedgingフラグにより、両側のポジションを同時に保持できます。

詳細

  • エントリー条件:
    • ロング: 価格が最後の買い価格からGapポイント下落。
    • ショート: 価格が最後の売り価格からGapポイント上昇。
  • 出来高管理: 各新規注文の量はLotsFactorで乗算。
  • エグジット条件:
    • 一方のサイドのポジションは、利益がTakeProfitポイントを超えたときに決済。
    • 一方のサイドのポジションは、損失がStopLossポイントを超えたときに決済。
  • パラメーター:
    • FirstLot – 初回注文量。
    • LotsFactor – 後続注文の乗数。
    • Gap – グリッドレベル間の基本距離(ポイント)。
    • GapFactor – 各取引後にギャップを拡大する乗数。
    • MaxTrades – 各サイドの最大取引数。
    • Hedging – ロングとショートのポジションを同時に保持することを許可。
    • TakeProfit – ポイント単位の目標。
    • StopLoss – ポイント単位の保護制限。
    • CandleType – 処理に使用するロウソク足の時間軸。
  • ロング/ショート: 両方。
  • フィルター:
    • カテゴリ: グリッド / 平均回帰
    • 方向: 両方
    • インジケーター: なし
    • ストップ: あり
    • 複雑さ: 中程度
    • 時間軸: 短期
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 高
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>
/// EMA crossover strategy (converted from grid).
/// </summary>
public class AeronRobotStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _hasPrev;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public AeronRobotStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 12)
			.SetDisplay("Fast EMA", "Fast EMA period", "Indicators");

		_slowPeriod = Param(nameof(SlowPeriod), 26)
			.SetDisplay("Slow EMA", "Slow EMA period", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = 0;
		_prevSlow = 0;
		_hasPrev = false;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var fast = new ExponentialMovingAverage { Length = FastPeriod };
		var slow = new ExponentialMovingAverage { Length = SlowPeriod };

		SubscribeCandles(CandleType).Bind(fast, slow, ProcessCandle).Start();
	}

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

		if (!_hasPrev)
		{
			_prevFast = fastVal;
			_prevSlow = slowVal;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevFast <= _prevSlow && fastVal > slowVal;
		var crossDown = _prevFast >= _prevSlow && fastVal < slowVal;

		if (crossUp && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (crossDown && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevFast = fastVal;
		_prevSlow = slowVal;
	}
}