Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

Metatrader 5 Libraries | Result - type-safe error handling for MQL5 without exceptions | MQL5 Code

Fresh MQL5 code release – perfect for enhancing your MT4/MT5 strategies.

Purpose

GetLastError() / ResetLastError() state — which is easy to forget, easy to overwrite, and forces out-parameters and "magic" return values.


What it does

Trade the powerful Traders Dynamic Index strategy automatically with this dedicated TDI Expert Advisor. More details.

  • Result<T>
  • Error
  • MQLError
  • Macros —
  • Optional callbacks —

Accessors:

Usage

Image for Result - type-safe error handling for MQL5 without exceptions

// 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);
}

Build better strategies with RobotFX professional tools – check them out.

74427

Best MetaTrader Indicators + Profitable Expert Advisors