在 GitHub 上查看

X Trader V3 策略

该策略交易基于两条中位价移动平均线的交叉。第一条移动平均线较长并带有位移,第二条非常短。当第一条均线自上而下穿越第二条并在两根K线内保持在其下方且两根K线前仍在上方时买入。相反条件下卖出。持仓可在反向信号出现时平仓。交易仅在设定的日内时间窗口内进行,并可使用保护性止损和止盈。

细节

  • 入场条件
    • 中位价SMA(Ma1Period)下穿中位价SMA(Ma2Period)并连续两根K线位于其下方 ⇒ 当 AllowBuy 为真时买入。
    • 中位价SMA(Ma1Period)上穿中位价SMA(Ma2Period)并连续两根K线位于其上方 ⇒ 当 AllowSell 为真时卖出。
    • K线时间在 StartTimeEndTime 之间。
  • 方向:多空皆可。
  • 出场条件
    • 反向交叉且 CloseOnReverseSignal 为真时平仓。
  • 止损
    • 可选的以 TakeProfitTicksStopLossTicks 指定的止盈止损(以tick计)。
  • 默认参数
    • Ma1Period = 16
    • Ma2Period = 1
    • TakeProfitTicks = 150
    • StopLossTicks = 100
  • 过滤器
    • 类型:交叉
    • 方向:多空
    • 指标: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>
/// X Trader V3 strategy based on EMA crossover.
/// </summary>
public class XTraderV3Strategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;

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

	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 XTraderV3Strategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe", "General");

		_fastPeriod = Param(nameof(FastPeriod), 12)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast EMA period", "Indicators");

		_slowPeriod = Param(nameof(SlowPeriod), 26)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow EMA period", "Indicators");
	}

	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;
	}
}