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());}
################################# 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());}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
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
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