在 GitHub 上查看

Charles EMA RSI 策略

该策略仿照 Charles 智能交易系统,将指数移动平均线 (EMA)、RSI 滤波和追踪止损结合使用,可做多也可做空,并自动保护持仓。

策略在选定的时间框架上监控快慢 EMA。当快速 EMA 向上穿越慢速 EMA 且 RSI 高于 55 时,开多仓;当快速 EMA 向下穿越慢速 EMA 且 RSI 低于 45 时,开空仓。入场后,追踪止损跟随价格锁定利润,同时内置的止损和止盈负责风险控制。

细节

  • 入场条件
    • 多头快速 EMA 上穿 慢速 EMARSI > 55
    • 空头快速 EMA 下穿 慢速 EMARSI < 45
  • 多/空:双向。
  • 出场条件
    • 追踪止损;
    • 止损或止盈。
  • 止损:使用内置保护并带有追踪。
  • 默认值
    • FastPeriod = 18
    • SlowPeriod = 60
    • RsiPeriod = 14
    • TakeProfit = 0.02
    • StopLoss = 0.008
    • TrailStart = 0.006
    • TrailOffset = 0.003
  • 过滤器
    • 类别:趋势跟随
    • 方向:双向
    • 指标:多个
    • 止损:是
    • 复杂度:中等
    • 时间框架:默认 1 小时
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险等级:中等
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>
/// Charles strategy: EMA crossover with RSI filter.
/// </summary>
public class CharlesStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private bool _wasFastBelowSlow;
	private bool _isInitialized;

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

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

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

		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Period", "RSI calculation length", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Time frame for candles", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_wasFastBelowSlow = false;
		_isInitialized = false;
	}

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

		var fast = new ExponentialMovingAverage { Length = FastPeriod };
		var slow = new ExponentialMovingAverage { Length = SlowPeriod };
		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };

		SubscribeCandles(CandleType)
			.Bind(fast, slow, rsi, ProcessCandle)
			.Start();
	}

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

		if (!_isInitialized)
		{
			_wasFastBelowSlow = fastValue < slowValue;
			_isInitialized = true;
			return;
		}

		var isFastBelowSlow = fastValue < slowValue;

		// Long signal: fast crosses above slow + RSI > 55
		if (_wasFastBelowSlow && !isFastBelowSlow && rsiValue > 55 && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		// Short signal: fast crosses below slow + RSI < 45
		else if (!_wasFastBelowSlow && isFastBelowSlow && rsiValue < 45 && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_wasFastBelowSlow = isFastBelowSlow;
	}
}