Discussion of article "How to call indicators in MQL5" - page 2

 

To me it doesn-t let me compile the file.

I don-t know if there is any idea what id going on.

Thanks,

 

Jordi 

 
Thank you for huge work!
 

Thanks!

Below is a simple example of this that I put together while playing around.

If it's wrong please correct me and I'll amend it.

What I do find strange is the values don't seem to match up to a simple moving average on the chart.

################### In a script

   int period = 21;        // The 21 bar moving average
   int sampleSize = 100;   //This is the number of bars of data you want to fetch
   int handle = iCustom(Symbol(),0,"Examples\\Custom Moving Average",
                          period,          // Period
                          0,          // Offset
                          MODE_SMA,   // Calculation method
                          PRICE_CLOSE // Calculating on Close prices
                 );

 

void OnTick()
{

   // This is the array where you moving averages will get copied into.
   // Each item in here is a moving average value
   // The first item in this array (item at position 0) is the most recent
   // The last item is the last item in the array ArraySize() - 1
   double movingAverageValues[];   
  
   ArraySetAsSeries(movingAverageValues, true);
   if (CopyBuffer(handle,0,0,sampleSize,movingAverageValues) < 0){Print("CopyBufferMA1 error =",GetLastError());}
  
   double currentMovingAverage = movingAverageValues[0];

   double earliestMovingAverage = movingAverageValues[ArraySize(movingAverageValues) - 1];


################################# In an EA (NOTE: the OnInit and OnTick)

  int OnInit(){

   int period = 21;        // The 21 bar moving average

   int sampleSize = 100;   //This is the number of bars of data you want to fetch
   int handle = iCustom(Symbol(),0,"Examples\\Custom Moving Average",
                          period,          // Period
                          0,          // Offset
                          MODE_SMA,   // Calculation method
                          PRICE_CLOSE // Calculating on Close prices
                 );

   }

OnTick()
{

   // This is the array where you moving averages will get copied into.
   // Each item in here is a moving average value
   // The first item in this array (item at position 0) is the most recent
   // The last item is the last item in the array ArraySize() - 1
   double movingAverageValues[];   
  
   ArraySetAsSeries(movingAverageValues, true);
   if (CopyBuffer(handle,0,0,sampleSize,movingAverageValues) < 0){Print("CopyBufferMA1 error =",GetLastError());}
  
   double currentMovingAverage = movingAverageValues[0];

   double earliestMovingAverage = movingAverageValues[ArraySize(movingAverageValues) - 1];

}

 
Great article
 
yeah... it's easier to invent your own indicator and build it into the robot code than to code these handles every time... tedium