在 GitHub 上查看

Charles 1.3.7 策略

该策略在当前价格上下对称地放置止损挂单,并使用跟踪退出以捕获突破行情。

参数

  • Anchor – 挂单与收盘价的距离(价格步长)。
  • XFactor – 订单成交量的倍数。
  • Trailing Stop – 跟踪止损距离(价格步长)。
  • Trailing Profit – 达到该利润后退出仓位。
  • Stop Loss – 固定止损距离(价格步长,0 表示禁用)。
  • Volume – 基础成交量。
  • Candle Type – 使用的蜡烛图时间框架。

交易逻辑

  1. 没有持仓时,取消现有挂单,并在最近一根蜡烛的收盘价上下 Anchor 步长处分别挂出 Buy Stop 和 Sell Stop。
  2. 仓位建立后,取消另一侧的挂单,并记录入场价格以计算退出条件。
  3. 多头仓位在利润达到 Trailing Profit 或价格回撤 Stop Loss 时平仓;空头仓位逻辑相同。

该策略演示了使用简单风控的突破交易方法。

using System;
using System.Collections.Generic;

using Ecng.Common;

using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Charles 1.3.7 breakout strategy using symmetric price levels.
/// </summary>
public class Charles137Strategy : Strategy
{
	private readonly StrategyParam<decimal> _anchor;
	private readonly StrategyParam<decimal> _trailingProfit;
	private readonly StrategyParam<decimal> _stopLoss;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _entryPrice;
	private decimal _buyLevel;
	private decimal _sellLevel;
	private bool _levelsSet;

	public decimal Anchor { get => _anchor.Value; set => _anchor.Value = value; }
	public decimal TrailingProfit { get => _trailingProfit.Value; set => _trailingProfit.Value = value; }
	public decimal StopLossVal { get => _stopLoss.Value; set => _stopLoss.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public Charles137Strategy()
	{
		_anchor = Param(nameof(Anchor), 200m)
			.SetGreaterThanZero()
			.SetDisplay("Anchor", "Distance for breakout levels", "General");

		_trailingProfit = Param(nameof(TrailingProfit), 500m)
			.SetGreaterThanZero()
			.SetDisplay("Trailing Profit", "Profit target distance", "General");

		_stopLoss = Param(nameof(StopLossVal), 300m)
			.SetGreaterThanZero()
			.SetDisplay("Stop Loss", "Stop loss distance", "General");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_entryPrice = 0;
		_buyLevel = 0;
		_sellLevel = 0;
		_levelsSet = false;
	}

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

		SubscribeCandles(CandleType)
			.Bind(ProcessCandle)
			.Start();
	}

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

		var price = candle.ClosePrice;

		if (Position == 0)
		{
			if (!_levelsSet)
			{
				_buyLevel = price + Anchor;
				_sellLevel = price - Anchor;
				_levelsSet = true;
				return;
			}

			if (price >= _buyLevel)
			{
				BuyMarket();
				_entryPrice = price;
				_levelsSet = false;
			}
			else if (price <= _sellLevel)
			{
				SellMarket();
				_entryPrice = price;
				_levelsSet = false;
			}
			else
			{
				_buyLevel = price + Anchor;
				_sellLevel = price - Anchor;
			}
		}
		else if (Position > 0)
		{
			var profit = price - _entryPrice;
			if (profit >= TrailingProfit || profit <= -StopLossVal)
			{
				SellMarket();
				_levelsSet = false;
			}
		}
		else if (Position < 0)
		{
			var profit = _entryPrice - price;
			if (profit >= TrailingProfit || profit <= -StopLossVal)
			{
				BuyMarket();
				_levelsSet = false;
			}
		}
	}
}