MQL 5 and vectors

 

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)...


Thanks in advance.

 
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)...


Thanks in advance.


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 copyright "nicholishen"
#property link      "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++)
      {
         temp.Add(this[i]+array[i]);
      }
      return temp;
      
   }
   void SumVectors(MyVector &v1,MyVector &v2)
   {
      for(int i=0;i<::MathMax(v1.Total(),v2.Total());i++)
      {
         this.Add(v1[i]+v2[i]);
      }
   }
   void SumVectors(double &v1[],double &v2[])
   {
      for(int i=0;i<::MathMax(ArraySize(v1),ArraySize(v2));i++)
      {
         this.Add(v1[i]+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;
   }
}
//+------------------------------------------------------------------+
 
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)...


Thanks in advance.

No, Costas, MQL5 does not have this type of data as a vector in MatLab or FreeMat. MQL5 has arrays, for example, array A []. Any element of the array A [] can be treated like this: A [5] or A [0]. Manipulating with arrays is most convenient by means of loops, for example, the for loop. MQL5 is a C-like language.

 
Costas Vorlow:

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)...

You can do this with OOP. I'm not sure if a ready-made class is available.

Reason: