Problem with code - page 2

 
serieux399 # :

I know that 

i like to correct this code

You should always check the result of the handle creation.

You MUST NOT return '0' in OnInit - in OnInit you must return:

ID

Description

INIT_SUCCEEDED

Initialization successful, EA test can be continued.

This code means the same as the zero value – the EA initialization in the tester is successful.

INIT_FAILED

Initialization failed. There is no point in continuing the test due to unavoidable errors. For example, it is impossible to create an indicator necessary for the EA operation.

The return of this value means the same as returning the value different from zero – EA initialization in the tester failed.

INIT_PARAMETERS_INCORRECT

Designed to denote an incorrect set of input parameters by a programmer. In the general optimization table, the result string with this return code is highlighted in red.

A test for such a set of EA inputs is not performed. The agent is ready to receive a new task.

When this value is received, the strategy tester does not pass this task to other agents for repeated execution.

INIT_AGENT_NOT_SUITABLE

No program execution errors during initialization. However, for some reasons, the agent is not suitable for conducting a test. For example, there is not enough RAM, no OpenCL support, etc.

After returning this code, the agent no longer receives tasks until the very end of this optimization.


Example:

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2020, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh>
CTrade         m_trade;                      // object of CTrade class
//--- input parameters
input group             "MACD"
input ENUM_TIMEFRAMES      Inp_MACD_period               = PERIOD_D1;      // MACD: timeframe
input int                  Inp_MACD_fast_ema_period      = 12;             // MACD: period for Fast average calculation
input int                  Inp_MACD_slow_ema_period      = 26;             // MACD: period for Slow average calculation
input int                  Inp_MACD_signal_period        = 9;              // MACD: period for their difference averaging
input ENUM_APPLIED_PRICE   Inp_MACD_applied_price        = PRICE_CLOSE;    // MACD: type of price
input group             "Trading settings"
input double               Lots4 = 0.2;
input int                  Stoploss4 = 40;
input int                  Takeprofit4 = 60;
//--- local variables
double   PipValue       = 1;     // this variable is here to support 5-digit brokers
bool     Terminated     = false;
string   LF             = "\n";  // use this in custom or utility blocks where you need line feeds
int      NDigits        = 4;     // used mostly for NormalizeDouble in Flex type blocks
int      ObjCount       = 0;     // count of all objects created on the chart, allows creation of objects with unique names
int      current        = 0;
int      handle_iMACD;           // variable for storing the handle of the iMACD indicator
bool     m_init_error   = false; // error on InInit
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   NDigits = Digits();
   if(NDigits == 3 || NDigits == 5)
      PipValue = 10;
   if(AccountInfoInteger(ACCOUNT_TRADE_EXPERT) == false)
     {
      Print("Check terminal options because EA m_trade option is set to not allowed.");
      Comment("Check terminal options because EA m_trade option is set to not allowed.");
     }
   ObjectsDeleteAll(0);      // clear the chart
//--- create handle of the indicator iMACD
   handle_iMACD=iMACD(Symbol(),Period(),Inp_MACD_fast_ema_period,Inp_MACD_slow_ema_period,
                      Inp_MACD_signal_period,Inp_MACD_applied_price);
//--- if the handle is not created
   if(handle_iMACD==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMACD indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      m_init_error=true;
      return(INIT_SUCCEEDED);
     }
   Comment("");    // clear the chart
//---
   return(INIT_SUCCEEDED);
  }
Documentation on MQL5: Working with OpenCL
Documentation on MQL5: Working with OpenCL
  • www.mql5.com
Working with OpenCL - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Vladimir Karputov #:

You should always check the result of the handle creation.

You MUST NOT return '0' in OnInit - in OnInit you must return:

ID

Description

INIT_SUCCEEDED

Initialization successful, EA test can be continued.

This code means the same as the zero value – the EA initialization in the tester is successful.

INIT_FAILED

Initialization failed. There is no point in continuing the test due to unavoidable errors. For example, it is impossible to create an indicator necessary for the EA operation.

The return of this value means the same as returning the value different from zero – EA initialization in the tester failed.

INIT_PARAMETERS_INCORRECT

Designed to denote an incorrect set of input parameters by a programmer. In the general optimization table, the result string with this return code is highlighted in red.

A test for such a set of EA inputs is not performed. The agent is ready to receive a new task.

When this value is received, the strategy tester does not pass this task to other agents for repeated execution.

INIT_AGENT_NOT_SUITABLE

No program execution errors during initialization. However, for some reasons, the agent is not suitable for conducting a test. For example, there is not enough RAM, no OpenCL support, etc.

After returning this code, the agent no longer receives tasks until the very end of this optimization.


Example:

That not resolve the problem  Sorry .

I will dig by myself