mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-19 02:58: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,69 @@
|
||||
#!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;
|
||||
|
||||
// 1. Initialize GBM Generator
|
||||
// GBM simulates price movements using Geometric Brownian Motion
|
||||
// Parameters: Start Price, Drift (mu), Volatility (sigma)
|
||||
|
||||
var gbm = new GBM(startPrice: 100.0, mu: 0.05, sigma: 0.2);
|
||||
Console.WriteLine("GBM Generator initialized (Start=100, Drift=5%, Vol=20%)");
|
||||
|
||||
#!csharp
|
||||
|
||||
// 2. Batch Generation
|
||||
// Generate a sequence of bars at once
|
||||
// Useful for backtesting or initializing indicators
|
||||
|
||||
long startTime = DateTime.UtcNow.Ticks;
|
||||
var interval = TimeSpan.FromMinutes(1);
|
||||
|
||||
var history = gbm.Fetch(10, startTime, interval);
|
||||
|
||||
Console.WriteLine($"Generated {history.Count} bars:");
|
||||
for (int i = 0; i < history.Count; i++)
|
||||
{
|
||||
Console.WriteLine($" Bar {i}: Time={history[i].AsDateTime:HH:mm}, Close={history[i].Close:F2}");
|
||||
}
|
||||
|
||||
#!csharp
|
||||
|
||||
// 3. Streaming Generation
|
||||
// Simulate real-time data feed bar by bar
|
||||
|
||||
Console.WriteLine("\nStreaming new bars:");
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
Console.WriteLine($" New Bar: {bar.Close:F2}");
|
||||
}
|
||||
|
||||
#!csharp
|
||||
|
||||
// 4. Intra-bar Updates
|
||||
// Simulate real-time price ticks within a single bar
|
||||
// The High/Low will expand, and Close will update
|
||||
|
||||
Console.WriteLine("\nSimulating intra-bar updates:");
|
||||
|
||||
// Start a new bar
|
||||
var liveBar = gbm.Next(isNew: true);
|
||||
Console.WriteLine($" Open: {liveBar.Open:F2}, Close: {liveBar.Close:F2}");
|
||||
|
||||
// Simulate 5 ticks
|
||||
for (int i = 1; i <= 5; i++)
|
||||
{
|
||||
liveBar = gbm.Next(isNew: false);
|
||||
Console.WriteLine($" Tick {i}: Close={liveBar.Close:F2}, High={liveBar.High:F2}, Low={liveBar.Low:F2}");
|
||||
}
|
||||
|
||||
// Finalize bar
|
||||
liveBar = gbm.Next(isNew: true);
|
||||
Console.WriteLine($" Finalized Previous, Started New: {liveBar.Open:F2}");
|
||||
@@ -0,0 +1,67 @@
|
||||
# GBM Class
|
||||
|
||||
`GBM` (Geometric Brownian Motion) is a synthetic data generator that simulates realistic financial price movements. It is useful for testing indicators, strategies, and system performance without relying on external data files.
|
||||
|
||||
## Key Features
|
||||
|
||||
- **Geometric Brownian Motion**: Uses the standard mathematical model for asset price dynamics.
|
||||
- **Configurable Parameters**: Control drift (trend) and volatility (noise).
|
||||
- **Stateless Design**: Minimal memory footprint; only maintains state needed for continuity.
|
||||
- **Dual Modes**: Supports both streaming (bar-by-bar) and batch generation.
|
||||
- **Intra-bar Updates**: Can simulate real-time price updates within a single bar.
|
||||
|
||||
## Mathematical Model
|
||||
|
||||
The price evolution follows the stochastic differential equation:
|
||||
|
||||
$$ dS_t = \mu S_t dt + \sigma S_t dW_t $$
|
||||
|
||||
Where:
|
||||
- $S_t$: Asset price at time $t$
|
||||
- $\mu$: Drift (expected return)
|
||||
- $\sigma$: Volatility (standard deviation of returns)
|
||||
- $W_t$: Wiener process (Brownian motion)
|
||||
|
||||
## Class Definition
|
||||
|
||||
```csharp
|
||||
public class GBM : IFeed
|
||||
{
|
||||
public GBM(double startPrice = 100.0, double mu = 0.05, double sigma = 0.2, TimeSpan? defaultTimeframe = null);
|
||||
|
||||
public TBar Next(bool isNew = true);
|
||||
public TBarSeries Fetch(int count, long startTime, TimeSpan interval);
|
||||
}
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### 1. Initialization
|
||||
```csharp
|
||||
// Default: Start at 100, 5% drift, 20% volatility
|
||||
var gbm = new GBM();
|
||||
|
||||
// Custom: Start at 50, 10% drift, 50% volatility
|
||||
var volatileGbm = new GBM(startPrice: 50.0, mu: 0.10, sigma: 0.50);
|
||||
```
|
||||
|
||||
### 2. Streaming Generation
|
||||
```csharp
|
||||
// Generate a new bar
|
||||
var bar = gbm.Next(isNew: true);
|
||||
|
||||
// Simulate intra-bar updates (e.g., real-time ticks)
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
var updatedBar = gbm.Next(isNew: false);
|
||||
Console.WriteLine($"Update: {updatedBar.Close}");
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Batch Generation
|
||||
```csharp
|
||||
long startTime = DateTime.UtcNow.Ticks;
|
||||
var interval = TimeSpan.FromMinutes(1);
|
||||
|
||||
// Generate 1000 bars
|
||||
var history = gbm.Fetch(1000, startTime, interval);
|
||||
Reference in New Issue
Block a user