Optimization on EA returns error message "OnInit returned non-zero code 1"

 

I recently ran optimization on an EA I generated using the mql5 wizard which returned an error message:

Core 1 genetic pass (0, 501) tested with error "OnInit returned non-zero code 1" in 0:00:00.027

I have only ran optimization on cash indices and not on currencies


I am new to expert advisor programming and I would really appreciate all the help I can get with regards to finding out where my code is broken or incomplete.

//+------------------------------------------------------------------+
//| Include                                                          |
//+------------------------------------------------------------------+
#include <Expert\Expert.mqh>
//--- available signals
#include <Expert\Signal\SignalBearsPower.mqh>
#include <Expert\Signal\SignalBullsPower.mqh>
#include <Expert\Signal\SignalMACD.mqh>
//--- available trailing
#include <Expert\Trailing\TrailingFixedPips.mqh>
//--- available money management
#include <Expert\Money\MoneyFixedLot.mqh>
//+------------------------------------------------------------------+
//| Inputs                                                           |
//+------------------------------------------------------------------+
//--- inputs for expert
input string             Expert_Title                  ="Power1.0";    // Document name
ulong                    Expert_MagicNumber            =23447;         //
bool                     Expert_EveryTick              =false;         //
//--- inputs for main signal
input int                Signal_ThresholdOpen          =10;            // Signal threshold value to open [0...100]
input int                Signal_ThresholdClose         =10;            // Signal threshold value to close [0...100]
input double             Signal_PriceLevel             =0.0;           // Price level to execute a deal
input double             Signal_StopLevel              =30.0;          // Stop Loss level (in points)
input double             Signal_TakeLevel              =120.0;          // Take Profit level (in points)
input int                Signal_Expiration             =4;             // Expiration of pending orders (in bars)
input int                Signal_BearsPower_PeriodBears =13;            // Bears Power(13) Period of calculation
input double             Signal_BearsPower_Weight      =1.0;           // Bears Power(13) Weight [0...1.0]
input int                Signal_BullsPower_PeriodBulls =13;            // Bulls Power(13) Period of calculation
input double             Signal_BullsPower_Weight      =1.0;           // Bulls Power(13) Weight [0...1.0]
input int                Signal_MACD_PeriodFast        =12;            // MACD(12,26,9,PRICE_TYPICAL) Period of fast EMA
input int                Signal_MACD_PeriodSlow        =26;            // MACD(12,26,9,PRICE_TYPICAL) Period of slow EMA
input int                Signal_MACD_PeriodSignal      =9;             // MACD(12,26,9,PRICE_TYPICAL) Period of averaging of difference
input ENUM_APPLIED_PRICE Signal_MACD_Applied           =PRICE_TYPICAL; // MACD(12,26,9,PRICE_TYPICAL) Prices series
input double             Signal_MACD_Weight            =1.0;           // MACD(12,26,9,PRICE_TYPICAL) Weight [0...1.0]
//--- inputs for trailing
input int                Trailing_FixedPips_StopLevel  =30;            // Stop Loss trailing level (in points)
input int                Trailing_FixedPips_ProfitLevel=120;           // Take Profit trailing level (in points)
//--- inputs for money
input double             Money_FixLot_Percent          =10.0;          // Percent
input double             Money_FixLot_Lots             =0.01;          // Fixed volume
//+------------------------------------------------------------------+
//| Global expert object                                             |
//+------------------------------------------------------------------+
CExpert ExtExpert;
//+------------------------------------------------------------------+
//| Initialization function of the expert                            |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Initializing expert
   if(!ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,Expert_MagicNumber))
     {
      //--- failed
      printf(__FUNCTION__+": error initializing expert");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- Creating signal
   CExpertSignal *signal=new CExpertSignal;
   if(signal==NULL)
     {
      //--- failed
      printf(__FUNCTION__+": error creating signal");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//---
   ExtExpert.InitSignal(signal);
   signal.ThresholdOpen(Signal_ThresholdOpen);
   signal.ThresholdClose(Signal_ThresholdClose);
   signal.PriceLevel(Signal_PriceLevel);
   signal.StopLevel(Signal_StopLevel);
   signal.TakeLevel(Signal_TakeLevel);
   signal.Expiration(Signal_Expiration);
//--- Creating filter CSignalBearsPower
   CSignalBearsPower *filter0=new CSignalBearsPower;
   if(filter0==NULL)
     {
      //--- failed
      printf(__FUNCTION__+": error creating filter0");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
   signal.AddFilter(filter0);
//--- Set filter parameters
   filter0.PeriodBears(Signal_BearsPower_PeriodBears);
   filter0.Weight(Signal_BearsPower_Weight);
//--- Creating filter CSignalBullsPower
   CSignalBullsPower *filter1=new CSignalBullsPower;
   if(filter1==NULL)
     {
      //--- failed
      printf(__FUNCTION__+": error creating filter1");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
   signal.AddFilter(filter1);
//--- Set filter parameters
   filter1.PeriodBulls(Signal_BullsPower_PeriodBulls);
   filter1.Weight(Signal_BullsPower_Weight);
//--- Creating filter CSignalMACD
   CSignalMACD *filter2=new CSignalMACD;
   if(filter2==NULL)
     {
      //--- failed
      printf(__FUNCTION__+": error creating filter2");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
   signal.AddFilter(filter2);
//--- Set filter parameters
   filter2.PeriodFast(Signal_MACD_PeriodFast);
   filter2.PeriodSlow(Signal_MACD_PeriodSlow);
   filter2.PeriodSignal(Signal_MACD_PeriodSignal);
   filter2.Applied(Signal_MACD_Applied);
   filter2.Weight(Signal_MACD_Weight);
//--- Creation of trailing object
   CTrailingFixedPips *trailing=new CTrailingFixedPips;
   if(trailing==NULL)
     {
      //--- failed
      printf(__FUNCTION__+": error creating trailing");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- Add trailing to expert (will be deleted automatically))
   if(!ExtExpert.InitTrailing(trailing))
     {
      //--- failed
      printf(__FUNCTION__+": error initializing trailing");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- Set trailing parameters
   trailing.StopLevel(Trailing_FixedPips_StopLevel);
   trailing.ProfitLevel(Trailing_FixedPips_ProfitLevel);
//--- Creation of money object
   CMoneyFixedLot *money=new CMoneyFixedLot;
   if(money==NULL)
     {
      //--- failed
      printf(__FUNCTION__+": error creating money");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- Add money to expert (will be deleted automatically))
   if(!ExtExpert.InitMoney(money))
     {
      //--- failed
      printf(__FUNCTION__+": error initializing money");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- Set money parameters
   money.Percent(Money_FixLot_Percent);
   money.Lots(Money_FixLot_Lots);
//--- Check all trading objects parameters
   if(!ExtExpert.ValidationSettings())
     {
      //--- failed
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- Tuning of all necessary indicators
   if(!ExtExpert.InitIndicators())
     {
      //--- failed
      printf(__FUNCTION__+": error initializing indicators");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- ok
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Deinitialization function of the expert                          |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ExtExpert.Deinit();
  }
//+------------------------------------------------------------------+
//| "Tick" event handler function                                    |
//+------------------------------------------------------------------+
void OnTick()
  {
   ExtExpert.OnTick();
  }
//+------------------------------------------------------------------+
//| "Trade" event handler function                                   |
//+------------------------------------------------------------------+
void OnTrade()
  {
   ExtExpert.OnTrade();
  }
//+------------------------------------------------------------------+
//| "Timer" event handler function                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
   ExtExpert.OnTimer();
  }
//+------------------------------------------------------------------+

MQL5 Wizard: New Version
MQL5 Wizard: New Version
  • www.mql5.com
The article contains descriptions of the new features available in the updated MQL5 Wizard. The modified architecture of signals allow creating trading robots based on the combination of various market patterns. The example contained in the article explains the procedure of interactive creation of an Expert Advisor.
 
If this error returned for one or several passes, then it is your fault here: you set unrealistic parameters for the MACD indicator.
 
Vladimir Karputov #:
If this error returned for one or several passes, then it is your fault here: you set unrealistic parameters for the MACD indicator.
Just to get clarification on the problem do I need to change the applied price that I set the indicator on or do I need to change the periods of the two EMA's and signal line?
 
KrugerG8 # :
Just to get clarification on the problem do I need to change the applied price that I set the indicator on or do I need to change the periods of the two EMA's and signal line?

You must first think hard. Then think hard again and only after that you will be able to select parameters for MACD. In general, the idea is this: if you are crazy about setting parameters, you will receive indicator errors.