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 31, 2021
1 parent 1083060 commit 1accd3b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 57 deletions.
77 changes: 26 additions & 51 deletions Array.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@
#define WHOLE_ARRAY 0
#endif

// Includes.
#include "String.mqh"

/*
* Class to provide methods to deal with arrays.
*/
Expand All @@ -52,7 +49,7 @@ class Array {
* Finds the median value in the array of any numeric type.
*/
template<typename T>
static T Median(T REF(_arr)[]) {
static T Median(ARRAY_REF(T, _arr)) {
int _size = ArraySize(_arr);
if (_size > 0) {
ArraySort(_arr);
Expand Down Expand Up @@ -91,7 +88,7 @@ class Array {
* Finds the highest value in the array of any numeric type.
*/
template<typename T>
static T Max(T REF(_arr)[]) {
static T Max(ARRAY_REF(T, _arr)) {
int i;
int _size = ArraySize(_arr);
if (_size > 0) {
Expand All @@ -107,7 +104,10 @@ class Array {
}

template <typename T>
static int ArrayCopy(ARRAY_REF(T, dst_array), const ARRAY_REF(T, src_array), const int dst_start = 0, const int src_start = 0, const int count = WHOLE_ARRAY);
static int ArrayCopy(ARRAY_REF(T, dst_array), const ARRAY_REF(T, src_array), const int dst_start = 0,
const int src_start = 0, const int count = WHOLE_ARRAY) {
throw NotImplementedException();
}

/**
* Return plain text of array values separated by the delimiter.
Expand Down Expand Up @@ -384,7 +384,7 @@ class Array {
* @return string
* String representation of array.
*/
static string ArrToString(int REF(arr)[], string dlm = ",") {
static string ArrToString(ARRAY_REF(int, arr), string dlm = ",") {
int i;
string res = "";
for (i = 0; i < ArraySize(arr); i++) {
Expand All @@ -405,7 +405,7 @@ class Array {
* @return string
* String representation of array.
*/
static string ArrToString(double REF(arr)[], string dlm = ",", int digits = 2) {
static string ArrToString(ARRAY_REF(double, arr), string dlm = ",", int digits = 2) {
int i;
string res = "";
for (i = 0; i < ArraySize(arr); i++) {
Expand All @@ -426,7 +426,7 @@ class Array {
* @return string
* String representation of array in hexadecimal format.
*/
static string ArrToHex(unsigned char REF(arr)[], int count = -1) {
static string ArrToHex(ARRAY_REF(unsigned char, arr), int count = -1) {
int i;
string res;
for (i = 0; i < (count > 0 ? count : ArraySize(arr)); i++) {
Expand Down Expand Up @@ -519,7 +519,7 @@ class Array {
* @return string
* String representation of array.
*/
static string ArrToString(string REF(arr)[], string dlm = ",", string prefix = "", string suffix = "") {
static string ArrToString(ARRAY_REF(string, arr), string dlm = ",", string prefix = "", string suffix = "") {
int i;
string output = "";
if (ArraySize(arr) > 0) output += prefix;
Expand All @@ -538,24 +538,22 @@ class Array {
* - https://www.mql5.com/en/docs/array/arrayprint
*/
template<typename T>
void ArrayPrint(
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.
long _count = WHOLE_ARRAY, // Number of printed elements.
long _flags = NULL
) {
#ifdef __MQL5__
void ArrayPrint(ARRAY_REF(T, _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.
long _count = WHOLE_ARRAY, // Number of printed elements.
long _flags = NULL) {
#ifdef __MQL5__
::ArrayPrint(_arr, _digits, _dlm, _start, _count, _flags);
#else
#else
int i;
string output = "";
for (i = _start; i < _count == WHOLE_ARRAY ? ArraySize(_arr) : _count; i++) {
output += (string) _arr[i] + _dlm;
output += (string)_arr[i] + _dlm;
}
Print(output);
#endif
#endif
}

/**
Expand All @@ -572,7 +570,7 @@ class Array {
*
* @see: http://www.forexfactory.com/showthread.php?p=2878455#post2878455
*/
static int ArrayResizeLeft(double REF(arr)[], int _new_size, int _reserve_size = 0) {
static int ArrayResizeLeft(ARRAY_REF(double, 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 @@ -601,7 +599,7 @@ class Array {
*/
// One dimensional array.
template<typename T>
static bool ArraySort(T REF(arr)[], int count = WHOLE_ARRAY, int start = 0, int direction = MODE_ASCEND) {
static bool ArraySort(ARRAY_REF(T, 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 @@ -647,7 +645,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 REF(array)[], int new_size, int reserve_size = 0, Y fill_value = EMPTY_VALUE) {
static int ArrayResizeFill(ARRAY_REF(X, 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 @@ -678,14 +676,8 @@ class Array {
* - https://www.mql5.com/en/docs/array/arrayinitialize
*/
template <typename X>
static int ArrayInitialize(X REF(array)[], char value) {
#ifdef __MQLBUILD__
static int ArrayInitialize(ARRAY_REF(X, array), char value) {
return ::ArrayInitialize(array, value);
#else
// @fixme
printf("Error: @fixme: %s(%g)", "ArrayInitialize", (X) value);
return 0;
#endif
}

/**
Expand All @@ -705,14 +697,8 @@ class Array {
* - https://www.mql5.com/en/docs/array/arraymaximum
*/
template <typename X>
static int ArrayMinimum(const X REF(array)[], int start = 0, int count = WHOLE_ARRAY) {
#ifdef __MQLBUILD__
return ::ArrayMinimum(array);
#else
// @fixme
printf("Error: @fixme: %s(%g)", "ArrayMinimum", (X) value);
return 0;
#endif
static int ArrayMinimum(const ARRAY_REF(X, _array), int _start = 0, int _count = WHOLE_ARRAY) {
return ::ArrayMinimum(_array);
}

/**
Expand All @@ -733,13 +719,7 @@ class Array {
*/
template <typename X>
static int ArrayMaximum(const ARRAY_REF(X, array), int start = 0, int count = WHOLE_ARRAY) {
#ifdef __MQLBUILD__
return ::ArrayMaximum(array);
#else
// @fixme
printf("Error: @fixme: %s(%g)", "ArrayMaximum", (X) value);
return 0;
#endif
}

/**
Expand All @@ -756,12 +736,7 @@ class Array {
*/
template <typename X>
static int ArraySize(const ARRAY_REF(X, array)) {
#ifdef __MQLBUILD__
return ::ArraySize(array);
#else
return sizeof(array) / sizeof(*array);
#endif
}

};
#endif // ARRAY_MQH
2 changes: 2 additions & 0 deletions Math.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#pragma once
#endif

#include "Data.struct.h"

// Prevents processing this includes file for the second time.
#ifndef MATH_STRUCT_H
#define MATH_STRUCT_H
Expand Down
6 changes: 3 additions & 3 deletions Order.enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ enum ENUM_ORDER_REASON_CLOSE {
ORDER_REASON_CLOSED_UNKNOWN, // Closed by unknown event
};

/* Defines modes for order type values (Take Profit and Stop Loss). */
enum ENUM_ORDER_TYPE_VALUE { ORDER_TYPE_TP = ORDER_TP, ORDER_TYPE_SL = ORDER_SL };

#ifndef __MQL5__
/* Defines the reason for order placing. */
enum ENUM_ORDER_REASON {
Expand Down Expand Up @@ -189,6 +186,9 @@ enum ENUM_ORDER_PROPERTY_STRING {
};
#endif

/* Defines modes for order type values (Take Profit and Stop Loss). */
enum ENUM_ORDER_TYPE_VALUE { ORDER_TYPE_TP = ORDER_TP, ORDER_TYPE_SL = ORDER_SL };

#ifndef __MQL__
/**
* Order operation type.
Expand Down
4 changes: 1 addition & 3 deletions String.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#ifndef STRING_MQH
#define STRING_MQH

#include "Array.mqh"

// Defines.
#define NL "\n" // New line: 0x0A (MQL file functions auto-convert 0x0A to 0x0D0A).
#define TAB "\t" // Tab: 0x09.
Expand Down Expand Up @@ -86,7 +84,7 @@ class String {
* Print multi-line text.
*/
static void PrintText(string text) {
string _result[];
ARRAY(string, _result);
ushort usep = StringGetCharacter("\n", 0);
for (int i = StringSplit(text, usep, _result) - 1; i >= 0; i--) {
Print(_result[i]);
Expand Down

0 comments on commit 1accd3b

Please sign in to comment.