Add TBar, TBarSeries, TSeries, TValue, and IFeed implementations with comprehensive documentation and examples

- Introduced TBar struct for efficient OHLCV data representation.
- Implemented TBarSeries class for high-performance collection of TBar instances using Structure of Arrays (SoA) layout.
- Added TSeries class for time-series data management with zero-copy access.
- Created TValue struct for time-value pairs with implicit conversions.
- Defined IFeed interface for consistent data feed implementations.
- Developed CsvFeed class for loading historical OHLCV data from CSV files.
- Implemented GBM class for generating synthetic financial data using Geometric Brownian Motion.
- Added Quantower project files for Averages indicator with necessary dependencies and configurations.
- Included extensive usage examples and notebooks for TBar, TBarSeries, TSeries, TValue, and feed implementations.
This commit is contained in:
Miha Kralj
2025-11-27 19:51:43 -08:00
parent 1c8f514756
commit 74b49d2bb4
37 changed files with 1379 additions and 462 deletions
+84
View File
@@ -0,0 +1,84 @@
#!meta
{"kernelInfo":{"defaultKernelName":"csharp","items":[{"name":"csharp"},{"name":"fsharp","languageName":"F#","aliases":["f#","fs"]},{"name":"html","languageName":"HTML"},{"name":"http","languageName":"HTTP"},{"name":"javascript","languageName":"JavaScript","aliases":["js"]},{"name":"mermaid","languageName":"Mermaid"},{"name":"pwsh","languageName":"PowerShell","aliases":["powershell"]},{"name":"value"}]}}
#!markdown
# TSeries Examples
This notebook demonstrates the usage of `TSeries`, the high-performance time series container in QuanTAlib.
For detailed documentation, see [TSeries.md](TSeries.md).
#!csharp
// Reference the library
#r "..\..\bin\QuanTAlib.dll"
using System;
using QuanTAlib;
#!markdown
## Creating and Adding Data
`TSeries` supports adding data via `DateTime` or `ticks`.
#!csharp
var series = new TSeries();
var now = DateTime.UtcNow;
// Add new values
series.Add(now, 10.0);
series.Add(now.AddMinutes(1), 11.0);
series.Add(now.AddMinutes(2), 12.0);
Console.WriteLine($"Count: {series.Count}");
Console.WriteLine($"Last Value: {series.Last.Value}");
#!markdown
## Streaming Updates (`isNew`)
In real-time scenarios, you often receive updates for the *current* bar before it closes. `TSeries` handles this via the `isNew` parameter.
#!csharp
var streamSeries = new TSeries();
long t = DateTime.UtcNow.Ticks;
// 1. New Bar
streamSeries.Add(t, 100.0, isNew: true);
Console.WriteLine($"New Bar: Count={streamSeries.Count}, Last={streamSeries.Last.Value}");
// 2. Update Current Bar (Price moves to 101.0)
streamSeries.Add(t, 101.0, isNew: false);
Console.WriteLine($"Update: Count={streamSeries.Count}, Last={streamSeries.Last.Value}");
// 3. Update Current Bar (Price moves to 100.5)
streamSeries.Add(t, 100.5, isNew: false);
Console.WriteLine($"Update: Count={streamSeries.Count}, Last={streamSeries.Last.Value}");
// 4. New Bar (Next minute)
streamSeries.Add(t + TimeSpan.TicksPerMinute, 102.0, isNew: true);
Console.WriteLine($"New Bar: Count={streamSeries.Count}, Last={streamSeries.Last.Value}");
#!markdown
## Zero-Copy Access (Spans)
You can access the underlying data arrays directly as `ReadOnlySpan<T>` for high-performance processing.
#!csharp
// Access Values as Span
Console.WriteLine("Values in Span:");
foreach (var v in series.Values)
{
Console.Write($"{v} ");
}
Console.WriteLine();
// Access Times as Span
Console.WriteLine($"First Time: {new DateTime(series.Times[0])}");
+56
View File
@@ -0,0 +1,56 @@
# TSeries: Time Series Data
## Overview
`TSeries` is a high-performance container for time-series data. Unlike a standard `List<TValue>`, it uses a **Structure of Arrays (SoA)** layout internally. This means it stores timestamps and values in separate contiguous arrays (`List<long>` and `List<double>`).
This layout is critical for performance because it allows:
1. **SIMD Optimization**: The `Values` property returns a `ReadOnlySpan<double>` that can be directly processed by CPU vector instructions (AVX/SSE).
2. **Cache Locality**: Iterating over values doesn't load timestamps into the CPU cache, and vice versa.
## Structure
```csharp
public class TSeries : IReadOnlyList<TValue>
{
// Internal SoA storage
protected readonly List<long> _t;
protected readonly List<double> _v;
// Public accessors
public ReadOnlySpan<double> Values => ...; // Zero-copy access
public ReadOnlySpan<long> Times => ...; // Zero-copy access
public TValue Last { get; }
public int Count { get; }
}
```
## Key Features
* **SoA Layout**: Optimized for numerical computing and SIMD.
* **Zero-Copy Access**: `Values` and `Times` properties expose internal storage as Spans without copying.
* **Streaming Support**: The `Add` method supports `isNew` parameter to handle intra-bar updates (replacing the last value instead of appending).
* **Event Publishing**: Optional `Pub` event for reactive pipelines.
## Usage
### Creating and Adding Data
```csharp
var series = new TSeries();
series.Add(DateTime.Now, 100.0); // isNew=true by default
```
### Streaming Updates
```csharp
// New bar
series.Add(time, 100.0, isNew: true);
// Update current bar (e.g. price change within same minute)
series.Add(time, 101.0, isNew: false);
```
### SIMD Processing
```csharp
// Calculate average using SIMD
double avg = series.Values.AverageSIMD();