1 Star 0 Fork 0

qin_yi/EA31337-classes

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Array.mqh 19.70 KB
一键复制 编辑 原始数据 按行查看 历史
kenorb 提交于 2020-08-30 00:31 . Improves formatting of headers
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2020, 31337 Investments Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+
/*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Prevents processing this includes file for the second time.
#ifndef ARRAY_MQH
#define ARRAY_MQH
// Defines.
#ifndef MODE_ASCEND
#define MODE_ASCEND 0
#endif
#ifndef MODE_DESCEND
#define MODE_DESCEND 1
#endif
// Other constants.
// @see https://docs.mql4.com/constants/namedconstants/otherconstants
#ifndef WHOLE_ARRAY
// Indicates that all array elements will be processed.
#define WHOLE_ARRAY 0
#endif
// Includes.
#include "String.mqh"
/*
* Class to provide methods to deal with arrays.
*/
class Array {
public:
/**
* Finds the median value in the array of any numeric type.
*/
template<typename T>
static T Median(T &_arr[]) {
int _size = ArraySize(_arr);
if (_size > 0) {
ArraySort(_arr);
return _arr[_size / 2];
}
else {
return 0;
}
}
/**
* Finds the highest value in the array of any numeric type.
*/
template<typename T>
static T Sum(T &_arr[]) {
int i;
int _size = ArraySize(_arr);
if (_size > 0) {
T _sum = _arr[0];
for (i = 1; i < _size; i++) {
_sum += _arr[i];
}
return _sum;
}
else {
return 0;
}
}
/**
* Finds the highest value in the array of any numeric type.
*/
template<typename T>
static T Max(T &_arr[]) {
int i;
int _size = ArraySize(_arr);
if (_size > 0) {
T _max = _arr[0];
for (i = 1; i < _size; i++) {
_max = _max < _arr[i] ? _arr[i] : _max;
}
return _max;
}
else {
return 0;
}
}
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);
/**
* Return plain text of array values separated by the delimiter.
*
* @param
* int arr[] - array to look for the values
* string sep - delimiter to separate array values
*/
static string GetArrayValues(int& arr[], string sep = ", ") {
int i;
string result = "";
for (i = 0; i < ArraySize(arr); i++) {
result += StringFormat("%d:%d%s", i, arr[i], sep);
}
// Return text without last separator.
return StringSubstr(result, 0, StringLen(result) - StringLen(sep));
}
/**
* Return plain text of array values separated by the delimiter.
*
* @param
* double arr[] - array to look for the values
* string sep - delimiter to separate array values
*/
static string GetArrayValues(double& arr[], string sep = ", ") {
int i;
string result = "";
for (i = 0; i < ArraySize(arr); i++) {
result += StringFormat("%d:%g%s", i, arr[i], sep);
}
// Return text without last separator.
return StringSubstr(result, 0, StringLen(result) - StringLen(sep));
}
/**
* Find lower value within the 1-dim array of floats.
*/
static double LowestArrValue(double& arr[]) {
return (arr[ArrayMinimum(arr)]);
}
/**
* Find higher value within the 1-dim array of floats.
*/
static double HighestArrValue(double& arr[]) {
return (arr[ArrayMaximum(arr)]);
}
/**
* Find lower value within the 2-dim array of floats by the key.
*/
#ifdef __MQL4__
static double LowestArrValue2(double& arr[][], int key1) {
int i;
double lowest = 999;
for (i = 0; i < ArrayRange(arr, 1); i++) {
if (arr[key1][i] < lowest) {
lowest = arr[key1][i];
}
}
return lowest;
}
#else
// @todo
#endif
/**
* Find higher value within the 2-dim array of floats by the key.
*/
#ifdef __MQL4__
static double HighestArrValue2(double& arr[][], int key1) {
double highest = -1;
int i;
for (i = 0; i < ArrayRange(arr, 1); i++) {
if (arr[key1][i] > highest) {
highest = arr[key1][i];
}
}
return highest;
}
#else
// @todo
#endif
/**
* Find highest value in 2-dim array of integers by the key.
*/
#ifdef __MQL4__
static int HighestValueByKey(int& arr[][], int key) {
int highest = -1;
int i;
for (i = 0; i < ArrayRange(arr, 1); i++) {
if (arr[key][i] > highest) {
highest = arr[key][i];
}
}
return highest;
}
#else
// @todo
#endif
/**
* Find lowest value in 2-dim array of integers by the key.
*/
#ifdef __MQL4__
static int LowestValueByKey(int& arr[][], int key) {
int i;
int lowest = 999;
for (i = 0; i < ArrayRange(arr, 1); i++) {
if (arr[key][i] < lowest) {
lowest = arr[key][i];
}
}
return lowest;
}
#else
// @todo
#endif
/*
#ifdef __MQL4__
static int GetLowestArrDoubleValue(double& arr[][], int key) {
int i, j;
double lowest = -1;
for (i = 0; i < ArrayRange(arr, 0); i++) {
for (j = 0; j < ArrayRange(arr, 1); j++) {
if (arr[i][j] < lowest) {
lowest = arr[i][j];
}
}
}
return lowest;
}
#else
// @todo
#endif
*/
/**
* Find key in array of integers with the highest value.
*/
#ifdef __MQL4__
static int GetArrKey1ByHighestKey2Value(int& arr[][], int key2) {
int i;
int key1 = EMPTY;
int highest = 0;
for (i = 0; i < ArrayRange(arr, 0); i++) {
if (arr[i][key2] > highest) {
highest = arr[i][key2];
key1 = i;
}
}
return key1;
}
#else
// @todo
#endif
/**
* Find key in array of integers with the lowest value.
*/
#ifdef __MQL4__
static int GetArrKey1ByLowestKey2Value(int& arr[][], int key2) {
int i;
int key1 = EMPTY;
int lowest = 999;
for (i = 0; i < ArrayRange(arr, 0); i++) {
if (arr[i][key2] < lowest) {
lowest = arr[i][key2];
key1 = i;
}
}
return key1;
}
#else
// @todo
#endif
/**
* Find key in array of doubles with the highest value.
*/
#ifdef __MQL4__
static int GetArrKey1ByHighestKey2ValueD(double& arr[][], int key2) {
int i;
int key1 = EMPTY;
double highest = -1;
for (i = 0; i < ArrayRange(arr, 0); i++) {
if (arr[i][key2] > highest) {
highest = arr[i][key2];
key1 = i;
}
}
return key1;
}
#else
// @todo
#endif
/**
* Find key in array of doubles with the lowest value.
*/
#ifdef __MQL4__
static int GetArrKey1ByLowestKey2ValueD(double& arr[][], int key2) {
int i;
int key1 = EMPTY;
double lowest = 999;
for (i = 0; i < ArrayRange(arr, 0); i++) {
if (arr[i][key2] < lowest) {
lowest = arr[i][key2];
key1 = i;
}
}
return key1;
}
#else
// @todo
#endif
/**
* Set array value for double items with specific keys.
*/
#ifdef __MQL4__
static void ArrSetValueD(double& arr[][], int key, double value) {
int i;
for (i = 0; i < ArrayRange(arr, 0); i++) {
arr[i][key] = value;
}
}
#else
// @todo
#endif
/**
* Set array value for integer items with specific keys.
*/
#ifdef __MQL4__
static void ArrSetValueI(int& arr[][], int key, int value) {
int i;
for (i = 0; i < ArrayRange(arr, 0); i++) {
arr[i][key] = value;
}
}
#else
// @todo
#endif
/**
* Calculate sum of 2 dimentional array based on given key.
*/
#ifdef __MQL4__
static double GetArrSumKey1(double& arr[][], int key1, int offset = 0) {
int i;
double sum = 0;
offset = MathMin(offset, ArrayRange(arr, 1) - 1);
for (i = offset; i < ArrayRange(arr, 1); i++) {
sum += arr[key1][i];
}
return sum;
}
#else
// @todo
#endif
/**
* Print a one-dimensional array.
*
* @param int arr
* The one dimensional array of integers.
* @param string dlm
* Delimiter to separate the items.
*
* @return string
* String representation of array.
*/
static string ArrToString(int& arr[], string dlm = ",") {
int i;
string res = "";
for (i = 0; i < ArraySize(arr); i++) {
res += (string)arr[i] + dlm;
}
res = StringSubstr(res, 0, StringLen(res) - StringLen(dlm));
return res;
}
/**
* Print a one-dimensional array.
*
* @param double arr
* The one dimensional array of doubles.
* @param string dlm
* Delimiter to separate the items.
*
* @return string
* String representation of array.
*/
static string ArrToString(double& arr[], string dlm = ",", int digits = 2) {
int i;
string res = "";
for (i = 0; i < ArraySize(arr); i++) {
res += StringFormat("%g%s", NormalizeDouble(arr[i], digits), dlm);
}
res = StringSubstr(res, 0, StringLen(res) - StringLen(dlm));
return res;
}
/**
* Print a one-dimensional array in hex format.
*
* @param double unsigned char[]
* The one dimensional array of characters.
* @param int count
* If specified, limit the number of printed characters.
*
* @return string
* String representation of array in hexadecimal format.
*/
static string ArrToHex(unsigned char &arr[], int count = -1) {
int i;
string res;
for (i = 0; i < (count > 0 ? count : ArraySize(arr)); i++) {
res += StringFormat("%.2X", arr[i]);
}
return res;
}
/**
* Print a two-dimensional array.
*
* @param string arr
* The two dimensional array of doubles.
* @param string dlm
* Delimiter to separate the items.
* @param string digits
* Number of digits after point.
*
* @return string
* String representation of array.
*/
#ifdef __MQL4__
static string ArrToString2D(double& arr[][], string dlm = ",", int digits = 2) {
string res = "";
int i, j;
for (i = 0; i < ArrayRange(arr, 0); i++) {
res += "[";
for (j = 0; j < ArrayRange(arr, 1); j++) {
res += StringFormat("%g%s", NormalizeDouble(arr[i][j], digits), dlm);
}
res = StringSubstr(res, 0, StringLen(res) - StringLen(dlm));
res += "]" + dlm;
}
res = StringSubstr(res, 0, StringLen(res) - StringLen(dlm));
return res;
}
#else
// @todo
#endif
/**
* Print a three-dimensional array.
*
* @param string arr
* The three dimensional array of doubles.
* @param string dlm
* Delimiter to separate the items.
* @param string digits
* Number of digits after point.
*
* @return string
* String representation of array.
*/
#ifdef __MQL4__
static string ArrToString3D(double& arr[][][], string dlm = ",", int digits = 2) {
string res = "";
int i, j, k;
for (i = 0; i < ArrayRange(arr, 0); i++) {
res += "[";
for (j = 0; j < ArrayRange(arr, 1); j++) {
res += "[";
for (k = 0; k < ArrayRange(arr, 2); k++) {
res += StringFormat("%g%s", NormalizeDouble(arr[i][j][k], digits), dlm);
}
res = StringSubstr(res, 0, StringLen(res) - StringLen(dlm));
res += "]" + dlm;
}
res = StringSubstr(res, 0, StringLen(res) - StringLen(dlm));
res += "]" + dlm;
}
res = StringSubstr(res, 0, StringLen(res) - StringLen(dlm));
return res;
}
#else
// @todo
#endif
/**
* Print a one-dimensional array.
*
* @param string arr
* The one dimensional array of strings.
* @param string dlm
* Delimiter to separate the items.
* @param string prefix
* Prefix to add if array is non-empty.
* @param string suffix
* Suffix to add if array is non-empty.
*
* @return string
* String representation of array.
*/
static string ArrToString(string& arr[], string dlm = ",", string prefix = "", string suffix = "") {
int i;
string output = "";
if (ArraySize(arr) > 0) output += prefix;
for (i = 0; i < ArraySize(arr); i++) {
output += (string) arr[i] + dlm;
}
output = StringSubstr(output, 0, StringLen(output) - StringLen(dlm));
if (ArraySize(arr) > 0) output += suffix;
return output;
}
/**
* Prints an array of a simple type.
*
* @docs:
* - https://www.mql5.com/en/docs/array/arrayprint
*/
template<typename T>
void ArrayPrint(
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 __MQL4__
int i;
string output = "";
for (i = _start; i < _count == WHOLE_ARRAY ? ArraySize(_arr) : _count; i++) {
output += (string) _arr[i] + _dlm;
}
Print(output);
#else
::ArrayPrint(_arr, _digits, _dlm, _start, _count, _flags);
#endif
}
/**
* Resize array from the left.
*
* @param string arr
* The one dimensional array of doubles.
* @param int _new_size
* New size of array.
*
* @return bool
* Returns count of all elements contained in the array after resizing,
* otherwise returns -1 without resizing array.
*
* @see: http://www.forexfactory.com/showthread.php?p=2878455#post2878455
*/
static int ArrayResizeLeft(double &arr[], int _new_size, int _reserve_size = 0) {
ArraySetAsSeries(arr, true);
int _res = ArrayResize(arr, _new_size, _reserve_size);
ArraySetAsSeries(arr, false);
return _res;
}
/**
* Sorts numeric arrays by first dimension.
*
* @param &array[] arr
* Numeric array for sorting.
* @param int count
* Count of elements to sort. By default, it sorts the whole array (WHOLE_ARRAY).
* @param int start
* Starting index to sort. By default, the sort starts at the first element.
* @param int direction
* Sort direction. It can be any of the following values: MODE_ASCEND or MODE_DESCEND.
*
* @return bool
* The function returns true on success, otherwise false.
*
* @docs:
* - https://docs.mql4.com/array/arraysort
* - https://www.mql5.com/en/docs/array/arraysort
* - https://www.mql5.com/en/docs/array/array_reverse
*/
// One dimensional array.
template<typename T>
static bool ArraySort(T &arr[], int count = WHOLE_ARRAY, int start = 0, int direction = MODE_ASCEND) {
#ifdef __MQL4__
return ::ArraySort(arr, count, start, direction);
#else
if (_direction == MODE_DESCEND) {
return ::ArrayReverse(arr, start, count);
}
else {
// @fixme: Add support for _count and _start.
return ::ArraySort(arr);
}
#endif
}
// Two dimensional array.
#ifdef __MQL4__
template<typename T>
static bool ArraySort2D(T &arr[][], int count = WHOLE_ARRAY, int start = 0, int direction = MODE_ASCEND) {
#ifdef __MQL4__
return (bool) ::ArraySort(arr, count, start, direction);
#else
if (_direction == MODE_DESCEND) {
return ::ArrayReverse(arr, start, count);
}
else {
// @fixme: Add support for _count amd _start.
return ::ArraySort(arr);
}
#endif
}
#endif
/**
* Resizes array and fills allocated slots with given value.
*
* @param &array[] array
* Single dimensonal array. For multi-dimensional array consider: template <typename X, typename Y> int ArrayResizeFill(X &array[][2], int new_size, int reserve_size = 0, Y fill_value = EMPTY) { ... }
* @param int new_size
* New array size.
* @param reserve_size
* Reserve size value (excess).
* @param fill_value
* Value to be used as filler for allocated slots.
* @return int
* 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) {
const int old_size = ArrayRange(array, 0);
if (new_size <= old_size)
return old_size;
// We want to fill all allocated slots (the whole allocated memory).
const int allocated_size = MathMax(new_size, reserve_size);
int result = ArrayResize(array, new_size, reserve_size);
ArrayFill(array, old_size, allocated_size - old_size, fill_value);
return result;
}
/**
* Initializes a numeric array by a preset value.
*
* @param array[]
* Numeric array that should be initialized.
* @param char value
* New value that should be set to all array elements.
* @return int
* Number of initialized elements.
*
* @docs
* - https://docs.mql4.com/array/arrayinitialize
* - https://www.mql5.com/en/docs/array/arrayinitialize
*/
template <typename X>
static int ArrayInitialize(X &array[], char value) {
#ifdef __MQLBUILD__
return ::ArrayInitialize(array, value);
#else
// @fixme
printf("Error: @fixme: %s(%g)", "ArrayInitialize", (X) value);
return 0;
#endif
}
/**
* Searches for the lowest element in the first dimension of a multidimensional numeric array.
*
* @param void &array[]
* A numeric array, in which search is made.
* @param int start
* Index to start checking with.
* @param int count
* Number of elements for search. By default, searches in the entire array.
* @return int
* The function returns an index of a found element.
*
* @docs
* - https://docs.mql4.com/array/arraymaximum
* - https://www.mql5.com/en/docs/array/arraymaximum
*/
template <typename X>
static int ArrayMinimum(const X &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
}
/**
* Searches for the largest element in the first dimension of a multidimensional numeric array.
*
* @param void &array[]
* A numeric array, in which search is made.
* @param int start
* Index to start checking with.
* @param int count
* Number of elements for search. By default, searches in the entire array.
* @return int
* The function returns an index of a found element.
*
* @docs
* - https://docs.mql4.com/array/arraymaximum
* - https://www.mql5.com/en/docs/array/arraymaximum
*/
template <typename X>
static int ArrayMaximum(const 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
}
/**
* Returns the number of elements of a selected array.
*
* @param void &array[]
* Array of any type.
* @return int
* Value of int type.
*
* @docs
* - https://docs.mql4.com/array/arraysize
* - https://www.mql5.com/en/docs/array/arraysize
*/
template <typename X>
static int ArraySize(const X &array[]) {
#ifdef __MQLBUILD__
return ::ArraySize(array);
#else
return sizeof(array) / sizeof(*array);
#endif
}
};
#endif // ARRAY_MQH
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/qinyi.git.org/EA31337-classes.git
git@gitee.com:qinyi.git.org/EA31337-classes.git
qinyi.git.org
EA31337-classes
EA31337-classes
master

搜索帮助