Watch how to download trading robots for free
Find us on Telegram!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Libraries

Result - type-safe error handling for MQL5 without exceptions - library for MetaTrader 5

Maksym Yaroslavovych Libovych
Maksym Yaroslavovych Libovych
  • Software Engineer | .NET | MQL5 | NinjaTrader at  Self-employed
  • Ukraine
  • 185
MQL5 Developer — Expert Advisors & Custom Indicators | NinjaTrader, QuantConnect
• I build and maintain algorithmic trading systems in MQL5 — Expert Advisors, custom indicators, and strategy backtesting/optimization.
| English Русский 中文 Español Deutsch 日本語 Português 한국어 Français Italiano Türkçe
Views:
837
Rating:
(1)
Published:
\MQL5\Include\Common\ \MQL5\Include\
MqlError.mqh (8.26 KB) view
\MQL5\Scripts\
ResultDemo.mq5 (4.17 KB) view
MQL5 Freelance Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

Purpose

This include file provides a value-or-error return type for MQL5, so functions can report success or failure explicitly instead of through the global GetLastError() / ResetLastError() state — which is easy to forget, easy to overwrite, and forces out-parameters and "magic" return values.


What it does

 A function returns one object that either holds a value or an Error; the caller must check before using the value.

  • ResultValue<T>  — for value types (numbers, structs).
  • Result<T>  — the same contract for objects held by pointer (classes).
  • Error  — a lightweight struct (code + description); codes map to readable names via EnumToString.
  • MQLError  — a thin wrapper over GetLastError / ResetLastError / SetUserError.
  • Macros —  TRY ,  RETURN_ON_ERROR ,  PRINT_ON_ERROR ,  RETURN_SAME_ON_ERROR ,  RESULT_ON_ERROR  — for early-return propagation without boilerplate.
  • Optional callbacks —  Then ,  Match ,  MapError  (they take top-level or static functions; MQL5 has no closures).

Accessors:  Value() ,  ValueOr(fallback) ,  CurrentError() ,  IsError() .

Usage

Error Handling

// Stage functions (top-level — MQL5 has no closures)
ResultValue<double> EnsurePositive(const double &value)
{
   if (value <= 0.0)
   {
      return ResultValue<double>::Fail("price must be positive");
   }
   return ResultValue<double>::Ok(value);
}

ResultValue<double> ToPips(const double &price)
{
   return ResultValue<double>::Ok(price / _Point);
}

Error Describe(const Error &error)
{
   return Error::Create(error.code, "pipeline failed: " + error.description);
}

void OnOk(const double &value)  { PrintFormat("[pipeline] result = %.1f pips", value); }
void OnFail(const Error &error) { PrintFormat("[pipeline] %s", error.description); }

// The pipeline: read -> validate -> transform -> (annotate error) -> handle
void RunPipeline()
{
   SymbolDouble("EURUSD", SYMBOL_BID)
      .Then(EnsurePositive)
      .Then(ToPips)
      .MapError(Describe)
      .Match(OnOk, OnFail);
}
Pip Value Calculator Pip Value Calculator

Live pip value, risk-based lot sizing, custom volume risk analysis & open position P/L — all in one panel.

ATR Ranked Support and Resistance Zones ATR Ranked Support and Resistance Zones

An educational MT5 indicator that builds support and resistance as price zones from confirmed pivots. Nearby reactions are merged using ATR-based distance, then ranked by repeated tests, rejection strength and recency. Only the strongest zones are displayed to keep the chart readable.

Equity Guard — Daily Loss Limit Guardian with Panic Panel Equity Guard — Daily Loss Limit Guardian with Panic Panel

Account-level daily loss guardian: when your daily loss reaches a configurable trigger, it closes all positions and pending orders and keeps the account flat until the next daily reset. Limits in percent or money, configurable reset time (server time), draggable visual panel with live gauge, and manual CLOSE ALL / LOCK buttons with click-to-confirm. Works on any broker, symbol, account size and currency — hedging and netting, no DLLs.

SR Zone Scanner SR Zone Scanner

Multi-timeframe Support & Resistance zone scanner with strength rating and instant alerts — M15, H1, H4, D1.