GitHub で見る

Sophia 1_1戦略

Sophia 1_1はマルチンゲール原理に基づくグリッド型取引戦略です。 同じ方向に4本連続したローソク足が出現した後にポジションを建てます:

  • 4本の上昇ローソク足がショートエントリーを引き起こします。
  • 4本の下降ローソク足がロングエントリーを引き起こします。

市場に参加した後、価格が現在のポジションに対して固定数の価格ステップ(Pip Step)分逆行するたびに、アルゴリズムがポジションを追加します。 各追加取引の数量は Lot Exponent 倍され、古典的なマルチンゲールグリッドを形成します。

リスク管理は Take ProfitStop Loss、およびオプションのトレーリングストップで行われます。 トレーリングメカニズムは利益が Trail Start に達した後に開始し、Trail Stop の価格ステップ分ストップレベルをトレーリングします。

パラメーター

  • Volume – 最初の取引のベース数量。
  • Pip Step – 新しいポジションを追加する前の価格ステップ単位の距離。
  • Lot Exponent – 各追加取引の数量の乗数。
  • Max Trades – グリッド内のポジションの最大数。
  • Take Profit – 平均エントリー価格からの価格ステップ単位の利益目標。
  • Stop Loss – 平均エントリー価格からの価格ステップ単位の損失閾値。
  • Use Trailing – トレーリングストップを有効または無効にする。
  • Trail Start – トレーリングストップが有効になる前に必要な利益。
  • Trail Stop – 価格ステップ単位のトレーリングストップの距離。
  • Candle Type – 計算に使用するローソク足の時間軸。
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>
/// Grid mean-reversion strategy. Enters on 3-bar momentum, exits at SMA or ATR stop.
/// </summary>
public class Sophia11Strategy : Strategy
{
	private readonly StrategyParam<int> _smaPeriod;
	private readonly StrategyParam<int> _atrPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prev1, _prev2, _prev3;

	public int SmaPeriod { get => _smaPeriod.Value; set => _smaPeriod.Value = value; }
	public int AtrPeriod { get => _atrPeriod.Value; set => _atrPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public Sophia11Strategy()
	{
		_smaPeriod = Param(nameof(SmaPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("SMA Period", "SMA for exit target", "Indicators");
		_atrPeriod = Param(nameof(AtrPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("ATR Period", "ATR for stops", "Indicators");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle type", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prev1 = _prev2 = _prev3 = 0;
	}

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

		var sma = new SimpleMovingAverage { Length = SmaPeriod };
		var atr = new StandardDeviation { Length = AtrPeriod };

		SubscribeCandles(CandleType).Bind(sma, atr, ProcessCandle).Start();
	}

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

		var close = candle.ClosePrice;

		if (_prev3 > 0)
		{
			// 3-bar declining => counter-trend buy
			if (_prev1 < _prev2 && _prev2 < _prev3 && Position <= 0)
			{
				if (Position < 0) BuyMarket();
				BuyMarket();
			}
			// 3-bar rising => counter-trend sell
			else if (_prev1 > _prev2 && _prev2 > _prev3 && Position >= 0)
			{
				if (Position > 0) SellMarket();
				SellMarket();
			}
			// Exit long at SMA or ATR stop
			else if (Position > 0 && (close >= sma || (atr > 0 && close < sma - atr * 3)))
			{
				SellMarket();
			}
			// Exit short at SMA or ATR stop
			else if (Position < 0 && (close <= sma || (atr > 0 && close > sma + atr * 3)))
			{
				BuyMarket();
			}
		}

		_prev3 = _prev2;
		_prev2 = _prev1;
		_prev1 = close;
	}
}