Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/EA31337/EA31337-classes i…
Browse files Browse the repository at this point in the history
…nto dev-cpp-support
  • Loading branch information
nseam committed Jun 8, 2021
2 parents 7076a40 + 2f68227 commit 59df30a
Show file tree
Hide file tree
Showing 41 changed files with 823 additions and 190 deletions.
2 changes: 1 addition & 1 deletion Account.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ class Account {
*/
SerializerNodeType Serialize(Serializer &_s) {
AccountEntry _entry = GetEntry();
_s.PassStruct(this, "account-entry", _entry, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.PassStruct(THIS_REF, "account-entry", _entry, SERIALIZER_FIELD_FLAG_DYNAMIC);
return SerializerNodeObject;
}

Expand Down
16 changes: 8 additions & 8 deletions Account.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ struct AccountEntry {
// Serializers.
void SerializeStub(int _n1 = 1, int _n2 = 1, int _n3 = 1, int _n4 = 1, int _n5 = 1) {}
SerializerNodeType Serialize(Serializer& _s) {
_s.Pass(this, "time", dtime, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.Pass(this, "balance", balance, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.Pass(this, "credit", credit, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.Pass(this, "equity", equity, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.Pass(this, "profit", profit, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.Pass(this, "margin_used", margin_used, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.Pass(this, "margin_free", margin_free, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.Pass(this, "margin_avail", margin_avail, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.Pass(THIS_REF, "time", dtime, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.Pass(THIS_REF, "balance", balance, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.Pass(THIS_REF, "credit", credit, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.Pass(THIS_REF, "equity", equity, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.Pass(THIS_REF, "profit", profit, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.Pass(THIS_REF, "margin_used", margin_used, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.Pass(THIS_REF, "margin_free", margin_free, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.Pass(THIS_REF, "margin_avail", margin_avail, SERIALIZER_FIELD_FLAG_DYNAMIC);
return SerializerNodeObject;
}
};
12 changes: 6 additions & 6 deletions Action.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ struct ActionEntry {
void SetTries(short _count) { tries = _count; }

SerializerNodeType Serialize(Serializer &s) {
s.Pass(this, "flags", flags);
s.Pass(this, "last_success", last_success);
s.Pass(this, "action_id", action_id);
// s.Pass(this, "tries", tries);
s.PassEnum(this, "type", type);
s.PassEnum(this, "frequency", frequency);
s.Pass(THIS_REF, "flags", flags);
s.Pass(THIS_REF, "last_success", last_success);
s.Pass(THIS_REF, "action_id", action_id);
// s.Pass(THIS_REF, "tries", tries);
s.PassEnum(THIS_REF, "type", type);
s.PassEnum(THIS_REF, "frequency", frequency);
s.PassArray(this, "args", args);
return SerializerNodeObject;
}
Expand Down
19 changes: 12 additions & 7 deletions Bar.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@ struct BarOHLC
_time = TimeCurrent();
}
}

BarOHLC(float REF(_ohlc)[], datetime _time = 0)
: time(_time), open(_ohlc[0]), high(_ohlc[1]), low(_ohlc[2]), close(_ohlc[3]) {
if (_time == 0) {
_time = TimeCurrent();
BarOHLC(float &_prices[], datetime _time = 0) : time(_time) {
_time = _time == 0 ? TimeCurrent() : _time;
int _size = ArraySize(_prices);
close = _prices[0];
open = _prices[_size - 1];
high = fmax(close, open);
low = fmin(close, open);
for (int i = 0; i < _size; i++) {
high = fmax(high, _prices[i]);
low = fmin(low, _prices[i]);
}
}
// Struct methods.
Expand Down Expand Up @@ -181,7 +186,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(ARRAY_REF(float, _out)) {
short GetType() const { return IsBull() ? 1 : (IsBear() ? -1 : 0); }
ArrayResize(_out, 4);
int _index = ArraySize(_out) - 4;
_out[_index++] = open;
Expand Down Expand Up @@ -223,7 +228,7 @@ struct BarOHLC

/* Method to serialize BarOHLC structure. */
SerializerNodeType BarOHLC::Serialize(Serializer &s) {
// s.Pass(this, "time", TimeToString(time));
// s.Pass(THIS_REF, "time", TimeToString(time));
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);
Expand Down
2 changes: 1 addition & 1 deletion Chart.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ class Chart : public Market {
*/
SerializerNodeType Serialize(Serializer &_s) {
ChartEntry _centry = GetEntry();
_s.PassStruct(this, "chart-entry", _centry, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.PassStruct(THIS_REF, "chart-entry", _centry, SERIALIZER_FIELD_FLAG_DYNAMIC);
return SerializerNodeObject;
}
};
Expand Down
8 changes: 4 additions & 4 deletions Chart.struct.serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ class Serializer;

/* Method to serialize ChartEntry structure. */
SerializerNodeType ChartEntry::Serialize(Serializer& _s) {
_s.PassStruct(this, "bar", bar, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.PassStruct(THIS_REF, "bar", bar, SERIALIZER_FIELD_FLAG_DYNAMIC);
return SerializerNodeObject;
}

/* Method to serialize ChartParams structure. */
SerializerNodeType ChartParams::Serialize(Serializer& s) {
s.Pass(this, "id", id);
s.Pass(this, "symbol", symbol);
s.PassStruct(this, "tf", tf);
s.Pass(THIS_REF, "id", id);
s.Pass(THIS_REF, "symbol", symbol);
s.PassStruct(THIS_REF, "tf", tf);
return SerializerNodeObject;
}
14 changes: 7 additions & 7 deletions Config.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct ConfigEntry : public MqlParam {
}

SerializerNodeType Serialize(Serializer& s) {
s.PassEnum(this, "type", type, SERIALIZER_FIELD_FLAG_HIDDEN);
s.PassEnum(THIS_REF, "type", type, SERIALIZER_FIELD_FLAG_HIDDEN);

string aux_string;

Expand All @@ -96,30 +96,30 @@ struct ConfigEntry : public MqlParam {
case TYPE_INT:
case TYPE_ULONG:
case TYPE_LONG:
s.Pass(this, "value", integer_value);
s.Pass(THIS_REF, "value", integer_value);
break;

case TYPE_DOUBLE:
s.Pass(this, "value", double_value);
s.Pass(THIS_REF, "value", double_value);
break;

case TYPE_STRING:
s.Pass(this, "value", string_value);
s.Pass(THIS_REF, "value", string_value);
break;

case TYPE_DATETIME:
if (s.IsWriting()) {
aux_string = TimeToString(integer_value);
s.Pass(this, "value", aux_string);
s.Pass(THIS_REF, "value", aux_string);
} else {
s.Pass(this, "value", aux_string);
s.Pass(THIS_REF, "value", aux_string);
integer_value = StringToTime(aux_string);
}
break;

default:
// Unknown type. Serializing anyway.
s.Pass(this, "value", aux_string);
s.Pass(THIS_REF, "value", aux_string);
}

return SerializerNodeObject;
Expand Down
1 change: 1 addition & 0 deletions Data.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
// Forward class declaration.
class Serializer;
struct MqlParam;
struct MqlRates;

// Includes.
#include "Data.enum.h"
Expand Down
2 changes: 1 addition & 1 deletion Dict.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class Dict : public DictBase<K, V> {
if (s.IsWriting()) {
for (DictIteratorBase<K, V> i(Begin()); i.IsValid(); ++i) {
V value = i.Value();
s.Pass(this, GetMode() == DictModeDict ? i.KeyAsString() : "", value);
s.Pass(THIS_REF, GetMode() == DictModeDict ? i.KeyAsString() : "", value);
}

return (GetMode() == DictModeDict) ? SerializerNodeObject : SerializerNodeArray;
Expand Down
1 change: 1 addition & 0 deletions DictBase.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "Convert.mqh"
#include "DictIteratorBase.mqh"
#include "DictSlot.mqh"
#include "Log.mqh"
#include "Serializer.mqh"

/**
Expand Down
9 changes: 4 additions & 5 deletions DictIteratorBase.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
*
*/

// Prevents processing this includes file for the second time.
#ifndef DICT_ITERATOR_BASE_MQH
#define DICT_ITERATOR_BASE_MQH
#ifndef __MQL__
// Allows the preprocessor to include a header file when it is needed.
#pragma once
#endif

#include "DictBase.mqh"

Expand Down Expand Up @@ -119,5 +120,3 @@ struct DictSlotsRef {
_num_used = 0;
}
};

#endif
8 changes: 4 additions & 4 deletions EA.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -866,8 +866,8 @@ class EA {
* Returns serialized representation of the object instance.
*/
SerializerNodeType Serialize(Serializer &_s) {
_s.Pass(this, "account", account, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.Pass(this, "market", market, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.Pass(THIS_REF, "account", account, SERIALIZER_FIELD_FLAG_DYNAMIC);
_s.Pass(THIS_REF, "market", market, SERIALIZER_FIELD_FLAG_DYNAMIC);
for (DictObjectIterator<ENUM_TIMEFRAMES, DictStruct<long, Ref<Strategy>>> _iter_tf = GetStrategies().Begin();
_iter_tf.IsValid(); ++_iter_tf) {
ENUM_TIMEFRAMES _tf = _iter_tf.Key();
Expand All @@ -878,8 +878,8 @@ class EA {
string _sname = _strat.GetName(); // + "@" + Chart::TfToString(_strat.GetTf()); // @todo
string _sparams = _strat.GetParams().ToString();
string _sresults = _strat.GetProcessResult().ToString();
_s.Pass(this, "strat:params:" + _sname, _sparams);
_s.Pass(this, "strat:results:" + _sname, _sresults);
_s.Pass(THIS_REF, "strat:params:" + _sname, _sparams);
_s.Pass(THIS_REF, "strat:results:" + _sname, _sresults);
}
}
return SerializerNodeObject;
Expand Down
4 changes: 2 additions & 2 deletions Indicator.enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ enum ENUM_IDATA_SOURCE_TYPE {
enum ENUM_IDATA_VALUE_RANGE {
IDATA_RANGE_ARROW, // Value is non-zero on signal.
IDATA_RANGE_BINARY, // E.g. 0 or 1.
IDATA_RANGE_FIXED, // E.g. 0 to 100.
IDATA_RANGE_MIXED,
IDATA_RANGE_PRICE, // Values represent price.
IDATA_RANGE_PRICE, // Values represent price.
IDATA_RANGE_RANGE, // E.g. 0 to 100.
IDATA_RANGE_UNKNOWN
};

Expand Down
92 changes: 92 additions & 0 deletions Indicator.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ class Indicator : public Chart {
#endif
}

/* Buffer methods */

/**
* Initializes a cached proxy between i*OnArray() methods and OnCalculate()
* used by custom indicators.
Expand Down Expand Up @@ -361,6 +363,47 @@ class Indicator : public Chart {
return GetIndicatorBuffers() > 0 && GetIndicatorBuffers() <= 512;
}

/**
* Gets indicator data from a buffer and copy into struct array.
*
* @return
* Returns true of successful copy.
* Returns false on invalid values.
*/
bool CopyEntries(IndicatorDataEntry& _data[], int _count, int _start_shift = 0) {
bool _is_valid = true;
if (ArraySize(_data) < _count) {
_is_valid &= ArrayResize(_data, _count);
}
for (int i = 0; i < _count; i++) {
IndicatorDataEntry _entry = GetEntry(_start_shift + i);
_is_valid &= _entry.IsValid();
_data[i] = _entry;
}
return _is_valid;
}

/**
* Gets indicator data from a buffer and copy into array of values.
*
* @return
* Returns true of successful copy.
* Returns false on invalid values.
*/
template <typename T>
bool CopyValues(T& _data[], int _count, int _start_shift = 0, int _mode = 0) {
if (ArraySize(_data) < _count) {
bool _is_valid = true;
_is_valid &= ArrayResize(_data, _count);
}
for (int i = 0; i < _count; i++) {
IndicatorDataEntry _entry = GetEntry(_start_shift + i);
_is_valid &= _entry.IsValid();
_data[i] = _entry<T>[_mode];
}
return _is_valid;
}

/**
* Loads and validates built-in indicators whose can be used as data source.
*/
Expand Down Expand Up @@ -703,6 +746,24 @@ class Indicator : public Chart {
*/
IndicatorParams GetParams() { return iparams; }

/**
* Gets indicator's signals.
*
* When indicator values are not valid, returns empty signals.
*/
IndicatorSignal GetSignals(int _count = 3, int _shift = 0, int _mode1 = 0, int _mode2 = 0) {
bool _is_valid = true;
IndicatorDataEntry _data[];
if (!CopyEntries(_data, _count, _shift)) {
// Some copied data is invalid, so returns empty signals.
IndicatorSignal _signals(0);
return _signals;
}
// Returns signals.
IndicatorSignal _signals(_data, iparams, cparams, _mode1, _mode2);
return _signals;
}

/**
* Get indicator type.
*/
Expand Down Expand Up @@ -981,6 +1042,9 @@ class Indicator : public Chart {
is_fed = true;
}

/**
* Returns indicator value for a given shift and mode.
*/
template <typename T>
T GetValue(int _shift = 0, int _mode = -1) {
T _result;
Expand All @@ -990,6 +1054,34 @@ class Indicator : public Chart {
return _result;
}

/**
* Returns price corresponding to indicator value for a given shift and mode.
*
* Can be useful for calculating trailing stops based on the indicator.
*
* @return
* Returns price value of the corresponding indicator values.
*/
template <typename T>
T GetValuePrice(int _shift = 0, int _mode = -1, ENUM_APPLIED_PRICE _ap = PRICE_CLOSE) {
float _price = 0;
if (iparams.GetIDataValueRange() != IDATA_RANGE_PRICE) {
_price = GetPrice(_ap, _shift);
} else if (iparams.GetIDataValueRange() == IDATA_RANGE_PRICE) {
// When indicator values are the actual prices.
T _values[3];
if (!CopyValues(_values, 4, _shift, _mode)) {
// When values aren't valid, return 0.
return _price;
}
datetime _bar_time = GetBarTime(_shift);
float _value = 0;
BarOHLC _ohlc(_values, _bar_time);
_price = _ohlc.GetAppliedPrice(_ap);
}
return _price;
}

/**
* Returns values for a given shift.
*
Expand Down
2 changes: 2 additions & 0 deletions Indicator.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@

// Forward declaration.
class Indicator;
struct ChartParams;

// Includes.
#include "Chart.struct.h"
#include "Chart.struct.tf.h"
#include "Data.struct.h"
#include "DateTime.struct.h"
#include "Indicator.enum.h"
#include "Indicator.struct.signal.h"
#include "SerializerNode.enum.h"

/**
Expand Down
Loading

0 comments on commit 59df30a

Please sign in to comment.