在 GitHub 上查看

Sophia 1_1 策略

Sophia 1_1 是一种基于网格的马丁格尔交易策略。 当连续出现四根同方向的蜡烛时开仓:

  • 四根上升蜡烛后开空。
  • 四根下降蜡烛后开多。

进入市场后,价格每逆当前持仓方向移动一定的价格步长(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;
	}
}