Ver en GitHub

Force Trend Strategy

Overview

  • Conversion of the MetaTrader 5 expert advisor Exp_ForceTrend.mq5 located in MQL/18817.
  • Uses the proprietary ForceTrend oscillator to detect transitions between bullish and bearish momentum.
  • Implements the logic with StockSharp's high-level API, relying on candle subscriptions and built-in indicators instead of direct series access.

ForceTrend indicator

  • The indicator looks back over Length candles and measures the distance between the highest high and lowest low within that window.
  • The mid price of the current candle is normalized within that range and smoothed twice:
    • The first stage produces an intermediate force value with coefficients 0.66 and 0.67.
    • The second stage applies a logarithmic transform combined with a half-life smoothing to obtain the final ForceTrend value.
  • Values above zero are treated as bullish (originally rendered in blue) and values below zero are bearish (rendered in magenta).

Parameters

  • Length – size of the ForceTrend lookback window; must remain positive.
  • SignalBar – how many finished candles to shift the signal. 0 reacts to the most recent closed bar, 1 mimics the default MT5 setting by waiting for one extra bar, and larger values delay the execution even more.
  • EnableLongEntry – if disabled the strategy will not open long positions on bullish transitions.
  • EnableShortEntry – if disabled the strategy will not open short positions on bearish transitions.
  • EnableLongExit – toggles whether bullish signals are allowed to close existing short positions.
  • EnableShortExit – toggles whether bearish signals are allowed to close existing long positions.
  • CandleType – timeframe of the candles used for indicator calculations.

Trading rules

  1. ForceTrend output is converted into a discrete direction (+1, 0, -1).
  2. Directions are stored in a fixed-length history so the strategy can compare the bar at SignalBar offset with the immediately preceding bar.
  3. A bullish signal (direction > 0) triggers:
    • Closing any open short position if EnableShortExit is true.
    • Opening or reversing into a long position (market order sized as Volume + |Position|) when the previous direction was not bullish and EnableLongEntry is true.
  4. A bearish signal (direction < 0) triggers the symmetric actions for long positions when EnableLongExit/EnableShortEntry are enabled.
  5. Neutral ForceTrend readings inherit the last known direction so that the system does not oscillate between flat states.
  6. Orders are submitted only when the strategy is fully formed, online, and trading is allowed by the StockSharp runtime.

Implementation notes

  • Candles are received through SubscribeCandles(CandleType); indicator processing is performed in the ProcessCandle callback.
  • The highest and lowest prices are obtained via StockSharp's Highest and Lowest indicators, ensuring that no manual buffer management or LINQ operations are required.
  • Direction history is stored in a small fixed array sized according to SignalBar to reproduce the original MT5 behaviour without recreating collections for every tick.
  • Position reversals use a single market order with volume equal to the sum of the desired exposure and the absolute current position, emulating the BuyPositionOpen/SellPositionOpen helpers from the MQL version.
  • Money management parameters from the expert advisor (lot sizing, stop-loss and take-profit in points) are intentionally omitted; the StockSharp strategy relies on the user-configured Volume and optional external protection modules.
  • The boolean toggles mirror the MT5 inputs (BuyPosOpen, SellPosOpen, BuyPosClose, SellPosClose).

Usage hints

  • Configure the Volume property before starting the strategy to control order size.
  • Choose a candle type that matches the timeframe used during MT5 testing (default is four-hour candles).
  • Combine with StockSharp risk/protection components if stop-loss or take-profit automation is required.

Files

  • Strategy implementation: CS/ForceTrendStrategy.cs
  • Original MQL files: MQL/18817/mql5/Experts/Exp_ForceTrend.mq5 and MQL/18817/mql5/Indicators/ForceTrend.mq5
using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

using StockSharp.Algo;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Force Trend strategy that mirrors the original MT5 expert advisor logic.
/// It reacts to ForceTrend indicator color changes to switch between long and short positions.
/// </summary>
public class ForceTrendStrategy : Strategy
{
	private readonly StrategyParam<int> _length;
	private readonly StrategyParam<int> _signalBar;
	private readonly StrategyParam<bool> _enableLongEntry;
	private readonly StrategyParam<bool> _enableShortEntry;
	private readonly StrategyParam<bool> _enableLongExit;
	private readonly StrategyParam<bool> _enableShortExit;
	private readonly StrategyParam<DataType> _candleType;

	private Highest _highest = null!;
	private Lowest _lowest = null!;
	private decimal _previousForceValue;
	private decimal _previousIndicatorValue;
	private int?[] _directionHistory = Array.Empty<int?>();
	private int _historyCount;
	private int? _lastKnownDirection;

	/// <summary>
	/// Initializes strategy parameters.
	/// </summary>
	public ForceTrendStrategy()
	{
		_length = Param(nameof(Length), 13)
			.SetDisplay("Length", "ForceTrend lookback length", "Indicator")
			.SetGreaterThanZero()
			;

		_signalBar = Param(nameof(SignalBar), 1)
			.SetDisplay("Signal Bar", "Number of finished bars to shift the signal", "Trading")
			;

		_enableLongEntry = Param(nameof(EnableLongEntry), true)
			.SetDisplay("Enable Long Entry", "Allow opening long positions", "Trading")
			;

		_enableShortEntry = Param(nameof(EnableShortEntry), true)
			.SetDisplay("Enable Short Entry", "Allow opening short positions", "Trading")
			;

		_enableLongExit = Param(nameof(EnableLongExit), true)
			.SetDisplay("Enable Long Exit", "Allow closing long positions", "Trading")
			;

		_enableShortExit = Param(nameof(EnableShortExit), true)
			.SetDisplay("Enable Short Exit", "Allow closing short positions", "Trading")
			;

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe used for ForceTrend calculations", "General");
	}

	/// <summary>
	/// ForceTrend lookback length.
	/// </summary>
	public int Length
	{
		get => _length.Value;
		set => _length.Value = value;
	}

	/// <summary>
	/// Number of finished candles used to shift the trade signal.
	/// </summary>
	public int SignalBar
	{
		get => _signalBar.Value;
		set => _signalBar.Value = value;
	}

	/// <summary>
	/// Enable opening long positions when the ForceTrend becomes bullish.
	/// </summary>
	public bool EnableLongEntry
	{
		get => _enableLongEntry.Value;
		set => _enableLongEntry.Value = value;
	}

	/// <summary>
	/// Enable opening short positions when the ForceTrend becomes bearish.
	/// </summary>
	public bool EnableShortEntry
	{
		get => _enableShortEntry.Value;
		set => _enableShortEntry.Value = value;
	}

	/// <summary>
	/// Enable closing long positions on bearish ForceTrend signals.
	/// </summary>
	public bool EnableLongExit
	{
		get => _enableLongExit.Value;
		set => _enableLongExit.Value = value;
	}

	/// <summary>
	/// Enable closing short positions on bullish ForceTrend signals.
	/// </summary>
	public bool EnableShortExit
	{
		get => _enableShortExit.Value;
		set => _enableShortExit.Value = value;
	}

	/// <summary>
	/// Candle type used for indicator calculations.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_previousForceValue = 0m;
		_previousIndicatorValue = 0m;
		_directionHistory = Array.Empty<int?>();
		_historyCount = 0;
		_lastKnownDirection = null;
	}

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

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

		_previousForceValue = 0m;
		_previousIndicatorValue = 0m;
		_historyCount = 0;
		_lastKnownDirection = null;
		_directionHistory = new int?[Math.Max(SignalBar + 2, 2)];

		_highest = new Highest { Length = Length };
		_lowest = new Lowest { Length = Length };

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

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

		var highestValue = _highest.Process(new CandleIndicatorValue(_highest, candle)).ToDecimal();
		var lowestValue = _lowest.Process(new CandleIndicatorValue(_lowest, candle)).ToDecimal();

		if (!_highest.IsFormed || !_lowest.IsFormed)
			return;

		var range = highestValue - lowestValue;
		decimal forceValue;

		if (range != 0m)
		{
			var average = (candle.HighPrice + candle.LowPrice) / 2m;
			var normalized = (average - lowestValue) / range - 0.5m;
			forceValue = 0.66m * normalized + 0.67m * _previousForceValue;
		}
		else
		{
			forceValue = 0.67m * _previousForceValue - 0.33m;
		}

		forceValue = Math.Clamp(forceValue, -0.999m, 0.999m);

		decimal indicatorValue;
		var denominator = 1m - forceValue;

		if (denominator != 0m)
		{
			var ratio = (forceValue + 1m) / denominator;
			indicatorValue = (decimal)(Math.Log((double)ratio) / 2.0) + _previousIndicatorValue / 2m;
		}
		else
		{
			indicatorValue = _previousIndicatorValue / 2m + 0.5m;
		}

		_previousForceValue = forceValue;
		_previousIndicatorValue = indicatorValue;

		var direction = indicatorValue > 0m ? 1 : indicatorValue < 0m ? -1 : _lastKnownDirection ?? 0;
		if (direction != 0)
			_lastKnownDirection = direction;

		AddDirection(direction);

		var currentDirection = GetDirection(SignalBar);
		if (currentDirection is null)
			return;

		var previousDirection = GetDirection(SignalBar + 1);
		var bullish = currentDirection.Value > 0;
		var bearish = currentDirection.Value < 0;
		var bullishFlip = bullish && previousDirection.HasValue && previousDirection.Value <= 0;
		var bearishFlip = bearish && previousDirection.HasValue && previousDirection.Value >= 0;

		// indicators processed manually, no BindEx

		if (bullish)
		{
			var volumeToBuy = 0m;

			if (EnableShortExit && Position < 0m)
				volumeToBuy += Math.Abs(Position);

			if (EnableLongEntry && bullishFlip && Position <= 0m)
				volumeToBuy += Volume;

			if (volumeToBuy > 0m)
				BuyMarket();
		}
		else if (bearish)
		{
			var volumeToSell = 0m;

			if (EnableLongExit && Position > 0m)
				volumeToSell += Math.Abs(Position);

			if (EnableShortEntry && bearishFlip && Position >= 0m)
				volumeToSell += 1m;

			if (volumeToSell > 0m)
				SellMarket();
		}
	}

	private void AddDirection(int direction)
	{
		if (_historyCount < _directionHistory.Length)
		{
			_directionHistory[_historyCount] = direction;
			_historyCount++;
		}
		else
		{
			for (var i = 1; i < _directionHistory.Length; i++)
				_directionHistory[i - 1] = _directionHistory[i];

			_directionHistory[^1] = direction;
		}
	}

	private int? GetDirection(int offset)
	{
		if (offset < 0)
			return null;

		var index = _historyCount - 1 - offset;
		if (index < 0)
			return null;

		return _directionHistory[index];
	}
}