Problem in EA - No memory in OnTick function

To add comments, please log in or register
Paulo Ricardo Marin
129
Paulo Ricardo Marin  
Dear,
My EA has problems with optimizations. It locks the computer and also by Google Coud. I thought that the MT5 self, but I realized that it only happens with my EA.

After that, I realized that the out-of-memory error is due to array storage.

Can someone explain or help me?
About the OnInit source code
int OnInit()
  {
   Comment("");
   if(!LicenseValidation())
      return(INIT_FAILED);
   sy.Name(Symbol());
   sy.Refresh();
   string pref=(MQLInfoInteger(MQL_TESTER)) ? "test" : "";
   expname=pref+" "+MQLInfoString(MQL_PROGRAM_NAME);
   compos=expname+" ["+IntegerToString(Magic)+"]";
   rulang=(StringFind(term.Language(),"Russian",0)>-1) ? true : false;
   isinit=true;
   test=(MQLInfoInteger(MQL_TESTER) || MQLInfoInteger(MQL_OPTIMIZATION));
   ch.Attach();
   EventSetTimer(1);

   trd.SetDeviationInPoints(Slipage);
   trd.SetExpertMagicNumber(Magic);
   trd.SetMarginMode();
   if(IsFillingTypeAllowed(SYMBOL_FILLING_FOK,sy.TradeFillFlags()))
      trd.SetTypeFilling(ORDER_FILLING_FOK);
   else if(IsFillingTypeAllowed(SYMBOL_FILLING_IOC,sy.TradeFillFlags()))
      trd.SetTypeFilling(ORDER_FILLING_IOC);
   else
      trd.SetTypeFilling(ORDER_FILLING_RETURN);

   nameB.Shutdown();
   for(int i=0; i<2; i++)
      nameB.Add(NULL);
   sig.Shutdown();
   for(int i=0; i<2; i++)
      sig.Add(false);
   nbar.SetSymbolPeriod(sy.Name(),WorkTF);
   if(ShowInd && PeriodSeconds(PERIOD_CURRENT)!=PeriodSeconds(WorkTF))
      ch.SetSymbolPeriod(sy.Name(),WorkTF);

   MqlParam    par[];
   int   c=0;  ArrayResize(par,c+1);   par[c].type=TYPE_STRING;   par[c].string_value="::Indicators\\IndicatorSignal.ex5";
   c++;        ArrayResize(par,c+1);   par[c].type=TYPE_INT;      par[c].integer_value=STSssp;
   c++;        ArrayResize(par,c+1);   par[c].type=TYPE_INT;      par[c].integer_value=STSrisk;
   c++;        ArrayResize(par,c+1);   par[c].type=TYPE_INT;      par[c].integer_value=STSbar;
   c++;        ArrayResize(par,c+1);   par[c].type=TYPE_COLOR;    par[c].integer_value=STScolorB;
   c++;        ArrayResize(par,c+1);   par[c].type=TYPE_COLOR;    par[c].integer_value=STScolorS;
   c++;        ArrayResize(par,c+1);   par[c].type=TYPE_INT;      par[c].integer_value=STSwidth;
   cus.NumBuffers(3);
   cus.Create(Symbol(),PERIOD_CURRENT,IND_CUSTOM,c+1,par);
   cus.Refresh();
   if(ShowInd)
      cus.AddToChart(0,0);
   TesterHideIndicators(false);

   SBE   sbe[5];
   sbe[0].start=BEstart1;   sbe[0].distance=BEdistance1;
   sbe[1].start=BEstart2;   sbe[1].distance=BEdistance2;
   sbe[2].start=BEstart3;   sbe[2].distance=BEdistance3;
   sbe[3].start=BEstart4;   sbe[3].distance=BEdistance4;
   sbe[4].start=BEstart5;   sbe[4].distance=BEdistance5;
   c=-1;
   for(int i=0; i<ArrayRange(sbe,0); i++)
     {
      if(sbe[i].start==NULL)
         continue;
      c++;
      ArrayResize(be,c+1);
      be[c].start=sbe[i].start;
      be[c].distance=sbe[i].distance;
     }

   Info();
   return(INIT_SUCCEEDED);
  }
If I need to add all the code, I'll add, but from what I noticed the array functions are there. Is there anything that can be improved to not crash? I've heard of zeroing in memory.

Paulo Ricardo Marin
129
Paulo Ricardo Marin  

Code from signal indicator

input    int      STSssp      =9;            //SSP
input    int      STSrisk     =3;            //Risk
input    E_BAR    STSbar      =BAR_CLOSED;   //Signal Bar
sinput   color    STScolorB   =clrLimeGreen; //Color Buy 
sinput   color    STScolorS   =clrOrangeRed; //Color Sell 
sinput   uint     STSwidth    =2;            //Width 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double   Buy[],Sell[];
double   Trend[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   Comment("");
   if(!LicenseValidation())
      return(INIT_FAILED);

   int cnt=0;  SetIndexBuffer(cnt,Buy,INDICATOR_DATA);      ArraySetAsSeries(Buy,true);
   cnt++;      SetIndexBuffer(cnt,Sell,INDICATOR_DATA);     ArraySetAsSeries(Sell,true);
   cnt++;      SetIndexBuffer(cnt,Trend,INDICATOR_DATA);    ArraySetAsSeries(Trend,true);

   cnt=0;
   PlotIndexSetString(cnt,PLOT_LABEL,"Buy");
   PlotIndexSetDouble(cnt,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   PlotIndexSetInteger(cnt,PLOT_DRAW_TYPE,(STScolorB==clrNONE) ?DRAW_NONE :DRAW_ARROW);
   PlotIndexSetInteger(cnt,PLOT_LINE_COLOR,STScolorB);
   PlotIndexSetInteger(cnt,PLOT_LINE_WIDTH,STSwidth);
   PlotIndexSetInteger(cnt,PLOT_ARROW,108);
   cnt++;
   PlotIndexSetString(cnt,PLOT_LABEL,"Sell");
   PlotIndexSetDouble(cnt,PLOT_EMPTY_VALUE,EMPTY_VALUE);
   PlotIndexSetInteger(cnt,PLOT_DRAW_TYPE,(STScolorS==clrNONE) ?DRAW_NONE :DRAW_ARROW);
   PlotIndexSetInteger(cnt,PLOT_LINE_COLOR,STScolorS);
   PlotIndexSetInteger(cnt,PLOT_LINE_WIDTH,STSwidth);
   PlotIndexSetInteger(cnt,PLOT_ARROW,108);

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());
   IndicatorSetString(INDICATOR_SHORTNAME,MQLInfoString(MQL_PROGRAM_NAME)+" ("+IntegerToString(STSssp)+"; "+IntegerToString(STSrisk)+")");

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                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[])
  {
   ArraySetAsSeries(high,true);
   ArraySetAsSeries(low,true);
   ArraySetAsSeries(close,true);
   
   static int  k;

   int limit=rates_total-prev_calculated;
   if(prev_calculated==0)
     {
      for(int i=0; i<rates_total; i++)
        {
         Buy[i]=EMPTY_VALUE;
         Sell[i]=EMPTY_VALUE;
         Trend[i]=WRONG_VALUE;
        }
      k=33-STSrisk;
      limit=(rates_total-1)-k;
     }

   for(int i=limit; i>=STSbar; i--)
     {
      double   rang=AvgRange(i,STSssp,high,low);
      double   max=low[i];
      double   min=close[i];
      GetMaxMin(i,STSssp,max,min,high,low);
      double   smin=min+(max-min)*k/100;
      double   smax=max-(max-min)*k/100;
      Trend[i]=(close[i]>smax) ?0.0 :((close[i]<smin) ?1.0 :Trend[i+1]);
      Buy[i]=(Trend[i]==0.0 && Trend[i+1]!=0.0) ?(low[i]-rang*0.5) :EMPTY_VALUE;
      Sell[i]=(Trend[i]==1.0 && Trend[i+1]!=1.0) ?(high[i]+rang*0.5) :EMPTY_VALUE;
     }

   return(rates_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void GetMaxMin(const int vStart,const int vCnt,double &vMax,double &vMin,const double &vHi[],const double &vLo[])
  {
   double   res=NULL;
   for(int i=0; i<vCnt; i++)
     {
      res=vHi[vStart+i];
      if(vMax<res)
         vMax=res;
      res=vLo[vStart+i];
      if(vMin>=res)
         vMin=res;
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double AvgRange(const int vStart,const int vCnt,const double &vHi[],const double &vLo[])
  {
   double   avg=NULL;
   for(int i=0; i<=vCnt; i++)
      avg+=(vHi[vStart+i]-vLo[vStart+i]);
   return(avg/(vCnt+1));
  }
Paulo Ricardo Marin
129
Paulo Ricardo Marin  
I discovered it is the SSP and RISK parameter of the SilverTrend flag. If I optimize with values ​​above 15 it locks up, it uses a lot of memory. I know it uses to calculate and should store in an array. Does anyone know how to fix it?
To add comments, please log in or register