Skip to content

Commit

Permalink
WIP. Refactoring of MQL code to be compatible with C++.
Browse files Browse the repository at this point in the history
  • Loading branch information
nseam committed May 26, 2021
1 parent 5520a15 commit 5d49a77
Show file tree
Hide file tree
Showing 18 changed files with 126 additions and 87 deletions.
61 changes: 28 additions & 33 deletions Array.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,16 @@ class Array {
* Finds the median value in the array of any numeric type.
*/
template<typename T>
#ifdef __cplusplus
static T Median(T (&_arr)[]) {
#else
static T Median(T &_arr[]) {
#endif
static T Median(T &_arr[]) {
int _size = ArraySize(_arr);
if (_size > 0) {
ArraySort(_arr);
return _arr[_size / 2];
}
else {
return 0;
}
static T Median(T REF(_arr)[]) {
int _size = ArraySize(_arr);
if (_size > 0) {
ArraySort(_arr);
return _arr[_size / 2];
}
else {
return 0;
}
}


/**
Expand Down Expand Up @@ -96,7 +91,7 @@ class Array {
* Finds the highest value in the array of any numeric type.
*/
template<typename T>
static T Max(T &_arr[]) {
static T Max(T REF(_arr)[]) {
int i;
int _size = ArraySize(_arr);
if (_size > 0) {
Expand All @@ -112,7 +107,7 @@ class Array {
}

template <typename T>
static int ArrayCopy( T &dst_array[], const T &src_array[], const int dst_start = 0, const int src_start = 0, const int count = WHOLE_ARRAY);
static int ArrayCopy( T REF(dst_array)[], const T REF(src_array)[], const int dst_start = 0, const int src_start = 0, const int count = WHOLE_ARRAY);

/**
* Return plain text of array values separated by the delimiter.
Expand All @@ -121,7 +116,7 @@ class Array {
* int arr[] - array to look for the values
* string sep - delimiter to separate array values
*/
static string GetArrayValues(int& arr[], string sep = ", ") {
static string GetArrayValues(int REF(arr)[], string sep = ", ") {
int i;
string result = "";
for (i = 0; i < ArraySize(arr); i++) {
Expand All @@ -138,7 +133,7 @@ class Array {
* double arr[] - array to look for the values
* string sep - delimiter to separate array values
*/
static string GetArrayValues(double& arr[], string sep = ", ") {
static string GetArrayValues(double REF(arr)[], string sep = ", ") {
int i;
string result = "";
for (i = 0; i < ArraySize(arr); i++) {
Expand All @@ -151,14 +146,14 @@ class Array {
/**
* Find lower value within the 1-dim array of floats.
*/
static double LowestArrValue(double& arr[]) {
static double LowestArrValue(double REF(arr)[]) {
return (arr[ArrayMinimum(arr)]);
}

/**
* Find higher value within the 1-dim array of floats.
*/
static double HighestArrValue(double& arr[]) {
static double HighestArrValue(double REF(arr)[]) {
return (arr[ArrayMaximum(arr)]);
}

Expand Down Expand Up @@ -389,11 +384,11 @@ class Array {
* @return string
* String representation of array.
*/
static string ArrToString(int& arr[], string dlm = ",") {
static string ArrToString(int REF(arr)[], string dlm = ",") {
int i;
string res = "";
for (i = 0; i < ArraySize(arr); i++) {
res += (string)arr[i] + dlm;
res += IntegerToString(arr[i]) + dlm;
}
res = StringSubstr(res, 0, StringLen(res) - StringLen(dlm));
return res;
Expand All @@ -410,7 +405,7 @@ class Array {
* @return string
* String representation of array.
*/
static string ArrToString(double& arr[], string dlm = ",", int digits = 2) {
static string ArrToString(double REF(arr)[], string dlm = ",", int digits = 2) {
int i;
string res = "";
for (i = 0; i < ArraySize(arr); i++) {
Expand All @@ -431,7 +426,7 @@ class Array {
* @return string
* String representation of array in hexadecimal format.
*/
static string ArrToHex(unsigned char &arr[], int count = -1) {
static string ArrToHex(unsigned char REF(arr)[], int count = -1) {
int i;
string res;
for (i = 0; i < (count > 0 ? count : ArraySize(arr)); i++) {
Expand Down Expand Up @@ -524,7 +519,7 @@ class Array {
* @return string
* String representation of array.
*/
static string ArrToString(string& arr[], string dlm = ",", string prefix = "", string suffix = "") {
static string ArrToString(string REF(arr)[], string dlm = ",", string prefix = "", string suffix = "") {
int i;
string output = "";
if (ArraySize(arr) > 0) output += prefix;
Expand All @@ -544,7 +539,7 @@ class Array {
*/
template<typename T>
void ArrayPrint(
T &_arr[], // Printed array.
T REF(_arr)[], // Printed array.
int _digits = NULL, // Number of decimal places.
const string _dlm = NULL, // Separator of the structure field values.
long _start = 0, // First printed element index.
Expand Down Expand Up @@ -577,7 +572,7 @@ class Array {
*
* @see: http://www.forexfactory.com/showthread.php?p=2878455#post2878455
*/
static int ArrayResizeLeft(double &arr[], int _new_size, int _reserve_size = 0) {
static int ArrayResizeLeft(double REF(arr)[], int _new_size, int _reserve_size = 0) {
ArraySetAsSeries(arr, true);
int _res = ArrayResize(arr, _new_size, _reserve_size);
ArraySetAsSeries(arr, false);
Expand Down Expand Up @@ -606,7 +601,7 @@ class Array {
*/
// One dimensional array.
template<typename T>
static bool ArraySort(T &arr[], int count = WHOLE_ARRAY, int start = 0, int direction = MODE_ASCEND) {
static bool ArraySort(T REF(arr)[], int count = WHOLE_ARRAY, int start = 0, int direction = MODE_ASCEND) {
#ifdef __MQL4__
return ::ArraySort(arr, count, start, direction);
#else
Expand Down Expand Up @@ -652,7 +647,7 @@ class Array {
* Returns the same value as ArrayResize function (count of all elements contained in the array after resizing or -1 if error occured).
*/
template <typename X, typename Y>
static int ArrayResizeFill(X &array[], int new_size, int reserve_size = 0, Y fill_value = EMPTY_VALUE) {
static int ArrayResizeFill(X REF(array)[], int new_size, int reserve_size = 0, Y fill_value = EMPTY_VALUE) {
const int old_size = ArrayRange(array, 0);

if (new_size <= old_size)
Expand Down Expand Up @@ -683,7 +678,7 @@ class Array {
* - https://www.mql5.com/en/docs/array/arrayinitialize
*/
template <typename X>
static int ArrayInitialize(X &array[], char value) {
static int ArrayInitialize(X REF(array)[], char value) {
#ifdef __MQLBUILD__
return ::ArrayInitialize(array, value);
#else
Expand All @@ -710,7 +705,7 @@ class Array {
* - https://www.mql5.com/en/docs/array/arraymaximum
*/
template <typename X>
static int ArrayMinimum(const X &array[], int start = 0, int count = WHOLE_ARRAY) {
static int ArrayMinimum(const X REF(array)[], int start = 0, int count = WHOLE_ARRAY) {
#ifdef __MQLBUILD__
return ::ArrayMinimum(array);
#else
Expand All @@ -737,7 +732,7 @@ class Array {
* - https://www.mql5.com/en/docs/array/arraymaximum
*/
template <typename X>
static int ArrayMaximum(const X &array[], int start = 0, int count = WHOLE_ARRAY) {
static int ArrayMaximum(const X REF(array)[], int start = 0, int count = WHOLE_ARRAY) {
#ifdef __MQLBUILD__
return ::ArrayMaximum(array);
#else
Expand All @@ -760,7 +755,7 @@ class Array {
* - https://www.mql5.com/en/docs/array/arraysize
*/
template <typename X>
static int ArraySize(const X &array[]) {
static int ArraySize(const X REF(array)[]) {
#ifdef __MQLBUILD__
return ::ArraySize(array);
#else
Expand Down
1 change: 1 addition & 0 deletions Bar.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "Chart.enum.h"
#include "Serializer.mqh"
#include "SerializerNode.enum.h"
#include "Bar.enum.h"

/* Struct for storing OHLC values. */
struct BarOHLC {
Expand Down
1 change: 1 addition & 0 deletions BufferStruct.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define BUFFER_STRUCT_MQH

// Includes.
#include "DictBase.mqh"
#include "DictStruct.mqh"
#include "Serializer.mqh"

Expand Down
6 changes: 3 additions & 3 deletions Chart.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ class Chart : public Market {
/**
* Class constructor.
*/
Chart(ChartParams &_cparams, string _symbol = NULL)
Chart(ChartParams &_cparams, string _symbol = "")
: cparams(_cparams), Market(_symbol), last_bar_time(GetBarTime()), tick_index(-1), bar_index(-1) {
// Save the first BarOHLC values.
SaveChartEntry();
cparams.Set(CHART_PARAM_ID, ChartStatic::ID());
}
Chart(ENUM_TIMEFRAMES _tf = PERIOD_CURRENT, string _symbol = NULL)
Chart(ENUM_TIMEFRAMES _tf = PERIOD_CURRENT, string _symbol = "")
: cparams(_tf, _symbol, ChartStatic::ID()),
Market(_symbol),
last_bar_time(GetBarTime()),
Expand All @@ -114,7 +114,7 @@ class Chart : public Market {
// Save the first BarOHLC values.
SaveChartEntry();
}
Chart(ENUM_TIMEFRAMES_INDEX _tfi, string _symbol = NULL)
Chart(ENUM_TIMEFRAMES_INDEX _tfi, string _symbol = "")
: cparams(_tfi, _symbol, ChartStatic::ID()),
Market(_symbol),
last_bar_time(GetBarTime()),
Expand Down
2 changes: 1 addition & 1 deletion Chart.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ struct ChartStatic {
#ifdef __MQL4__
return ::iClose(_symbol, _tf, _shift); // Same as: Close[_shift]
#else // __MQL5__
double _arr[];
ARRAY(double, _arr);
ArraySetAsSeries(_arr, true);
return (_shift >= 0 && CopyClose(_symbol, _tf, _shift, 1, _arr) > 0) ? _arr[0] : -1;
#endif
Expand Down
4 changes: 2 additions & 2 deletions Chart.struct.tf.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct ChartTf {
// Constructors.
ChartTf(ENUM_TIMEFRAMES _tf = PERIOD_CURRENT) : tf(_tf), tfi(ChartTf::TfToIndex(_tf)){};
ChartTf(ENUM_TIMEFRAMES_INDEX _tfi) : tfi(_tfi), tf(ChartTf::IndexToTf(_tfi)){};
ChartTf(ChartTf& _ctf) : tf(_ctf.tf), tfi(_ctf.tfi){};
ChartTf(const ChartTf& _ctf) : tf(_ctf.tf), tfi(_ctf.tfi){};

// Struct operators.
void operator=(ENUM_TIMEFRAMES _tf) { SetTf(_tf); }
Expand Down Expand Up @@ -158,7 +158,7 @@ struct ChartTf {
case MN1:
return PERIOD_MN1; // Monthly.
default:
return NULL;
return (ENUM_TIMEFRAMES)-1;
}
}

Expand Down
1 change: 1 addition & 0 deletions Convert.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
// Includes.
#include "Math.h"
#include "SymbolInfo.mqh"
#include "Order.enum.h"

/**
* Class to provide conversion methods.
Expand Down
51 changes: 26 additions & 25 deletions Data.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,28 @@ struct MqlParam;

// Includes.
#include "Data.enum.h"
#include "Indicator.struct.h"
#include "Serializer.mqh"
#include "SerializerNode.enum.h"

#ifndef __MQL__
/**
* Struct to provide input parameters.
*
* For example input parameters for technical indicators.
*
* @see: https://www.mql5.com/en/docs/constants/structures/mqlparam
*/
struct MqlParam {
ENUM_DATATYPE type; // Type of the input parameter, value of ENUM_DATATYPE.
union {
long integer_value; // Field to store an integer type.
double double_value; // Field to store a double type.
string string_value; // Field to store a string type.
};
};
#endif

/**
* Struct to provide multitype data parameters.
*
Expand Down Expand Up @@ -110,7 +129,7 @@ struct DataParamEntry : public MqlParam {

/* Method to serialize DataParamEntry struct. */
SerializerNodeType DataParamEntry::Serialize(Serializer &s) {
s.PassEnum(this, "type", type, SERIALIZER_FIELD_FLAG_HIDDEN);
s.PassEnum(THIS_REF, "type", type, SERIALIZER_FIELD_FLAG_HIDDEN);
string aux_string;

switch (type) {
Expand All @@ -123,48 +142,30 @@ SerializerNodeType DataParamEntry::Serialize(Serializer &s) {
case TYPE_INT:
case TYPE_ULONG:
case TYPE_LONG:
s.Pass(this, "value", integer_value);
s.Pass(THIS_REF, "value", integer_value);
break;

case TYPE_DOUBLE:
s.Pass(this, "value", double_value);
s.Pass(THIS_REF, "value", double_value);
break;

case TYPE_STRING:
s.Pass(this, "value", string_value);
s.Pass(THIS_REF, "value", string_value);
break;

case TYPE_DATETIME:
if (s.IsWriting()) {
aux_string = TimeToString(integer_value);
s.Pass(this, "value", aux_string);
s.Pass(THIS_REF, "value", aux_string);
} else {
s.Pass(this, "value", aux_string);
s.Pass(THIS_REF, "value", aux_string);
integer_value = StringToTime(aux_string);
}
break;

default:
// Unknown type. Serializing anyway.
s.Pass(this, "value", aux_string);
s.Pass(THIS_REF, "value", aux_string);
}
return SerializerNodeObject;
}

#ifndef __MQL__
/**
* Struct to provide input parameters.
*
* For example input parameters for technical indicators.
*
* @see: https://www.mql5.com/en/docs/constants/structures/mqlparam
*/
struct DataParamEntry : public MqlParam {
ENUM_DATATYPE type; // Type of the input parameter, value of ENUM_DATATYPE.
union {
long integer_value; // Field to store an integer type.
double double_value; // Field to store a double type.
string string_value; // Field to store a string type.
}
};
#endif
2 changes: 2 additions & 0 deletions DictIteratorBase.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,5 @@ struct DictSlotsRef {
_num_used = 0;
}
};

#endif
2 changes: 2 additions & 0 deletions DictSlot.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ class DictSlot {

void RemoveFlags(unsigned char flags) { _flags &= ~flags; }
};

#endif
Loading

0 comments on commit 5d49a77

Please sign in to comment.