Function return array

 

Hi,

Does anyone here tried to create a function that returns an array? Instead of creating a function that returns ticket number, open price for a specific logic, store all of the necessary data on array returned to a function.


Thanks.

 

It's all in the docs e.g. editor's Reference (F1): "What can be returned: simple types, simple structures, object pointers."

There you'll find even examples...

 
blustacker:

Hi,

Does anyone here tried to create a function that returns an array? Instead of creating a function that returns ticket number, open price for a specific logic, store all of the necessary data on array returned to a function.


Thanks.

You cannot return an array from a function.

You can pass an array by reference to the function an it can assign new values to the array.

 
i tried to use vector but i think it is not supported on mql.
 
blustacker:
i tried to use vector but i think it is not supported on mql.

Just pass an Array by reference (as stated by GumRai above)!

If you use a Dynamic array, it can even be empty before calling the function and that function can populate it or manipulate it in any way you wish it; and when the function returns, you will effectively have returned an array.

 
RemoveElement(x,as,TradesDetails);
void RemoveElement(int x,int as,Trades &array[])
  {
   for(;x<as-1;x++)
     {
      array[x].ticket=array[x+1].ticket;
      array[x].breakeven=array[x+1].breakeven;
      array[x].entry=array[x+1].entry;
      array[x].lotsize=array[x+1].lotsize;
      array[x].signal_time=array[x+1].signal_time;
      array[x].stoploss=array[x+1].stoploss;
      array[x].takeprofit=array[x+1].takeprofit;
      array[x].trail=array[x+1].trail;
     }
   ArrayResize(array,as-1);
   return;
  }

Here's an example of passing an array by reference, in this case a struct array.

 

Functions or methods can return structures. A structure can contain arrays (even dynamic arrays). Hence this gives another way to return an array from a function. Here is an example returning an array from an object that builds parameter lists for custom indicators:


#include <Object.mqh>

//+------------------------------------------------------------------+
//   Convenient macro for creating custom indicators using the param
//   builder.                                                              |
//
//   CParamBuilder builder;
//   builder.Init(MYIND);
//   builder.AddInteger(IntParam1);
//   builder.AddDouble(DblParam1);
//   builder.AddDouble(DblParam2);
//   
//   if (!CREATE_CUSTOM(myInd,Symbol(),PERIOD_CURRENT,builder))
//     {
//      Print("Unabled to create bbStop");
//      return(INIT_FAILED);
//     }
//   m_bbStop.RefreshCurrent(true);
//+------------------------------------------------------------------+
#define  CREATE_CUSTOM(myInd,symbol,timeframe,builder) \
   myInd.Create(symbol,timeframe,IND_CUSTOM,builder.ParamCount(),builder.Params().p)


// Structure for passing an array back from a function
//---
struct PStruct
  {
   MqlParam          p[];
  };

//+------------------------------------------------------------------+
//| An object for building Mql parameter arrays for initialising     |
//| indicators.                                                      |
//+------------------------------------------------------------------+
class CParamBuilder : public CObject
  {
private:
   PStruct           m_params;

   int               AddSlot(void);

public:
                     CParamBuilder();
                    ~CParamBuilder();

   void              Init(const string path);

   void              AddString(const string stringValue);
   void              AddInteger(const int intValue);
   void              AddDouble(const double doubleValue);

   int               ParamCount(void) const;
   PStruct           Params(void) const;
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
CParamBuilder::CParamBuilder()
   : CObject()
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
CParamBuilder::~CParamBuilder()
  {
  }
//+------------------------------------------------------------------+
//| Allocate space for a new parameter and return its index          |
//+------------------------------------------------------------------+
int CParamBuilder::AddSlot(void)
  {
   int n = ArraySize(m_params.p);
   ArrayResize(m_params.p,n+1);
   return(n);
  }
//+------------------------------------------------------------------+
//| Initialise the parameter builder by passing in the path of the   |
//| indicator (eg; "::MyIndicator.ex5"). This is the first parameter.|
//+------------------------------------------------------------------+
void CParamBuilder::Init(const string path)
  {
   ArrayFree(m_params.p);
   AddString(path);
  }
//+------------------------------------------------------------------+
//| Add a string parameter                                           |
//+------------------------------------------------------------------+
void CParamBuilder::AddString(const string stringValue)
  {
   int n = AddSlot();
   m_params.p[n].type = TYPE_STRING;
   m_params.p[n].string_value = stringValue;
  }
//+------------------------------------------------------------------+
//| Add an integer parameter                                         |
//+------------------------------------------------------------------+
void CParamBuilder::AddInteger(const int intValue)
  {
   int n = AddSlot();
   m_params.p[n].type = TYPE_INT;
   m_params.p[n].integer_value = intValue;
  }
//+------------------------------------------------------------------+
//| Add a double parameter                                           |
//+------------------------------------------------------------------+
void CParamBuilder::AddDouble(const double doubleValue)
  {
   int n = AddSlot();
   m_params.p[n].type = TYPE_DOUBLE;
   m_params.p[n].double_value = doubleValue;
  }
//+------------------------------------------------------------------+
//| Returns the number of parameters                                 |
//+------------------------------------------------------------------+
int CParamBuilder::ParamCount(void) const
  {
   return(ArraySize(m_params.p));
  }
//+------------------------------------------------------------------+
//| Returns the structure containing the parameters. The array can   |
//| then be referenced as builder.Params().p                         |
//+------------------------------------------------------------------+
PStruct CParamBuilder::Params(void) const
  {
   return(m_params);
  }
Reason: