Skip to content

C# TA library for real-time financial analysis, offering ~100 indicators. Available on NuGet, Quantower compatible. Ensures early validity of calculated data, calculation accuracy tested against four TA libraries.

License

Notifications You must be signed in to change notification settings

mihakralj/QuanTAlib

Repository files navigation

Codacy grade

codecov Security Rating CodeFactor

Nuget GitHub last commit Nuget GitHub watchers .NET

QuanTAlib - Quantitative Technical Analysis Library

Quantitative TA library (QuanTAlib) is a high-performance C# library for quantitative technical analysis, designed for Quantower and other C#-based trading platforms.

Key Features

  • Real-time streaming - Indicators calculate results from incoming data without re-processing history
  • Update/correction support - Last value can be recalculated multiple times before advancing to next bar
  • Valid from first bar - Mathematically correct results from the first value with IsHot warmup indicator
  • SIMD-optimized - Hardware-accelerated vector operations (AVX/SSE) for batch processing
  • Zero-allocation hot paths - Minimal GC pressure for high-frequency scenarios

Architecture

QuanTAlib uses a Structure of Arrays (SoA) memory layout optimized for numerical computing:

┌─────────────────────────────────────────────────────────────┐
│                    Core Data Types                          │
├─────────────────────────────────────────────────────────────┤
│  TValue (16 bytes)     │  Time-value pair (long + double)  │
│  TBar (48 bytes)       │  OHLCV bar (long + 5 doubles)     │
│  TSeries               │  Time series with SoA layout      │
│  TBarSeries            │  OHLCV series with SoA layout     │
└─────────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────────┐
│                    Data Feeds                               │
├─────────────────────────────────────────────────────────────┤
│  IFeed                 │  Unified feed interface            │
│  GBM                   │  Geometric Brownian Motion sim     │
│  CsvFeed               │  CSV file reader                   │
└─────────────────────────────────────────────────────────────┘

Performance Design

The SoA layout stores timestamps and values in separate contiguous arrays:

// TSeries internal structure
protected readonly List<long> _t;    // Timestamps (contiguous)
protected readonly List<double> _v;  // Values (contiguous)

// Direct SIMD access via Span<T>
ReadOnlySpan<double> values = series.Values;
double avg = values.AverageSIMD();  // Hardware-accelerated

This enables:

  • Cache locality - Sequential memory access patterns
  • SIMD vectorization - Process 4-8 values per CPU instruction
  • Zero-copy access - CollectionsMarshal.AsSpan() exposes internal arrays

Quick Start

Installation

dotnet add package QuanTAlib

Basic Usage

using QuanTAlib;

// Create EMA indicator
var ema = new Ema(period: 10);

// Streaming mode - process one value at a time
TValue result = ema.Update(new TValue(DateTime.Now, price), isNew: true);

// Update current bar (e.g., price tick within same minute)
result = ema.Update(new TValue(DateTime.Now, newPrice), isNew: false);

// Batch mode - process entire series
var series = new TSeries();
series.Add(prices);  // Add historical data
TSeries emaResults = Ema.Calculate(series, period: 10);

Multi-Period Analysis with SIMD

// Calculate multiple EMAs in parallel using SIMD
int[] periods = { 9, 12, 26 };
var emaVector = new EmaVector(periods);

// Single update calculates all periods
TValue[] results = emaVector.Update(new TValue(time, price));
Console.WriteLine($"EMA(9)={results[0]}, EMA(12)={results[1]}, EMA(26)={results[2]}");

Using Data Feeds

// Geometric Brownian Motion simulator
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2);
TBarSeries bars = gbm.Fetch(count: 1000, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));

// CSV file reader
var csv = new CsvFeed("data/daily_IBM.csv");
TBar bar = csv.Next(isNew: true);

Installation to Quantower

Copy DLL files to Quantower installation:

<Quantower_root>\Settings\Scripts\Indicators\QuanTAlib\Trends\Trends.dll

Where <Quantower_root> is the directory containing Start.lnk.

Project Structure

QuanTAlib/
├── lib/
│   ├── core/
│   │   ├── tvalue/      # TValue struct
│   │   ├── tseries/     # TSeries class
│   │   ├── tbar/        # TBar struct
│   │   ├── tbarseries/  # TBarSeries class
│   │   └── simd/        # SIMD extensions
│   ├── trends/
│   │   └── ema/         # EMA indicator + tests + docs
│   └── feeds/
│       ├── csv/         # CSV file feed
│       └── gbm/         # GBM simulator
└── quantower/           # Quantower integration

Each indicator follows a consistent file pattern:

  • Indicator.cs - Core implementation
  • Indicator.Tests.cs - Unit tests
  • Indicator.Validation.Tests.cs - Cross-validation with other libraries
  • Indicator.md - Documentation
  • Indicator.Notebook.dib - Interactive notebook
  • Indicator.Quantower.cs - Quantower wrapper

Validation

QuanTAlib validates results against established TA libraries:

Requirements

  • .NET 8.0, 9.0, or 10.0
  • Hardware with AVX/SSE support recommended for optimal SIMD performance

License

Apache License 2.0 - See LICENSE for details.

Contributing

Contributions welcome! Each indicator should include:

  1. Core implementation with streaming support
  2. Unit tests covering edge cases
  3. Validation tests against reference libraries
  4. Documentation with mathematical formulas
  5. Quantower wrapper (optional)

Links

About

C# TA library for real-time financial analysis, offering ~100 indicators. Available on NuGet, Quantower compatible. Ensures early validity of calculated data, calculation accuracy tested against four TA libraries.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages