Ver en GitHub

Tester v0.14 Strategy

This sample strategy is a simplified port of the MQL4 "Tester v0.14" script originally designed for EURUSD on the H4 timeframe.

Logic

  • Calculates a 14-period simple moving average and MACD.
  • Generates a buy signal when the close price is above the SMA and MACD is positive.
  • Generates a sell signal when the close price is below the SMA and MACD is negative.
  • After an order is opened, the position is closed after a configurable number of bars.

This port uses the high-level StockSharp API, relying on SubscribeCandles and Bind to receive indicator values.

Parameters

  • MinSignSum – minimum number of signals required to open a position.
  • Risk – percentage of account balance used for money management.
  • TakeProfit / StopLoss – fixed levels in points.
  • BarsNumber – number of bars to keep a position open.
  • CandleType – candle series used (default: 4H).

Notes

The original MQL file contained hundreds of rule combinations. This C# example illustrates the structure using a reduced rule set for clarity.

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>
/// Tester strategy using SMA and MACD for entries.
/// Closes after specified number of bars.
/// </summary>
public class TesterV014Strategy : Strategy
{
	private readonly StrategyParam<int> _barsNumber;
	private readonly StrategyParam<DataType> _candleType;

	private int _barsCounter;
	private bool _positionOpened;

	public int BarsNumber { get => _barsNumber.Value; set => _barsNumber.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public TesterV014Strategy()
	{
		_barsNumber = Param(nameof(BarsNumber), 3)
			.SetGreaterThanZero()
			.SetDisplay("Bars Number", "Holding period in bars", "General");

		_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();
		_barsCounter = 0;
		_positionOpened = false;
	}

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

		var sma = new SimpleMovingAverage { Length = 14 };
		var macd = new MovingAverageConvergenceDivergence();

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(sma, macd, ProcessCandle)
			.Start();
	}

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

		// Close after specified bars
		if (_positionOpened)
		{
			_barsCounter++;
			if (_barsCounter >= BarsNumber)
			{
				if (Position > 0)
					SellMarket();
				else if (Position < 0)
					BuyMarket();
				_positionOpened = false;
				_barsCounter = 0;
			}
		}

		// Entry
		if (Position == 0 && !_positionOpened)
		{
			if (candle.ClosePrice > smaVal && macdVal > 0)
			{
				BuyMarket();
				_barsCounter = 0;
				_positionOpened = true;
			}
			else if (candle.ClosePrice < smaVal && macdVal < 0)
			{
				SellMarket();
				_barsCounter = 0;
				_positionOpened = true;
			}
		}
	}
}