QuanTAlib employs a **Tri-Modal Architecture** to unify high-performance batch processing with low-latency streaming updates. This design segregates the indicator lifecycle into three distinct mathematical modes, solving the "two-world problem" of quantitative finance (backtesting vs. live trading).
All indicators inherit from `AbstractBase` and implement the `ITValuePublisher` interface, ensuring a consistent API across the entire library.
> **Note:** The examples below use `Sma` (Simple Moving Average), but this pattern applies to all indicators in the library.
## 1. Core Interface (`AbstractBase`)
Every indicator exposes the following core properties and methods:
A wrapper for `TSeries` objects that returns a new series with aligned timestamps.
```csharp
TSerieshistory=...;
TSeriessma=Sma.Batch(history,14);
```
---
## 3. Mode B: Streaming (Stateful)
**Purpose:** Live Trading, Event Processing
**Method:**`Update`
Streaming mode handles real-time data ingestion using O(1) complexity per update. It maintains internal state (circular buffers, running sums) to process ticks with minimal latency.
Handles intra-bar updates (re-calculation of the current bar) without corrupting state.
```csharp
// New bar opens
indicator.Update(newTValue(t,100),isNew:true);
// Price updates within the same bar (correction)
indicator.Update(newTValue(t,101),isNew:false);
indicator.Update(newTValue(t,102),isNew:false);
// Next bar opens
indicator.Update(newTValue(t+1,105),isNew:true);
```
### Reactive Chaining
Indicators can subscribe to other `ITValuePublisher` sources (like `TSeries` or other indicators).
```csharp
TSeriessource=...;
// Chain: Source -> SMA(14) -> EMA(5)
varsma=newSma(source,14);
varema=newEma(sma,5);
// Updates flow automatically
source.Add(newTValue(time,price));
// sma updates, then ema updates automatically
```
---
## 4. Mode C: Priming (The Bridge)
**Purpose:** Switching from Batch to Streaming
**Method:**`Prime`
Priming mode hydrates a streaming instance using the minimal required tail of historical data. It calculates the intersection of *History Available* and *State Required*, allowing an indicator to become "Hot" without processing the entire history.
```csharp
// Signature
publicvoidPrime(ReadOnlySpan<double>source);
// Usage
varindicator=newSma(14);
double[]history=...;// e.g., 100,000 bars
// Efficiently processes only the last 'period' bars needed to fill the buffer
// O(Warmup) initialization instead of O(History)
indicator.Prime(history);
// Indicator is now "Hot" and ready for the next live tick
Console.WriteLine(indicator.IsHot);// true
```
---
## 5. One-Shot Hybrid (`Calculate`)
A high-level helper that combines Batch and Priming modes. It calculates the entire history and returns a "hot" instance ready for immediate real-time updates.