在 GitHub 上查看

SAW System 1 策略

该突破策略在每天开始时放置买入和卖出止损单。策略计算最近几天的平均日波幅,并将其用作止损和止盈的基准。两侧都挂单,预期只有一侧会被触发。

在设定的 OpenHour,策略根据当前价格和平均波幅的一半距离计算 Buy Stop 和 Sell Stop 价格。止损和止盈以平均波幅的百分比表示。当一侧触发后,另一侧可以被取消,也可以保留用于反向开仓。可选的马丁加尔选项会在触发后按 MartingaleMultiplier 放大剩余挂单的数量。

如果到 CloseHour 仍有挂单未成交,则全部撤单以避免隔夜风险。开仓后立即按照成交价放置止损和止盈保护单。

细节

  • 入场条件:
    • 使用 ATR 计算 VolatilityDays 天的平均日波幅。
    • 根据该波幅的 StopLossRate% 和 TakeProfitRate% 计算止损与止盈距离。
    • OpenHouroffset = stopLoss/2 的距离放置买卖止损单。
  • 出场条件:
    • 保护性止损和止盈单平仓。
    • CloseHour 未成交的挂单全部撤销。
  • 反转模式:
    • Reverse 为真时,保留相反方向的止损单以实现反向开仓。
    • 若同时启用 UseMartingale,该挂单的数量乘以 MartingaleMultiplier
  • 方向: 做多与做空。
  • 止损: 基于日波幅的固定止损和止盈。
  • 默认参数:
    • VolatilityDays = 5
    • OpenHour = 7
    • CloseHour = 10
    • StopLossRate = 15%
    • TakeProfitRate = 30%
    • Reverse = false
    • UseMartingale = false
    • MartingaleMultiplier = 2.0

该策略旨在在平静的夜间交易后捕捉早晨的突破,同时通过基于波动性的目标控制风险。

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>
/// SAW System breakout strategy.
/// Uses ATR to calculate volatility range, then enters on breakout above/below
/// the open price offset by a fraction of ATR.
/// </summary>
public class SawSystem1Strategy : Strategy
{
	private readonly StrategyParam<int> _atrPeriod;
	private readonly StrategyParam<decimal> _breakoutMultiplier;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevAtr;
	private decimal _sessionOpen;
	private bool _traded;
	private DateTime _currentDate;

	public int AtrPeriod
	{
		get => _atrPeriod.Value;
		set => _atrPeriod.Value = value;
	}

	public decimal BreakoutMultiplier
	{
		get => _breakoutMultiplier.Value;
		set => _breakoutMultiplier.Value = value;
	}

	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	public SawSystem1Strategy()
	{
		_atrPeriod = Param(nameof(AtrPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("ATR Period", "Period for ATR calculation", "Indicators");

		_breakoutMultiplier = Param(nameof(BreakoutMultiplier), 0.5m)
			.SetDisplay("Breakout Multiplier", "Fraction of ATR for breakout offset", "Parameters");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
			.SetDisplay("Candle Type", "Candle timeframe", "General");
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevAtr = null;
		_sessionOpen = 0;
		_traded = false;
		_currentDate = default;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		_prevAtr = null;
		_sessionOpen = 0;
		_traded = false;
		_currentDate = default;

		var atr = new AverageTrueRange { Length = AtrPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(atr, ProcessCandle)
			.Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, atr);
			DrawOwnTrades(area);
		}
	}

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var date = candle.OpenTime.Date;

		// New day: record open price and reset
		if (date != _currentDate)
		{
			_currentDate = date;
			_sessionOpen = candle.OpenPrice;
			_traded = false;

			// Close any open position at start of new day
			if (Position > 0)
				SellMarket();
			else if (Position < 0)
				BuyMarket();

			_prevAtr = atrValue;
			return;
		}

		if (_traded || _prevAtr is null || _sessionOpen == 0)
		{
			_prevAtr = atrValue;
			return;
		}

		var offset = _prevAtr.Value * BreakoutMultiplier;
		var upperBreak = _sessionOpen + offset;
		var lowerBreak = _sessionOpen - offset;

		if (candle.ClosePrice > upperBreak && Position <= 0)
		{
			BuyMarket();
			_traded = true;
		}
		else if (candle.ClosePrice < lowerBreak && Position >= 0)
		{
			SellMarket();
			_traded = true;
		}

		_prevAtr = atrValue;
	}
}