mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-13 16:18: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,68 @@
|
||||
#!meta
|
||||
|
||||
{"kernelInfo":{"defaultKernelName":"csharp","items":[{"name":"csharp","languageName":"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"}]}}
|
||||
|
||||
#!csharp
|
||||
|
||||
// Reference the library
|
||||
#r "..\..\bin\QuanTAlib.dll"
|
||||
|
||||
using QuanTAlib;
|
||||
using System.IO;
|
||||
|
||||
// 1. Setup: Use existing CSV file
|
||||
// CsvFeed expects a CSV with header: timestamp,open,high,low,close,volume
|
||||
// Timestamp format: YYYY-MM-DD
|
||||
|
||||
string csvPath = "daily_IBM.csv";
|
||||
Console.WriteLine($"Using CSV file: {csvPath}");
|
||||
|
||||
#!csharp
|
||||
|
||||
// 2. Initialize CsvFeed
|
||||
// The feed loads the data and prepares it for streaming
|
||||
|
||||
var feed = new CsvFeed(csvPath);
|
||||
Console.WriteLine("CsvFeed initialized.");
|
||||
|
||||
#!csharp
|
||||
|
||||
// 3. Streaming Data
|
||||
// Simulate processing historical data bar by bar
|
||||
|
||||
Console.WriteLine("\nStreaming data (first 5 bars):");
|
||||
int count = 0;
|
||||
bool isNew = true;
|
||||
|
||||
// Get first bar
|
||||
var bar = feed.Next(isNew: true);
|
||||
|
||||
while (isNew && count < 5)
|
||||
{
|
||||
count++;
|
||||
Console.WriteLine($" Bar {count}: {bar}");
|
||||
|
||||
// Get next bar
|
||||
bar = feed.Next(ref isNew);
|
||||
}
|
||||
|
||||
Console.WriteLine($"Streamed {count} bars.");
|
||||
|
||||
#!csharp
|
||||
|
||||
// 4. Batch Fetching
|
||||
// Retrieve a specific range of data
|
||||
|
||||
Console.WriteLine("\nBatch fetching:");
|
||||
// Using a date range present in daily_IBM.csv (July 2025)
|
||||
long startTime = new DateTime(2025, 7, 8).Ticks;
|
||||
var interval = TimeSpan.FromDays(1);
|
||||
|
||||
// Fetch 3 bars starting from July 8th, 2025
|
||||
var batch = feed.Fetch(5, startTime, interval);
|
||||
|
||||
Console.WriteLine($"Fetched {batch.Count} bars:");
|
||||
foreach (var b in batch)
|
||||
{
|
||||
Console.WriteLine($" {b}");
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
# CsvFeed Class
|
||||
|
||||
`CsvFeed` is a file-based feed implementation that loads historical OHLCV data from CSV files. It supports both streaming access (simulating real-time playback) and batch retrieval.
|
||||
|
||||
## Key Features
|
||||
|
||||
- **Historical Data Loading**: Reads standard OHLCV CSV files.
|
||||
- **Chronological Ordering**: Automatically reverses data if needed (assumes newest-first in file, provides oldest-first).
|
||||
- **Streaming Interface**: Implements `IFeed` for consistent usage with other feed types.
|
||||
- **Batch Retrieval**: Supports fetching specific time ranges via `Fetch()`.
|
||||
|
||||
## CSV Format Requirements
|
||||
|
||||
The file must have a header row and follow this column order:
|
||||
`timestamp, open, high, low, close, volume`
|
||||
|
||||
- **Timestamp**: `YYYY-MM-DD` (assumed UTC midnight)
|
||||
- **Prices/Volume**: Numeric values
|
||||
|
||||
Example:
|
||||
```csv
|
||||
Date,Open,High,Low,Close,Volume
|
||||
2024-01-01,100.0,105.0,99.0,102.5,10000
|
||||
2024-01-02,102.5,103.0,101.0,101.5,8500
|
||||
```
|
||||
|
||||
## Class Definition
|
||||
|
||||
```csharp
|
||||
public class CsvFeed : IFeed
|
||||
{
|
||||
public CsvFeed(string filePath);
|
||||
public TBar Next(bool isNew = true);
|
||||
public TBarSeries Fetch(int count, long startTime, TimeSpan interval);
|
||||
}
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### 1. Loading Data
|
||||
```csharp
|
||||
var feed = new CsvFeed("path/to/data.csv");
|
||||
```
|
||||
|
||||
### 2. Streaming Data (Simulation)
|
||||
```csharp
|
||||
// Get first bar
|
||||
var bar = feed.Next(isNew: true);
|
||||
|
||||
// Loop through all data
|
||||
while (true)
|
||||
{
|
||||
// Process bar...
|
||||
Console.WriteLine(bar);
|
||||
|
||||
// Get next bar
|
||||
bool isNew = true;
|
||||
bar = feed.Next(ref isNew);
|
||||
|
||||
// Stop if no more new data
|
||||
if (!isNew) break;
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Fetching a Batch
|
||||
```csharp
|
||||
long startTime = new DateTime(2024, 1, 1).Ticks;
|
||||
var batch = feed.Fetch(10, startTime, TimeSpan.FromDays(1));
|
||||
Reference in New Issue
Block a user