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 Jun 1, 2021
1 parent 1accd3b commit 5a93edf
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 97 deletions.
6 changes: 1 addition & 5 deletions Account.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,7 @@ class Account {
* @return
* Returns true when the condition is met.
*/
#ifdef __cplusplus
bool CheckCondition(ENUM_ACCOUNT_CONDITION _cond, DataParamEntry (&_args)[]) {
#else
bool CheckCondition(ENUM_ACCOUNT_CONDITION _cond, DataParamEntry &_args[]) {
#endif
bool CheckCondition(ENUM_ACCOUNT_CONDITION _cond, ARRAY_REF(DataParamEntry, _args)) {
switch (_cond) {
/* @todo
case ACCOUNT_COND_BALM_GT_YEARLY:
Expand Down
11 changes: 5 additions & 6 deletions Bar.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include "Chart.enum.h"
#include "Serializer.mqh"
#include "SerializerNode.enum.h"
#include "Bar.enum.h"

/* Struct for storing OHLC values. */
struct BarOHLC {
Expand Down Expand Up @@ -174,7 +173,7 @@ struct BarOHLC {
float GetWickSum() const { return GetWickLower() + GetWickUpper(); }
float GetWickUpper() const { return high - GetMaxOC(); }
float GetWickUpperInPct() const { return GetRange() > 0 ? 100 / GetRange() * GetWickUpper() : 0; }
void GetValues(float REF(_out)[]) {
void GetValues(ARRAY_REF(float, _out)) {
ArrayResize(_out, 4);
int _index = ArraySize(_out) - 4;
_out[_index++] = open;
Expand Down Expand Up @@ -215,10 +214,10 @@ struct BarOHLC {
/* Method to serialize BarOHLC structure. */
SerializerNodeType BarOHLC::Serialize(Serializer &s) {
// s.Pass(this, "time", TimeToString(time));
s.Pass(this, "open", open, SERIALIZER_FIELD_FLAG_DYNAMIC);
s.Pass(this, "high", high, SERIALIZER_FIELD_FLAG_DYNAMIC);
s.Pass(this, "low", low, SERIALIZER_FIELD_FLAG_DYNAMIC);
s.Pass(this, "close", close, SERIALIZER_FIELD_FLAG_DYNAMIC);
s.Pass(THIS_REF, "open", open, SERIALIZER_FIELD_FLAG_DYNAMIC);
s.Pass(THIS_REF, "high", high, SERIALIZER_FIELD_FLAG_DYNAMIC);
s.Pass(THIS_REF, "low", low, SERIALIZER_FIELD_FLAG_DYNAMIC);
s.Pass(THIS_REF, "close", close, SERIALIZER_FIELD_FLAG_DYNAMIC);
return SerializerNodeObject;
}

Expand Down
68 changes: 34 additions & 34 deletions Chart.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,6 @@ class Class;
#include "Serializer.mqh"
#include "Terminal.define.h"

/**
* Wrapper struct that returns open time of each bar of the current chart.
*
* @see: https://docs.mql4.com/predefined/time
*/
struct ChartBarTime {
protected:
string symbol;
ENUM_TIMEFRAMES tf;

public:
ChartBarTime() : symbol(_Symbol), tf(PERIOD_CURRENT) {}
datetime operator[](const int _shift) const { return Get(symbol, tf, _shift); }
static datetime Get(const string _symbol, const ENUM_TIMEFRAMES _tf, const int _shift) {
return ChartStatic::iTime(_symbol, _tf, _shift);
}
};

/* Defines struct to store bar entries. */
struct ChartEntry {
BarEntry bar;
Expand Down Expand Up @@ -129,7 +111,40 @@ struct ChartParams {
SerializerNodeType Serialize(Serializer& s);
} chart_params_defaults(PERIOD_CURRENT, _Symbol);

/**
/* Defines struct for chart static methods. */
struct ChartStatic {
/**
* Returns the number of bars on the specified chart.
*/
static int iBars(string _symbol = NULL, ENUM_TIMEFRAMES _tf = PERIOD_CURRENT) {
#ifdef __MQL4__
// In MQL4, for the current chart, the information about the amount of bars is in the Bars predefined variable.
return ::iBars(_symbol, _tf);
#else // _MQL5__
// ENUM_TIMEFRAMES _tf = MQL4::TFMigrate(_tf);
return ::Bars(_symbol, _tf);
#endif
}

/**
* Wrapper struct that returns open time of each bar of the current chart.
*
* @see: https://docs.mql4.com/predefined/time
*/
struct ChartBarTime {
protected:
string symbol;
ENUM_TIMEFRAMES tf;

public:
ChartBarTime() : symbol(_Symbol), tf(PERIOD_CURRENT) {}
datetime operator[](const int _shift) const { return Get(symbol, tf, _shift); }
static datetime Get(const string _symbol, const ENUM_TIMEFRAMES _tf, const int _shift) {
return ChartStatic::iTime(_symbol, _tf, _shift);
}
};

/**
* Wrapper struct that returns close prices of each bar of the current chart.
*
* @see: https://docs.mql4.com/predefined/close
Expand Down Expand Up @@ -193,21 +208,6 @@ struct ChartPriceOpen {
}
};

/* Defines struct for chart static methods. */
struct ChartStatic {
/**
* Returns the number of bars on the specified chart.
*/
static int iBars(string _symbol = NULL, ENUM_TIMEFRAMES _tf = PERIOD_CURRENT) {
#ifdef __MQL4__
// In MQL4, for the current chart, the information about the amount of bars is in the Bars predefined variable.
return ::iBars(_symbol, _tf);
#else // _MQL5__
// ENUM_TIMEFRAMES _tf = MQL4::TFMigrate(_tf);
return ::Bars(_symbol, _tf);
#endif
}

/**
* Search for a bar by its time.
*
Expand Down
2 changes: 1 addition & 1 deletion Collection.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Collection {
// Variables.
string name;
int index;
Ref<X> data[];
ARRAY(Ref<X>, data);

public:
/**
Expand Down
1 change: 0 additions & 1 deletion Data.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ struct MqlParam;

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

Expand Down
2 changes: 1 addition & 1 deletion DateTime.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class DateTime {
}
}
static bool CheckCondition(ENUM_DATETIME_CONDITION _cond) {
DataParamEntry _args[] = {};
ARRAY(DataParamEntry, _args);
return DateTime::CheckCondition(_cond, _args);
}
};
Expand Down
49 changes: 24 additions & 25 deletions DateTime.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ struct DateTimeStatic;

// Includes.
#include "DateTime.enum.h"
#include "DateTime.struct.h"

#ifndef __MQLBUILD__
/**
Expand All @@ -57,7 +56,27 @@ struct MqlDateTime {
};
#endif

struct DateTimeEntry : MqlDateTime {
/*
* Struct to provide static date and time methods.
*/
struct DateTimeStatic {
/**
* Returns the current day of the month (e.g. the day of month of the last known server time).
*/
static int Day(datetime dt = NULL) {
if (dt == 0) {
dt = TimeCurrent();
}
#ifdef __MQL4__
return ::TimeDay(dt);
#else
MqlDateTime _dt;
TimeToStruct(dt, _dt);
return _dt.day;
#endif
}

struct DateTimeEntry : MqlDateTime {
int week_of_year;
// Struct constructors.
DateTimeEntry() { SetDateTime(); }
Expand Down Expand Up @@ -108,10 +127,10 @@ struct DateTimeEntry : MqlDateTime {
return GetValue((ENUM_DATETIME_UNIT)_unit);
}
int GetYear() { return year; }
datetime GetTimestamp() { return StructToTime(this); }
datetime GetTimestamp() { return StructToTime(THIS_REF); }
// Setters.
void SetDateTime() { TimeToStruct(TimeCurrent(), this); }
void SetDateTime(datetime _dt) { TimeToStruct(_dt, this); }
void SetDateTime() { TimeToStruct(TimeCurrent(), THIS_REF); }
void SetDateTime(datetime _dt) { TimeToStruct(_dt, THIS_REF); }
void SetDayOfMonth(int _value) {
day = _value;
day_of_week = DateTimeStatic::DayOfWeek(); // Zero-based day of week.
Expand Down Expand Up @@ -171,26 +190,6 @@ struct DateTimeEntry : MqlDateTime {
void SetYear(int _value) { year = _value; }
};

/*
* Struct to provide static date and time methods.
*/
struct DateTimeStatic {
/**
* Returns the current day of the month (e.g. the day of month of the last known server time).
*/
static int Day(datetime dt = NULL) {
if (dt == 0) {
dt = TimeCurrent();
}
#ifdef __MQL4__
return ::TimeDay(dt);
#else
MqlDateTime _dt;
TimeToStruct(dt, _dt);
return _dt.day;
#endif
}

/**
* Returns the current zero-based day of the week of the last known server time.
*/
Expand Down
35 changes: 34 additions & 1 deletion File.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,39 @@
// Includes.
#include "Terminal.mqh"

#ifndef __MQL__
enum ENUM_FILE_PROPERTY_INTEGER {
FILE_EXISTS,
FILE_CREATE_DATE,
FILE_MODIFY_DATE,
FILE_ACCESS_DATE,
FILE_SIZE,
FILE_POSITION,
FILE_END,
FILE_LINE_END,
FILE_IS_COMMON,
FILE_IS_TEXT,
FILE_IS_BINARY,
FILE_IS_CSV,
FILE_IS_ANSI,
FILE_IS_READABLE,
FILE_IS_WRITABLE,
};
enum ENUM_FILE_OPEN_FLAGS {
FILE_READ = 1,
FILE_WRITE = 2,
FILE_BIN = 4,
FILE_CSV = 8,
FILE_TXT = 16,
FILE_ANSI = 32,
FILE_UNICODE = 64,
FILE_SHARE_READ = 128,
FILE_SHARE_WRITE = 256,
FILE_REWRITE = 512,
FILE_COMMON = 4096,
};
#endif

/**
* Class to provide a group of functions for working with files.
*/
Expand All @@ -52,7 +85,7 @@ class File {
string str;
if (file_handle < 0) {
if (verbose) {
PrintFormat("%s: Error: Failed to open %s file: %s", __FUNCTION__, file_name, GetLastError());
PrintFormat("%s: Error: Failed to open %s file: %i", __FUNCTION__, C_STR(file_name), GetLastError());
}
return "";
}
Expand Down
3 changes: 3 additions & 0 deletions Indicator.enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#pragma once
#endif

// Includes.
#include "Indicator.define.h"

/* Indicator actions. */
enum ENUM_INDICATOR_ACTION {
INDI_ACTION_CLEAR_CACHE, // Clear cache.
Expand Down
19 changes: 11 additions & 8 deletions Indicator.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ struct IndicatorCalculateCache {
bool price_was_as_series;

// Buffers used for OnCalculate calculations.
double buffer1[];
double buffer2[];
double buffer3[];
double buffer4[];
double buffer5[];
ARRAY(double, buffer1);
ARRAY(double, buffer2);
ARRAY(double, buffer3);
ARRAY(double, buffer4);
ARRAY(double, buffer5);

/**
* Constructor.
Expand Down Expand Up @@ -113,7 +113,7 @@ struct IndicatorCalculateCache {
/**
* Updates prev_calculated value used by indicator's OnCalculate method.
*/
void SetPrevCalculated(double REF(price)[], int _prev_calculated) {
void SetPrevCalculated(ARRAY_REF(double, price), int _prev_calculated) {
prev_calculated = _prev_calculated;
ArraySetAsSeries(price, price_was_as_series);
}
Expand Down Expand Up @@ -239,7 +239,10 @@ struct IndicatorDataEntry {
string ToString() {
return (string)Get<T>();
}
} values[];
};

ARRAY(IndicatorDataEntryValue, values);

// Constructors.
IndicatorDataEntry(int _size = 1) : flags(INDI_ENTRY_FLAG_NONE), timestamp(0) { ArrayResize(values, _size); }
int GetSize() { return ArraySize(values); }
Expand Down Expand Up @@ -546,7 +549,7 @@ struct IndicatorParams {
idstype = IDATA_INDICATOR;
}
void SetIndicatorType(ENUM_INDICATOR_TYPE _itype) { itype = _itype; }
void SetInputParams(DataParamEntry REF(_params)[]) {
void SetInputParams(ARRAY_REF(DataParamEntry, _params)) {
int _asize = ArraySize(_params);
SetMaxParams(ArraySize(_params));
for (int i = 0; i < _asize; i++) {
Expand Down
6 changes: 3 additions & 3 deletions Log.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class Log : public Object {
bool Add(string msg, string prefix, string suffix, ENUM_LOG_LEVEL entry_log_level = V_INFO) {
return Add(prefix, msg, suffix, entry_log_level);
}
bool Add(double REF(arr)[], string prefix, string suffix, ENUM_LOG_LEVEL entry_log_level = V_INFO) {
bool Add(ARRAY_REF(double, arr), string prefix, string suffix, ENUM_LOG_LEVEL entry_log_level = V_INFO) {
return Add(prefix, Array::ArrToString(arr), suffix, entry_log_level);
}

Expand Down Expand Up @@ -193,7 +193,7 @@ class Log : public Object {
/**
* Copy logs into another array.
*/
bool Copy(log_entry REF(_logs)[]) {
bool Copy(ARRAY_REF(log_entry, _logs)) {
// @fixme
// Error: 'ArrayCopy<log_entry>' - cannot to apply function template
// Array::ArrayCopy(_logs, data, 0, 0, WHOLE_ARRAY);
Expand All @@ -209,7 +209,7 @@ class Log : public Object {
/**
* Append logs into another array.
*/
bool Append(log_entry REF(_logs)[]) {
bool Append(ARRAY_REF(log_entry, _logs)) {
// @fixme
// Error: 'ArrayCopy<log_entry>' - cannot to apply function template
// Array::ArrayCopy(_logs, data, 0, 0, WHOLE_ARRAY);
Expand Down
3 changes: 3 additions & 0 deletions Refs.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@

// Forward class declaration.
class Refs;
class ReferenceCounter;
template <typename X>
struct WeakRef;

/**
* Class used to hold strong reference to reference-counted object.
Expand Down
Loading

0 comments on commit 5a93edf

Please sign in to comment.