Download MetaTrader 5
To add comments, please log in or register
Interested in the Market activity? Come and track!
Antoine
92
Antoine 2010.11.19 13:06 

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
Slawa
Moderator
6798
Slawa 2010.11.19 14:18  

Please, properly insert code.

Pawel Wojnarowski
685
Pawel Wojnarowski 2010.11.19 14:28  

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.

 

 

Antoine
92
Antoine 2010.11.19 15:23  
//+------------------------------------------------------------------+
//| 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;
      }        

/
To add comments, please log in or register