`TBarSeries` is a high-performance collection of OHLCV bars. It is the primary data structure for managing historical and real-time market data in QuanTAlib. It uses a **Structure of Arrays (SoA)** layout to optimize memory access and enable efficient SIMD operations across individual price components.
## Design Philosophy
A naive implementation of a bar series would be a `List<TBar>`. However, this is inefficient for technical analysis. Most indicators only need one component at a time (e.g., SMA uses Close prices). Iterating over a `List<TBar>` to get Close prices loads unnecessary Open, High, Low, and Volume data into the CPU cache, wasting bandwidth.
`TBarSeries` solves this by storing each component in its own contiguous array. This allows:
* **Component Views**: You can access `Close` prices as a `TSeries` without copying data.
* **Cache Efficiency**: Iterating over `Close` prices loads *only* Close prices.
* **Unified Time**: All component series share a single Time array, ensuring synchronization.
## How It Works
Internally, `TBarSeries` maintains six parallel lists:
1.`_t` (Time)
2.`_o` (Open)
3.`_h` (High)
4.`_l` (Low)
5.`_c` (Close)
6.`_v` (Volume)
It exposes these internal lists as `TSeries` properties (`Open`, `High`, `Low`, `Close`, `Volume`), which act as read-only views into the master data.
## Structure
### Definition
```csharp
publicclassTBarSeries:IReadOnlyList<TBar>
{
// Component Views (TSeries)
publicTSeriesOpen{get;}
publicTSeriesHigh{get;}
publicTSeriesLow{get;}
publicTSeriesClose{get;}
publicTSeriesVolume{get;}
// Aliases
publicTSeriesO=>Open;
publicTSeriesH=>High;
publicTSeriesL=>Low;
publicTSeriesC=>Close;
publicTSeriesV=>Volume;
}
```
### Core Methods
| Method | Description |
| ------ | ------ |
| `Add(TBar bar, bool isNew)` | Adds a bar or updates the last one. |
| `Add(DateTime time, double o, double h, double l, double c, double v)` | Adds raw values directly. |
* **Iteration**: Cache-friendly for single-component analysis.
## Integration
`TBarSeries` is the standard input for multi-input indicators (like ATR, ADX) and the primary data source for trading strategies.
* **Indicators**: Can be passed to indicators that require full bar data.
* **Strategies**: Provides the historical context needed for signal generation.
## Architecture Notes
* **Shared Storage**: The `TSeries` views (`Open`, `Close`, etc.) do not own their data; they point to the internal lists of the `TBarSeries`. This means modifying the `TBarSeries` automatically updates all views.
* **Synchronization**: Because all views share the same `_t` (Time) list, they are guaranteed to be perfectly synchronized.
## References
* [Structure of Arrays (SoA)](https://en.wikipedia.org/wiki/AOS_and_SOA)