在 GitHub 上查看

Lucky Jump 策略

Lucky Jump 策略是一种超短线的均值回归系统,它根据最新的买一卖一报价的突然跳动来交易。当卖价相比上一笔报价向上跳动预设的点数时,策略做空并期待价格回落;当买价向下跳动同样的点数时,策略做多。持仓在出现任何盈利后立即平仓,如果亏损超过设定的限制也会立即退出。

该方法旨在捕捉市场剧烈波动后的快速回调,只依赖 Level1 行情,不使用 K 线或技术指标。

细节

  • 入场条件
    • 做空Ask(t) - Ask(t-1) >= Shift * PriceStep
    • 做多Bid(t-1) - Bid(t) >= Shift * PriceStep
  • 出场条件
    • 只要持仓出现盈利就平仓。
    • 当亏损超过 Limit * PriceStep 时平仓。
  • 止损:由 Limit 参数决定的隐式止损。
  • 默认值
    • Shift = 30 点。
    • Limit = 180 点。
    • Volume = 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>
/// Price jump reversal strategy using EMA crossover.
/// </summary>
public class LuckyJumpStrategy : 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 LuckyJumpStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 12)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast EMA period", "Trading");
		_slowPeriod = Param(nameof(SlowPeriod), 26)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow EMA period", "Trading");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle type", "General");
	}

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