Skip to content

Commit

Permalink
Fixes timeframe handling
Browse files Browse the repository at this point in the history
Indicator/Util: Adds more methods for iCustom5
  • Loading branch information
kenorb committed Jul 2, 2022
1 parent b010f5d commit b26dfa5
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 18 deletions.
29 changes: 23 additions & 6 deletions Indicator.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ double iCustom5(string _symbol, ENUM_TIMEFRAMES _tf, string _name, A _a, B _b, C
ICUSTOM_DEF(_handlers.Set(_key, _handle),
COMMA _a COMMA _b COMMA _c COMMA _d COMMA _e COMMA _f COMMA _g COMMA _h COMMA _i COMMA _j);
}
template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I,
typename J, typename K, typename L, typename M>
double iCustom5(string _symbol, ENUM_TIMEFRAMES _tf, string _name, A _a, B _b, C _c, D _d, E _e, F _f, G _g, H _h, I _i,
J _j, K _k, L _l, M _m, int _mode, int _shift) {
ResetLastError();
static Dict<string, int> _handlers;
string _key = Util::MakeKey(_symbol, (string)_tf, _name, _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m);
int _handle = _handlers.GetByKey(_key);
ICUSTOM_DEF(_handlers.Set(_key, _handle), COMMA _a COMMA _b COMMA _c COMMA _d COMMA _e COMMA _f COMMA _g COMMA _h
COMMA _i COMMA _j COMMA _k COMMA _l COMMA _m);
}
#endif

/**
Expand All @@ -87,9 +98,7 @@ class Indicator : public IndicatorData {
protected:
/* Protected methods */

bool Init() {
return InitDraw();
}
bool Init() { return InitDraw(); }

/**
* Initialize indicator data drawing on custom data.
Expand Down Expand Up @@ -129,7 +138,8 @@ class Indicator : public IndicatorData {
/**
* Class constructor.
*/
Indicator(const TS& _iparams, const IndicatorDataParams& _idparams, IndicatorData* _indi_src = NULL, int _indi_mode = 0)
Indicator(const TS& _iparams, const IndicatorDataParams& _idparams, IndicatorData* _indi_src = NULL,
int _indi_mode = 0)
: IndicatorData(_idparams, _indi_src, _indi_mode) {
iparams = _iparams;
Init();
Expand Down Expand Up @@ -380,7 +390,8 @@ class Indicator : public IndicatorData {
return true;
}

if (Get<ENUM_IDATA_SOURCE_TYPE>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_IDSTYPE)) == IDATA_INDICATOR && GetDataSourceRaw() == NULL && _try_initialize) {
if (Get<ENUM_IDATA_SOURCE_TYPE>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_IDSTYPE)) == IDATA_INDICATOR &&
GetDataSourceRaw() == NULL && _try_initialize) {
SetDataSource(OnDataSourceRequest());
}

Expand All @@ -402,6 +413,11 @@ class Indicator : public IndicatorData {
*/
// ENUM_TIMEFRAMES GetTf() { return Get<ENUM_TIMEFRAMES>(CHART_PARAM_TF); }

/**
* Gets indicator's time-frame.
*/
ENUM_TIMEFRAMES GetTf() { return iparams.tf.GetTf(); }

/**
* Gets indicator's signals.
*
Expand Down Expand Up @@ -605,7 +621,8 @@ class Indicator : public IndicatorData {
int _max_modes = Get<int>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_MAX_MODES));
// Print("Drawing ", GetName(), iparams.indi_data != NULL ? (" (over " + iparams.indi_data.GetName() + ")") : "");
for (int i = 0; i < _max_modes; ++i)
draw.DrawLineTo(GetName() + "_" + IntegerToString(i) + "_" + Get<string>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_SRC_MODE)),
draw.DrawLineTo(GetName() + "_" + IntegerToString(i) + "_" +
Get<string>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_SRC_MODE)),
GetBarTime(0), GetEntry(0)[i], iparams.draw_window);
}
}
Expand Down
7 changes: 0 additions & 7 deletions IndicatorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,18 +320,11 @@ class IndicatorBase : public Chart {
*/
// virtual bool ToString() = NULL; // @fixme?

/* Methods to get rid of */

/**
* Gets indicator's symbol.
*/
string GetSymbol() { return Get<string>(CHART_PARAM_SYMBOL); }

/**
* Gets indicator's time-frame.
*/
ENUM_TIMEFRAMES GetTf() { return Get<ENUM_TIMEFRAMES>(CHART_PARAM_TF); }

/* Defines MQL backward compatible methods */

double iCustom(int& _handle, string _symbol, ENUM_TIMEFRAMES _tf, string _name, int _mode, int _shift) {
Expand Down
6 changes: 3 additions & 3 deletions IndicatorData.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,8 @@ class IndicatorData : public IndicatorBase {

/* Getters */

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

if (!idparams.Get<bool>(STRUCT_ENUM(IndicatorDataParams, IDATA_PARAM_IS_FED))) {
// Calculating start_bar.
Expand Down Expand Up @@ -521,7 +521,7 @@ class IndicatorData : public IndicatorBase {
Ref<IndicatorData> _indi = FetchDataSource(_type);
if (!_indi.IsSet()) {
Alert(GetFullName(), " does not define required indicator type ", EnumToString(_type), " for symbol ",
GetSymbol(), ", and timeframe ", GetTf(), "!");
GetSymbol(), "!");
DebugBreak();
} else {
indicators.Set((int)_type, _indi);
Expand Down
4 changes: 2 additions & 2 deletions Storage/ValueStorage.indicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class IndicatorBufferValueStorage : public HistoryValueStorage<C> {
/**
* Constructor.
*/
IndicatorBufferValueStorage(IndicatorData *_indi, int _mode = 0, bool _is_series = false)
: indicator(_indi), mode(_mode), HistoryValueStorage(_indi.GetSymbol(), _indi.GetTf()) {}
IndicatorBufferValueStorage(IndicatorData *_indi, int _mode = 0, ENUM_TIMEFRAMES _tf = NULL, bool _is_series = false)
: indicator(_indi), mode(_mode), HistoryValueStorage(_indi.GetSymbol(), _tf) {}

/**
* Fetches value from a given shift. Takes into consideration as-series flag.
Expand Down
59 changes: 59 additions & 0 deletions Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,65 @@ class Util {
SerializerConversions::ValueToString(_l) + SerializerConversions::ValueToString(_m);
}

/**
* Creates string-based key using given variables.
*/
template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I,
typename J, typename K, typename L, typename M, typename N>
static string MakeKey(const A _a, const B _b, const C _c, const D _d, const E _e, const F _f, const G _g, const H _h,
const I _i, const J _j, const K _k, const L _l, const M _m, const N _n) {
return SeparatedMaybe(SerializerConversions::ValueToString(_a)) +
SeparatedMaybe(SerializerConversions::ValueToString(_b)) +
SeparatedMaybe(SerializerConversions::ValueToString(_c)) +
SeparatedMaybe(SerializerConversions::ValueToString(_d)) +
SeparatedMaybe(SerializerConversions::ValueToString(_e)) +
SeparatedMaybe(SerializerConversions::ValueToString(_f)) + SerializerConversions::ValueToString(_g) +
SerializerConversions::ValueToString(_h) + SerializerConversions::ValueToString(_i) +
SerializerConversions::ValueToString(_j) + SerializerConversions::ValueToString(_k) +
SerializerConversions::ValueToString(_l) + SerializerConversions::ValueToString(_m) +
SerializerConversions::ValueToString(_n);
}

/**
* Creates string-based key using given variables.
*/
template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I,
typename J, typename K, typename L, typename M, typename N, typename O>
static string MakeKey(const A _a, const B _b, const C _c, const D _d, const E _e, const F _f, const G _g, const H _h,
const I _i, const J _j, const K _k, const L _l, const M _m, const N _n, const O _o) {
return SeparatedMaybe(SerializerConversions::ValueToString(_a)) +
SeparatedMaybe(SerializerConversions::ValueToString(_b)) +
SeparatedMaybe(SerializerConversions::ValueToString(_c)) +
SeparatedMaybe(SerializerConversions::ValueToString(_d)) +
SeparatedMaybe(SerializerConversions::ValueToString(_e)) +
SeparatedMaybe(SerializerConversions::ValueToString(_f)) + SerializerConversions::ValueToString(_g) +
SerializerConversions::ValueToString(_h) + SerializerConversions::ValueToString(_i) +
SerializerConversions::ValueToString(_j) + SerializerConversions::ValueToString(_k) +
SerializerConversions::ValueToString(_l) + SerializerConversions::ValueToString(_m) +
SerializerConversions::ValueToString(_n) + SerializerConversions::ValueToString(_o);
}

/**
* Creates string-based key using given variables.
*/
template <typename A, typename B, typename C, typename D, typename E, typename F, typename G, typename H, typename I,
typename J, typename K, typename L, typename M, typename N, typename O, typename P>
static string MakeKey(const A _a, const B _b, const C _c, const D _d, const E _e, const F _f, const G _g, const H _h,
const I _i, const J _j, const K _k, const L _l, const M _m, const N _n, const O _o,
const P _p) {
return SeparatedMaybe(SerializerConversions::ValueToString(_a)) +
SeparatedMaybe(SerializerConversions::ValueToString(_b)) +
SeparatedMaybe(SerializerConversions::ValueToString(_c)) +
SeparatedMaybe(SerializerConversions::ValueToString(_d)) +
SeparatedMaybe(SerializerConversions::ValueToString(_e)) +
SeparatedMaybe(SerializerConversions::ValueToString(_f)) + SerializerConversions::ValueToString(_g) +
SerializerConversions::ValueToString(_h) + SerializerConversions::ValueToString(_i) +
SerializerConversions::ValueToString(_j) + SerializerConversions::ValueToString(_k) +
SerializerConversions::ValueToString(_l) + SerializerConversions::ValueToString(_m) +
SerializerConversions::ValueToString(_n) + SerializerConversions::ValueToString(_o) +
SerializerConversions::ValueToString(_p);
}

/**
* Creates string with separator if string was not empty.
*/
Expand Down

0 comments on commit b26dfa5

Please sign in to comment.