mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-04 20:17:43 +00:00
5.2 KiB
5.2 KiB
AGENTS.md - QuanTAlib Protocol
To all AI Agents: This file defines the laws, physics, and protocols of the QuanTAlib repository. Read this before writing a single line of code. Failure to adhere to these standards will result in rejected code.
1. Identity & Mission
QuanTAlib is a high-performance, zero-allocation C# library for quantitative technical analysis.
- Target: Quantower and custom C# trading engines.
- Core Philosophy: Speed, Correctness, and Memory Efficiency.
- Key Constraint: Hot paths must be allocation-free (GC pressure is the enemy).
2. Architecture & "Physics"
Memory Model: Structure of Arrays (SoA)
We do not store objects in lists. We store primitive arrays.
- TSeries: Internally uses
List<long> _t(timestamps) andList<double> _v(values). - Access: Expose data via
ReadOnlySpan<double>for SIMD operations.
Core Types
TValue: Struct (16 bytes).DateTime Time,double Value.TBar: Struct (48 bytes).DateTime Time,double Open, High, Low, Close, Volume.TSeries: The primary data structure for time series.ITValuePublisher: The interface for reactive data flow.
Performance Rules
- Zero Allocation: The
Updatemethod MUST NOT allocate memory on the heap. Usestackallocor pre-allocated buffers. - O(1) Complexity: Streaming updates must be constant time. Use circular buffers (
RingBuffer) or running sums. - SIMD: Batch operations (
Calculate) should useSystem.Runtime.Intrinsics(AVX2) where possible. - Inlining: Use
[MethodImpl(MethodImplOptions.AggressiveInlining)]on hot methods. - Locals: Use
[SkipLocalsInit]to avoid zero-init costs in tight loops.
3. Indicator Implementation Standards
Every indicator must follow the Good Indicator Guidelines strictly.
File Structure
Directory: lib/[category]/[name]/ (e.g., lib/trends/sma/)
| File | Naming | Purpose |
|---|---|---|
| Source | [Name].cs |
Main logic. public sealed class. |
| Tests | [Name].Tests.cs |
xUnit tests (correctness, edge cases). |
| Validation | [Name].Validation.Tests.cs |
Compare against TA-Lib, Skender, etc. |
| Docs | [Name].md |
User documentation with formulas. |
| Adapter | [Name].Quantower.cs |
Quantower platform integration. |
| Adapter Tests | [Name].Quantower.Tests.cs |
Tests for the adapter. |
The Update Method Contract
The Update method is the heart of the indicator.
public TValue Update(TValue input, bool isNew = true)
isNew = true: A new bar has arrived. Save current state to history (or_p_variables), then calculate.isNew = false: The current bar is updating (tick data). Restore state from history (or_p_variables), then recalculate.- NaN Handling: If input is
NaNorInfinity, use the last valid value. Never propagateNaN.
State Management
- Scalar State: Use a
private record struct Stateto group all scalar state variables. This ensures value semantics, automaticIEquatableimplementation, and cleaner rollback logic. - State Variables: Maintain
private State _state;(current) andprivate State _p_state;(previous valid state). - Buffers: Use
RingBufferfor sliding windows. - Resync: Periodically recalculate running sums to prevent floating-point drift.
Dual API Requirement
- Stateful (Streaming):
Update(TValue)for live data. - Stateless (Vector):
static void Calculate(ReadOnlySpan<double> src, Span<double> dst)for batch history.
4. Testing Protocol
Unit Tests ([Name].Tests.cs)
- Use
GBM(Geometric Brownian Motion) for data generation. - Test
isNew=truevsisNew=falseconsistency. - Test
Reset()andIsHot(warmup). - Test edge cases:
NaNinputs, empty series, period=1.
Validation Tests ([Name].Validation.Tests.cs)
- Mandatory: You MUST validate against at least one external authority (TA-Lib, Skender, Tulip, OoplesFinance, Python libs).
- Tolerance: Typically
1e-6to1e-9.
5. Documentation Standards
- Format: Markdown.
- Content: Title, Description, Parameters, Formula (LaTeX), C# Usage Examples.
- Index: Add the new indicator to the category index (e.g.,
lib/trends/_index.md).
6. Development Checklist
When creating a new indicator, you are DONE only when:
- Source algorithm is verified.
- All 6 required files exist.
UpdatehandlesisNewandNaNcorrectly.- No heap allocations in
Update. - Static
Calculate(Span)is implemented. - Unit tests pass (including edge cases).
- Validation tests pass against external libs.
- Documentation is complete and linked in
_index.md. - CodeRabbit review issues are resolved.
7. Forbidden Actions
- DO NOT use LINQ in hot paths (
UpdateorCalculate). - DO NOT use
newinsideUpdate. - DO NOT change
Directory.Build.propswithout explicit instruction. - DO NOT remove
[SkipLocalsInit]or[MethodImpl]attributes. - DO NOT ignore
NaNinputs; handle them safely.
8. Context & Resources
- Time: Use
DateTime.UtcNow. - Math: Use
System.MathorSystem.Numerics. - Root Namespace:
QuanTAlib.