在 GitHub 上查看

Turtle Trader V1 将多种动量振荡指标与移动平均过滤器结合。 当快速 EMA 高于慢速 EMA 并且 RSI、随机指标、CCI、动量和 Chaikin 振荡器全部向上时开多单。 空头需要相反条件。

细节

  • 入场条件:
    • 快速 EMA 高于慢速 EMA(空头反之)
    • 多头 RSI 上升且低于 70,空头 RSI 下降且高于 30
    • 多头随机指标 %K 低于 88,空头高于 12
    • 多头 CCI 与动量上升,空头下降
    • Chaikin 振荡器朝交易方向移动
  • 多空: 双向
  • 离场条件: 反向信号
  • 止损: 默认无
  • 默认参数:
    • FastMaPeriod = 10
    • SlowMaPeriod = 50
    • RsiPeriod = 14
    • StochPeriod = 14
    • CciPeriod = 20
    • MomentumPeriod = 10
    • ChoFastPeriod = 3
    • ChoSlowPeriod = 10
  • 过滤器:
    • 类别: 趋势/动量
    • 方向: 双向
    • 指标: EMA、RSI、随机指标、CCI、动量、Chaikin 振荡器
    • 止损: 无
    • 复杂度: 高级
    • 时间框架: 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>
/// Turtle Trader strategy based on EMA crossover with RSI confirmation.
/// </summary>
public class TurtleTraderV1Strategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _hasPrev;

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

	public TurtleTraderV1Strategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("Fast MA", "Fast EMA period", "General");
		_slowPeriod = Param(nameof(SlowPeriod), 50)
			.SetGreaterThanZero()
			.SetDisplay("Slow MA", "Slow EMA period", "General");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle type", "Data");
	}

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

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

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

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

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

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

		if (!_hasPrev)
		{
			_prevFast = fastVal;
			_prevSlow = slowVal;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevFast <= _prevSlow && fastVal > slowVal;
		var crossDown = _prevFast >= _prevSlow && fastVal < slowVal;

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

		_prevFast = fastVal;
		_prevSlow = slowVal;
	}
}