Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling exceptions in MQL4

In the Java language one can handle exceptions with a try-catch mechanism.

Does it exist any similar behaviour in MQL4?

This code throws the error 'array out of range' in the Close[bar] expression and I cannot handle it properly.

Any help will be appreciated.

bool isBarClosed( int bar ) {
    bool   barClosed  = true;
    double closePrice = Close[bar];
    int    error      = GetLastError();

    if ( error == 4202 ) {
        barClosed = false;
    }   
    return barClosed;
}
like image 347
JLLMNCHR Avatar asked Sep 26 '22 22:09

JLLMNCHR


1 Answers

No.

MQL4 has no syntax support for a construct alike a try/except/finally or try/catch use-case in python / java and alike languages.

How to handle exceptions?

Assume there are no compile-time errors.

The run-time errors are hard to be handled, some even cause the software to crash.

One could and rather shall proactively sanitize the MQL4-code with a due type-checking and use-case pre-validations so as to prevent exceptions.

Exceptions to this are dbPool operations, which may, under some conditions, "legitimately" fail to yield an expected result.

A GetLastError() ( if it was cleared a-priori the exception itself ) may serve as an almost-post-mortem identification, not as an exception handler.

4202? Not your problem, Bro'

_LastError == 4202 ... does not explain the trouble                <<< stdlib.mqh

4202
    ERR_OBJECT_DOES_NOT_EXIST
    Object does not exist

Your problem seems to be related with bar "pointing" outside of the TimeSeries-reverse-stepping-index of Close[] values.

0 <= aBarPtrIDX < Bars

Next target? A Close[aBarPtrIDX] misconcept

After some time spent in MQL4 domain, one becomes familiar with a few contradicting facts. One of potential surprises is, that a current bar, the "hot-zero" [0], contains Close[0] == Bid during it's all live-life-span.

After the running bar gets terminated by aNewBarEVENT ( signalled by a Volume[0] == 1 ( or Volume[0] < aPreviousVolume_0 -- a safer mode for a case, the MQL4-loosely-coupled event-loop has missed a few quote-arrivals during it's busy-episode )), the Close[1] represents the last-visited price during the respective Bar-period and Close[0] keeps surfing on the always-changing Bid price

like image 115
user3666197 Avatar answered Nov 16 '22 23:11

user3666197