Skip to content
All library documents

Restricting an MQL5 Expert Advisor to Long or Short Positions

Article MQL5 code base

Summary

This MQL5 example shows how to extend the CExpert class to allow only long positions, only short positions, or both. The subclass overrides the opening and reversal checks: it routes opening decisions to the corresponding long- or short-check method when one direction is selected, and disables reversals in those single-direction modes. When both directions are allowed, it keeps the base class behavior.

A companion MACD expert illustrates how to use the subclass: include it, expose a position-selection input, instantiate the subclass, and set the selected mode after initialization. The article describes an implementation pattern for controlling permitted order directions; it does not present a trading signal evaluation, performance results, or evidence that restricting direction improves returns. The example is specific to the MQL5 CExpert framework, so adapting it to another platform or expert design would require corresponding changes.

Key ideas

  • A CExpert subclass can add a configurable long-only, short-only, or two-way mode.
  • The opening check selects the long or short opening logic when a single direction is configured.
  • The reversal check blocks reversals in long-only and short-only modes.
  • A MACD expert example shows where to include, instantiate, and configure the subclass.
  • The document explains order-direction control but provides no performance evaluation.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.