거래 로봇을 무료로 다운로드 하는 법을 시청해보세요
당사를 Facebook에서 찾아주십시오!
당사 팬 페이지에 가입하십시오
스크립트가 흥미로우신가요?
그렇다면 링크 to it -
하셔서 다른 이들이 평가할 수 있도록 해보세요
스크립트가 마음에 드시나요? MetaTrader 5 터미널에서 시도해보십시오
라이브러리

Result - type-safe error handling for MQL5 without exceptions - MetaTrader 5용 라이브러리

Maksym Yaroslavovych Libovych
Maksym Yaroslavovych Libovych
  • Algorithmic Trading Developer | MQL5, NinjaTrader, QuantConnect 에  Self-employed
  • 우크라이나
  • 116
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.
조회수:
72
게시됨:
\MQL5\Include\Common\ \MQL5\Include\
MqlError.mqh (8.26 KB) 조회
\MQL5\Scripts\
MQL5 프리랜스 이 코드를 기반으로 한 로봇이나 지표가 필요하신가요? 프리랜스로 주문하세요 프리랜스로 이동

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.

Accelerator Oscillator (AC) Accelerator Oscillator (AC)

The Acceleration/Deceleration Indicator (AC) measures acceleration and deceleration of the current driving force.

MACD Signals MACD Signals

Indicator edition for new platform.