Auf GitHub ansehen

X Alert 3 Strategy

This strategy replicates the logic of the original X_alert_3.mq4 expert. It monitors two moving averages with configurable parameters and produces an informational alert when a crossover occurs.

How it works

  1. Two moving averages are calculated on every finished candle.
  2. A bullish alert is generated when:
    • MA1 is above MA2 on the current candle.
    • MA1 is above MA2 on the previous candle.
    • MA1 was below MA2 two candles ago.
  3. A bearish alert is generated when:
    • MA1 is below MA2 on the current candle.
    • MA1 is below MA2 on the previous candle.
    • MA1 was above MA2 two candles ago.
  4. The strategy does not open or close any positions. It only writes messages to the log.

Parameters

Parameter Description Default
Ma1Period Period of the first moving average. 1
Ma1Type Type of the first moving average (Simple, Exponential, Smoothed, Weighted). Simple
Ma2Period Period of the second moving average. 14
Ma2Type Type of the second moving average. Simple
PriceType Source price used in calculations (Close, Open, High, Low, Median, Typical, Weighted). Median
CandleType Candle series used for processing. 1-minute time frame

Notes

  • The implementation keeps track of the last two differences between the moving averages to detect crossovers without accessing historical indicator values directly.
  • Alerts are written using AddInfoLog to keep the strategy side-effect free.
  • The original MetaTrader parameter RunIntervalSeconds is not required in StockSharp and has been omitted.
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;
	}
}