Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev-indi-no-chart2' into v3.007-…
Browse files Browse the repository at this point in the history
…dev-new

* origin/dev-indi-no-chart2:
  Indicator: Removes Chart inheritance (1st part of refactor) [WIP]
  Chart: ChartTf: Adds SecsToTf()
  • Loading branch information
kenorb committed May 1, 2024
2 parents 5b9c843 + fd383ca commit 0b707a1
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 20 deletions.
2 changes: 2 additions & 0 deletions EA.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ class EA : public Taskable<DataParamEntry> {
ChartEntry _entry = Chart().GetEntry();
data_chart.Add(_entry, _entry.bar.ohlc.time);
}
/* @fixme
if (eparams.CheckFlagDataStore(EA_DATA_STORE_INDICATOR)) {
for (DictStructIterator<long, Ref<Strategy>> iter = strats.Begin(); iter.IsValid(); ++iter) {
Strategy *_strati = iter.Value().Ptr();
Expand All @@ -520,6 +521,7 @@ class EA : public Taskable<DataParamEntry> {
}
}
}
*/
/*
if (eparams.CheckFlagDataStore(EA_DATA_STORE_STRATEGY)) {
for (DictStructIterator<long, Ref<Strategy>> iter = strats.Begin(); iter.IsValid(); ++iter) {
Expand Down
53 changes: 45 additions & 8 deletions Indicator.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ class Indicator : public IndicatorData {

/* Getters */

/**
* Gets an indicator's property value.
*/
template <typename T>
T Get(STRUCT_ENUM(IndicatorParams, ENUM_INDI_PARAMS_PROP) _param) const {
return iparams.Get<T>(_param);
}

/**
* Gets a value from IndicatorDataParams struct.
*/
Expand Down Expand Up @@ -192,6 +200,14 @@ class Indicator : public IndicatorData {

/* Setters */

/**
* Sets an indicator's chart parameter value.
*/
template <typename T>
void Set(STRUCT_ENUM(IndicatorParams, ENUM_INDI_PARAMS_PROP) _param, T _value) {
iparams.Set<T>(_param, _value);
}

/**
* Sets the value for IndicatorDataParams struct.
*/
Expand Down Expand Up @@ -333,6 +349,13 @@ class Indicator : public IndicatorData {
*/
// int GetModeCount() override { return (int)iparams.max_modes; }

/**
* Gets indicator's timeframe.
*/
ENUM_TIMEFRAMES GetTf() {
return ChartTf::SecsToTf(iparams.Get<uint>(STRUCT_ENUM(IndicatorParams, INDI_PARAMS_PROP_BPS)));
}

/**
* Gets indicator's params.
*/
Expand Down Expand Up @@ -447,6 +470,7 @@ class Indicator : public IndicatorData {
return false;
default:
GetLogger().Error(StringFormat("Invalid indicator condition: %s!", EnumToString(_cond), __FUNCTION_LINE__));
SetUserError(ERR_INVALID_PARAMETER);
return false;
}
}
Expand Down Expand Up @@ -475,6 +499,7 @@ class Indicator : public IndicatorData {
return true;
default:
GetLogger().Error(StringFormat("Invalid Indicator action: %s!", EnumToString(_action), __FUNCTION_LINE__));
SetUserError(ERR_INVALID_PARAMETER);
return false;
}
return _result;
Expand Down Expand Up @@ -511,14 +536,26 @@ class Indicator : public IndicatorData {
/**
* Adds entry to the indicator's buffer. Invalid entry won't be added.
*/
bool AddEntry(IndicatorDataEntry& entry, int _shift = 0) {
if (!entry.IsValid()) return false;

datetime timestamp = GetBarTime(_shift);
entry.timestamp = timestamp;
idata.Add(entry, timestamp);
bool AddEntry(IndicatorDataEntry& entry, datetime _timestamp = 0) {
if (entry.IsValid()) {
entry.timestamp = _timestamp;
idata.Add(entry, _timestamp);
return true;
}
return false;
}

return true;
/**
* Adds entry to the indicator's buffer. Invalid entry won't be added.
*/
bool AddEntry(IndicatorDataEntry& entry, int _shift = 0) {
if (entry.IsValid()) {
datetime timestamp = GetBarTime(_shift);
entry.timestamp = timestamp;
idata.Add(entry, timestamp);
return true;
}
return false;
}

/* Data representation methods */
Expand Down Expand Up @@ -606,7 +643,7 @@ class Indicator : public IndicatorData {
if (_bar_time > 0 && !_entry.IsValid() && !_entry.CheckFlag(INDI_ENTRY_FLAG_INSUFFICIENT_DATA)) {
int _max_modes = Get<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_MAX_MODES));
_entry.Resize(_max_modes);
_entry.timestamp = GetBarTime(_ishift);
_entry.timestamp = _bar_time;
#ifndef __MQL4__
if (IndicatorBase::Get<bool>(STRUCT_ENUM(IndicatorState, INDICATOR_STATE_PROP_IS_CHANGED))) {
// Resets the handle on any parameter changes.
Expand Down
38 changes: 35 additions & 3 deletions Indicator.struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,30 @@ struct ChartParams;

/* Structure for indicator parameters. */
struct IndicatorParams {
protected:
void Init() {}
public: // @todo: Change it to protected.
string name; // Name of the indicator.
int shift; // Shift (relative to the current bar, 0 - default).
uint bps; // A candle chart per number of seconds (e.g. for M1 is 60).
unsigned int max_params; // Max supported input params.
ENUM_INDICATOR_TYPE itype; // Indicator type (e.g. INDI_RSI).
color indi_color; // Indicator color.
ARRAY(DataParamEntry, input_params); // Indicator input params.
string custom_indi_name; // Name of the indicator passed to iCustom() method.
string symbol; // Symbol used by indicator.
public:
/* Enumerations */
// Defines action entry properties.
enum ENUM_INDI_PARAMS_PROP {
INDI_PARAMS_PROP_BPS,
};
public:
/* Special methods */
// Constructor.
IndicatorParams(ENUM_INDICATOR_TYPE _itype = INDI_NONE, ENUM_TIMEFRAMES _tf = PERIOD_CURRENT, string _name = "")
: custom_indi_name(""),
: bps(0),
custom_indi_name(""),
name(_name),
shift(0),
// max_modes(_max_modes),
Expand All @@ -75,9 +85,19 @@ struct IndicatorParams {
itype(_itype) {
Init();
};
IndicatorParams(string _name) : custom_indi_name(""), name(_name), shift(0) { Init(); };
void Init() {}
IndicatorParams(string _name) : bps(0), custom_indi_name(""), name(_name), shift(0) { Init(); };
/* Getters */
template <typename T>
T Get(STRUCT_ENUM(IndicatorParams, ENUM_INDI_PARAMS_PROP) _prop) const {
switch (_prop) {
case INDI_PARAMS_PROP_BPS: // Bar chart per seconds.
return (T)bps;
default:
break;
}
SetUserError(ERR_INVALID_PARAMETER);
return WRONG_VALUE;
}
string GetCustomIndicatorName() const { return custom_indi_name; }
int GetMaxParams() const { return (int)max_params; }
int GetShift() const { return shift; }
Expand Down Expand Up @@ -109,6 +129,18 @@ struct IndicatorParams {
return (T)WRONG_VALUE;
}
/* Setters */
/* Setters */
template <typename T>
void Set(STRUCT_ENUM(IndicatorParams, ENUM_INDI_PARAMS_PROP) _prop, T _value) {
switch (_prop) {
case INDI_PARAMS_PROP_BPS:
flags = (uint)_value;
return;
default:
break;
}
SetUserError(ERR_INVALID_PARAMETER);
}
void SetCustomIndicatorName(string _name) { custom_indi_name = _name; }
void SetIndicatorType(ENUM_INDICATOR_TYPE _itype) { itype = _itype; }
void SetInputParams(ARRAY_REF(DataParamEntry, _params)) {
Expand Down
4 changes: 2 additions & 2 deletions Indicators/Indi_Alligator.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ class Indi_Alligator : public Indicator<IndiAlligatorParams> {
#endif
switch (Get<ENUM_IDATA_SOURCE_TYPE>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_IDSTYPE))) {
case IDATA_BUILTIN:
_value = Indi_Alligator::iAlligator(GetSymbol(), GetTf(), GetJawPeriod(), GetJawShift(), GetTeethPeriod(),
_value = Indi_Alligator::iAlligator(_Symbol, GetTf(), GetJawPeriod(), GetJawShift(), GetTeethPeriod(),
GetTeethShift(), GetLipsPeriod(), GetLipsShift(), GetMAMethod(),
GetAppliedPrice(), (ENUM_ALLIGATOR_LINE)_mode, _ishift, THIS_PTR);
break;
case IDATA_ICUSTOM:
_value = iCustom(istate.handle, GetSymbol(), GetTf(), iparams.GetCustomIndicatorName(), /*[*/
_value = iCustom(istate.handle, _Symbol, GetTf(), iparams.GetCustomIndicatorName(), /*[*/
GetJawPeriod(), GetJawShift(), GetTeethPeriod(), GetTeethShift(), GetLipsPeriod(),
GetLipsShift(), GetMAMethod(),
GetAppliedPrice()
Expand Down
8 changes: 4 additions & 4 deletions Indicators/Indi_MA.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -655,20 +655,20 @@ class Indi_MA : public Indicator<IndiMAParams> {
int _ishift = _shift >= 0 ? _shift : iparams.GetShift();
switch (Get<ENUM_IDATA_SOURCE_TYPE>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_IDSTYPE))) {
case IDATA_BUILTIN:
_value = Indi_MA::iMA(GetSymbol(), GetTf(), GetPeriod(), GetMAShift(), GetMAMethod(), GetAppliedPrice(),
_value = Indi_MA::iMA(_Symbol, GetTf(), GetPeriod(), GetMAShift(), GetMAMethod(), GetAppliedPrice(),
_ishift, THIS_PTR);
break;
case IDATA_ONCALCULATE:
_value = Indi_MA::iMAOnIndicator(THIS_PTR, GetDataSource(), GetSymbol(), GetTf(), GetPeriod(), GetMAShift(),
_value = Indi_MA::iMAOnIndicator(THIS_PTR, GetDataSource(), _Symbol, GetTf(), GetPeriod(), GetMAShift(),
GetMAMethod(), GetAppliedPrice(), _ishift);
break;
case IDATA_ICUSTOM:
_value = iCustom(istate.handle, GetSymbol(), GetTf(), iparams.custom_indi_name, /* [ */ GetPeriod(),
_value = iCustom(istate.handle, _Symbol, GetTf(), iparams.custom_indi_name, /* [ */ GetPeriod(),
GetMAShift(), GetMAMethod(), GetAppliedPrice() /* ] */, 0, _ishift);
break;
case IDATA_INDICATOR:
// Calculating MA value from specified indicator.
_value = Indi_MA::iMAOnIndicator(THIS_PTR, GetDataSource(), GetSymbol(), GetTf(), GetPeriod(), GetMAShift(),
_value = Indi_MA::iMAOnIndicator(THIS_PTR, GetDataSource(), _Symbol, GetTf(), GetPeriod(), GetMAShift(),
GetMAMethod(), GetAppliedPrice(), _ishift);
break;
}
Expand Down
1 change: 0 additions & 1 deletion Indicators/Indi_Pivot.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ class Indi_Pivot : public Indicator<IndiPivotParams> {
// must have at least 4 buffers and define OHLC in the first 4 buffers.
// Indi_Price is an example of such indicator.
if (!HasDataSource()) {
GetLogger().Error("Invalid data source!");
SetUserError(ERR_INVALID_PARAMETER);
_is_valid &= false;
}
Expand Down
4 changes: 2 additions & 2 deletions Indicators/Special/Indi_Math.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ class Indi_Math : public Indicator<IndiMathParams> {
}
switch (iparams.op_mode) {
case MATH_OP_MODE_BUILTIN:
_value = Indi_Math::iMathOnIndicator(GetDataSource(), GetSymbol(), GetTf(),
_value = Indi_Math::iMathOnIndicator(GetDataSource(), _Symbol, GetTf(),
/*[*/ GetOpBuiltIn(), GetMode1(), GetMode2(), GetShift1(),
GetShift2() /*]*/, 0, _ishift, &this);
break;
case MATH_OP_MODE_CUSTOM_FUNCTION:
_value = Indi_Math::iMathOnIndicator(GetDataSource(), GetSymbol(), GetTf(),
_value = Indi_Math::iMathOnIndicator(GetDataSource(), _Symbol, GetTf(),
/*[*/ GetOpFunction(), GetMode1(), GetMode2(), GetShift1(),
GetShift2() /*]*/, 0, _ishift, &this);
break;
Expand Down

0 comments on commit 0b707a1

Please sign in to comment.