Skip to content

Commit

Permalink
WIP. Making code C++/Emscripten-compatible.
Browse files Browse the repository at this point in the history
  • Loading branch information
nseam committed Jan 19, 2023
1 parent efa69da commit db0b8a0
Show file tree
Hide file tree
Showing 18 changed files with 525 additions and 276 deletions.
3 changes: 2 additions & 1 deletion Account/AccountMt.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class AccountMt;
#include "../Orders.mqh"
#include "../Serializer/Serializer.h"
#include "../SymbolInfo.mqh"
#include "../Task/TaskCondition.enum.h"
#include "../Trade.struct.h"
#include "Account.define.h"
#include "Account.enum.h"
Expand Down Expand Up @@ -283,7 +284,7 @@ class AccountMt {
return ::AccountFreeMarginMode();
#else
// @todo: Not implemented yet.
return NULL;
return NULL_VALUE;
#endif
}
static double GetAccountFreeMarginMode() { return AccountMt::AccountFreeMarginMode(); }
Expand Down
4 changes: 2 additions & 2 deletions Chart.struct.static.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ struct ChartStatic {
}
return _bar_shift;
#else // __MQL5__
if (_time < 0) return (-1);
if (_time == (datetime)0) return (-1);
ARRAY(datetime, arr);
datetime _time0;
// ENUM_TIMEFRAMES _tf = MQL4::TFMigrate(_tf);
Expand Down Expand Up @@ -337,7 +337,7 @@ struct ChartStatic {
// ENUM_TIMEFRAMES _tf = MQL4::TFMigrate(_tf);
// @todo: Improves performance by caching values.

datetime _time = (_shift >= 0 && ::CopyTime(_symbol, _tf, _shift, 1, _arr) > 0) ? _arr[0] : 0;
datetime _time = (_shift >= 0 && ::CopyTime(_symbol, _tf, _shift, 1, _arr) > 0) ? _arr[0] : (datetime)0;

if (_LastError != ERR_NO_ERROR) {
Print("Error: ", _LastError, " while doing CopyTime() in ChartStatic::GetBarTime(", _symbol, ", ",
Expand Down
3 changes: 0 additions & 3 deletions Indicator/Indicator.define.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
#pragma once
#endif

// Includes.
#include "../Platform.extern.h"

// Defines macros.
#define COMMA ,
#define DUMMY
Expand Down
70 changes: 36 additions & 34 deletions Indicator/IndicatorData.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#endif

// Forward class declaration.
class IndicatorBase;
class IndicatorData;
class DrawIndicator;

// Includes.
Expand All @@ -48,6 +48,8 @@ class DrawIndicator;
#include "IndicatorData.struct.serialize.h"
#include "IndicatorData.struct.signal.h"

extern IValueStorage* InstantiateIndicatorBufferValueStorageDouble(IndicatorData* _indi, int _mode);

/**
* Implements class to store indicator data.
*/
Expand All @@ -71,7 +73,7 @@ class IndicatorData : public IndicatorBase {
IndicatorCalculateCache<double> cache;
IndicatorDataParams idparams; // Indicator data params.
IndicatorState istate;
Ref<IndicatorBase> indi_src; // Indicator used as data source.
Ref<IndicatorData> indi_src; // Indicator used as data source.

protected:
/* Protected methods */
Expand Down Expand Up @@ -134,7 +136,7 @@ class IndicatorData : public IndicatorBase {
/**
* Class constructor.
*/
IndicatorData(const IndicatorDataParams& _idparams, IndicatorBase* _indi_src = NULL, int _indi_mode = 0)
IndicatorData(const IndicatorDataParams& _idparams, IndicatorData* _indi_src = NULL, int _indi_mode = 0)
: do_draw(false), idparams(_idparams), indi_src(_indi_src) {
Init();
}
Expand Down Expand Up @@ -576,7 +578,7 @@ class IndicatorData : public IndicatorBase {
} else if (Get<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_SRC_ID)) != -1) {
int _source_id = Get<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_SRC_ID));

Print("Setting data source by id is now obsolete. Please use SetDataSource(IndicatorBase*) method for ",
Print("Setting data source by id is now obsolete. Please use SetDataSource(IndicatorData*) method for ",
GetName(), " (data source id ", _source_id, ").");
DebugBreak();

Expand Down Expand Up @@ -608,7 +610,7 @@ class IndicatorData : public IndicatorBase {
}

if (_validate) {
ValidateDataSource(&this, _result);
ValidateDataSource(THIS_PTR, _result);
}

return _result;
Expand Down Expand Up @@ -776,7 +778,7 @@ class IndicatorData : public IndicatorBase {

// If _indi or any of the _indi's data source points to this indicator then this would create circular dependency.
for (_curr = _indi; _curr != nullptr && _iterations_left != 0;
_curr = _curr.GetDataSource(false), --_iterations_left) {
_curr = _curr PTR_DEREF GetDataSource(false), --_iterations_left) {
if (_curr == THIS_PTR) {
// Circular dependency found.
Print("Error: Circular dependency found when trying to attach " + _indi PTR_DEREF GetFullName() + " into " +
Expand All @@ -788,30 +790,30 @@ class IndicatorData : public IndicatorBase {

if (indi_src.IsSet()) {
if (bool(flags | INDI_FLAG_SOURCE_REQ_INDEXABLE_BY_SHIFT) &&
!bool(_indi.GetFlags() | INDI_FLAG_INDEXABLE_BY_SHIFT)) {
Print(GetFullName(), ": Cannot set data source to ", _indi.GetFullName(),
!bool(_indi PTR_DEREF GetFlags() | INDI_FLAG_INDEXABLE_BY_SHIFT)) {
Print(GetFullName(), ": Cannot set data source to ", _indi PTR_DEREF GetFullName(),
", because source indicator isn't indexable by shift!");
DebugBreak();
return;
}
if (bool(flags | INDI_FLAG_SOURCE_REQ_INDEXABLE_BY_TIMESTAMP) &&
!bool(_indi.GetFlags() | INDI_FLAG_INDEXABLE_BY_TIMESTAMP)) {
Print(GetFullName(), ": Cannot set data source to ", _indi.GetFullName(),
!bool(_indi PTR_DEREF GetFlags() | INDI_FLAG_INDEXABLE_BY_TIMESTAMP)) {
Print(GetFullName(), ": Cannot set data source to ", _indi PTR_DEREF GetFullName(),
", because source indicator isn't indexable by timestamp!");
DebugBreak();
return;
}
}

if (indi_src.IsSet() && indi_src.Ptr() != _indi) {
indi_src.Ptr().RemoveListener(THIS_PTR);
indi_src REF_DEREF RemoveListener(THIS_PTR);
}
indi_src = _indi;
if (_indi != NULL) {
indi_src.Ptr().AddListener(THIS_PTR);
indi_src REF_DEREF AddListener(THIS_PTR);
Set<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_SRC_ID), -1);
Set<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_SRC_MODE), _input_mode);
indi_src.Ptr().OnBecomeDataSourceFor(THIS_PTR);
indi_src REF_DEREF OnBecomeDataSourceFor(THIS_PTR);
}
}

Expand Down Expand Up @@ -870,14 +872,14 @@ class IndicatorData : public IndicatorBase {
last_tick_index = _global_tick_index;

// Checking and potentially initializing new data source.
if (HasDataSource(true) != NULL) {
if (HasDataSource(true)) {
// Ticking data source if not yet ticked.
GetDataSource().Tick(_global_tick_index);
GetDataSource() PTR_DEREF Tick(_global_tick_index);
}

// Also ticking all used indicators if they've not yet ticked.
for (DictStructIterator<int, Ref<IndicatorData>> iter = indicators.Begin(); iter.IsValid(); ++iter) {
iter.Value().Ptr().Tick(_global_tick_index);
iter.Value() REF_DEREF Tick(_global_tick_index);
}

// Overridable OnTick() method.
Expand Down Expand Up @@ -925,27 +927,27 @@ class IndicatorData : public IndicatorBase {
return;
}

if (!_target.IsDataSourceModeSelectable()) {
if (!_target PTR_DEREF IsDataSourceModeSelectable()) {
// We don't validate source mode as it will use all modes.
return;
}

if (_source.GetModeCount() > 1 &&
_target.idparams.Get<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_DATA_SRC_MODE)) == -1) {
if (_source PTR_DEREF GetModeCount() > 1 &&
_target PTR_DEREF idparams.Get<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_DATA_SRC_MODE)) == -1) {
// Mode must be selected if source indicator has more that one mode.
Alert("Warning! ", GetName(),
" must select source indicator's mode via SetDataSourceMode(int). Defaulting to mode 0.");
_target.idparams.Set(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_DATA_SRC_MODE), 0);
_target PTR_DEREF idparams.Set(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_DATA_SRC_MODE), 0);
DebugBreak();
} else if (_source.Get<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_DATA_SRC_MODE)) == 1 &&
_target.idparams.Get<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_DATA_SRC_MODE)) == -1) {
_target.idparams.Set(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_DATA_SRC_MODE), 0);
} else if (_target.idparams.Get<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_DATA_SRC_MODE)) < 0 ||
_target.idparams.Get<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_DATA_SRC_MODE)) >
_source.Get<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_MAX_MODES))) {
Alert("Error! ", _target.GetName(),
} else if (_source PTR_DEREF Get<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_DATA_SRC_MODE)) == 1 &&
_target PTR_DEREF idparams.Get<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_DATA_SRC_MODE)) == -1) {
_target PTR_DEREF idparams.Set(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_DATA_SRC_MODE), 0);
} else if (_target PTR_DEREF idparams.Get<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_DATA_SRC_MODE)) < 0 ||
_target PTR_DEREF idparams.Get<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_DATA_SRC_MODE)) >
_source PTR_DEREF Get<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_MAX_MODES))) {
Alert("Error! ", _target PTR_DEREF GetName(),
" must select valid source indicator's mode via SetDataSourceMode(int) between 0 and ",
_source.Get<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_MAX_MODES)), ".");
_source PTR_DEREF Get<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_MAX_MODES)), ".");
DebugBreak();
}
}
Expand Down Expand Up @@ -1329,7 +1331,7 @@ class IndicatorData : public IndicatorBase {
default:
Print("Error: Invalid applied price " + EnumToString(_ap) +
", only PRICE_(OPEN|HIGH|LOW|CLOSE|MEDIAN|TYPICAL|WEIGHTED) are currently supported by "
"IndicatorBase::HasSpecificAppliedPriceValueStorage()!");
"IndicatorData::HasSpecificAppliedPriceValueStorage()!");
DebugBreak();
return false;
}
Expand Down Expand Up @@ -1372,7 +1374,7 @@ class IndicatorData : public IndicatorBase {
default:
Print("Error: Invalid applied price " + EnumToString(_ap) +
", only PRICE_(OPEN|HIGH|LOW|CLOSE|MEDIAN|TYPICAL|WEIGHTED) are currently supported by "
"IndicatorBase::GetSpecificAppliedPriceValueStorage()!");
"IndicatorData::GetSpecificAppliedPriceValueStorage()!");
DebugBreak();
return NULL;
}
Expand Down Expand Up @@ -1736,7 +1738,7 @@ class IndicatorData : public IndicatorBase {
}

if (!value_storages[_mode].IsSet()) {
value_storages[_mode] = new IndicatorBufferValueStorage<double>(THIS_PTR, _mode);
value_storages[_mode] = InstantiateIndicatorBufferValueStorageDouble(THIS_PTR, _mode);
}
return value_storages[_mode].Ptr();
}
Expand All @@ -1754,7 +1756,7 @@ class IndicatorData : public IndicatorBase {
void EmitEntry(IndicatorDataEntry& entry) {
for (int i = 0; i < ArraySize(listeners); ++i) {
if (listeners[i].ObjectExists()) {
listeners[i].Ptr().OnDataSourceEntry(entry);
listeners[i] REF_DEREF OnDataSourceEntry(entry);
}
}
}
Expand Down Expand Up @@ -1891,7 +1893,7 @@ class IndicatorData : public IndicatorBase {
/**
* BarsCalculated()-compatible method to be used on Indicator instance.
*/
int BarsCalculated(IndicatorData* _indi) { return _indi.GetBarsCalculated(); }
int BarsCalculated(IndicatorData* _indi) { return _indi PTR_DEREF GetBarsCalculated(); }

/**
* CopyBuffer() method to be used on Indicator instance with ValueStorage buffer.
Expand All @@ -1909,7 +1911,7 @@ int CopyBuffer(IndicatorData* _indi, int _mode, int _start, int _count, ValueSto
}

for (int i = _start; i < _count; ++i) {
IndicatorDataEntry _entry = _indi.GetEntry(i);
IndicatorDataEntry _entry = _indi PTR_DEREF GetEntry(i);

if (!_entry.IsValid()) {
break;
Expand Down
3 changes: 2 additions & 1 deletion Order.enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ enum ENUM_ORDER_TYPE {
// price.
ORDER_TYPE_SELL_STOP_LIMIT, // Upon reaching the order price, a pending Sell Limit order is placed at the StopLimit
// price.
ORDER_TYPE_CLOSE_BY // Order to close a position by an opposite one.
ORDER_TYPE_CLOSE_BY, // Order to close a position by an opposite one.
ORDER_TYPE_UNSET // A NULL value.
};
#endif

Expand Down
20 changes: 10 additions & 10 deletions Order.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -1081,9 +1081,9 @@ class Order : public SymbolInfo {
if (IsClosed()) {
Refresh();
} else {
GetLogger().Warning(StringFormat("Failed to modify order (#%d/p:%g/sl:%g/tp:%g/code:%d).",
odata.Get<long>(ORDER_PROP_TICKET), _price, _sl, _tp, _last_error),
__FUNCTION_LINE__, ToCSV());
GetLogger() PTR_DEREF Warning(StringFormat("Failed to modify order (#%d/p:%g/sl:%g/tp:%g/code:%d).",
odata.Get<long>(ORDER_PROP_TICKET), _price, _sl, _tp, _last_error),
__FUNCTION_LINE__, ToCSV());
Refresh(ORDER_SL);
Refresh(ORDER_TP);
// TODO: Refresh(ORDER_PRI)
Expand Down Expand Up @@ -1693,7 +1693,7 @@ class Order : public SymbolInfo {

if (!_result || _last_error > ERR_NO_ERROR) {
if (_last_error > ERR_NO_ERROR && _last_error != 4014) { // @fixme: In MT4 (why 4014?).
GetLogger().Warning(StringFormat("Update failed! Error: %d", _last_error), __FUNCTION_LINE__);
GetLogger() PTR_DEREF Warning(StringFormat("Update failed! Error: %d", _last_error), __FUNCTION_LINE__);
}
odata.ProcessLastError();
ResetLastError();
Expand Down Expand Up @@ -1984,7 +1984,7 @@ class Order : public SymbolInfo {
* Return text representation of the order.
*/
static string OrderTypeToString(ENUM_ORDER_TYPE _cmd, bool _lc = false) {
_cmd = _cmd != NULL ? _cmd : OrderType();
_cmd = _cmd != ORDER_TYPE_UNSET ? _cmd : OrderType();
string _res = StringSubstr(EnumToString(_cmd), 11);
StringReplace(_res, "_", " ");
if (_lc) {
Expand Down Expand Up @@ -2059,8 +2059,8 @@ class Order : public SymbolInfo {
/**
* Get color of the order based on its type.
*/
static color GetOrderColor(ENUM_ORDER_TYPE _cmd = (ENUM_ORDER_TYPE)-1, color cbuy = Blue, color csell = Red) {
if (_cmd == NULL) _cmd = (ENUM_ORDER_TYPE)OrderType();
static color GetOrderColor(ENUM_ORDER_TYPE _cmd = ORDER_TYPE_UNSET, color cbuy = Blue, color csell = Red) {
if (_cmd == ORDER_TYPE_UNSET) _cmd = (ENUM_ORDER_TYPE)OrderType();
return OrderData::GetTypeValue(_cmd) > 0 ? cbuy : csell;
}

Expand Down Expand Up @@ -2308,7 +2308,7 @@ class Order : public SymbolInfo {
_out = OrderGetString(property_id);
return true;
#else
return OrderGetParam(property_id, selected_ticket_type, ORDER_SELECT_DATA_TYPE_STRING, _out) != (string)NULL_VALUE;
return OrderGetParam(property_id, selected_ticket_type, ORDER_SELECT_DATA_TYPE_STRING, _out) != NULL_STRING;
#endif
}

Expand All @@ -2333,7 +2333,7 @@ class Order : public SymbolInfo {
static long OrderGetValue(int property_id, ENUM_ORDER_SELECT_TYPE type, long &_out) {
switch (type) {
case ORDER_SELECT_TYPE_NONE:
return NULL;
return 0;
case ORDER_SELECT_TYPE_ACTIVE:
_out = ::OrderGetInteger((ENUM_ORDER_PROPERTY_INTEGER)property_id);
break;
Expand Down Expand Up @@ -2371,7 +2371,7 @@ class Order : public SymbolInfo {
static double OrderGetValue(int property_id, ENUM_ORDER_SELECT_TYPE type, double &_out) {
switch (type) {
case ORDER_SELECT_TYPE_NONE:
return NULL;
return 0;
case ORDER_SELECT_TYPE_ACTIVE:
_out = ::OrderGetDouble((ENUM_ORDER_PROPERTY_DOUBLE)property_id);
break;
Expand Down
1 change: 1 addition & 0 deletions Order.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
// Includes.
#include "Data.struct.h"
#include "Order.enum.h"
#include "Platform.extern.h"
#include "Serializer/Serializer.h"
#include "SymbolInfo.struct.static.h"
#include "Terminal.mqh"
Expand Down
Loading

0 comments on commit db0b8a0

Please sign in to comment.