Problem of parameters definition in OnCalculate() part of code

 

Hello,

I would like to know ow is it possible to define values of indicator on the OnCalculate() initialisation parameters of my EA.

In fact I have to use the Fastval and SlowVal (or Main and Signal) of the MACD indicator for calculate some values in a loop For.

For example: (sorry for the format but MQL5 is not on the same computer and it has not internet on)

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

//| Expert onCalculate 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 &TickVolume[],

                const long &Volume[],

                const int &Spread[])

  {

//--- we can copy not all data

   int to_copy;

   if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;

   else

     {

      to_copy=rates_total-prev_calculated;

      if(prev_calculated>0) to_copy++;

     }

//--- get Fast EMA buffer

   if(CopyBuffer(FastHandle,0,0,to_copy,FastVal)<=0)

     {

      Print("Getting fast EMA is failed! Error",GetLastError());

      return(0);

     }

//--- get SlowSMA buffer

   if(CopyBuffer(SlowHandle,0,0,to_copy,SlowVal)<=0)

     {

      Print("Getting slow SMA is failed! Error",GetLastError());

      return(0);

     }

   if(CopyBuffer(MainHandle,0,0,to_copy,MainVal)<=0)

     {

      Alert("Getting MainVal is failed! Error",GetLastError());

      return(0);

     }

   if(CopyBuffer(SignalBisHandle,0,0,to_copy,SignalVal)<=0)

     {

      Alert("Getting SignalVal is failed! Error",GetLastError());

      return(0);

     }

 

//---

   int limit;

   if(prev_calculated==0)

      limit=0;

   else limit=prev_calculated-1;

//--- calculate MACD

   for(int l=limit;l<rates_total;l++)

      MainVal[l]=FastVal[l]-SlowVal[l];

//--- calculate Signal

   SimpleMAOnBuffer(rates_total,prev_calculated,0,PeriodSignal,MainVal,SignalVal);

  

//--- Croisement MACD, stockage des valeurs

   for(int i=limit;i<rates_total;i++)  

      {

      double MACDP=0.0;

      double CoursMACDP=0.0;

      datetime DateMACDP=Time[i];

      double MACDN=0.0;

      double CoursMACDN=0.0;

      datetime DateMACDN=Time[i];

     

      if (FastVal[i] < FastVal[i-1] && FastVal[i]<SlowVal[i])

         {

         MACDP = FastVal[i];

         CoursMACDP = Low[i];

         DateMACDP = Time[i];

         }

      MACDPos[i] = MACDP;

      CoursMACDPos[i] = CoursMACDP;

      DateMACDPos[i] = DateMACDP;

      }        

 

How can I do???....

 

Thank you 

 

PS: Is it posssible to do a loop For on the OnTick() part?? 

 

Documentation on MQL5: Language Basics / Functions / Event Handling Functions
  • www.mql5.com
Language Basics / Functions / Event Handling Functions - Documentation on MQL5
 

Please, properly insert code.

 

Antoine:


... 

(sorry for the format but MQL5 is not on the same computer and it has not internet on)

...


 But it is not dependant on the computer :) just use "SRC" button when posting and paste your code there.

 

 

 
//+------------------------------------------------------------------+
//| Expert onCalculate 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 &TickVolume[],
                const long &Volume[],
                const int &Spread[])
  {
//--- we can copy not all data
   int to_copy;
   if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      if(prev_calculated>0) to_copy++;
     }
//--- get Fast EMA buffer
   if(CopyBuffer(FastHandle,0,0,to_copy,FastVal)<=0)
     {
      Print("Getting fast EMA is failed! Error",GetLastError());
      return(0);
     }
//--- get SlowSMA buffer
   if(CopyBuffer(SlowHandle,0,0,to_copy,SlowVal)<=0)
     {
      Print("Getting slow SMA is failed! Error",GetLastError());
      return(0);
     }
   if(CopyBuffer(MainHandle,0,0,to_copy,MainVal)<=0)
     {
      Alert("Getting MainVal is failed! Error",GetLastError());
      return(0);
     }
   if(CopyBuffer(SignalBisHandle,0,0,to_copy,SignalVal)<=0)
     {
      Alert("Getting SignalVal is failed! Error",GetLastError());
      return(0);
     }

//---
   int limit;
   if(prev_calculated==0)
      limit=0;
   else limit=prev_calculated-1;
//--- calculate MACD
   for(int l=limit;l<rates_total;l++)
      MainVal[l]=FastVal[l]-SlowVal[l];
//--- calculate Signal
   SimpleMAOnBuffer(rates_total,prev_calculated,0,PeriodSignal,MainVal,SignalVal);
  
//--- Croisement MACD, stockage des valeurs
   for(int i=limit;i<rates_total;i++)  
      {
      double MACDP=0.0;
      double CoursMACDP=0.0;
      datetime DateMACDP=Time[i];
      double MACDN=0.0;
      double CoursMACDN=0.0;
      datetime DateMACDN=Time[i];
      
      if (FastVal[i] < FastVal[i-1] && FastVal[i]<SlowVal[i])
         {
         MACDP = FastVal[i];
         CoursMACDP = Low[i];
         DateMACDP = Time[i];
         }
      MACDPos[i] = MACDP;
      CoursMACDPos[i] = CoursMACDP;
      DateMACDPos[i] = DateMACDP;
      }        

Reason: