在 GitHub 上查看

Q2MA交叉策略

Q2MA交叉策略基于蜡烛收盘价和开盘价的平滑移动平均线交叉进行交易。当收盘平均线从上方下穿开盘平均线时开多仓,反向交叉时开空仓。当出现相反趋势时平仓,并使用以跳动为单位的止损和止盈。

详情

  • 入场条件:收盘价与开盘价移动平均线交叉
  • 多空方向:双向
  • 出场条件:相反交叉或止损/止盈
  • 止损:是
  • 默认值:
    • Length = 8
    • StopLoss = 1000
    • TakeProfit = 2000
    • CandleType = TimeSpan.FromHours(4).TimeFrame()
    • Volume = 1
    • BuyPosOpen = true
    • SellPosOpen = true
    • BuyPosClose = true
    • SellPosClose = true
    • Invert = false
  • 过滤器:
    • 类别: 趋势
    • 方向: 双向
    • 指标: 移动平均
    • 止损: 是
    • 复杂度: 中等
    • 时间框架: H4
    • 季节性: 否
    • 神经网络: 否
    • 背离: 否
    • 风险等级: 中等
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>
/// Q2MA cross strategy based on open and close moving averages.
/// Buys when close MA crosses above open MA, sells on opposite.
/// </summary>
public class Q2maCrossStrategy : Strategy
{
	private readonly StrategyParam<int> _length;
	private readonly StrategyParam<DataType> _candleType;

	private ExponentialMovingAverage _closeMa;
	private ExponentialMovingAverage _openMa;
	private decimal? _prevUp;
	private decimal? _prevDn;

	public int Length { get => _length.Value; set => _length.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public Q2maCrossStrategy()
	{
		_length = Param(nameof(Length), 8)
			.SetDisplay("Length", "Moving average length", "Indicator");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Indicator timeframe", "General");
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_closeMa = null;
		_openMa = null;
		_prevUp = null;
		_prevDn = null;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		_prevUp = null;
		_prevDn = null;

		_closeMa = new ExponentialMovingAverage { Length = Length };
		_openMa = new ExponentialMovingAverage { Length = Length };

		Indicators.Add(_closeMa);
		Indicators.Add(_openMa);

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(ProcessCandle)
			.Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawOwnTrades(area);
		}
	}

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

		var t = candle.ServerTime;

		var upResult = _closeMa.Process(new DecimalIndicatorValue(_closeMa, candle.ClosePrice, t) { IsFinal = true });
		var dnResult = _openMa.Process(new DecimalIndicatorValue(_openMa, candle.OpenPrice, t) { IsFinal = true });

		if (!_closeMa.IsFormed || !_openMa.IsFormed)
			return;

		var up = upResult.GetValue<decimal>();
		var dn = dnResult.GetValue<decimal>();

		if (_prevUp is null || _prevDn is null)
		{
			_prevUp = up;
			_prevDn = dn;
			return;
		}

		// Close MA crosses above Open MA -> buy signal
		if (_prevUp <= _prevDn && up > dn && Position <= 0)
			BuyMarket();
		// Close MA crosses below Open MA -> sell signal
		else if (_prevUp >= _prevDn && up < dn && Position >= 0)
			SellMarket();

		_prevUp = up;
		_prevDn = dn;
	}
}