在 GitHub 上查看

Liquidex V1 策略

Liquidex V1 是从原始 MQL 智能交易系统转换而来的剥头皮策略。该策略结合 范围过滤器加权移动平均线(WMA) 来捕捉短期突破。

交易逻辑

  1. 对每根完成的K线计算其波动范围 (high - low)。
  2. 如果范围小于 RangeFilter,则忽略该K线。
  3. 根据收盘价计算周期为 MaPeriod 的 WMA。
  4. 当K线开盘价在 WMA 下方并收盘价在其上方时,发送 买入 市价单。
  5. 当K线开盘价在 WMA 上方并收盘价在其下方时,发送 卖出 市价单。
  6. 每个仓位都使用 StopLoss 设置的止损进行保护。

参数

  • RangeFilter – 触发交易所需的最小K线波动范围。
  • MaPeriod – 加权移动平均线的周期。
  • StopLoss – 止损点数。
  • CandleType – 用于分析的K线类型。

策略使用 Strategy.Volume 作为下单数量,在出现反向信号时会反向开仓。

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>
/// WMA crossover strategy with range filter.
/// </summary>
public class LiquidexV1Strategy : Strategy
{
	private readonly StrategyParam<int> _maPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevClose;
	private decimal _prevWma;
	private bool _hasPrev;

	public int MaPeriod { get => _maPeriod.Value; set => _maPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public LiquidexV1Strategy()
	{
		_maPeriod = Param(nameof(MaPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("MA Period", "WMA period", "Indicators");
		_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();
		_prevClose = 0;
		_prevWma = 0;
		_hasPrev = false;
	}

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

		var wma = new WeightedMovingAverage { Length = MaPeriod };

		SubscribeCandles(CandleType)
			.Bind(wma, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevClose = candle.ClosePrice;
			_prevWma = wmaVal;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevClose <= _prevWma && candle.ClosePrice > wmaVal;
		var crossDown = _prevClose >= _prevWma && candle.ClosePrice < wmaVal;

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

		_prevClose = candle.ClosePrice;
		_prevWma = wmaVal;
	}
}