Skip to content

Commit

Permalink
Fixes problem with not preserving TF passed to Candle indicators.
Browse files Browse the repository at this point in the history
  • Loading branch information
nseam committed Jun 15, 2023
1 parent b246c9d commit 78d8634
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
5 changes: 1 addition & 4 deletions Indicator/IndicatorTf.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@
template <typename TFP>
class IndicatorTf : public IndicatorCandle<TFP, double, ItemsHistoryTfCandleProvider<double>> {
protected:
// Time-frame used to create candles.
ENUM_TIMEFRAMES tf;

/* Protected methods */

/**
Expand Down Expand Up @@ -80,7 +77,7 @@ class IndicatorTf : public IndicatorCandle<TFP, double, ItemsHistoryTfCandleProv
/**
* Gets indicator's time-frame.
*/
ENUM_TIMEFRAMES GetTf() override { return tf; }
ENUM_TIMEFRAMES GetTf() override { return THIS_ATTR iparams.tf.GetTf(); }

/**
* Returns current tick index (incremented every OnTick()).
Expand Down
23 changes: 19 additions & 4 deletions Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ class Platform {
*/
static void Add(IndicatorData *_indi) {
Ref<IndicatorData> _ref = _indi;

DictStructIterator<long, Ref<IndicatorData>> _iter;
for (_iter = indis_dflt.Begin(); _iter.IsValid(); ++_iter) {
if (_iter.Value() == _ref) {
Alert("Warning: ", _indi PTR_DEREF GetFullName(),
" was already added as default candle/tick indicator and shouldn't be added by Platform:Add() as default "
"indicators are also ticked when calling Platform::Tick().");
DebugBreak();
}
}

indis.Set(_indi PTR_DEREF GetId(), _ref);
}

Expand Down Expand Up @@ -333,11 +344,15 @@ class Platform {
// Adding indicator to list of default indicators in order to tick it on every Tick() call.
Ref<IndicatorData> _ref = _indi_candle;
indis_dflt.Set(_indi_candle PTR_DEREF GetId(), _ref);
}

if (!_indi_candle PTR_DEREF HasDataSource()) {
// Missing tick indicator.
_indi_candle PTR_DEREF InjectDataSource(FetchDefaultTickIndicator(_symbol));
if (!_indi_candle PTR_DEREF HasDataSource()) {
// Missing tick indicator.
_indi_candle PTR_DEREF InjectDataSource(FetchDefaultTickIndicator(_symbol));
}
#ifdef __debug__
Print("Added default candle indicator for symbol ", _symbol, " and time-frame ", _tf, ". Now it has symbol ",
_indi_candle PTR_DEREF GetSymbol(), " and time-frame ", EnumToString(_indi_candle PTR_DEREF GetTf()));
#endif
}

return _indi_candle;
Expand Down
9 changes: 4 additions & 5 deletions tests/TradeTest.mq5
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ bool _finish_test = false;
*/
int OnInit() {
Platform::Init();
_chart_m1 = Platform::FetchDefaultCandleIndicator(_Symbol, PERIOD_M1);
_chart_m5 = Platform::FetchDefaultCandleIndicator(_Symbol, PERIOD_M5);
Platform::Add(_chart_m1.Ptr());
Platform::Add(_chart_m5.Ptr());
_chart_m1 = Platform::FetchDefaultCandleIndicator("EURUSD", PERIOD_M1);
_chart_m5 = Platform::FetchDefaultCandleIndicator("EURUSD", PERIOD_M5);

return INIT_SUCCEEDED;
}

Expand Down Expand Up @@ -83,7 +82,7 @@ int Test() {
// Test market.
assertTrueOrFail(trade1 PTR_DEREF IsTradeAllowed(), "Trade not allowed!");
assertTrueOrFail(trade1 PTR_DEREF GetSource() PTR_DEREF GetTf() == PERIOD_M1,
StringFormat("Fail on GetTf() => [%s]!", trade1 PTR_DEREF GetSource() PTR_DEREF GetTf()));
StringFormat("Fail on GetTf() => [%d]!", trade1 PTR_DEREF GetSource() PTR_DEREF GetTf()));
assertTrueOrFail(trade1 PTR_DEREF GetSource() PTR_DEREF GetOpen() > 0, "Fail on GetOpen()!");
assertTrueOrFail(trade1 PTR_DEREF GetSource() PTR_DEREF GetSymbol() == _Symbol, "Fail on GetSymbol()!");
// assertTrueOrFail(trade1.IsTradeAllowed(), "Fail on IsTradeAllowed()!"); // @fixme
Expand Down

0 comments on commit 78d8634

Please sign in to comment.