Genetic Strategy does not work.

 

Hi to all,

I've created 2 custom indicators that use other indicators.

In my EA I m using the 2 custom indicators.

All is fine but if I launch the genetic Optimizer nothings happens Or I get an error OnInit()...How can I get more informations or trying to solve it?

Thanks in advance.

 
tradatore: How can I get more informations
Check your return codes, Print information, and find out why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
 


Thanks but I can not find anything wrong...no errors, single simulation is ok....it s liek the problem is on multithread....

In few words, i d like to have different timeframe of stochastic indicator in only 1 indicator

Here is my indicator, any idea?

Thanks


#property copyright "Copyright 2017,Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
//--- number of indicator buffers 3
#property indicator_buffers 6 
//--- three plots are used
#property indicator_plots   6
//+----------------------------------------------+
//|  declaring constants                         |
//+----------------------------------------------+
#define RESET 0                     // A constant for returning an indicator recalculation command back to the terminal
#define INDICATOR_NAME "Partial" // A constant for the indicator name
#define SIZE 5                      // A constant for the number of calls of the CountIndicator function in the code
#define PRINT_FREQ 0




//+----------------------------------------------+
//|  Indicator 1 drawing parameters              |
//+----------------------------------------------+
//--- drawing indicator 1 as a line
#property indicator_type1   DRAW_LINE
//--- the color of the indicator
#property indicator_color1  clrRed
//--- indicator 1 line width is equal to 2
#property indicator_width1  1
//--- displaying the indicator label
#property indicator_label1  "M1"
//+----------------------------------------------+
//|  Indicator 2 drawing parameters              |
//+----------------------------------------------+
//--- drawing indicator 2 as a line
#property indicator_type2   DRAW_LINE
//--- the color of the indicator
#property indicator_color2  clrOrange
//--- indicator 2 line width is equal to 3
#property indicator_width2  1
//--- displaying the indicator label
#property indicator_label2  "M5"
//+----------------------------------------------+
//|  Indicator 3 drawing parameters              |
//+----------------------------------------------+
//--- drawing indicator 3 as a line
#property indicator_type3  DRAW_LINE
//--- the color of the indicator
#property indicator_color3  clrGreenYellow
#property indicator_style3 STYLE_DASH
//---- indicator 3 line width is equal to 5
#property indicator_width3  2
//--- displaying the indicator label
#property indicator_label3  "M15"

//--- drawing indicator 3 as a line
#property indicator_type4   DRAW_LINE
//--- the color of the indicator
#property indicator_color4  clrGreen
//---- indicator 3 line width is equal to 5
#property indicator_style4 STYLE_DASH
#property indicator_width4  2
//--- displaying the indicator label
#property indicator_label4  "M30"


//--- drawing indicator 3 as a line
#property indicator_type5   DRAW_LINE
//--- the color of the indicator
#property indicator_color5 clrYellow
 #property indicator_style5 STYLE_DASH
 
//---- indicator 3 line width is equal to 5
#property indicator_width5  2
//--- displaying the indicator label
#property indicator_label5  "H1"




//+----------------------------------------------+
//| Parameters of displaying horizontal levels   |
//+----------------------------------------------+
#property indicator_level1 80.0
#property indicator_level2 50.0
#property indicator_level3 20.0
#property indicator_levelcolor clrMagenta
#property indicator_levelstyle STYLE_DASHDOTDOT
//+-------------------------------------+
//| Indicator input parameters          |
//+-------------------------------------+ 
 ENUM_TIMEFRAMES TimeFrame1=PERIOD_M1; // Indicator 1 chart period (smallest timeframe)
 ENUM_TIMEFRAMES TimeFrame2=PERIOD_M5; // Indicator 2 chart period (medium timeframe)
 ENUM_TIMEFRAMES TimeFrame3=PERIOD_M15; // Indicator 3 chart period (highest timeframe)
 ENUM_TIMEFRAMES TimeFrame4=PERIOD_M30; // Indicator 3 chart period (highest timeframe)
 ENUM_TIMEFRAMES TimeFrame5=PERIOD_H1; // Indicator 3 chart period (highest timeframe)

input int KPeriod=5;
int       DPeriod=3;
input int Slowing=3;
input ENUM_MA_METHOD MA_Method=MODE_SMA;
input ENUM_STO_PRICE Price_field=STO_LOWHIGH;
input int            Shift=0;                // Horizontal shift of the indicator in bars
//+-------------------------------------+
//--- declaration of dynamic arrays that
//--- will be used as indicator buffers

double IndM1Buffer[];
double IndM5Buffer[];
double IndM15Buffer[];
double IndM30Buffer[];
double IndM60Buffer[];



static int LastCountBar[SIZE];


//--- declaration of the integer variables for the start of data calculation
int min_rates_total=5;
//--- declaration of integer variables for the indicators handles
int Ind1_Handle,Ind2_Handle,Ind3_Handle,Ind4_Handle,Ind5_Handle;
//+------------------------------------------------------------------+
//|  Getting a timeframe as a line                                   |
//+------------------------------------------------------------------+
//string GetStringTimeframe(ENUM_TIMEFRAMES timeframe)
  //{return(StringSubstr(EnumToString(timeframe),7,-1));}
//+------------------------------------------------------------------+    
//| Custom indicator initialization function                         | 
//+------------------------------------------------------------------+  
int OnInit()
  {
//--- Checking correctness of the chart periods
   Print("Init");
//--- initialization of variables 
   //min_rates_total=5;
//--- getting the handle of Stochastic 1
   Ind1_Handle=iStochastic(Symbol(),TimeFrame1,KPeriod,DPeriod,Slowing,MA_Method,Price_field);
   if(Ind1_Handle==INVALID_HANDLE)
     {
      Print(" Failed to get the handle of Stochastic 1");
      return(INIT_FAILED);
     }
//--- getting handle of the MA 2 indicator
   Ind2_Handle=iStochastic(Symbol(),TimeFrame2,KPeriod,DPeriod,Slowing,MA_Method,Price_field);
   if(Ind2_Handle==INVALID_HANDLE)
     {
      Print(" Failed to get handle of Stochastic 2");
      return(INIT_FAILED);
     }
//--- getting handle of the MA 3 indicator
   Ind3_Handle=iStochastic(Symbol(),TimeFrame3,KPeriod,DPeriod,Slowing,MA_Method,Price_field);
   if(Ind3_Handle==INVALID_HANDLE)
     {
      Print(" Failed to get the handle of Stochastic 3");
      return(INIT_FAILED);
     }
//--- getting handle of the MA 3 indicator
   Ind4_Handle=iStochastic(Symbol(),TimeFrame4,KPeriod,DPeriod,Slowing,MA_Method,Price_field);
   if(Ind4_Handle==INVALID_HANDLE)
     {
      Print(" Failed to get the handle of Stochastic 3");
      return(INIT_FAILED);
     }
//--- getting handle of the MA 3 indicator
   Ind5_Handle=iStochastic(Symbol(),TimeFrame5,KPeriod,DPeriod,Slowing,MA_Method,Price_field);
   if(Ind5_Handle==INVALID_HANDLE)
     {
      Print(" Failed to get the handle of Stochastic 3");
      return(INIT_FAILED);
     }          
//--- initialize indicator buffers
  
   
   IndInit(0,IndM1Buffer,0.0,min_rates_total,Shift);
   IndInit(1,IndM5Buffer,0.0,min_rates_total,Shift);
   IndInit(2,IndM15Buffer,0.0,min_rates_total,Shift);
   IndInit(3,IndM30Buffer,0.0,min_rates_total,Shift);
   IndInit(4,IndM60Buffer,0.0,min_rates_total,Shift);
  
   
 
   
   
//---- Creating a name for displaying in a separate sub-window and in tooltip
  
   IndicatorSetString(INDICATOR_SHORTNAME,"OK");
//--- Determining the accuracy of displaying the indicator values
   IndicatorSetInteger(INDICATOR_DIGITS,0);
//--- initialization end
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+  
//| Custom iteration function                                        | 
//+------------------------------------------------------------------+  
int OnCalculate(const int rates_total,    // number of bars in history at the current tick
                const int prev_calculated,// amount of history in bars at the previous tick
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
  
  
//--- checking if the number of bars is enough for the calculation
   if(rates_total<min_rates_total) return(0);
   
   if(BarsCalculated(Ind1_Handle)<Bars(Symbol(),TimeFrame1)) return(prev_calculated);
   if(BarsCalculated(Ind2_Handle)<Bars(Symbol(),TimeFrame2)) return(prev_calculated);
   if(BarsCalculated(Ind3_Handle)<Bars(Symbol(),TimeFrame3)) return(prev_calculated);
   if(BarsCalculated(Ind4_Handle)<Bars(Symbol(),TimeFrame4)) return(prev_calculated);
   if(BarsCalculated(Ind5_Handle)<Bars(Symbol(),TimeFrame5)) return(prev_calculated);
//--- indexing elements in arrays as in timeseries  
   //ArraySetAsSeries(time,true);
//---
//if (rates_total!=prev_calculated)
//{
 
   CopyBuffer(Ind1_Handle,0,0,rates_total,IndM1Buffer);
   CopyBuffer(Ind2_Handle,0,0,rates_total,IndM5Buffer);
   CopyBuffer(Ind3_Handle,0,0,rates_total,IndM15Buffer);
   CopyBuffer(Ind4_Handle,0,0,rates_total,IndM30Buffer);
   CopyBuffer(Ind5_Handle,0,0,rates_total,IndM60Buffer);
 

// }  
  
        
        //--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Indicator buffer initialization                                  |
//+------------------------------------------------------------------+    
void IndInit(int Number,double &Buffer[],double Empty_Value,int Draw_Begin,int nShift)
  {
//--- Set dynamic array as an indicator buffer
   SetIndexBuffer(Number,Buffer,INDICATOR_DATA);
//--- shifting the start of drawing of the indicator
   PlotIndexSetInteger(Number,PLOT_DRAW_BEGIN,Draw_Begin);
//--- setting the indicator values that won't be visible on a chart
   PlotIndexSetDouble(Number,PLOT_EMPTY_VALUE,Draw_Begin);
//---- shifting the indicator 2 horizontally by Shift
   PlotIndexSetInteger(Number,PLOT_SHIFT,nShift);
//--- Indexing elements in the buffer as in timeseries
   ArraySetAsSeries(Buffer,true);
//---
  }
  

  

  
  

Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
tradatore: All is fine but if I launch the genetic Optimizer nothings happens Or I get an error OnInit()...How can I get more informations or trying to solve it?
You posted an indicator. There is nothing to optimize on an indicator. You can only optimize an EA that trades.
 
whroeder1:
You posted an indicator. There is nothing to optimize on an indicator. You can only optimize an EA that trades.


I know, I posted the indicator because it seems to be the problem...Launching a test with EA all is fine, but optimizing does not work...

if you want to try, this is the EA that uses the indicator. Obviously I am learning so i used pieces of codes of others. My goal was having an indicator that can give me  some indicators I need.

Thanks

//+------------------------------------------------------------------+



//|                                                      TestEA.mq5 |
//|                                Copyright 2017,Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

#property copyright "Copyright 2017,Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"


#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>  
#include <Tools\DateTime.mqh>
CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;                      // trading object
CSymbolInfo    m_symbol;                     // symbol info object
//--- inpur parameter
input double            hidden_TakeProfit    = 60;
input double            hidden_StopLoss      = 30;
input double            Lots                 = 0.1;            // Lots
input double            target_tp1           = 20;             // the first level of profit
input double            target_tp2           = 35;             // the second level of profit
input double            target_tp3           = 50;             // the third level of profit
ulong                   m_magic=12345; // magic number

int                     handle_PartialStock;//handle to average

double                  m_digits_adjust=0.0; // 
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit(){

      
     handle_PartialStock= iCustom(Symbol(),PERIOD_M1,"Partial");
     if(handle_PartialStock==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code 
      PrintFormat("Failed to create handle of the iStochastic indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early 
      return(INIT_FAILED);
     }

   m_symbol.Name(Symbol());                  // sets symbol name
   if(!RefreshRates())
     {
      Print("Error RefreshRates. Bid=",DoubleToString(m_symbol.Bid(),Digits()),
            ", Ask=",DoubleToString(m_symbol.Ask(),Digits()));
      return(INIT_FAILED);
     }
   m_symbol.Refresh();
//---
   m_trade.SetExpertMagicNumber(m_magic);    // sets magic number
//--- tuning for 3 or 5 digits
   int digits_adjust=1;
   if(m_symbol.Digits()==3 || m_symbol.Digits()==5)
      digits_adjust=10;
   m_digits_adjust = digits_adjust * m_symbol.Point();
//---
   if(hidden_TakeProfit*m_digits_adjust<10*m_digits_adjust)
     {
      Print("TakeProfit less than 10");
      return(INIT_FAILED);  // check TakeProfit
     }
     GetLastError();
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+ 
//| Expert deinitialization function                                 | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
  Print("DEINIT:"+GetLastError());
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
  
//---
   int total=0;
   for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of open positions
      if(m_position.SelectByIndex(i))
         if(m_position.Symbol()==Symbol() && m_position.Magic()==m_magic)
            total++;

   if(total<1)
     {
     
     double prevValM1= iStochasticGet(handle_PartialStock,1,1);//a 1M
     double currValM1= iStochasticGet(handle_PartialStock,1,0);//a 1M
     Print("ValCurr M1 "+currValM1);
     Print("prevValM1 M1 "+prevValM1);
     
     double prevValM5= iStochasticGet(handle_PartialStock,2,1);//a 5M
     double currValM5= iStochasticGet(handle_PartialStock,2,0);//a 5M
     
     double currValM15= iStochasticGet(handle_PartialStock,3,0);//a 5M
     double prevValM15= iStochasticGet(handle_PartialStock,3,1);//a 5M
     
     double ValM30= iStochasticGet(handle_PartialStock,4,0);//a 5M
     
     double ValM60= iStochasticGet(handle_PartialStock,5,0);//a 5M
     
     if(currValM1==0 ||currValM5==0)
      return;
     
     if (prevValM1>currValM1)
        {
       
            if(!RefreshRates())
               return;
            Print("Buy:preM1:"+prevValM1+" currM1"+currValM1+"-currM5"+currValM5+"-currM15"+currValM15+"-preM60"+ValM60);
            if(m_trade.Buy(Lots,Symbol(),m_symbol.Ask(),0.0,0.0,"Cash machine buy"))
              {
               Print("BUY opened : ",m_trade.ResultPrice());
              }
              
            else
              {
               Print("Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
                     ", description of result: ",m_trade.ResultRetcodeDescription(),
                     ", ticket of deal: ",m_trade.ResultDeal());
              }
            return;
           }
        
        
      if(prevValM1<currValM1)
        {
         
            if(!RefreshRates())
               return;
         Print("Sell prevValM1:"+prevValM1+"-currValM1:"+currValM1+"-currValM5"+currValM5+"-currValM15"+currValM15+"-prevValM60"+ValM60);
            if(m_trade.Sell(Lots,Symbol(),m_symbol.Bid(),0.0,0.0,"Cash machine sell"))
              {
               Print("Sell opened : ",m_trade.ResultPrice());
              }
            else
              {
               Print("Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),
                     ", description of result: ",m_trade.ResultRetcodeDescription(),
                     ", ticket of deal: ",m_trade.ResultDeal());
              }
            return;
           }
        
      return;
     }
//---

   for(int i=PositionsTotal()-1;i>=0;i--)
      if(m_position.SelectByIndex(i))
         if(m_position.Symbol()==Symbol() && m_position.Magic()==m_magic)
           {
            if(!RefreshRates())
               return;

            if(m_position.PositionType()==POSITION_TYPE_BUY) // ????  ??????? ??????? ???????
              {
               //--- ?????????? ???????? ??????? ??? ??????? ???????????? ??????, ??????? ????????? hidden_StopLoss

               if(m_symbol.Bid()<=(m_position.PriceOpen()-(hidden_StopLoss*m_digits_adjust)) ||
                  m_symbol.Bid()>=(m_position.PriceOpen()+(hidden_TakeProfit*m_digits_adjust)))
                 {
                  m_trade.PositionClose(m_position.Ticket());
                  return;
                 }
               //--- ??????? ???????? ???????? ??????????? ??????

               
                  if(m_symbol.Bid()>=m_position.PriceOpen()+(target_tp3*m_digits_adjust))
                    {
                   
                         m_trade.PositionModify(m_position.Ticket(),
                                            m_symbol.Bid() -(m_digits_adjust *(target_tp3-13)),
                                            m_symbol.Ask()+(m_digits_adjust*hidden_TakeProfit));
                                      
                     return;
                    }
               //--- ??????? ???????? ??????? ??????????? ??????

               if(m_symbol.Bid()>=m_position.PriceOpen()+(target_tp2*m_digits_adjust) && 
                  m_symbol.Bid()<m_position.PriceOpen()+(target_tp3*m_digits_adjust))
                 {
                  m_trade.PositionModify(m_position.Ticket(),
                                         m_symbol.Bid() -(m_digits_adjust *(target_tp2-13)),
                                         m_symbol.Ask()+(m_digits_adjust*hidden_TakeProfit));
                  return;
                 }
               //--- ??????? ???????? ??????? ??????????? ??????

               if(m_symbol.Bid()>=m_position.PriceOpen()+(target_tp1*m_digits_adjust) && 
                  m_symbol.Bid()<m_position.PriceOpen()+(target_tp3*m_digits_adjust) &&
                  m_symbol.Bid()<m_position.PriceOpen()+(target_tp2*m_digits_adjust))
                 {
                  m_trade.PositionModify(m_position.Ticket(),
                                         m_symbol.Bid() -(m_digits_adjust *(target_tp1-13)),
                                         m_symbol.Ask()+(m_digits_adjust*hidden_TakeProfit));
                  return;
                 }
              }

            if(m_position.PositionType()==POSITION_TYPE_SELL) // ????  ??????? ???????? ???????
              {
               //--- ?????????? ???????? ??????? ??? ??????? ???????????? ??????, ??????? ????????? hidden_StopLoss

               if(m_symbol.Ask()>=(m_position.PriceOpen()+(hidden_StopLoss*m_digits_adjust)) ||
                  m_symbol.Ask()<=(m_position.PriceOpen()-(hidden_TakeProfit*m_digits_adjust)))
                 {
                  m_trade.PositionClose(m_position.Ticket());
                  return;
                 }
               //--- ??????? ???????? ???????? ??????????? ??????       

               if(m_symbol.Ask()<=m_position.PriceOpen()-(target_tp3*m_digits_adjust))
                 {
                 
                   m_trade.PositionModify(m_position.Ticket(),
                                         m_symbol.Ask()+(m_digits_adjust *(target_tp3+13)),
                                         m_symbol.Bid() -(m_digits_adjust*hidden_TakeProfit));
                    
                   
                  return;
                 }
               //--- ??????? ???????? ??????? ??????????? ??????

               if(m_symbol.Ask()<=m_position.PriceOpen()-(target_tp2*m_digits_adjust) && 
                  m_symbol.Ask()>m_position.PriceOpen()-(target_tp3*m_digits_adjust))
                 {
                  m_trade.PositionModify(m_position.Ticket(),
                                         m_symbol.Ask()+(m_digits_adjust *(target_tp2+13)),
                                         m_symbol.Bid() -(m_digits_adjust*hidden_TakeProfit));
                  return;
                 }
               //--- ??????? ???????? ??????? ??????????? ??????

               if(m_symbol.Ask()<=m_position.PriceOpen()-(target_tp1*m_digits_adjust) && 
                  m_symbol.Ask()>m_position.PriceOpen()-(target_tp2*m_digits_adjust) &&
                  m_symbol.Ask()>m_position.PriceOpen()-(target_tp3*m_digits_adjust))
                 {
                  m_trade.PositionModify(m_position.Ticket(),
                                         m_symbol.Ask()+(m_digits_adjust *(target_tp1+13)),
                                         m_symbol.Bid() -(m_digits_adjust*hidden_TakeProfit));
                  return;
                 }
              }
           }
         
           

   return;
  }
  


//+------------------------------------------------------------------+
//| Get value of buffers for the iStochastic                         |
//|  the buffer numbers are the following:                           |
//|   0 - MAIN_LINE, 1 - SIGNAL_LINE                                 |
//+------------------------------------------------------------------+
double iStochasticGet(int pointer,const int buffer,const int index)
  {
   double Stochastic[1];
//--- reset error code 
   ResetLastError();
//--- fill a part of the iStochasticBuffer array with values from the indicator buffer that has 0 index 
   if(CopyBuffer(pointer,buffer,index,1,Stochastic)<0)
     {
      //--- if the copying fails, tell the error code 
      PrintFormat("Failed to copy data from the iStochastic indicator, error code %d",GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated 
      return(0.0);
     }
   return(Stochastic[0]);
  }

  
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data                                 |
//+------------------------------------------------------------------+
bool RefreshRates()
  {
//--- refresh rates
   if(!m_symbol.RefreshRates())
      return(false);
//--- protection against the return value of "zero"
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
      return(false);
//---
   return(true);
  }
//+------------------------------------------------------------------+ 
Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions