# MQL 5 and vectors

20

Hello there and pardon my ignorance,

Is there an easy way to access vectors in MQL5 (suchs as in R or Matlab) in one go or do you have to use for-next loops to access each individual point in the vector/array?

In other words, can I download closing prices from two symbols (say last 10 candles) in   10-element vectors x[10] and y[10] and then manipulate them in one go? (x + y = a new 10 element vector which adds up the closing prices)...

2156

Costas Vorlow:

Hello there and pardon my ignorance,

Is there an easy way to access vectors in MQL5 (suchs as in R or Matlab) in one go or do you have to use for-next loops to access each individual point in the vector/array?

In other words, can I download closing prices from two symbols (say last 10 candles) in   10-element vectors x[10] and y[10] and then manipulate them in one go? (x + y = a new 10 element vector which adds up the closing prices)...

To the best of my knowledge, no. Var (a) would need to be an object so (a) would have to be a pointer to an object created an operator overload method. Here's how you could implement it different ways.

```//+------------------------------------------------------------------+
//|                                                       Vector.mq4 |
//|                                                      nicholishen |
//|                                   www.reddit.com/u/nicholishenFX |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict

#include <Arrays\ArrayDouble.mqh>

class MyVector : public CArrayDouble
{
public:
MyVector* operator + (MyVector &array)
{
if(this.Total() != array.Total())
{
Print(__FUNCTION__," Vectors not the same size.");
return NULL;
}
MyVector *temp = new MyVector;
for(int i=0;i<this.Total();i++)
{
}
return temp;

}
void SumVectors(MyVector &v1,MyVector &v2)
{
for(int i=0;i<::MathMax(v1.Total(),v2.Total());i++)
{
}
}
void SumVectors(double &v1[],double &v2[])
{
for(int i=0;i<::MathMax(ArraySize(v1),ArraySize(v2));i++)
{
}
}
};
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
//---

double high[],low[];
ArraySetAsSeries(high,true);
ArraySetAsSeries(low,true);
CopyHigh(Symbol(),Period(),0,10,high);
CopyLow(Symbol(),Period(),0,10,low);
/////////////////////////////////////////////////////////////////////
{
MyVector sum_vector;
sum_vector.SumVectors(high,low);

for(int i=0;i<sum_vector.Total();i++)
Print("High + Low vector = ",sum_vector[i]);
}
/////////////////////////////////////////////////////////////////////
{
MyVector vHigh, vLow, *sum_vector;
vHigh.AssignArray(high);
vLow.AssignArray(low);

sum_vector = vHigh + vLow;

for(int i=0;i<sum_vector.Total();i++)
Print("High + Low vector = ",sum_vector[i]);

delete sum_vector;
}
}
//+------------------------------------------------------------------+
```
2826

Costas Vorlow:

Hello there and pardon my ignorance,

Is there an easy way to access vectors in MQL5 (suchs as in R or Matlab) in one go or do you have to use for-next loops to access each individual point in the vector/array?

In other words, can I download closing prices from two symbols (say last 10 candles) in   10-element vectors x[10] and y[10] and then manipulate them in one go? (x + y = a new 10 element vector which adds up the closing prices)...