# Technical indicators Indicator math and strategy execution are separate concerns. Indicator helpers return values; they do not own cash, positions, order submission, account risk or recovery. ## TypeScript trailing-window math `@stock-bot/calculations/indicators` supplies the scalar operations used by the supported TypeScript SMA and mean-reversion strategies: ```ts import { movingAverageCrossover, trailingMean, trailingPopulationDeviation, } from '@stock-bot/calculations/indicators'; const prices = [3, 2, 1, 4]; const mean = trailingMean(prices, 3); const deviation = trailingPopulationDeviation(prices, 3); const crossover = movingAverageCrossover(prices, 2, 3); ``` Periods must be positive integers; prices in the requested window must be finite. Incomplete warm-up returns `undefined`, not zero. Deviation uses population variance. A crossover needs complete current and previous windows (`slowPeriod + 1` prices) and `fastPeriod < slowPeriod`. Indexed loops avoid temporary price-window arrays; this is not an incremental O(1) indicator implementation. ## Native batch and incremental indicators Portable reviewed EMA, Wilder RSI and Wilder ATR (batch and restartable streaming), plus derived chart calculations, are available from `@stock-bot/calculations/indicators`. See [the versioned contracts](../../../../libs/calculations/docs/indicators.md) before choosing a seed, flat-data policy or ATR first range. They are not drop-in aliases for every historical native behavior. Mean-reversion v2 keeps its rolling simple RSI; adopting Wilder RSI for an existing strategy requires a deliberate new version. Rust implementations live in `apps/stock/engine/src/indicators/`, exposed by `@stock-bot/engine`. Build the native engine before importing the binding. `src/indicators/TechnicalAnalysis.ts` provides typed batch wrappers for SMA, EMA, RSI, MACD, Bollinger bands, stochastic and ATR: ```ts import { TechnicalAnalysis } from '../src/indicators/TechnicalAnalysis'; const ta = new TechnicalAnalysis(); const prices = Array.from({ length: 80 }, (_, i) => 100 + Math.sin(i / 3)); const sma = ta.sma(prices, 20); const macd = ta.macd(prices, 12, 26, 9); const bands = ta.bollingerBands(prices, 20, 2); const latestMean = TechnicalAnalysis.latest(sma); ``` Do not assume every batch result has the same length or warm-up offset. For example, native MACD retains its full-length aligned series, while SMA returns only complete windows. Align different indicators by source-bar time before testing a crossover. The wrapper's `latest` returns `undefined` for an empty result. `IncrementalIndicators` wraps native incremental SMA/EMA/RSI instances: ```ts import { IncrementalIndicators } from '../src/indicators/TechnicalAnalysis'; const indicators = new IncrementalIndicators(); indicators.createSMA('AAPL.US:fast', 5); const value = indicators.update('AAPL.US:fast', 100); ``` Use a separate instance/key per instrument and parameter set. Incremental warm-up is `null`; sharing a key across instruments mixes their histories. Native indicator objects are not a complete serializable paper checkpoint. A strategy using them must define a replay-safe restoration procedure, including every custom decision input. `SignalGenerator` remains an exploratory indicator-combination utility, not a registered strategy or an order gateway. Using it does not bypass `BaseStrategy` and `ExecutionService`. ## Strategy integration and parity Implement `updateIndicators` and `generateSignal` on `BaseStrategy`; do not override the delivery loop to return orders or introduce another position/risk manager. The supported implementations are under `src/strategies/builtin/`. Follow the [strategy extension guide](architecture-improvements.md) for registration, bounded state and checkpoint requirements. Tests compare scalar SMA/population deviation with actual native batch and incremental bindings, including non-default windows and warm-up. That does not imply identical trading algorithms or RSI conventions across backends. The TypeScript mean-reversion strategy keeps its trailing-window RSI; native indicators can use a different recurrence. ## Verification and extension From the repository root: ```sh bun run build bun test test/integration/strategy-contracts.test.ts scripts/test/native-bindings.test.ts bun run test:native ``` The complete repository gate is `bun run check`. Run native builds before native-dependent tests, not concurrently with them. To add an indicator, implement and test its Rust math under `engine/src/indicators/`, expose it through `engine/src/api/indicators.rs`, rebuild the generated declarations/binary, and add the typed wrapper in `TechnicalAnalysis.ts`. Keep warm-up/alignment semantics explicit and test known values and incremental/batch equivalence where appropriate. Do not hand-edit generated declarations or claim performance improvements without measurement.