Skip to content

guilh-dev/Forex-Engine-Linear-NonLinear

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FX Engine: High-Frequency Time-Series Forecaster 🦀⚡

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.

🧠 Model Architecture

The engine utilizes a Hybrid approach, combining classical control theory with online Machine Learning:

  1. 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.
  2. 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.
  3. 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.

🛠️ Tech Stack & Optimizations

  • Language: Rust (2021 Edition)
  • Async Runtime: tokio for non-blocking I/O management.
  • WebSocket: tokio-tungstenite for 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 fat level to maximize the inlining of aggressive mathematical functions within the final binary.

⚡ Compilation Optimizations

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.

🚀 Getting Started

Prerequisites:

  • Rust (Minimum stable recommended: 1.75+)

Installation

  1. 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
  2. Build and run in development mode (for quick testing):
    cargo run
  3. Run in production mode (High Performance):
    cargo run --release

📊 Model Mechanics

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.

⚖️ License

This project is licensed under the GNU GPLv3. See the LICENSE file for more details.

About

This is a Forex engine for time-series forecasting, designed for low computational cost and high performance in Rust, based on a hybrid model combining a linear Kalman filter and stochastic gradient descent (machine learning).

Resources

License

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages