Indicator Mathematical Testing
Relevant source files
The following files were used as context for generating this wiki page:
- packages/technical-indicators/src/macd.test.ts
- packages/technical-indicators/src/pmaxRSI.test.ts
- packages/technical-indicators/package.json
- frontend/src/klinechart/tests/technical-indicator-template1.html
- frontend/src/klinechart/tests/technical-indicator-template2.html
- frontend/src/klinechart/tests/technical-indicator.html
The mathematical testing framework within this project ensures the accuracy and reliability of technical indicators by validating their outputs against expected historical data. This testing scope encompasses unit tests for core indicator logic and integration tests within the charting environment to verify correct rendering and parameter handling.
The primary goal is to ensure that indicators—such as MACD, PMaxRSI, and RSI—produce mathematically consistent results when processing sequential candle data, supporting both static batch processing and real-time streaming updates.
Sources: packages/technical-indicators/package.json:1-15, packages/technical-indicators/src/macd.test.ts:1-10
Testing Architecture and Tooling
The project utilizes a multi-layered testing strategy involving Jest for core mathematical validation and browser-based templates for visual and integration verification.
Core Logic Testing (Jest)
The @aduryagin/technical-indicators package uses Jest as its primary test runner. These tests focus on the raw mathematical output of the indicator functions.
| Tool | Usage | Version |
|---|---|---|
| Jest | Test runner for executing unit tests | ^26.6.3 |
| Babel-Jest | Integration between Babel and Jest for TypeScript support | ^26.6.3 |
| TypeScript | Static typing for indicator logic and tests | ^4.5.4 |
Sources: packages/technical-indicators/package.json:24-29
Charting Integration Tests
HTML-based test templates are used to verify how indicators interact with the klinecharts library. These tests ensure that custom indicator templates can be added, overridden, and rendered correctly within a canvas-based viewport.
Sources: frontend/src/klinechart/tests/technical-indicator-template1.html:1-35, frontend/src/klinechart/tests/technical-indicator-template2.html:1-45
Mathematical Validation Logic
Testing involves feeding a controlled set of candle data into an indicator and asserting that the output matches a predefined expectedResult map.
Data Flow for Indicator Tests
The following diagram illustrates the flow of data during a mathematical unit test for an indicator like MACD or PMaxRSI.
The diagram shows how both batch results and incremental updates are validated against expected mathematical outputs.
Validation Methods
Tests typically verify three distinct operational modes of an indicator:
- Batch Processing: Passing an entire array of candles and checking the complete result set.
- Incremental Updates: Using the
.update()method to simulate real-time data arrival. - Historical Lookups: Retrieving a specific result by timestamp using the
result(time)method.
Sources: packages/technical-indicators/src/macd.test.ts:59-131, packages/technical-indicators/src/pmaxRSI.test.ts:89-145
Test Case Structure: MACD Example
The MACD test verifies the histogram, signal line, and cross-over states (e.g., fallBelow, growAbove).
// Example of expected state validation in MACD
[
candles[4].time,
{
candle: candles[4],
time: 5,
histogram: 0.47179012345679006,
macd: 1.8901851851851852,
signal: 1.4183950617283951,
cross: { long: true, name: "signal", time: 5 },
histogramState: "growAbove",
},
]
Sources: packages/technical-indicators/src/macd.test.ts:18-31
Comparison Table: Indicators under Test
| Indicator | Key Tested Parameters | Expected Output Fields |
|---|---|---|
| MACD | fastLength, slowLength, signalSmoothing | macd, signal, histogram, cross, histogramState |
| PMaxRSI | rsi.period, t3.period, atr.multiplier | rsi, t3, pmax, pmaxReverse, cross |
| Custom Render | params, plots | volume, barHeight (visual calculation) |
Sources: packages/technical-indicators/src/macd.test.ts:55-58, packages/technical-indicators/src/pmaxRSI.test.ts:84-88, frontend/src/klinechart/tests/technical-indicator-template2.html:36-40
Chart Template Testing
Beyond pure math, the system tests the lifecycle of indicator templates within the UI.
Indicator Lifecycle in Tests
The charting tests follow a specific sequence to ensure technical indicator templates are correctly registered and utilized.
This sequence ensures that the chart correctly identifies and executes the mathematical calculation logic (calcTechnicalIndicator) defined in the template.
Sources: frontend/src/klinechart/tests/technical-indicator-template1.html:27-38, frontend/src/klinechart/tests/technical-indicator-template2.html:30-58
Rendering Logic Verification
For indicators with custom visual requirements, the render function is tested to ensure it calculates coordinate offsets and dimensions (like barHeight and barWidth) based on the mathematical data values.
Sources: frontend/src/klinechart/tests/technical-indicator-template2.html:43-55
Conclusion
Indicator Mathematical Testing in this repository provides a robust safeguard against regression in trading logic. By combining strict Jest unit tests for high-precision calculations with charting templates for visual verification, the system ensures that traders receive accurate signals regardless of whether they are viewing historical data or streaming real-time updates.