简体   繁体   English

如何使用 MetaTrader4 错误代码刷新进行正确的错误处理?

[英]How to use a MetaTrader4 Error Code Refresh for proper error handling?

I have a "set up" on my EA to ping me an email when my Expert Advisor experiences an error and provides me the error code in accordance with the pre-determined 3-4 digit error code in the documentation on the MQL4 website .我的 EA 上有一个“设置”,当我的“EA 交易”遇到错误时,它会向我发送一封电子邮件,并根据MQL4 网站上的文档中预先确定的 3-4 位错误代码向我提供错误代码。

/* technically speaking,
   error codes with 5+ digits are also possible,
   Using:
   ------                                                            */
   SetUserError( 1000000 );                                          /*

// this will set an error-state
// with a number 1065536 -- having a bit more than the said 3 ~ 4 digits
// composed as ( 1000000 + ERR_USER_ERROR_FIRST )
*/

This is to enable me to diagnose the problem.这是为了让我能够诊断问题。

Just wanting to clarify if I need to refresh the error code ( from a previous error ) I'm getting in my email, or does it do this automatically, when a new error is presented in the Journal of my MT4 Platform?只是想澄清一下我是否需要刷新我收到的电子邮件中的错误代码(来自之前的错误),还是在我的 MT4 平台日志中出现新错误时自动执行此操作?

There is a helpful function ResetLastError() that explicitly sets _LastError to zero.有一个有用的函数ResetLastError()明确地将_LastError设置为零。 Next, there is also a side effect of each call to a GetLastError() function, as it also sets zero to the _LastError variable.接下来,每次调用GetLastError()函数也会产生副作用,因为它还_LastError变量设置为零。

Either way, it is a common practice to embed ( surround ) a section, where some error-states need to get handled accordingly, right by a:无论哪种方式,嵌入(环绕)一个部分是一种常见的做法,其中一些错误状态需要得到相应的处理,正确的是:

// -------------------------------------------- // START_________________________
     GetLastError();                            // implicit  pre-reset _LastError
   ResetLastError();                            // explicit  pre-reset _LastError
   bool an_OK_flag = OrderModify( ... );        // XTO call  w ex-post _LastError
   int  anErrorSTATEtoHANDLE = GetLastError();  /* get a value of the  _LastError
                                                 + implicit post-reset _LastError */
// -------------------------------------------- // HANDLE ERROR-STATE(s)
   switch( anErrorSTATEtoHANDLE ){
         case ERR_NO_ERROR:                       break;
         case ERR_NO_RESULT:                      ...
                                                  break;
         case ERR_INVALID_TRADE_PARAMETERS:       ...
                                                  break;
         case ERR_SERVER_BUSY:                    ...
                                                  break;
         case ERR_BROKER_BUSY:                    ...
                                                  break;
         case ERR_TOO_MANY_REQUESTS:              ...
                                                  break;
         case ERR_TRADE_MODIFY_DENIED:            ...
                                                  break;
         ...
         default:                                 break;
   }
// -------------------------------------------- // FIN __________________________

This makes your code robust against any kind of some "forgotten" ( coincidentally un-reset ) last error ( which was not crashing your code-execution at the place, where such error has appeared but ... ) which would trigger later an unwanted behaviour in the "next" error-handling section, thus potentially sending you an email also in cases, where the email-signal was not present, but the "forgotten" ( un-reset ) value inside the _LastError system register co-incidentally matched the error-handling case, resulting in sending the said email ( as if there were an email-signal present ( which was not, as described above ).这使您的代码能够抵抗任何类型的“遗忘”(巧合地未重置)最后一个错误(这不会在出现此类错误的地方使您的代码执行崩溃,但是......)这将在以后触发一个不需要的“下一个”错误处理部分中的行为,因此在不存在电子邮件信号但_LastError系统寄存器中的“忘记”(未重置)值偶然匹配的情况下,也可能向您发送电子邮件错误处理情况,导致发送所述电子邮件(好像存在电子邮件信号(如上所述)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 MQL4 EA 流程(MetaTrader4 终端)运行 Python 脚本? - How to run a python script from an MQL4 EA-process ( MetaTrader4 Terminal )? MetaTrader4终端,策略测试器:在交易发生之前如何从[结果窗口]跳至[图表]? - MetaTrader4 Terminal, Strategy Tester: how to jump from a [ Results Window ] to the [ Graph ] BEFORE a Trade Happens? 如何将 MetaTrader 与 Node.JS 连接? - How to connect MetaTrader with a Node.JS? 如何使用 JAVA(或任何东西!)向/从 MetaTrader Terminal 4 发送/接收数据 - How to send/receive data to/from MetaTrader Ternminal 4 with JAVA (or anything!) 我如何更正 MQL4 OrderModify() 调用中的错误代码 0? - How do I correct an error code 0 in MQL4 OrderModify() call? 我如何将 Metatrader 4 警报或电子邮件指标信号转换为 Expert Advisor 以进行交易? - How can I convert Metatrader 4 alert or email indicator signal to Expert Advisor to open trades? Metatrader 5中的自定义优化 - Custom Optimization in Metatrader 5 算法交易代码中的except子句错误 - Error on except clause in algo trading code 为什么OrderModify()返回代码错误130? - Why OrderModify() returns a code-error 130? MetaTrader 终端 4:根据历史数据调试智能交易系统 - MetaTrader Terminal 4: debugging an expert advisor on historical data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM