在 GitHub 上查看

Charles SMA Trailing 策略

该策略利用两条简单移动平均线的交叉并可选使用跟踪止损。当快速 SMA 上穿慢速 SMA 时开多单;当快速 SMA 下穿慢速 SMA 时开空单。策略支持固定的止损、止盈以及在达到预设盈利后激活的跟踪止损。

细节

  • 入场条件
    • 快速 SMA 上穿慢速 SMA → 做多。
    • 快速 SMA 下穿慢速 SMA → 做空。
  • 多空方向:双向。
  • 离场条件
    • 反向交叉。
    • 触发止损或止盈。
    • 当盈利达到 TrailStart 后启动跟踪止损,间距为 TrailingAmount
  • 止损/止盈
    • StopLoss 定义固定的止损价格距离。
    • TakeProfit 定义固定的止盈目标。
    • TrailStartTrailingAmount 控制跟踪止损。
  • 默认参数
    • FastPeriod = 18
    • SlowPeriod = 60
    • StopLoss = 0
    • TakeProfit = 25
    • TrailStart = 25
    • TrailingAmount = 5
  • 过滤器
    • 类型:趋势跟随
    • 方向:多 & 空
    • 指标:SMA
    • 止损:是
    • 复杂度:中等
    • 时间周期:任意
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险等级:中等
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 with trailing stop management.
/// </summary>
public class CharlesSmaTrailingStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _isInitialized;

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

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

		_fastPeriod = Param(nameof(FastPeriod), 18)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Length of fast EMA", "Indicators");

		_slowPeriod = Param(nameof(SlowPeriod), 60)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Length of slow EMA", "Indicators");
	}

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

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

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

		var fastEma = new ExponentialMovingAverage { Length = FastPeriod };
		var slowEma = new ExponentialMovingAverage { Length = SlowPeriod };

		SubscribeCandles(CandleType)
			.Bind(fastEma, slowEma, ProcessCandle)
			.Start();
	}

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

		if (!_isInitialized)
		{
			_prevFast = fast;
			_prevSlow = slow;
			_isInitialized = true;
			return;
		}

		var crossUp = _prevFast <= _prevSlow && fast > slow;
		var crossDown = _prevFast >= _prevSlow && fast < slow;

		_prevFast = fast;
		_prevSlow = slow;

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