This commit is contained in:
2026-11-20 14:12:26 +03:30
parent b8b426d3a7
commit cdcaa59b6f
21 changed files with 6591 additions and 904 deletions
@@ -0,0 +1,349 @@
//+------------------------------------------------------------------+
//| Module: Lang/GlobalVariable.mqh |
//| This file is part of the mql4-lib project: |
//| https://github.com/dingmaotu/mql4-lib |
//| |
//| Copyright 2017 Li Ding <dingmaotu@126.com> |
//| |
//| Licensed under the Apache License, Version 2.0 (the "License"); |
//| you may not use this file except in compliance with the License. |
//| You may obtain a copy of the License at |
//| |
//| http://www.apache.org/licenses/LICENSE-2.0 |
//| |
//| Unless required by applicable law or agreed to in writing, |
//| software distributed under the License is distributed on an |
//| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
//| either express or implied. |
//| See the License for the specific language governing permissions |
//| and limitations under the License. |
//+------------------------------------------------------------------+
#property strict
//+------------------------------------------------------------------+
//| Encapsulates global variable functions |
//+------------------------------------------------------------------+
class GlobalVariable
{
public:
static int total()
{
return GlobalVariablesTotal();
}
static string name(int index)
{
return GlobalVariableName(index);
}
static void flush()
{
GlobalVariablesFlush();
}
static bool exists(string name)
{
return GlobalVariableCheck(name);
}
static datetime lastAccess(string name)
{
return GlobalVariableTime(name);
}
static bool makeTemp(string name)
{
return GlobalVariableTemp(name);
}
static double get(string name)
{
return GlobalVariableGet(name);
}
static bool get(string name, double &value)
{
return (bool)GlobalVariableGet(name, value);
}
static bool set(string name, double value)
{
return (bool)GlobalVariableSet(name, value);
}
static bool setOn(string name, double value, double check)
{
return GlobalVariableSetOnCondition(name, value, check);
}
static bool remove(string name)
{
return (bool)GlobalVariableDel(name);
}
static bool removeAll(string prefix = NULL, datetime before = 0)
{
return (bool)GlobalVariablesDeleteAll(prefix, before);
}
};
//+------------------------------------------------------------------+
//| TempVar is a variable whose life time is the same as the program |
//+------------------------------------------------------------------+
class TempVar
{
private:
string m_name;
bool m_owned;
public:
TempVar(string name, bool create = false) : m_name(name), m_owned(create)
{
if (create)
{
GlobalVariable::makeTemp(name);
}
}
~TempVar()
{
if (m_owned && isValid())
{
GlobalVariable::remove(m_name);
}
}
bool isValid() const
{
return GlobalVariable::exists(m_name);
}
string getName() const
{
return m_name;
}
bool set(double value)
{
return GlobalVariable::set(m_name, value);
}
double get() const
{
return GlobalVariable::get(m_name);
}
bool setOn(double value, double check)
{
return GlobalVariable::setOn(m_name, value, check);
}
datetime lastAccess() const
{
return GlobalVariable::lastAccess(m_name);
}
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class AtomicVar : public TempVar
{
public:
AtomicVar(string name, long initial, bool create = false) : TempVar(name, create)
{
set(initial);
}
long increment(long by = 1);
long decrement(long by = 1)
{
return increment(-by);
}
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
long AtomicVar::increment(long by)
{
bool success = false;
long value;
do
{
value = (long)get();
success = setOn(value + by, value);
} while (!success && !IsStopped());
return (value + by);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class Semaphore
{
private:
TempVar m_var;
public:
Semaphore(string name, long initial = 0);
bool isValid() const
{
return m_var.isValid();
}
bool acquire();
void release();
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
Semaphore::Semaphore(string name, long initial)
: m_var(name, initial != 0)
{
if (initial != 0)
{
m_var.set(initial);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool Semaphore::acquire(void)
{
bool success = false;
do
{
long value = (long)m_var.get();
if (value == 0)
return false;
success = m_var.setOn(value - 1, value);
} while (!success && !IsStopped());
return success;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void Semaphore::release(void)
{
bool success = false;
do
{
long value = (long)m_var.get();
success = m_var.setOn(value + 1, value);
} while (!success && !IsStopped());
}
//+------------------------------------------------------------------+
//| CriticalSection object for making atomic operations |
//| |
//| An exmaple of creating a global context (the creation and destroy|
//| are both enclosed between the SAME critical section): |
//| |
//| enter() |
//| if(refcount==0) create context |
//| else refcontext |
//| increase refcount |
//| leave() |
//| |
//| enter() |
//| decrease refcount |
//| if(refcount==0) context destroy |
//| leave() |
//+------------------------------------------------------------------+
class CriticalSection
{
private:
const string m_name;
public:
CriticalSection(string name) : m_name(name) {}
bool isValid() const
{
return m_name != NULL;
}
string getName() const
{
return m_name;
}
void enter()
{
GlobalVariable::remove(m_name);
while (!GlobalVariable::makeTemp(m_name) && !IsStopped())
Sleep(100);
}
bool tryEnter()
{
return GlobalVariable::makeTemp(m_name);
}
void leave()
{
GlobalVariable::remove(m_name);
}
};
//+------------------------------------------------------------------+
//| HandleManager should implement 2 methods: create & destroy |
//+------------------------------------------------------------------+
template <typename T>
interface HandleManager
{
T create();
void destroy(T);
};
//+------------------------------------------------------------------+
//| A reference counted global pointer (or handle) |
//| Generic type parameter T can be long or int depending the handle |
//| length (64bit or 32bit) |
//+------------------------------------------------------------------+
template <typename T, typename HM>
class GlobalHandle
{
private:
CriticalSection m_cs;
string m_refName;
string m_counterName;
protected:
T m_ref;
HandleManager<T> *m_hm;
public:
GlobalHandle(string sharedKey = NULL) : m_cs(sharedKey)
{
m_refName = m_cs.getName() + "_Ref";
m_counterName = m_cs.getName() + "_Count";
m_hm = new HM;
if (!m_cs.isValid())
m_ref = m_hm.create();
else
{
m_cs.enter();
if (!GlobalVariable::exists(m_counterName))
{
GlobalVariable::makeTemp(m_counterName);
GlobalVariable::set(m_counterName, 0);
}
if (long(GlobalVariable::get(m_counterName)) == 0)
{
m_ref = m_hm.create();
if (!GlobalVariable::exists(m_refName))
{
GlobalVariable::makeTemp(m_refName);
GlobalVariable::set(m_refName, m_ref);
}
}
else
{
m_ref = (T)(GlobalVariable::get(m_refName));
}
GlobalVariable::set(m_counterName, GlobalVariable::get(m_counterName) + 1);
m_cs.leave();
}
}
~GlobalHandle()
{
if (!m_cs.isValid())
{
m_hm.destroy(m_ref);
}
else
{
m_cs.enter();
GlobalVariable::set(m_counterName, GlobalVariable::get(m_counterName) - 1);
if (long(GlobalVariable::get(m_counterName)) == 0)
{
m_hm.destroy(m_ref);
GlobalVariable::remove(m_refName);
GlobalVariable::remove(m_counterName);
}
m_cs.leave();
}
delete m_hm;
}
T ref() const
{
return m_ref;
}
};
//+------------------------------------------------------------------+
@@ -0,0 +1,155 @@
//+------------------------------------------------------------------+
//| Module: Lang/Mql.mqh |
//| This file is part of the mql4-lib project: |
//| https://github.com/dingmaotu/mql4-lib |
//| |
//| Copyright 2017 Li Ding <dingmaotu@126.com> |
//| |
//| Licensed under the Apache License, Version 2.0 (the "License"); |
//| you may not use this file except in compliance with the License. |
//| You may obtain a copy of the License at |
//| |
//| http://www.apache.org/licenses/LICENSE-2.0 |
//| |
//| Unless required by applicable law or agreed to in writing, |
//| software distributed under the License is distributed on an |
//| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
//| either express or implied. |
//| See the License for the specific language governing permissions |
//| and limitations under the License. |
//+------------------------------------------------------------------+
#property strict
#include "Error.mqh"
//+------------------------------------------------------------------+
//| Mql language specific methods |
//+------------------------------------------------------------------+
class Mql
{
public:
static int getLastError() { return GetLastError(); }
static string getErrorMessage(int errorCode) { return GetErrorDescription(errorCode); }
//--- Prefer global DoubleToString function
static string doubleToString(double value, int precision) { return DoubleToString(value, precision); }
//--- Adapted from stdlib.mq4 by using `StringSetCharacter` instead of `StringSetChar`
static string integerToHexString(int value)
{
static const string digits = "0123456789ABCDEF";
string hex = "00000000";
int digit, shift = 28;
for (int i = 0; i < 8; i++)
{
digit = (value >> shift) & 0x0F;
StringSetCharacter(hex, i, digits[digit]);
shift -= 4;
}
return (hex);
}
//--- Prefer Canvas/Canvas.mqh XRGB
static int rgb(int r, int g, int b) { return int(0xFF000000 | (uchar(r) << 16) | (uchar(g) << 8) | uchar(b)); }
//--- Prefer Lang/Number.mqh Double::IsEqual
static bool isEqual(double a, double b) { return NormalizeDouble(a - b, 8) == 0; }
static bool isStopped() { return IsStopped(); }
static int getCodePage() { return MQLInfoInteger(MQL_CODEPAGE); }
static ENUM_PROGRAM_TYPE getProgramType() { return (ENUM_PROGRAM_TYPE)MQLInfoInteger(MQL_PROGRAM_TYPE); }
static bool isScript() { return Mql::getProgramType() == PROGRAM_SCRIPT; }
static bool isExpert() { return Mql::getProgramType() == PROGRAM_EXPERT; }
static bool isIndicator() { return Mql::getProgramType() == PROGRAM_INDICATOR; }
static bool isDllAllowed() { return MQLInfoInteger(MQL_DLLS_ALLOWED) != 0; }
static bool isTradeAllowed() { return MQLInfoInteger(MQL_TRADE_ALLOWED) != 0; }
static bool isSignalsAllowed() { return MQLInfoInteger(MQL_SIGNALS_ALLOWED) != 0; }
static bool isDebug() { return MQLInfoInteger(MQL_DEBUG) != 0; }
static bool isProfiling() { return MQLInfoInteger(MQL_PROFILER) != 0; }
static bool isTesting() { return MQLInfoInteger(MQL_TESTER) != 0; }
static bool isOptimizing() { return MQLInfoInteger(MQL_OPTIMIZATION) != 0; }
static bool isVisual() { return MQLInfoInteger(MQL_VISUAL_MODE) != 0; }
static bool isFrameMode() { return MQLInfoInteger(MQL_FRAME_MODE) != 0; }
static ENUM_LICENSE_TYPE getLicenseType() { return (ENUM_LICENSE_TYPE)MQLInfoInteger(MQL_LICENSE_TYPE); }
static bool isFreeLicense() { return Mql::getLicenseType() == LICENSE_FREE; }
static bool isDemoLicense() { return Mql::getLicenseType() == LICENSE_DEMO; }
static bool isFullLicense() { return Mql::getLicenseType() == LICENSE_FULL; }
static bool isTimeLicense() { return Mql::getLicenseType() == LICENSE_TIME; }
static string getProgramName() { return MQLInfoString(MQL_PROGRAM_NAME); }
static string getProgramPath() { return MQLInfoString(MQL_PROGRAM_PATH); }
};
//+------------------------------------------------------------------+
//| Object getter/setter generator |
//| ObjectAttr generates: |
//| 1. private member m_property |
//| 2. public method setProperty |
//| 3. public method getProperty |
//| ObjectAttrBool is specific to boolean type properties: |
//| 1. private member m_isProperty |
//| 2. public method setProperty |
//| 3. public method isProperty |
//| Use *Read or *Write versions for read only or write only |
//| properties |
//+------------------------------------------------------------------+
#define ObjectAttr(Type, Private, Public) \
public: \
Type get##Public() const { return m_##Private; } \
void set##Public(Type value) { m_##Private = value; } \
\
private: \
Type m_##Private
#define ObjectAttrRead(Type, Private, Public) \
public: \
Type get##Public() const { return m_##Private; } \
\
private: \
Type m_##Private
#define ObjectAttrWrite(Type, Private, Public) \
public: \
void set##Public(Type value) { m_##Private = value; } \
\
private: \
Type m_##Private
#define ObjectAttrBool(Public) \
public: \
bool is##Public() const { return m_is##Public; } \
void set##Public(bool value) { m_is##Public = value; } \
\
private: \
bool m_is##Public
#define ObjectAttrBoolRead(Public) \
public: \
bool is##Public() const { return m_is##Public; } \
\
private: \
bool m_is##Public
#define ObjectAttrBoolWrite(Public) \
public: \
void set##Public(bool value) { m_is##Public = value; } \
\
private: \
bool m_##Private //+------------------------------------------------------------------+
//| Print debug messages: only generate code for debugging runs |
//+------------------------------------------------------------------+
#ifdef _DEBUG
#define Debug(msg) PrintFormat(">>> DEBUG[%s,%d,%s]: %s", __FILE__, __LINE__, __FUNCTION__, msg);
#else
#define Debug(msg)
#endif
//+------------------------------------------------------------------+
//| Execute some code in the global scope |
//+------------------------------------------------------------------+
#define BEGIN_EXECUTE(Name) \
class __Execute##Name \
{ \
public: \
__Execute##Name() \
{
#define END_EXECUTE(Name) \
} \
} \
__execute##Name;
//+------------------------------------------------------------------+
@@ -0,0 +1,215 @@
//+------------------------------------------------------------------+
//| Module: Lang/Native.mqh |
//| This file is part of the mql4-lib project: |
//| https://github.com/dingmaotu/mql4-lib |
//| |
//| Copyright 2016-2017 Li Ding <dingmaotu@126.com> |
//| |
//| Licensed under the Apache License, Version 2.0 (the "License"); |
//| you may not use this file except in compliance with the License. |
//| You may obtain a copy of the License at |
//| |
//| http://www.apache.org/licenses/LICENSE-2.0 |
//| |
//| Unless required by applicable law or agreed to in writing, |
//| software distributed under the License is distributed on an |
//| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
//| either express or implied. |
//| See the License for the specific language governing permissions |
//| and limitations under the License. |
//+------------------------------------------------------------------+
#property strict
// Assume MT5 is 64bit, which is the default.
// Even though MT5 can be 32bit, there is no way to detect this
// by using preprocessor macros. Instead, MetaQuotes provides a
// function called IsX64 to detect this dynamically
// This is just absurd. Why do you want to know the bitness of
// the runtime? To define pointer related entities at compile time!
// All integer types in MQL is uniform on both 32bit or 64bit
// architectures, so it is almost useless to have a runtime function IsX64.
// Why not a __X64__?
#ifdef __MQL5__
#define __X64__
#endif
#ifdef __X64__
#define intptr_t long
#define uintptr_t ulong
#define size_t long
#else
#define intptr_t int
#define uintptr_t uint
#define size_t int
#endif
#import "kernel32.dll"
void RtlMoveMemory(intptr_t dest, const uchar &array[], size_t length);
void RtlMoveMemory(uchar &array[], intptr_t src, size_t length);
void RtlMoveMemory(intptr_t dest, const MqlRates &array[], size_t length);
void RtlMoveMemory(MqlRates &array[], intptr_t src, size_t length);
void RtlMoveMemory(intptr_t dest, const MqlTick &value, size_t length);
void RtlMoveMemory(MqlTick &value, intptr_t src, size_t length);
void RtlMoveMemory(intptr_t &dest, intptr_t src, size_t length);
int lstrlen(intptr_t psz);
int lstrlenW(intptr_t psz);
uintptr_t lstrcpynW(string &s1, uintptr_t s2, int length);
uintptr_t lstrcpynW(uintptr_t s1, string &s2, int length);
int MultiByteToWideChar(uint codePage,
uint flags,
const intptr_t multiByteString,
int lengthMultiByte,
string &str,
int length);
#import
//+------------------------------------------------------------------+
//| Copy the memory contents pointed by src to array |
//| array parameter should be initialized to the desired size |
//| Need to import RtlMoveMemory with appropriate parameter type T |
//+------------------------------------------------------------------+
template <typename T>
void ArrayFromPointer(T &array[], intptr_t src, int count = WHOLE_ARRAY)
{
int size = (count == WHOLE_ARRAY) ? ArraySize(array) : count;
RtlMoveMemory(array, src, (size_t)(size * sizeof(T)));
}
//+------------------------------------------------------------------+
//| Copy array to the memory pointed by dest |
//| Need to import RtlMoveMemory with appropriate parameter type T |
//+------------------------------------------------------------------+
template <typename T>
void ArrayToPointer(const T &array[], intptr_t dest, int count = WHOLE_ARRAY)
{
int size = (count == WHOLE_ARRAY) ? ArraySize(array) : count;
RtlMoveMemory(dest, array, (size_t)(size * sizeof(T)));
}
//+------------------------------------------------------------------+
//| Copy the memory contents pointed by src to value type T |
//| Need to import RtlMoveMemory with appropriate parameter type T |
//+------------------------------------------------------------------+
template <typename T>
void ValueFromPointer(T &value, intptr_t src)
{
RtlMoveMemory(value, src, (size_t)sizeof(T));
}
//+------------------------------------------------------------------+
//| Copy value to the memory pointed by dest |
//| Need to import RtlMoveMemory with appropriate parameter type T |
//+------------------------------------------------------------------+
template <typename T>
void ValueToPointer(const T &value, intptr_t dest)
{
RtlMoveMemory(dest, value, (size_t)sizeof(T));
}
//+------------------------------------------------------------------+
//| For void** type, dereference a level to void* |
//+------------------------------------------------------------------+
intptr_t DereferencePointer(intptr_t pointer)
{
intptr_t res = 0;
RtlMoveMemory(res, pointer, sizeof(intptr_t));
return res;
}
//+------------------------------------------------------------------+
//| Read a valid wide character string to the MQL environment |
//+------------------------------------------------------------------+
string StringFromPointer(intptr_t psz, int len = 0)
{
if (len < 0)
return NULL;
if (len == 0)
{
len = lstrlenW(psz);
}
string res;
StringInit(res, len + 1);
lstrcpynW(res, psz, len + 1);
return res;
}
//+------------------------------------------------------------------+
//| Get the pointer address of a string |
//+------------------------------------------------------------------+
uintptr_t StringToPointer(string &s)
{
return lstrcpynW(s, 0, 0);
}
//+------------------------------------------------------------------+
//| Read a valid utf-8 string to the MQL environment |
//| With this function, there is no need to copy the string to char |
//| array, and convert with CharArrayToString |
//+------------------------------------------------------------------+
string StringFromUtf8Pointer(intptr_t psz, int len)
{
if (len < 0)
return NULL;
string res;
int required = MultiByteToWideChar(CP_UTF8, 0, psz, len, res, 0);
// need to include the NULL terminating character
StringInit(res, required + 1);
int rlen = MultiByteToWideChar(CP_UTF8, 0, psz, len, res, required);
return rlen != required ? NULL : res;
}
//+------------------------------------------------------------------+
//| for null-terminated string |
//+------------------------------------------------------------------+
string StringFromUtf8Pointer(intptr_t psz)
{
if (psz == 0)
return NULL;
int len = lstrlen(psz);
if (len == 0)
return NULL;
return StringFromUtf8Pointer(psz, len);
}
//+------------------------------------------------------------------+
//| Convert a utf-8 byte array to a string |
//+------------------------------------------------------------------+
string StringFromUtf8(const uchar &utf8[])
{
return CharArrayToString(utf8, 0, -1, CP_UTF8);
}
//+------------------------------------------------------------------+
//| Convert a char array to a string (overload for char arrays) |
//+------------------------------------------------------------------+
string StringFromUtf8(const char &utf8[])
{
uchar temp[];
int size = ArraySize(utf8);
ArrayResize(temp, size);
for (int i = 0; i < size; i++)
{
temp[i] = (uchar)utf8[i];
}
return CharArrayToString(temp, 0, -1, CP_UTF8);
}
//+------------------------------------------------------------------+
//| Convert a string to a utf-8 byte array |
//+------------------------------------------------------------------+
void StringToUtf8(const string str, uchar &utf8[], bool ending = true)
{
if (!ending && str == "")
return;
int count = ending ? -1 : StringLen(str);
StringToCharArray(str, utf8, 0, count, CP_UTF8);
}
//+------------------------------------------------------------------+
//| Convert a string to a char array (overload for char arrays) |
//+------------------------------------------------------------------+
void StringToUtf8(const string str, char &utf8[], bool ending = true)
{
if (!ending && str == "")
return;
uchar temp[];
int count = ending ? -1 : StringLen(str);
StringToCharArray(str, temp, 0, count, CP_UTF8);
int size = ArraySize(temp);
ArrayResize(utf8, size);
for (int i = 0; i < size; i++)
{
utf8[i] = (char)temp[i];
}
}
//+------------------------------------------------------------------+