Add a new indicator¶
C++ extension point
Indicators are defined once in C++ and surface automatically in every binding. Most users don't need this guide — the built-in indicators cover the standard set.
Adding a new indicator takes one C++ class plus one line in a
registry. After those two changes it appears in C++,
Python, every binding, in lrvx.list_indicators() discovery, in the parity
tests, and in the docs site, all automatically.
1. Write the C++ class¶
Create include/lrvx/indicator/my_indicator.h. The math lives in one place:
a small state object, the kernel, whose step() takes one input and returns
the indicator's value at that position (NaN until it is ready). Inherit from
the matching streaming mixin with that kernel to get
update()/value()/ready()/reset()/count() on the same class, and
implement compute() by folding a fresh kernel over the input:
#pragma once
#include "lrvx/indicator/streaming.h"
#include <cmath>
#include <span>
#include <vector>
namespace lrvx::indicator
{
namespace detail
{
// Mean of the last `period` inputs, recomputed over a ring buffer.
class MyIndicatorKernel
{
public:
explicit MyIndicatorKernel(size_t period) : _period(period), _window(period) {}
LRVX_INDICATOR_STEP double step(double x)
{
_window.push(x);
if (_period == 0 || !_window.full())
{
return std::nan("");
}
double sum = 0.0;
for (double v : _window.window())
{
sum += v;
}
return sum / static_cast<double>(_period);
}
void reset() { _window.clear(); }
private:
size_t _period;
Ring _window;
};
} // namespace detail
class MyIndicator : public StreamingSingle<MyIndicator, detail::MyIndicatorKernel>
{
public:
explicit MyIndicator(size_t period)
: StreamingSingle(detail::MyIndicatorKernel(period)), _period(period)
{
}
std::vector<double> compute(std::span<const double> input) const
{
std::vector<double> out(input.size(), std::nan(""));
fold(detail::MyIndicatorKernel(_period), input, out);
return out;
}
size_t period() const noexcept { return _period; }
private:
size_t _period;
};
} // namespace lrvx::indicator
#include "lrvx/indicator/indicator.h"
static_assert(lrvx::indicator::SingleIndicator<lrvx::indicator::MyIndicator>);
Keep everything the kernel needs inside it and size it in the constructor:
update() must not allocate, and its cost must depend on the window, never on
how many values the indicator has seen. tests/test_indicator_streaming_cost.cpp
checks both for the built-in indicators.
The streaming mixin you choose depends on the input shape; the kernel's
step() takes the same arguments as update():
| Input | Mixin |
|---|---|
Single series (compute(span)) |
StreamingSingle<T, Kernel> |
compute(high, low, close) |
StreamingBar<T, Kernel> |
compute(high, low) |
StreamingHighLow<T, Kernel> |
compute(open, high, low, close) |
StreamingOhlc<T, Kernel> |
compute(x, y) |
StreamingPair<T, Kernel> |
| Multi-output result struct | the class holds its kernel and one getter per output (see MACD, Bollinger, Stochastic) |
2. Register it¶
Add one line to include/lrvx/indicator/registry.def:
The Kind matches the mixin you used (SingleInput, BarInput,
HighLowInput, OhlcInput, PairInput, or MultiOutput).
3. Build, regenerate docs, run tests¶
You now have:
lrvx::indicator::MyIndicatorin C++ withcompute()(batch) andupdate()/value()/ready()/reset()(streaming) on the same instance.lrvx.MyIndicator(...)in Python withcompute(arr)andupdate(v)/value/ready/reset()on the same instance.lrvx.MyIndicator(...)in Node.js — same surface, same semantics (thin N-API wrapper around the same C++ class).- A C API handle:
lrvx_streaming_indicator_create("my_indicator", args, n)creates one from the registry row, with_update*/_value/_ready/_count/_reset/_destroyforwarding to the C++ class (include the new header insrc/capi/lrvx_capi.cpp). AMultiOutputrow also needs aStreamingIndicatorOutputsspecialisation there, or it does not compile. Add the row's constructor arguments tokCasesintests/support/streaming_indicator_cases.h; the C API and QuickJS tests fail until you do, and then hold the handle against the class bit for bit. MyIndicatorin QuickJS (quickjs/lrvx/indicators.js) — a 3-line class on one of the_Stream*bases, which hold the C handle; sameupdate/value/ready/count/reset, one kernel step per update.MyIndicatorin Codon (codon/lrvx/indicators.codon) — a class around_Stream, the same handle; add it tocodon/examples/indicator_streaming_smoke.codon.- The streaming-vs-batch parity test
(
tests/test_streaming_parity.cpp) automatically covers it once you add one line to the registry-driven test list. lrvx.list_indicators()returns it (Python and Node).- The docs site lists it on every per-binding page (run
python3 scripts/gen_indicator_docs.py).
The parity is by construction: update() runs one step() of the
instance's kernel and compute() runs the same step() over the whole input
on a fresh kernel, so there is no separate streaming logic to drift. The
parity test compares every step bit for bit.
4. Reading the existing 22 indicators¶
Every indicator listed in registry.def (EMA, SMA, RSI, ATR, MACD, ...)
follows exactly this pattern. Look at any include/lrvx/indicator/<name>.h
for a working example.
Cost of a streaming update¶
An update() is one kernel step. The mixin keeps no input history, so the
cost of a step depends only on the indicator's parameters. Recursive
indicators (SMA's running sum, EMA, RMA, RSI, DEMA, TEMA, Slope, ATR, MACD)
step in O(1). Indicators that recompute their window each step to keep the
exact numerics of a two-pass computation (Skewness, Kurtosis, RollingZScore,
Correlation, AutoCorrelation, ShannonEntropy, KAMA, CCI, CHOP, Bollinger,
Stochastic, ParkinsonVol, RogersSatchellVol) step in O(period). Memory is a
ring buffer of at most the last period inputs, allocated when the indicator
is constructed.