Download MetaTrader 5

platform mt4 "crash" after I put my code ?? Oo

To add comments, please log in or register
Publish your article and thousands of people all over the world will read it!
musetti
51
musetti 2014.03.08 22:37 

Hello, I have some problem with mt4, I tried to make a little code wich calculate the High average for "period" Bars but after compiling, the mt4 of my broker crashed. Please can you try, the problem is it my computer, I don't understand, have you got the same problem ? thanks


//+------------------------------------------------------------------+
//|                                      moyenne mobile projecty.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_chart_window Indigo
double mmh[];
double sum;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
      SetIndexBuffer(0,mmh);
      SetIndexStyle(0,DRAW_LINE); 
   

   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[])
  {
      IndicatorBuffers(1);
      int countedbar = IndicatorCounted();
      int i = Bars - countedbar - 1;
      int period = 20;
      
      while(i>=0)
        { 
          for(int k = 0 ; k <=period-1 ; k ++)
            {
               sum += sum + High[i];
            }
         mmh[i] = sum/period;
        }
        
       return(rates_total);
  }
//+------------------------------------------------------------------+
Alain Verleyen
Moderator
30734
Alain Verleyen 2014.03.08 23:37  
abricot91:

Hello, I have some problem with mt4, I tried to make a little code wich calculate the High average for "period" Bars but after compiling, the mt4 of my broker crashed. Please can you try, the problem is it my computer, I don't understand, have you got the same problem ? thanks


Of course it's not your computer, it's your code. You have an infinite loop
      while(i>=0)
        { 
          for(int k = 0 ; k <=period-1 ; k ++)
            {
               sum += sum + High[i];
            }
         mmh[i] = sum/period;
        }
Where is variable i decreased ?
Keith Watford
Moderator
9627
Keith Watford 2014.03.08 23:51  
      //IndicatorBuffers(1);    You Have already set the buffer
      int countedbar = IndicatorCounted();
      int i = Bars - countedbar - 1;
      int period = 20;
      if(i>Bars - period)
         i = Bars - period -1;
      
      while(i>=0)
        { 
          sum = 0;
          for(int k = 0 ; k < period ; k ++)
            {
               sum += High[i + k];
            }
         mmh[i] = sum/period;
         i--
        }
        
       return(rates_total);

Not compiled or tested

sum was not reset to zero and why were you adding it to itself?

I'm not sure about the IndicatorBuffers(1)

Shouldn't that be in init? and is it necessary as you already have the buffer.

musetti
51
musetti 2014.03.09 01:24  

ok thanks moderator for helping :) now the correct code for high average of period


#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Indigo
double mmh[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
     
      SetIndexBuffer(0,mmh);
      SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2); 
   

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
     
      int countedbar = IndicatorCounted();
      int i = Bars - countedbar - 1;
      int period = 20;
      double sum;
      
      while(i>=0)
        { 
          sum = 0;
          
          for(int k = i ; k<= i +period -1 ; k ++)
            {
               
               sum = sum + High[k];
            }
         mmh[i] = sum/period;
         i--;
        
       }
        
        
        
        
       return(0);
  }
  
//+------------------------------------------------------------------+
Keith Watford
Moderator
9627
Keith Watford 2014.03.09 01:57  
abricot91:

ok thanks moderator for helping :) now the correct code for high average of period


Did you note in my earlier post that I checked the value of i against bars ?

      if(i>Bars - period)
         i = Bars - period -1;

What happens in your code when the indicator is first initialised

int countedbar = IndicatorCounted();
      int i = Bars - countedbar - 1;     //Eg Bars = 1000 and counted bars = 0     i= 999
      int period = 20;
      double sum;
      
      while(i>=0)
        { 
          sum = 0;
          
          for(int k = i ; k<= i +period -1 ; k ++)   //On the 2nd pass k=i+1 , k=999+1 , 1000 , High[1000] does not exist
            {                                        //So array out of range
               
               sum = sum + High[k];
            }
         mmh[i] = sum/period;
         i--;
        
       }
musetti
51
musetti 2014.03.09 13:21  

ok thanks gumrai, I note your precision about


if(i>Bars - perdiod)
  {
    i = Bars - period - 1;
  }


whroeder1
14504
whroeder1 2014.03.09 13:32  
  1. Now that you got it working
          while(i>=0)
            { 
              sum = 0;
              for(int k = i ; k<= i +period -1 ; k ++)
                {
                   sum = sum + High[k];
                }
             mmh[i] = sum/period;
             i--;
           }
    
    You can simplify it
          while(i>=0)
            { 
             mmh[i] = iMA(NULL,0, period ,0, MODE_SMA, PRICE_HIGH, i);
             i--;
           }
    

  2. This doesn't handle the look back of period.
    int countedbar = IndicatorCounted();
    int i = Bars - countedbar - 1; //Eg Bars = 1000 and counted bars = 0 i= 999
    int period = 20;
    Don't look past the end
    int period = 20;
    int countedbar = IndicatorCounted();
    if( countedbar < period) countedbar = period;
    for(int i = Bars - countedbar - 1; i >= 0; i--)
       mmh[i] = iMA(NULL,0, period ,0, MODE_SMA, PRICE_HIGH, i);

Keith Watford
Moderator
9627
Keith Watford 2014.03.09 13:44  

WHRoeder

Didn't I already explain your point 2 a few posts back?

whroeder1
14504
whroeder1 2014.03.09 16:29  
Yes you did
      if(i>Bars - period)
         i = Bars - period -1;
I just showed my version
if( countedbar < period) countedbar = period;
Daniel Petrovai
440
Daniel Petrovai 2014.03.10 00:42  
GumRai:

WHRoeder

Didn't I already explain your point 2 a few posts back?


Hi GumRai,

Looking at the original (first post on the top), since that is the new format we need to get used to, is there a point in using

IndicatorCounted();
High[];
Bars;

if we already have available :

rates_total 
high[]
prev_calculated

?

Keith Watford
Moderator
9627
Keith Watford 2014.03.10 06:45  
thrdel:


Hi GumRai,

Looking at the original (first post on the top), since that is the new format we need to get used to, is there a point in using

if we already have available :

?


Well, I was just adjusting the OP's code, apart from that old habits die hard :)

To be honest, I've only just learnt how to use the new arrays. Before I was trying &high[index] and I now know that they need to be set as series if one wants to use them in the way that we are used to.

There seems so much new stuff that it will take some time to change. I do try to use the SymbolInfo stuff instead of Market info as that looks pretty good.

12
To add comments, please log in or register