[BUG] DEMA, TEMA and MACD never return a value; Stochastic %D silently equals %K

Author: LudwigJMarxCreated Sep 20, 2026Updated Sep 20, 2026

What happened?

Four indicators in the algo builder are unusable, all from one cause in fincept-qt/src/algo_engine/IndicatorEngine.cpp.

sma_series() and ema_series() both emit a series whose first period - 1 samples are NaN, and both seed their own window unconditionally at index 0:

cpp
for (int i = 0; i < period; ++i)
    sum += src[i];
out[period - 1] = sum / period;

Four call sites feed exactly such a NaN-prefixed series back in:

caller call input
compute_dema ema_series(e1, period) EMA of an EMA
compute_tema ema_series(e2, period) EMA of an EMA of an EMA
compute_macd ema_series(macd_line, signal_period) signal line over the MACD line
compute_stochastic sma_series(k_vals, d_period) %D over %K

One NaN in the seed window makes the seed NaN. It never leaves the rolling sum (sum += src[i] - src[i - period]) and never leaves the EMA recursion (out[i] = (src[i] - out[i - 1]) * mult + out[i - 1]), so the entire output is NaN however long the input is.

Effect, with the catalogue defaults from AlgoTradingTypes.h:

indicator effect
DEMA never returns a value unless period == 1
TEMA never returns a value unless period == 1
MACD never returns a value unless slow == 1
STOCHASTIC %D silently falls back to %K unless k_period == 1

All four are selectable in the strategy builder. ConditionEvaluator::operand_value turns the first three into NaN plus "Insufficient data for ...", so the condition never fires no matter how much history is loaded. The fourth is worse: a wrong number with no error at all.

Expected: with 500 bars and period = 20, DEMA, TEMA and MACD return finite values, and Stochastic's %D is the d_period average of %K.

Steps to Reproduce

  1. Build the unit tests: cmake -B build -S fincept-qt -DFINCEPT_BUILD_TESTS=ON -DCMAKE_PREFIX_PATH=<Qt 6.8.3>
  2. Add a Qt Test target for IndicatorEngine.cpp and call IndicatorEngine::compute("DEMA", candles, {{"period", 20}}, "value") with 500 synthetic candles.
  3. r.valid is false and r.error reads Insufficient data for DEMA. Same for TEMA, and for MACD with the default 12 / 26 / 9.
  4. For Stochastic on an oscillating series, current["d"] equals current["k"] instead of their 3-bar average.

In the UI: build an algo condition on MACD or DEMA and run it against any symbol with full history. It reports insufficient data on every bar.

Environment

  • OS: macOS 26.6.2, arm64 (the code path is platform independent)
  • Fincept Terminal Version: main at b7d850b49dc033bb133e6e5d2476444ac5c422b1, present since fa7f840f (2026-05-27)
  • Qt Version: 6.8.3

Error Logs

FAIL!  : dema_produces_a_value_from_ample_history() 'r.valid' returned FALSE. (Insufficient data for DEMA)
FAIL!  : tema_produces_a_value_from_ample_history() 'r.valid' returned FALSE. (Insufficient data for TEMA)
FAIL!  : macd_produces_a_signal_line_from_ample_history() 'r.valid' returned FALSE. (Insufficient data for MACD)
FAIL!  : stochastic_d_is_the_average_of_the_last_k_values() Compared doubles are not the same (fuzzy compare)
   Actual   (d)                         : 40.5600865331
   Expected ((k[0] + k[1] + k[2]) / 3.0): 59.3901506623

I have a fix and a tst_indicator_engine suite that is red on the four cases above and green after it, plus the three existing suites still passing. Per .github/CONTRIBUTING.md I am waiting for a scope label before opening the PR.

Source: Fincept-Corporation/FinceptTerminal