🚀 AI One-Click Generation of mql4 Strategy Code

Mql4 Guide: Mastering ArrayCopySeries(), ArrayCompare(), and ArrayFill()

mql4 | Published on: 2025/12/11
The following content is generated by EasyQuant .

Problem Description

How do I efficiently copy price series data from one timeframe to another, compare two arrays, or quickly initialize a large array with a specific value?

Solution

Here is a comprehensive guide and MQL4 code examples for managing arrays efficiently, covering copying price series, comparing arrays, and initializing data.

1. Copying Price Series Data (ArrayCopySeries)

To copy market data (Open, High, Low, Close, Time, Volume) from a different symbol or timeframe into an array, use the ArrayCopySeries function.

Key Feature: This function handles the memory allocation for the destination array automatically. However, you must handle the specific error code 4066 (ERR_HISTORY_WILL_UPDATED), which occurs if the requested history data is not yet loaded in the terminal.

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   // --- 1. Copying Price Series Data ---
   string symbol = "EURUSD";
   int timeframe = PERIOD_H1;
   
   // Destination array for Close prices
   double close_prices[];
   
   // Attempt to copy the Close price series
   // MODE_CLOSE, MODE_OPEN, MODE_HIGH, MODE_LOW, MODE_TIME, MODE_VOLUME
   int copied_count = ArrayCopySeries(close_prices, MODE_CLOSE, symbol, timeframe);
   
   // Check for history loading error
   if(GetLastError() == ERR_HISTORY_WILL_UPDATED)
   {
      Print("History is updating, please try again later.");
   }
   else if(copied_count > 0)
   {
      // Accessing the data
      // Note: ArrayCopySeries creates a series array (index 0 is the newest)
      Print("Successfully copied ", copied_count, " bars.");
      Print("Current H1 Close: ", close_prices[0]);
      Print("Previous H1 Close: ", close_prices[1]);
   }
   else
   {
      Print("Failed to copy series. Error: ", GetLastError());
   }
}

2. Comparing Two Arrays (ArrayCompare)

To compare two arrays of the same type to see if they are identical, or to determine which one is "greater" (lexicographically), use ArrayCompare.

Return Values:

  • 0: Arrays are identical.
  • 1: First array is greater.
  • -1: First array is smaller.
  • -2: Error (type mismatch).
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   // --- 2. Comparing Arrays ---
   double array_A[] = {1.1, 1.2, 1.3, 1.4, 1.5};
   double array_B[] = {1.1, 1.2, 1.3, 1.4, 1.5};
   double array_C[] = {1.1, 1.2, 1.0, 1.4, 1.5}; // 3rd element is smaller
   
   // Compare A and B (Identical)
   int res_AB = ArrayCompare(array_A, array_B);
   
   // Compare A and C
   int res_AC = ArrayCompare(array_A, array_C);
   
   // Output results
   string result_text = "";
   
   if(res_AB == 0) result_text = "Identical";
   else result_text = "Different";
   Print("Comparison A vs B: ", result_text); // Output: Identical
   
   if(res_AC == 1) result_text = "Array A is greater";
   else if(res_AC == -1) result_text = "Array A is smaller";
   else result_text = "Identical";
   Print("Comparison A vs C: ", result_text); // Output: Array A is greater
}

3. Efficient Array Initialization (ArrayInitialize vs ArrayFill)

There are two ways to set values in an array quickly without using a for loop.

  • ArrayInitialize: Sets all elements of an array to a specific value. This is ideal for resetting buffers or clearing data.
  • ArrayFill: Fills a specific range of an array with a value. This is useful if you only want to modify a portion of the array.
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   // --- 3. Initializing Arrays ---
   
   // Define a large array
   double data_buffer[10];
   
   // Method A: ArrayInitialize (Sets the WHOLE array)
   // Useful for resetting buffers to 0 or EMPTY_VALUE
   ArrayInitialize(data_buffer, 0.0);
   
   string dump = "After Initialize: ";
   for(int i=0; i<10; i++) dump += DoubleToString(data_buffer[i], 1) + " ";
   Print(dump); 
   // Output: 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 
   
   // Method B: ArrayFill (Sets a RANGE)
   // Syntax: ArrayFill(array, start_index, count, value)
   // Let's fill the middle 4 elements with 9.9
   ArrayFill(data_buffer, 3, 4, 9.9);
   
   dump = "After ArrayFill:  ";
   for(int i=0; i<10; i++) dump += DoubleToString(data_buffer[i], 1) + " ";
   Print(dump);
   // Output: 0.0 0.0 0.0 9.9 9.9 9.9 9.9 0.0 0.0 0.0 
}

Q&A: MQL4 Array Management

Q: Why does ArrayCopySeries sometimes return 0 or an error?
A: If ArrayCopySeries returns 0 or sets error 4066, it usually means the requested timeframe data is not yet synchronized in the terminal. You must handle this by checking GetLastError() and retrying the operation on the next tick.

Q: Can ArrayCompare be used on arrays of structures?
A: Yes, but only if the structures contain simple types (int, double, bool, etc.). It cannot compare structures that contain strings, dynamic arrays, or class objects.

Q: What is the difference between ArrayResize and ArrayInitialize?
A: ArrayResize changes the number of elements in a dynamic array. ArrayInitialize sets the value of existing elements. Often, you will call ArrayResize first to set the size, and then ArrayInitialize to set the default values.

Q: Does ArrayCopySeries work with custom indicators?
A: No, ArrayCopySeries is strictly for standard price data (OHLCV). To get data from custom indicators, you must use iCustom. To copy data between arrays generally, use ArrayCopy.