- 1 1. What Is ArraySetAsSeries? (A Function That Changes Array Direction in MQL5)
- 2 2. ArraySetAsSeries Syntax and Usage (Basic Specification)
- 3 3. Relationship Between CopyBuffer and ArraySetAsSeries (The Most Important Point in EA Development)
- 3.1 3.1 What Is CopyBuffer? (A Quick Review)
- 3.2 3.2 Order of Arrays Obtained by CopyBuffer
- 3.3 3.3 Correct Combination of CopyBuffer and ArraySetAsSeries
- 3.4 3.4 Example of EA Trading Logic
- 3.5 3.5 Common Bug (Very Frequent)
- 3.6 3.6 Recommended Template When Using CopyBuffer
- 3.7 Common Stumbling Blocks (CopyBuffer Related)
- 4 4. Arrays Where ArraySetAsSeries Cannot Be Used and Related Limitations
- 5 5. Practical Code Using ArraySetAsSeries (EA Development Examples)
- 6 6. Frequently Asked Questions About ArraySetAsSeries
- 6.1 6.1 Does ArraySetAsSeries Rearrange the Contents of an Array?
- 6.2 6.2 If I Use CopyBuffer, Should I Write ArraySetAsSeries Every Time?
- 6.3 6.3 Do Time[] and Close[] Also Need ArraySetAsSeries?
- 6.4 6.4 Can ArraySetAsSeries Prevent Bugs?
- 6.5 6.5 What Rule Should Beginners Follow?
- 6.6 Common Stumbling Blocks, Cautions, and Frequent Mistakes
- 7 7. Best Practices for Using ArraySetAsSeries Safely
- 7.1 7.1 Always Use It Together with CopyBuffer
- 7.2 7.2 Standardize the Array Direction Rule Inside the EA
- 7.3 7.3 Always Check the Return Value of CopyBuffer
- 7.4 7.4 Do Not Retrieve More Data Than Necessary
- 7.5 7.5 Be Aware of the Latest Bar and Confirmed Bars
- 7.6 Common Stumbling Blocks (Frequent Practical Mistakes)
- 7.7 Minimum Template Recommended in Practice
- 8 8. ArraySetAsSeries FAQ
- 8.1 Q1. What does ArraySetAsSeries do?
- 8.2 Q2. Does ArraySetAsSeries reorder the array data?
- 8.3 Q3. Is ArraySetAsSeries necessary when using CopyBuffer?
- 8.4 Q4. Do Time[] and Close[] need ArraySetAsSeries?
- 8.5 Q5. Can ArraySetAsSeries be used with any array?
- 8.6 Q6. Where should ArraySetAsSeries be called?
- 8.7 Q7. Is buffer[0] a confirmed bar?
- 8.8 Q8. What happens if I do not use ArraySetAsSeries?
1. What Is ArraySetAsSeries? (A Function That Changes Array Direction in MQL5)
ArraySetAsSeries is a function in MQL5 that sets whether an array should be treated as time-series data.
In simple terms, it is a setting that matches the meaning of an array index with the time order used on a chart.
In MQL5, price data and indicator values are handled as time-ordered data (time-series data).
However, arrays in ordinary programming languages usually store data from the beginning in the order old data → new data.
ArraySetAsSeries is used to adjust this difference.
It is almost always used in processing such as the following:
- Getting indicator values
- Getting data with the CopyBuffer function
- Signal checks in an EA (automated trading program)
- Getting data for backtesting
If you do not understand this function, it becomes easy to create bugs that reference the wrong indicator value.
1.1 Basic Concept of ArraySetAsSeries
MQL5 chart data has a structure where the newest bar comes first.
For example, consider the following price data.
| Bar | Meaning |
|---|---|
| 0 | Latest candlestick |
| 1 | One bar ago |
| 2 | Two bars ago |
In other words, MQL5 handles data with the following rule.
index 0 = latest bar
index 1 = one bar ago
index 2 = two bars ago
This structure is called a Series array (time-series array).
By contrast, a normal array is ordered like this.
index 0 = oldest data
index n = latest data
This difference is one of the points that most often confuses MQL5 beginners.
When you use ArraySetAsSeries, you can handle an array as follows.
ArraySetAsSeries(array, true);
Then that array is treated as a Series array (time-series array), like this.
array[0] = latest data
array[1] = previous data
array[2] = data from two bars ago
This lets you work with arrays in the same way you work with chart data.
1.2 Why You Need to Change Array Direction
The reason to change array direction in MQL5 is to match the structure of chart data.
For example, consider a process that gets RSI values.
double rsi[];
CopyBuffer(rsi_handle,0,0,10,rsi);
In this state, the array order remains a normal array.
That means it is in the following state.
rsi[0] = old data
rsi[9] = latest data
However, many EAs use it like this.
latest RSI = rsi[0]
RSI from one bar ago = rsi[1]
If ArraySetAsSeries is not set at this point,
you may reference old data instead of the latest data.
As a result, problems like these can occur:
- Trading signals become reversed
- Backtest results become strange
- The EA does not behave as expected
For this reason, in practical EA development, ArraySetAsSeries is almost always set for arrays used with CopyBuffer.

1.3 Main Use Cases
ArraySetAsSeries is mainly used in the following situations.
1. Getting Indicator Data
This is when you get values such as RSI or moving averages.
double rsi[];
ArraySetAsSeries(rsi,true);
CopyBuffer(handle,0,0,10,rsi);
2. EA Signal Checks
For example, logic like this:
RSI at or below 30 → buy
RSI at or above 70 → sell
In this case, you usually use the following data.
rsi[0] = current
rsi[1] = one bar ago
3. Multi-Timeframe EAs
In EAs that handle data from multiple timeframes,
unifying array direction is very important.
Common Stumbling Blocks (Where Beginners Often Fail)
The following mistakes often happen in MQL5 development.
1. ArraySetAsSeries is not set
Result
- Indicator values are reversed
2. It is set after CopyBuffer
Recommended order
ArraySetAsSeries
↓
CopyBuffer
3. The meaning of indexes is misunderstood
A common beginner misunderstanding
array[0] = latest
array[1] = one bar ago
4. Backtest and live results differ
An array direction mistake can become
a cause of incorrect backtest decisions.
2. ArraySetAsSeries Syntax and Usage (Basic Specification)
ArraySetAsSeries is a function that sets whether an array should be treated as a time-series array (Series array).
It is mainly used to handle price data, indicator data, and arrays obtained by CopyBuffer in the same time order as the chart.
The important point is that this function does not change the contents of the array. It only changes how the array indexes are interpreted (the reference direction).
In other words, the data itself does not change. What changes is which element is treated as index 0.
2.1 ArraySetAsSeries Syntax
The basic syntax of ArraySetAsSeries is as follows.
bool ArraySetAsSeries(
void& array[],
bool flag
);
The arguments mean the following.
| Argument | Description |
|---|---|
| array | The target array |
| flag | Whether to make it a Series array |
The behavior changes depending on the value of flag.
| flag | Meaning |
|---|---|
| true | Treat as a time-series array |
| false | Treat as a normal array |
Most EAs use it with true specified.
2.2 Basic Usage Example
The most basic usage example is getting indicator data.
double rsi[];
ArraySetAsSeries(rsi,true);
CopyBuffer(rsi_handle,0,0,10,rsi);
This code performs the following steps.
- Prepare an array for RSI
- Set the array as a Series array
- Get RSI data with CopyBuffer
As a result, the array has the following structure.
rsi[0] = latest RSI
rsi[1] = RSI from one bar ago
rsi[2] = RSI from two bars ago
Because this form is the easiest to use in EAs, it has become almost the standard way to write this code.
2.3 Difference from a Normal Array
If you do not use ArraySetAsSeries, the array is a normal array.
double rsi[];
CopyBuffer(rsi_handle,0,0,10,rsi);
In this case, the array structure is as follows.
rsi[0] = oldest data
rsi[9] = latest data
In other words, the latest value is stored at the end of the array.
Therefore, code like the following becomes necessary.
double latest_rsi = rsi[9];
However, in EAs it is common to handle data as follows:
latest value = index 0
So using ArraySetAsSeries makes the code easier to understand.
2.4 Return Value
ArraySetAsSeries returns a bool value.
| Return value | Meaning |
|---|---|
| true | Setting succeeded |
| false | Setting failed |
In ordinary EAs, the return value is sometimes not checked, but
checking it is safer when writing robust programs.
Example
if(!ArraySetAsSeries(rsi,true))
{
Print("ArraySetAsSeries failed");
}
Setting failure is rare, but it can happen if an invalid array is specified.
Common Stumbling Blocks (Frequent Mistakes)
ArraySetAsSeries is a simple function, but there are points that beginners often get wrong.
1. Using It Before Declaring the Array
This causes a compile error.
ArraySetAsSeries(rsi,true);
double rsi[];
Always write it after the array declaration.
2. Setting It After CopyBuffer
The following order is not recommended.
CopyBuffer(handle,0,0,10,rsi);
ArraySetAsSeries(rsi,true);
Reason
- Data interpretation becomes confusing
- It can cause bugs
Recommended order
ArraySetAsSeries
↓
CopyBuffer
3. Misunderstanding Series Array Indexes
This is the part that beginners find most confusing.
In a Series array:
array[0] = latest
array[1] = one bar ago
array[2] = two bars ago
4. Not Considering Array Size
If the array size is smaller than the number of values obtained by CopyBuffer,
an Array out of range error may occur.
3. Relationship Between CopyBuffer and ArraySetAsSeries (The Most Important Point in EA Development)
In MQL5, ArraySetAsSeries becomes especially important when it is used together with the CopyBuffer function.
In real EA development, most processes that get indicator values are implemented as CopyBuffer + ArraySetAsSeries.
If you do not understand this relationship, you may create a bug that references old data instead of the latest data, so be careful.
3.1 What Is CopyBuffer? (A Quick Review)
CopyBuffer is a function that copies an indicator calculation result into an array.
Basic syntax
int CopyBuffer(
int indicator_handle,
int buffer_num,
int start_pos,
int count,
double buffer[]
);
Main argument meanings
| Argument | Meaning |
|---|---|
| indicator_handle | Indicator handle |
| buffer_num | Buffer number to get |
| start_pos | Start position for retrieval |
| count | Number of data items to get |
| buffer | Destination array |
Using this function lets you get indicator values into an array.
Example (getting RSI)
double rsi[];
CopyBuffer(rsi_handle,0,0,10,rsi);
The issue here is the order of the array.
3.2 Order of Arrays Obtained by CopyBuffer
Data obtained by CopyBuffer is stored as a normal array.
That means the array structure is as follows.
rsi[0] = oldest data
rsi[1] = ...
rsi[9] = latest data
However, in many EAs you usually want to use it like this.
rsi[0] = latest value
rsi[1] = one bar ago
This is where ArraySetAsSeries is used.
3.3 Correct Combination of CopyBuffer and ArraySetAsSeries
The standard code pattern in EA development is as follows.
double rsi[];
ArraySetAsSeries(rsi,true);
CopyBuffer(rsi_handle,0,0,10,rsi);
After this process, the array has the following structure.
rsi[0] = latest RSI
rsi[1] = RSI from one bar ago
rsi[2] = RSI from two bars ago
In other words, you can handle the array in the same way as chart bar numbers.
3.4 Example of EA Trading Logic
When ArraySetAsSeries is set, trading logic can be written as follows.
Example: trading decision using RSI
if(rsi[0] < 30)
{
// Buy signal
}
if(rsi[0] > 70)
{
// Sell signal
}
It also becomes easier to write cross-detection logic.
Example: RSI reversal
if(rsi[1] < 30 && rsi[0] > 30)
{
// Buy
}
This makes EA logic much easier to read because you can easily compare the latest bar with the previous bar.
3.5 Common Bug (Very Frequent)
If ArraySetAsSeries is not set, code like the following will not work as intended.
double rsi[];
CopyBuffer(handle,0,0,10,rsi);
if(rsi[0] < 30)
{
Buy();
}
In this case, rsi[0] is an old value, not the latest value.
That means the EA trades using:
- Old data
- Past signals
As a result, problems such as the following occur:
- Backtest results look wrong
- The EA enters trades late
- Signals are shifted
3.6 Recommended Template When Using CopyBuffer
In practice, the following style is the safest.
double buffer[];
ArraySetAsSeries(buffer,true);
if(CopyBuffer(handle,0,0,10,buffer) <= 0)
{
Print("CopyBuffer error");
return;
}
double current = buffer[0];
double prev = buffer[1];
This helps prevent problems such as:
- Wrong array direction
- Failed data retrieval
- Insufficient buffer data
Common Stumbling Blocks (CopyBuffer Related)
1. Forgetting ArraySetAsSeries
This is the mistake EA beginners make most often.
2. Not Checking the Return Value of CopyBuffer
CopyBuffer returns the number of data items obtained.
When it fails, it may return:
0
or
-1
3. Insufficient Array Size
If the requested count is larger than the array size, this can cause an
array out of range
error.
4. Mixing Series Arrays and Normal Arrays
If the same logic mixes:
- Series arrays
- Normal arrays
it can cause bugs.
4. Arrays Where ArraySetAsSeries Cannot Be Used and Related Limitations
ArraySetAsSeries is a useful function, but it cannot be used with every array.
In MQL5, depending on the array type, some arrays can be changed to Series arrays (time-series arrays), and others cannot.
If you do not understand these limitations, they can cause compile errors or runtime errors.
4.1 Arrays That Can Use ArraySetAsSeries
In general, it can be used with dynamic arrays.
A dynamic array is an array declared without a fixed size.
Example
double price[];
You can set this kind of array as follows.
double price[];
ArraySetAsSeries(price,true);
It can also be used with the following types.
| Array type | Can be used? |
|---|---|
| double[] | Yes |
| int[] | Yes |
| datetime[] | Yes |
| long[] | Yes |
| bool[] | Yes |
In other words, ordinary one-dimensional dynamic arrays can basically use it without problems.
4.2 Arrays That Cannot Use ArraySetAsSeries
ArraySetAsSeries cannot be used with the following kinds of arrays.
1. Fixed-Size Arrays
These are arrays declared with a fixed size.
Example
double price[100];
In this case:
ArraySetAsSeries(price,true);
will cause an error.
The reason is that fixed-size arrays have their memory layout determined at compile time,
so the mechanism for changing array direction cannot be applied.
2. Multidimensional Arrays
ArraySetAsSeries supports one-dimensional arrays only.
Example
double matrix[10][10];
You cannot use:
ArraySetAsSeries(matrix,true);
with this array.
3. Structure Arrays
It also cannot be used with arrays that store structures.
Example
struct Data
{
double value;
};
Data arr[];
If you apply the Series setting to this kind of array, it may cause an error.
- Behavior may differ depending on the environment and code structure.
4.3 Standard Time-Series Arrays in MQL5
MQL5 has data that is treated as Series arrays from the beginning.
Representative examples
| Array | Meaning |
|---|---|
| Time[] | Bar time |
| Open[] | Open price |
| High[] | High price |
| Low[] | Low price |
| Close[] | Close price |
| Volume[] | Volume |
These are chart price data arrays.
These arrays already have the following structure.
index 0 = latest bar
index 1 = one bar ago
index 2 = two bars ago
In other words, you do not need to set ArraySetAsSeries for them.
4.4 How to Disable ArraySetAsSeries
You can also return a Series array to a normal array.
The method is simply to set flag to false.
Example
ArraySetAsSeries(buffer,false);
In this case, the array becomes as follows.
buffer[0] = old data
buffer[n] = latest data
However, in EA development, there are very few cases where you return it to false.
Usually, it is used as:
ArraySetAsSeries(array,true);
Common Stumbling Blocks (Limitations)
1. Using It with a Fixed-Size Array
The following code causes an error.
double buffer[100];
ArraySetAsSeries(buffer,true);
Solution
double buffer[];
Change it to this.
2. Applying It to a Multidimensional Array
The Series setting is only for one-dimensional arrays.
3. Mistaking an Array for a Time-Series Array
The following arrays are already Series arrays.
Close[]
Open[]
High[]
Low[]
Therefore, ArraySetAsSeries is usually unnecessary.
4. Writing Code on the Assumption That an Array Is a Series Array
If you write:
buffer[0] = latest
for an array where ArraySetAsSeries has not been set, the logic may break down.
5. Practical Code Using ArraySetAsSeries (EA Development Examples)
In actual EAs (automated trading programs), ArraySetAsSeries is almost always used together with the process that gets indicator data.
Here, as representative examples often used in practice, we cover:
- Trading decisions using RSI
- Moving average cross decisions
These are also typical CopyBuffer + ArraySetAsSeries patterns.
5.1 Trading Decisions Using RSI
First, here is the simplest example.
It gets RSI values and performs oversold and overbought checks.
Code Example
int rsi_handle;
double rsi[];
int OnInit()
{
rsi_handle = iRSI(_Symbol,_Period,14,PRICE_CLOSE);
return(INIT_SUCCEEDED);
}
void OnTick()
{
ArraySetAsSeries(rsi,true);
if(CopyBuffer(rsi_handle,0,0,3,rsi) <= 0)
return;
double current = rsi[0];
double previous = rsi[1];
if(current < 30)
{
Print("Buy signal");
}
if(current > 70)
{
Print("Sell signal");
}
}
Process Flow
- Create an RSI indicator handle
- Get RSI values into an array
- Reference the latest value and the value from one bar ago
- Check trading signals
Because it is a Series array, you can handle it as:
rsi[0] = latest RSI
rsi[1] = one bar ago
5.2 Moving Average Cross Decision
One of the most commonly used EA logic patterns is the moving average cross.
- Short-term MA
- Long-term MA
are compared to make trading decisions.
Code Example
int fast_handle;
int slow_handle;
double fast_ma[];
double slow_ma[];
int OnInit()
{
fast_handle = iMA(_Symbol,_Period,10,0,MODE_EMA,PRICE_CLOSE);
slow_handle = iMA(_Symbol,_Period,50,0,MODE_EMA,PRICE_CLOSE);
return(INIT_SUCCEEDED);
}
void OnTick()
{
ArraySetAsSeries(fast_ma,true);
ArraySetAsSeries(slow_ma,true);
if(CopyBuffer(fast_handle,0,0,3,fast_ma) <= 0)
return;
if(CopyBuffer(slow_handle,0,0,3,slow_ma) <= 0)
return;
if(fast_ma[1] < slow_ma[1] && fast_ma[0] > slow_ma[0])
{
Print("Golden Cross");
}
if(fast_ma[1] > slow_ma[1] && fast_ma[0] < slow_ma[0])
{
Print("Dead Cross");
}
}
Using Series arrays makes cross decisions very easy to write because:
fast_ma[0] = current
fast_ma[1] = one bar ago
5.3 Safe Template Used in Practice
In EA development, a template like the following is often used.
double buffer[];
ArraySetAsSeries(buffer,true);
int copied = CopyBuffer(handle,0,0,3,buffer);
if(copied <= 0)
{
Print("CopyBuffer error");
return;
}
double current = buffer[0];
double previous = buffer[1];
This style helps prevent:
- Failed data retrieval
- Wrong array order
- Index bugs
Common Implementation Mistakes
1. Writing ArraySetAsSeries in OnInit
The following style can cause problems.
int OnInit()
{
ArraySetAsSeries(buffer,true);
}
Reason
- The array may be regenerated
- It may be overwritten by CopyBuffer
The safer approach is:
set it in OnTick
2. Getting Too Few Values with CopyBuffer
The following code is risky.
CopyBuffer(handle,0,0,1,buffer);
If you then reference:
buffer[1]
you get:
array out of range
3. Not Considering Array Size
A common safe retrieval count is around:
3 to 10
4. Writing Logic on the Assumption That the Array Is a Series Array
If you forget ArraySetAsSeries, the assumption:
latest = buffer[0]
breaks.
This mistake is a typical bug for EA beginners.
6. Frequently Asked Questions About ArraySetAsSeries
ArraySetAsSeries looks like a simple function, but in real MQL5 development it is deeply related to CopyBuffer, price arrays, and EA logic. As a result, there are many points that can confuse beginners.
This section organizes the questions that come up most often in practice and how to think about them.
6.1 Does ArraySetAsSeries Rearrange the Contents of an Array?
The short answer is: it does not rearrange the array contents themselves.
What ArraySetAsSeries changes is only the reference direction of array elements.
In other words, it does not physically reorder the array data. It switches whether index 0 is treated as the latest data or as old data.
Therefore, you must understand the following points:
- The meaning assigned to the data changes
- The way you write logic changes
- The meaning of indexes in existing code changes
For example, the same array has different meanings depending on the setting.
double buffer[];
ArraySetAsSeries(buffer,true);
In this case:
buffer[0] = latest
buffer[1] = one bar ago
With false, however:
buffer[0] = old data
buffer[n] = latest
If you misunderstand this point, you can get a troublesome bug where the code runs, but only the trading decision is wrong.
6.2 If I Use CopyBuffer, Should I Write ArraySetAsSeries Every Time?
In practice, it is safer to state it clearly before using the target array.
Especially in EAs, readability and reproducibility matter, so it is safer to write the same rule every time for arrays used with CopyBuffer.
The recommended form is as follows.
double ma_buffer[];
ArraySetAsSeries(ma_buffer,true);
if(CopyBuffer(handle,0,0,3,ma_buffer) <= 0)
return;
This style has the following benefits:
- It prevents missing the array direction setting
- The intent is clear during code review
- Other developers are less likely to misread it
By contrast, if you set it only once somewhere and then omit it later, later code changes can make it unclear whether the Series setting is already applied.
6.3 Do Time[] and Close[] Also Need ArraySetAsSeries?
Usually, no.
Price arrays such as Time[] Open[] High[] Low[] and Close[] are generally based on MQL5’s time-series data model, so the latest bar is index 0.
Therefore, if you are only referencing these arrays in a normal EA or indicator, you usually do not need to set ArraySetAsSeries again.
However, there is an important caution.
- Standard price arrays
- Arrays you prepare yourself
are different things.
For example, arrays obtained with CopyBuffer can easily become confusing unless you manage their direction yourself.
In other words, it is important to organize your thinking as follows: standard arrays are time-series from the start, while custom arrays must be managed by you.
6.4 Can ArraySetAsSeries Prevent Bugs?
Not completely.
If used correctly, it greatly reduces array direction mistakes, but it does not guarantee the safety of the entire EA.
Common mistakes include:
- Not checking the return value of
CopyBuffer - Referencing
buffer[1]orbuffer[2]even though too few values were obtained - Mixing Series arrays and normal arrays
- Confusing the current bar with a confirmed bar
One thing beginners often miss is whether to use the current bar (the forming bar) or a confirmed bar.
For example:
buffer[0] = currently forming bar
buffer[1] = confirmed previous bar
In some strategies, using buffer[0] causes the signal to change, so if you want decisions based on confirmed bars, you may design the logic to use buffer[1].
In short, even after understanding ArraySetAsSeries, you still need to design which bar to use for real trading.
6.5 What Rule Should Beginners Follow?
For beginners, the most practical and safe approach is to standardize on the following rules.
- Use dynamic arrays for
CopyBufferarrays - Write
ArraySetAsSeries(array,true);before retrieving data - Always check the return value of
CopyBuffer - Treat the latest value as
array[0]and the confirmed bar asarray[1] - Use the same array direction rule throughout the same EA
The especially important point is to standardize array direction within each EA.
If some arrays are Series arrays and others are normal arrays, it becomes easy to make mistakes when adding logic later.
The minimum beginner-friendly template is as follows.
double buffer[];
ArraySetAsSeries(buffer,true);
int copied = CopyBuffer(handle,0,0,3,buffer);
if(copied <= 0)
return;
double current = buffer[0];
double previous = buffer[1];
If you repeatedly use this pattern, you can greatly reduce basic mistakes around ArraySetAsSeries.
Common Stumbling Blocks, Cautions, and Frequent Mistakes
1. Thinking ArraySetAsSeries Alone Is Enough
In practice, you need to understand it together with:
- CopyBuffer
- Array size
- Return value checks
- Current bars and confirmed bars
2. Confusing the Latest Bar with a Confirmed Bar
buffer[0] is the latest bar, but it is not always confirmed.
3. Changing the Array Direction Rule Midway
This greatly reduces EA maintainability.
4. Handling Standard Price Arrays and Custom Arrays in the Same Way
If you confuse these, the logic can easily break down.
7. Best Practices for Using ArraySetAsSeries Safely
ArraySetAsSeries is a simple function, but it is an important setting directly connected to the accuracy of EA trading logic.
In practice, array direction mistakes can cause problems such as:
- Signals becoming reversed
- Backtest results becoming abnormal
- Entry timing shifting
This section summarizes safe implementation rules (best practices) recommended in practical MQL5 development.
7.1 Always Use It Together with CopyBuffer
Most situations where ArraySetAsSeries is needed in an EA are cases where indicator values are obtained with CopyBuffer.
In this case, always follow this order.
ArraySetAsSeries
↓
CopyBuffer
↓
array reference
Code example
double buffer[];
ArraySetAsSeries(buffer,true);
int copied = CopyBuffer(handle,0,0,3,buffer);
if(copied <= 0)
return;
double current = buffer[0];
double previous = buffer[1];
Following this order helps prevent confusion about array direction.
7.2 Standardize the Array Direction Rule Inside the EA
If an EA mixes:
- Series arrays
- Normal arrays
the readability of the logic drops significantly.
This kind of confusion is especially likely in EAs that use multiple indicators.
Indicator A → index 0 is latest
Indicator B → index 0 is old
In this state, mistakes become likely every time trading logic is added.
Therefore, in practice, the following rule is recommended.
“Make all indicator arrays Series arrays.”
In other words, use:
ArraySetAsSeries(array,true);
as the basic rule.
7.3 Always Check the Return Value of CopyBuffer
CopyBuffer returns the number of data items obtained.
| Return value | Meaning |
|---|---|
| Positive number | Retrieval succeeded |
| 0 | Could not retrieve data |
| -1 | Error |
If you do not check the return value, problems like the following can occur.
No data exists
↓
reference buffer[0]
↓
invalid data
Safe style
int copied = CopyBuffer(handle,0,0,3,buffer);
if(copied <= 0)
{
Print("CopyBuffer failed");
return;
}
Adding only this check can greatly improve EA stability.
7.4 Do Not Retrieve More Data Than Necessary
With CopyBuffer, you can specify the number of data items to retrieve.
Example
CopyBuffer(handle,0,0,3,buffer);
In EA logic, the following is usually enough:
- Latest bar
- One bar ago
- Two bars ago
Therefore, in practice, retrieving around:
3 to 10 bars
is common.
Retrieving more data than necessary can lead to:
- Wasted memory use
- Lower performance
7.5 Be Aware of the Latest Bar and Confirmed Bars
An important point in EA logic is the difference between the current bar (unconfirmed bar) and confirmed bars.
In a Series array, the structure is:
buffer[0] = current bar (unconfirmed)
buffer[1] = one bar ago (confirmed)
buffer[2] = two bars ago
Depending on the strategy, signals can change if you use buffer[0].
For example, a condition like:
buffer[0] > buffer[1]
can change before the bar is confirmed.
Therefore, some designs use:
judge based on buffer[1]
In other words:
double current = buffer[1];
double previous = buffer[2];
This creates logic based on confirmed bars.
Common Stumbling Blocks (Frequent Practical Mistakes)
1. Forgetting ArraySetAsSeries
This is the most common bug among EA beginners.
Result
trade using old data
2. Not Checking the Return Value of CopyBuffer
When data retrieval fails, you may reference:
garbage data
3. Assuming buffer[0] Is a Confirmed Bar
buffer[0] is the forming bar.
4. Array Direction Rules Are Not Unified
EA maintainability drops significantly.
Minimum Template Recommended in Practice
The safest beginner-friendly template is as follows.
double buffer[];
ArraySetAsSeries(buffer,true);
int copied = CopyBuffer(handle,0,0,3,buffer);
if(copied <= 0)
return;
double current = buffer[0];
double previous = buffer[1];
Using this as a base can prevent most basic mistakes related to ArraySetAsSeries.
8. ArraySetAsSeries FAQ
Q1. What does ArraySetAsSeries do?
ArraySetAsSeries is a function that sets an MQL5 array to be handled as time-series data (a Series array).
In a Series array, indexes have the following meaning.
array[0] = latest data
array[1] = one bar ago
array[2] = two bars ago
This is the same order as the MetaTrader chart structure.
For that reason, it is often used in EAs that handle indicator data or price data.
Q2. Does ArraySetAsSeries reorder the array data?
No.
The array data itself is not reordered.
This function does not change:
- Data order
- Memory layout
It only changes the array reference direction (how indexes are interpreted).
Therefore, when ArraySetAsSeries is set:
array[0]
is treated as the latest data.
Q3. Is ArraySetAsSeries necessary when using CopyBuffer?
In many cases, it is recommended.
Arrays obtained with CopyBuffer are normal arrays by default.
That means:
array[0] = old data
array[n] = latest data
However, in EA logic, it is more natural to handle them as:
array[0] = latest
array[1] = one bar ago
Therefore, code like the following is common.
double buffer[];
ArraySetAsSeries(buffer,true);
CopyBuffer(handle,0,0,3,buffer);
Q4. Do Time[] and Close[] need ArraySetAsSeries?
Usually, no.
The following price arrays are treated as time-series arrays from the beginning.
- Time[]
- Open[]
- High[]
- Low[]
- Close[]
- Volume[]
They have the following structure:
index 0 = latest bar
Q5. Can ArraySetAsSeries be used with any array?
No.
It can mainly be used with one-dimensional dynamic arrays.
Examples where it cannot be used:
- Fixed-size arrays
- Multidimensional arrays
- Some structure arrays
Example (cannot be used)
double buffer[100];
ArraySetAsSeries(buffer,true);
This case causes an error.
Q6. Where should ArraySetAsSeries be called?
Usually, call it right before using the array.
Especially when combining it with CopyBuffer, the following order is safe.
ArraySetAsSeries
↓
CopyBuffer
↓
array reference
Example
ArraySetAsSeries(buffer,true);
CopyBuffer(handle,0,0,3,buffer);
Q7. Is buffer[0] a confirmed bar?
Not always.
In a Series array:
buffer[0] = current bar (forming)
buffer[1] = one bar ago (confirmed bar)
Therefore, depending on the strategy, you may use:
buffer[1]
for trading decisions.
Q8. What happens if I do not use ArraySetAsSeries?
The EA logic may operate based on old data.
For example, the following code:
if(buffer[0] > buffer[1])
may mean:
old data > even older data
As a result, problems such as the following can occur:
- Trading timing shifts
- Backtest results become unnatural