mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-17 10:08:05 +00:00
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:
@@ -0,0 +1,74 @@
|
||||
#!meta
|
||||
|
||||
{"kernelInfo":{"defaultKernelName":"csharp","items":[{"aliases":[],"languageName":"csharp","name":"csharp"}]}}
|
||||
|
||||
#!csharp
|
||||
|
||||
// Reference the library
|
||||
#r "..\..\bin\QuanTAlib.dll"
|
||||
|
||||
using QuanTAlib;
|
||||
|
||||
// 1. Creating a TBar
|
||||
// TBar represents a single OHLCV bar (Open, High, Low, Close, Volume)
|
||||
// It is an immutable struct optimized for memory and performance
|
||||
|
||||
long now = DateTime.UtcNow.Ticks;
|
||||
var bar = new TBar(now, 100.0, 105.0, 95.0, 102.0, 1000.0);
|
||||
|
||||
Console.WriteLine($"Created TBar: {bar}");
|
||||
Console.WriteLine($"Time: {bar.AsDateTime}");
|
||||
Console.WriteLine($"Open: {bar.Open}");
|
||||
Console.WriteLine($"High: {bar.High}");
|
||||
Console.WriteLine($"Low: {bar.Low}");
|
||||
Console.WriteLine($"Close: {bar.Close}");
|
||||
Console.WriteLine($"Volume: {bar.Volume}");
|
||||
|
||||
#!csharp
|
||||
|
||||
// 2. Computed Properties
|
||||
// TBar provides on-demand calculation of common price averages
|
||||
// These are calculated when accessed, saving storage space
|
||||
|
||||
Console.WriteLine($"HL2 (High+Low)/2: {bar.HL2}");
|
||||
Console.WriteLine($"OC2 (Open+Close)/2: {bar.OC2}");
|
||||
Console.WriteLine($"OHL3 (Open+High+Low)/3: {bar.OHL3}");
|
||||
Console.WriteLine($"HLC3 (High+Low+Close)/3: {bar.HLC3}");
|
||||
Console.WriteLine($"OHLC4 (Open+High+Low+Close)/4: {bar.OHLC4}");
|
||||
Console.WriteLine($"HLCC4 (High+Low+Close+Close)/4: {bar.HLCC4}");
|
||||
|
||||
#!csharp
|
||||
|
||||
// 3. TValue Accessors
|
||||
// You can efficiently access individual components as TValue (Time-Value pair)
|
||||
// This is useful when you need to treat a specific price component as a time series point
|
||||
|
||||
Console.WriteLine($"Open TValue: {bar.O}");
|
||||
Console.WriteLine($"High TValue: {bar.H}");
|
||||
Console.WriteLine($"Low TValue: {bar.L}");
|
||||
Console.WriteLine($"Close TValue: {bar.C}");
|
||||
Console.WriteLine($"Volume TValue: {bar.V}");
|
||||
|
||||
#!csharp
|
||||
|
||||
// 4. Implicit Conversions
|
||||
// TBar supports implicit conversions to double (Close price), TValue (Close), and DateTime
|
||||
|
||||
double closePrice = bar;
|
||||
TValue value = bar;
|
||||
DateTime dt = bar;
|
||||
|
||||
Console.WriteLine($"Implicit double (Close): {closePrice}");
|
||||
Console.WriteLine($"Implicit TValue (Close): {value}");
|
||||
Console.WriteLine($"Implicit DateTime: {dt}");
|
||||
|
||||
#!csharp
|
||||
|
||||
// 5. Equality and Immutability
|
||||
// Being a struct, TBar has value semantics
|
||||
|
||||
var bar2 = new TBar(now, 100.0, 105.0, 95.0, 102.0, 1000.0);
|
||||
var bar3 = new TBar(now, 101.0, 106.0, 96.0, 103.0, 1100.0);
|
||||
|
||||
Console.WriteLine($"bar equals bar2? {bar == bar2}"); // True, same values
|
||||
Console.WriteLine($"bar equals bar3? {bar == bar3}"); // False, different values
|
||||
@@ -0,0 +1,68 @@
|
||||
# TBar Struct
|
||||
|
||||
`TBar` is a lightweight, immutable struct representing a single OHLCV (Open, High, Low, Close, Volume) bar. It is designed for high-performance financial data processing with minimal memory overhead.
|
||||
|
||||
## Key Features
|
||||
|
||||
- **Memory Efficient**: Pure data type occupying exactly 48 bytes (1 `long` + 5 `double`s).
|
||||
- **Immutable**: Thread-safe by design.
|
||||
- **Zero-Copy Conversions**: Efficiently converts to `TValue` for individual price components (Open, High, Low, Close, Volume).
|
||||
- **Computed Properties**: Provides on-demand calculation of common price averages (HL2, HLC3, etc.) without storage overhead.
|
||||
- **SIMD Compatible**: Layout is optimized for potential vectorization in collection types.
|
||||
|
||||
## Structure Definition
|
||||
|
||||
```csharp
|
||||
public readonly struct TBar : IEquatable<TBar>
|
||||
{
|
||||
public readonly long Time; // Unix ticks
|
||||
public readonly double Open;
|
||||
public readonly double High;
|
||||
public readonly double Low;
|
||||
public readonly double Close;
|
||||
public readonly double Volume;
|
||||
}
|
||||
```
|
||||
|
||||
## Properties
|
||||
|
||||
| Property | Type | Description |
|
||||
|----------|------|-------------|
|
||||
| `Time` | `long` | Timestamp in ticks. |
|
||||
| `Open` | `double` | Opening price. |
|
||||
| `High` | `double` | Highest price. |
|
||||
| `Low` | `double` | Lowest price. |
|
||||
| `Close` | `double` | Closing price. |
|
||||
| `Volume` | `double` | Traded volume. |
|
||||
| `AsDateTime` | `DateTime` | `Time` converted to UTC DateTime. |
|
||||
|
||||
### Computed Averages
|
||||
These properties are calculated on the fly:
|
||||
- `HL2`: (High + Low) / 2
|
||||
- `OC2`: (Open + Close) / 2
|
||||
- `OHL3`: (Open + High + Low) / 3
|
||||
- `HLC3`: (High + Low + Close) / 3
|
||||
- `OHLC4`: (Open + High + Low + Close) / 4
|
||||
- `HLCC4`: (High + Low + Close + Close) / 4
|
||||
|
||||
### TValue Accessors
|
||||
Efficiently access components as `TValue` (Time-Value pair):
|
||||
- `O`: (Time, Open)
|
||||
- `H`: (Time, High)
|
||||
- `L`: (Time, Low)
|
||||
- `C`: (Time, Close)
|
||||
- `V`: (Time, Volume)
|
||||
|
||||
## Usage
|
||||
|
||||
### Creating a TBar
|
||||
```csharp
|
||||
long now = DateTime.UtcNow.Ticks;
|
||||
var bar = new TBar(now, 100.0, 105.0, 95.0, 102.0, 1000.0);
|
||||
```
|
||||
|
||||
### Implicit Conversions
|
||||
```csharp
|
||||
double closePrice = bar; // Implicitly converts to Close price
|
||||
TValue value = bar; // Implicitly converts to (Time, Close)
|
||||
DateTime dt = bar; // Implicitly converts to DateTime
|
||||
Reference in New Issue
Block a user