A quantitative forecasting engine for the Forex market, developed in Rust. This project was designed with an extreme focus on low latency (HFT), zero memory allocation on the hot path, and continuous learning to adapt to market regime changes.
The engine utilizes a Hybrid approach, combining classical control theory with online Machine Learning:
- Kalman Filter (Linear Component): Tracks the asset's state and underlying trend. It acts as an ultra-fast "smoother" that reacts to new ticks in real-time.
- ML via SGD (Non-Linear/Residual Component): An online linear regression trained via Stochastic Gradient Descent (SGD). Instead of predicting the price directly, it predicts the error generated by the Kalman Filter, correcting market biases based on volatility and Z-Score.
-
Forgetting Factor (
$\lambda$ ): Implemented within the SGD to ensure model weights decay exponentially. This forces the model to prioritize recent market behavior and "forget" obsolete patterns from previous sessions.
- Language: Rust (2021 Edition)
-
Async Runtime:
tokiofor non-blocking I/O management. -
WebSocket:
tokio-tungstenitefor real-time data ingestion. -
Zero-Allocation Ring Buffer: A custom circular buffer for Feature Engineering with
$O(1)$ insertion complexity, avoiding costly calls to the OS memory allocator. -
LTO (Link-Time Optimization): Configured at the
fatlevel to maximize the inlining of aggressive mathematical functions within the final binary.
To ensure the lowest possible latency during tick processing, the Cargo.toml file is configured to extract maximum performance from the Rust compiler in --release mode:
- "Fat" LTO (Link-Time Optimization): Enables aggressive inlining across different modules.
- Codegen Units = 1: Forces the optimization of the binary as a single unit.
- Panic = Abort: Removes the code overhead required for stack unwinding.
Prerequisites:
- Rust (Minimum stable recommended: 1.75+)
Installation
- Clone the repository:
git clone [https://github.com/guilh-dev/Forex-Engine-Linear-NonLinear.git](https://github.com/guilh-dev/Forex-Engine-Linear-NonLinear) cd Forex-Engine-Linear-NonLinear - Build and run in development mode (for quick testing):
cargo run
- Run in production mode (High Performance):
cargo run --release
The per-tick processing flow strictly follows the order below to prevent any form of look-ahead bias:
- Ingestion: The tick arrives via WebSocket and is dispatched to the asynchronous channel.
- Feature Calculation: The sliding window calculates the current Z-Score and Volatility.
- Kalman Update: The Kalman Filter updates its state and generates the current estimate.
- Error Computation: The real error (Price - Kalman Estimate) is calculated.
- SGD Learning: The SGD model updates its weights using the real error and the previous features.
- Forecasting: The SGD predicts the next residual based on the new features.
- Signal Synthesis: The final signal combines the Kalman output and the SGD prediction.
This project is licensed under the GNU GPLv3. See the LICENSE file for more details.