在 GitHub 上查看

X Alert 3 策略

该策略复现了 X_alert_3.mq4 的逻辑。它计算两条可配置参数的移动平均线,并在发生交叉时输出提示信息。

工作原理

  1. 每根完成的K线都会计算两条移动平均线。
  2. 当满足以下条件时产生看涨提示:
    • 当前K线上 MA1 高于 MA2;
    • 前一根K线上 MA1 仍高于 MA2;
    • 两根K线之前 MA1 低于 MA2。
  3. 当满足以下条件时产生看跌提示:
    • 当前K线上 MA1 低于 MA2;
    • 前一根K线上 MA1 仍低于 MA2;
    • 两根K线之前 MA1 高于 MA2。
  4. 策略不会开仓或平仓,只是向日志写入消息。

参数

参数 说明 默认值
Ma1Period 第一条移动平均线的周期。 1
Ma1Type 第一条移动平均线的类型(Simple、Exponential、Smoothed、Weighted)。 Simple
Ma2Period 第二条移动平均线的周期。 14
Ma2Type 第二条移动平均线的类型。 Simple
PriceType 计算所使用的价格(Close、Open、High、Low、Median、Typical、Weighted)。 Median
CandleType 使用的K线类型。 1分钟

说明

  • 为了检测交叉,策略保存最近两次移动平均线差值,不直接访问历史指标数据。
  • 提示信息通过 AddInfoLog 写入日志,不会产生其他副作用。
  • 原始 MetaTrader 参数 RunIntervalSeconds 在 StockSharp 中无必要,因此被省略。
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>
/// Strategy based on crossover of two moving averages.
/// Buys when fast MA crosses above slow MA, sells when crosses below.
/// </summary>
public class XAlert3Strategy : Strategy
{
	private readonly StrategyParam<int> _ma1Period;
	private readonly StrategyParam<int> _ma2Period;
	private readonly StrategyParam<DataType> _candleType;

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

	public int Ma1Period { get => _ma1Period.Value; set => _ma1Period.Value = value; }
	public int Ma2Period { get => _ma2Period.Value; set => _ma2Period.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public XAlert3Strategy()
	{
		_ma1Period = Param(nameof(Ma1Period), 10)
			.SetGreaterThanZero()
			.SetDisplay("MA1 Period", "Fast moving average period", "Indicators");

		_ma2Period = Param(nameof(Ma2Period), 30)
			.SetGreaterThanZero()
			.SetDisplay("MA2 Period", "Slow moving average period", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "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 = Ma1Period };
		var slow = new ExponentialMovingAverage { Length = Ma2Period };

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

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

		if (!_hasPrev)
		{
			_prevFast = fast;
			_prevSlow = slow;
			_hasPrev = true;
			return;
		}

		if (_prevFast <= _prevSlow && fast > slow)
		{
			if (Position < 0) BuyMarket();
			if (Position <= 0) BuyMarket();
		}
		else if (_prevFast >= _prevSlow && fast < slow)
		{
			if (Position > 0) SellMarket();
			if (Position >= 0) SellMarket();
		}

		_prevFast = fast;
		_prevSlow = slow;
	}
}