Skip to content

Commit

Permalink
Probably finished IndicatorDataEntry per-value type refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
nseam committed Nov 3, 2021
1 parent 587d149 commit fc5eccf
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 32 deletions.
1 change: 0 additions & 1 deletion Indicator.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,6 @@ class Indicator : public IndicatorBase {
SetUserError(ERR_INVALID_PARAMETER);
break;
}
_entry.values[_mode] = GetValue<double>(_mode, _ishift);
}
GetEntryAlter(_entry, _ishift);
_entry.SetFlag(INDI_ENTRY_FLAG_IS_VALID, IsValidEntry(_entry));
Expand Down
11 changes: 9 additions & 2 deletions Indicator.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct IndicatorDataEntryValue {

// Sets type of the value.
void SetDataType(ENUM_DATATYPE _type) {
// CLearing type.
// Clearing type.
flags &= 0x0F;

// Setting type.
Expand Down Expand Up @@ -142,7 +142,9 @@ struct IndicatorDataEntryValue {
void Get(double &_out) { _out = value.vdbl; }
void Get(float &_out) { _out = value.vflt; }
void Get(int &_out) { _out = value.vint; }
void Get(unsigned int &_out) { _out = (unsigned int)value.vint; }
void Get(long &_out) { _out = value.vlong; }
void Get(unsigned long &_out) { _out = (unsigned long)value.vint; }
// Setters.
template <typename T>
void Set(T _value) {
Expand Down Expand Up @@ -377,7 +379,12 @@ struct IndicatorDataEntry {
// State checkers.
bool IsValid() { return CheckFlags(INDI_ENTRY_FLAG_IS_VALID); }
// Serializers.
void SerializeStub(int _n1 = 1, int _n2 = 1, int _n3 = 1, int _n4 = 1, int _n5 = 1) { ArrayResize(values, _n1); }
void SerializeStub(int _n1 = 1, int _n2 = 1, int _n3 = 1, int _n4 = 1, int _n5 = 1) {
ArrayResize(values, _n1);
for (int i = 0; i < _n1; ++i) {
values[i] = (int)1;
}
}
SerializerNodeType Serialize(Serializer &_s);
template <typename T>
string ToCSV() {
Expand Down
28 changes: 18 additions & 10 deletions IndicatorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,24 @@ class IndicatorBase : public Chart {
/**
* Class constructor.
*/
IndicatorBase() : indi_src(NULL) { is_fed = false; }
IndicatorBase() : indi_src(NULL) {
calc_start_bar = 0;
is_fed = false;
}

/**
* Class constructor.
*/
IndicatorBase(ChartParams& _cparams) : indi_src(NULL), Chart(_cparams) { is_fed = false; }
IndicatorBase(ChartParams& _cparams) : indi_src(NULL), Chart(_cparams) {
calc_start_bar = 0;
is_fed = false;
}

/**
* Class constructor.
*/
IndicatorBase(ENUM_TIMEFRAMES _tf = PERIOD_CURRENT, string _symbol = NULL) : Chart(_tf, _symbol) {
calc_start_bar = 0;
is_fed = false;
indi_src = NULL;
}
Expand All @@ -120,6 +127,7 @@ class IndicatorBase : public Chart {
* Class constructor.
*/
IndicatorBase(ENUM_TIMEFRAMES_INDEX _tfi, string _symbol = NULL) : Chart(_tfi, _symbol) {
calc_start_bar = 0;
is_fed = false;
indi_src = NULL;
}
Expand Down Expand Up @@ -988,31 +996,31 @@ class IndicatorBase : public Chart {
IndicatorDataEntry _stub_entry;
_stub_entry.AddFlags(_entry.GetFlags());
SerializerConverter _stub = SerializerConverter::MakeStubObject(_stub_entry, _serializer_flags, _entry.GetSize());

return SerializerConverter::FromObject(_entry, _serializer_flags).ToString<SerializerCsv>(0, &_stub);
}

int GetBarsCalculated() {
int _bars = Bars(GetSymbol(), GetTf());

if (!is_fed) {
calc_start_bar = 0;

// Calculating start_bar.
for (int i = 0; i < _bars; ++i) {
// Iterating from the oldest.
IndicatorDataEntry _entry = GetEntry(_bars - i - 1);
for (; calc_start_bar < _bars; ++calc_start_bar) {
// Iterating from the oldest or previously iterated.
IndicatorDataEntry _entry = GetEntry(_bars - calc_start_bar - 1);

if (_entry.IsValid()) {
// From this point we assume that future entries will be all valid.
calc_start_bar = i;
is_fed = true;

return _bars - calc_start_bar;
}
}
}

if (!is_fed) {
Print("Can't find valid bars for ", GetFullName());
return 0;
}

// Assuming all entries are calculated (even if have invalid values).
return _bars;
}
Expand Down
1 change: 0 additions & 1 deletion Indicators/Indi_AC.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ double iAC(string _symbol, int _tf, int _shift) { return Indi_AC::iAC(_symbol, (
struct IndiACParams : IndicatorParams {
// Struct constructor.
IndiACParams(int _shift = 0) : IndicatorParams(INDI_AC, 1, TYPE_DOUBLE) {
SetDataSourceType(IDATA_ICUSTOM);
SetDataValueRange(IDATA_RANGE_MIXED);
SetCustomIndicatorName("Examples\\Accelerator");
shift = _shift;
Expand Down
1 change: 0 additions & 1 deletion Indicators/Indi_AD.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ struct IndiADParams : IndicatorParams {
IndiADParams(int _shift = 0) : IndicatorParams(INDI_AD, 1, TYPE_DOUBLE) {
SetDataValueRange(IDATA_RANGE_MIXED);
SetCustomIndicatorName("Examples\\AD");
SetDataSourceType(IDATA_ICUSTOM);
shift = _shift;
};
IndiADParams(IndiADParams &_params, ENUM_TIMEFRAMES _tf) {
Expand Down
2 changes: 1 addition & 1 deletion Indicators/Indi_Alligator.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class Indi_Alligator : public Indicator<IndiAlligatorParams> {
#ifdef __MQL4__
if (_mode == 0) {
// In MQL4 mode 0 should be treated as mode 1 as Alligator buffers starts from index 1.
return GetValue((ENUM_ALLIGATOR_LINE)1, _shift);
return GetMixedValue((ENUM_ALLIGATOR_LINE)1, _shift);
}
#endif
double _value = EMPTY_VALUE;
Expand Down
2 changes: 1 addition & 1 deletion Indicators/Indi_BWMFI.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class Indi_BWMFI : public Indicator<IndiBWIndiMFIParams> {
#ifdef __MQL4__
// @see: https://en.wikipedia.org/wiki/Market_facilitation_index
bool _vol_up = GetVolume(_shift) > GetVolume(_shift);
bool _val_up = GetValue(BWMFI_BUFFER, _shift) > GetValue(BWMFI_BUFFER, _shift);
bool _val_up = GetValue<double>(BWMFI_BUFFER, _shift) > GetValue<double>(BWMFI_BUFFER, _shift);
double _histcolor = EMPTY_VALUE;
switch (_vol_up) {
case true:
Expand Down
2 changes: 1 addition & 1 deletion Indicators/Indi_Envelopes.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class Indi_Envelopes : public Indicator<IndiEnvelopesParams> {
Indicator<IndiEnvelopesParams>::GetEntryAlter(_entry);
#ifdef __MQL4__
// The LINE_MAIN only exists in MQL4 for Envelopes.
_entry.values[LINE_MAIN] = GetValue((ENUM_LO_UP_LINE)LINE_MAIN, _shift);
_entry.values[LINE_MAIN] = GetValue<double>((ENUM_LO_UP_LINE)LINE_MAIN, _shift);
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion Indicators/Indi_Killzones.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class Indi_Killzones : public Indicator<IndiKillzonesParams> {
/**
* Returns the indicator's value.
*/
float GetValue(unsigned int _mode, int _shift = 0) {
IndicatorDataEntryValue GetMixedValue(int _mode = 0, int _shift = 0) {
float _value = FLT_MAX;
int _index = (int)_mode / 2;
switch (iparams.idstype) {
Expand Down
1 change: 0 additions & 1 deletion Indicators/Indi_RSI.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ class Indi_RSI : public Indicator<IndiRSIParams> {
*/
virtual IndicatorDataEntryValue GetMixedValue(int _mode = 0, int _shift = 0) {
double _value = EMPTY_VALUE;
int _bars_calc;
double _res[];
switch (iparams.idstype) {
case IDATA_BUILTIN:
Expand Down
10 changes: 5 additions & 5 deletions Refs.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ struct Ref {
}
// Dropping strong reference.
if (!--ptr_object.ptr_ref_counter.num_strong_refs) {
#ifdef __debug__
#ifdef __debug_ref__
Print(ptr_object.ptr_ref_counter.Debug());
#endif

Expand Down Expand Up @@ -211,7 +211,7 @@ struct Ref {
return Ptr();
}
++ptr_object.ptr_ref_counter.num_strong_refs;
#ifdef __debug__
#ifdef __debug_ref__
Print(ptr_object.ptr_ref_counter.Debug());
#endif
}
Expand Down Expand Up @@ -300,7 +300,7 @@ struct WeakRef {

ptr_ref_counter = _ptr.ptr_ref_counter;

#ifdef __debug__
#ifdef __debug_ref__
Print(ptr_ref_counter.Debug());
#endif

Expand Down Expand Up @@ -338,7 +338,7 @@ struct WeakRef {
// Dropping weak reference.
if (!--ptr_ref_counter.num_weak_refs) {
// No more weak references.
#ifdef __debug__
#ifdef __debug_ref__
Print(ptr_ref_counter.Debug());
#endif

Expand All @@ -350,7 +350,7 @@ struct WeakRef {
// Avoiding double deletion in Dynamic's destructor.
ptr_ref_counter.ptr_object.ptr_ref_counter = NULL;

#ifdef __debug__
#ifdef __debug_ref__
Print("Refs: Deleting object ", ptr_ref_counter.ptr_object);
#endif

Expand Down
8 changes: 8 additions & 0 deletions SerializerConverter.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ class SerializerConverter {

SerializerNode* Node() { return root_node; }

string ToDebugString(int _json_flags = 0) {
if (root_node == NULL) {
return "<NULL>";
}

return root_node.ToString(_json_flags);
}

template <typename X>
static SerializerConverter FromObject(X& _value, int serializer_flags = SERIALIZER_FLAG_INCLUDE_ALL) {
Serializer _serializer(NULL, Serialize, serializer_flags);
Expand Down
12 changes: 5 additions & 7 deletions tests/IndicatorsTest.mq5
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,13 @@ void OnTick() {
IndicatorDataEntry _entry(_indi.GetEntry());
if (_indi.Get<bool>(STRUCT_ENUM(IndicatorState, INDICATOR_STATE_PROP_IS_READY))) {
if (_entry.IsValid()) {
PrintFormat("%s: bar %d: %s", _indi.GetFullName(), bar_processed, _indi.ToString());
// tested.Set(iter.Key(), true); // Mark as tested.
// indis.Unset(iter.Key());
} else if (bar_processed > 20) {
DebugBreak();
PrintFormat("%s: bar %d: %s", _indi.GetFullName(), bar_processed, _indi.ToString());
tested.Set(iter.Key(), true); // Mark as tested.
indis.Unset(iter.Key());
}
}
}
}
}
}

/**
Expand Down Expand Up @@ -535,7 +533,7 @@ bool InitIndicators() {
}

// Push white-listed indicators here.
whitelisted_indis.Push(_indi_test);
// whitelisted_indis.Push(_indi_test);

return GetLastError() == ERR_NO_ERROR;
}
Expand Down

0 comments on commit fc5eccf

Please sign in to comment.