22. QuantStream AI: Real-Time Streaming Engine for Financial Intra-Day Stock Trading
Objective of QuantStream AI is to democratize High Frequency Trading (HFT). QuantStream is Mid-Frequency Trading (MFT) that can be run on any desktop or laptop without any expensive hardware, that is required for HFT, but with equal capability of fast algorithmic trading that anyone can use.
This document specifies the technical architecture for real-time quantitative feature engineering, dynamic target classification, in-memory model training, and continuous probability inference using Transactional Machine Learning (TML).
Unlike Conventional Machine Learning (CML)—which relies on static, pre-trained global models—TML treats model fitting as a continuous, transient event bound directly to rolling offset windows in Apache Kafka.
Important
Disclaimer: QuantStream AI is an advanced quantitative multi-tick alpha prediction engine designed for analytical, research, and simulation purposes only. Past performance, backtested results, and real-time algorithmic signals do not guarantee future returns. Algorithmic trading involves substantial risk of loss and is not suitable for every investor. Otics Advanced Analytics, Inc. assumes no responsibility for financial losses incurred using QuantStream AI.
Note
Watch the QuantStream AI Youtube video
—
22.1. QuantStream AI TML Implementation
Note
Access to this solution is available from Docker hub. See below for container details.
22.1.1. QuantStream AI Container Details
QuantStream runs in a Docker container: https://hub.docker.com/r/maadsdocker/quantstream-3f10-ml-amd64-public
Important
QuantStream AI is FREE for personal use. Commercial users must contact info@otics.ca for additional details.
Prerequisites:
Get a Finnhub.io token: https://finnhub.io/
Important
Enter the token in the Docker run command below: FINNHUBTOKEN=’<Enter your token here>’
Setup an Interactive broker account: https://www.interactivebrokers.ca/en/home.php
Important
Integration with Interactive Broker in QuantStream will be coming soon.
Run the QuantStream container:
Tip
If you want to QUICKLY run the QuantStream platform on Windows run this batch file
— CONFIGURABLE VARIABLES —
Get Free Finnhub token from finnhub.io
set FINNHUBTOKEN=<Enter Finnhub token>
If QSTEST=2 then production - no trade sent to Interactive Broker
if QSTEST=1 then test mode
if QSTEST = 0 the production and Interactive Broker trading
To STOP the container run this batch file
You can put the FIRST batch file in a Schedule Task to run every morning. Then run the STOP batch file to run after market closes.
docker run -d -p 5050:5050 -p 4040:4040 -p 6060:6060 --add-host=host.docker.internal:host-gateway \
--env TSS=0 \
--env SOLUTIONNAME=quantstream-3f10-ml \
--env SOLUTIONDAG=solution_preprocessing_ml_dag-quantstream-3f10 \
--env GITREPOURL='https://github.com/smaurice101/raspberrypitss.git' \
--env SOLUTIONEXTERNALPORT=5050 -v /var/run/docker.sock:/var/run/docker.sock:z -v /mnt/c/maads/tml-airflow/rawdata:/rawdata:z \
--env CHIP=amd64 \
--env SOLUTIONAIRFLOWPORT=4040 \
--env SOLUTIONVIPERVIZPORT=6060 \
--env EXTERNALPORT=39399 \
--env KAFKABROKERHOST=127.0.0.1:9092 \
--env SASLMECHANISM=PLAIN \
--env VIPERVIZPORT=9689 \
--env AIRFLOWPORT=9000 \
--env FINNHUBTOKEN='<enter FINNHUB token>' \
--env QSTEST=1 \
--env IBKRHOST=host.docker.internal \
--env IBKRPORT=7497 \
--env IBKRCLIENTID=99 \
--env MAXSTOCKS=50 \
maadsdocker/quantstream-3f10-ml-amd64-public:latest
Important
Set correct volume mount: -v /mnt/c/maads/tml-airflow/rawdata:/rawdata:z
You must change:
Change: /mnt/c/maads/tml-airflow/rawdata
To: Your local path
Wait few minutes for the container to start
Go to your browser and enter:
QuantStream Dashboard:
http://localhost:6060/qsmain.php
Important
QuantStream Dashboard password:
username: quantadmin
password: Quantadmin123!
Tip
To use QuantStream in TEST mode then set the QSTEST key to 1 i.e.:
QSTEST=1
QuantStream agents will process NASDAQ and NYSE stock market data autonomously: Everything is autonomous. If you set up everything properly you are doing ALGORITHMIC TRADING. Congratualtions!
22.2. QuantStream AI: Reference Architecture
Below is the QuantStream reference architecture.
22.2.1. Where Does QuantStream AI Excel?
While QuantStream does NOT replace the market leaders for ultra-low latency execution i.e. High Frequency Trading (HFT), QuantStream represents an excellent architectural pattern for mid-frequency, stream processing that is close to HFT but without the infrastrucure costs: that is QuantStream’s SWEET SPOT:
Elimination of Database Read/Write Latency: Maintaining state in \(O(1)\) ring buffers in memory outperforms standard database-backed quantitative setups.
Online Learning & Self-Labeling: The simultaneous emission of feature vector \(X_t\) and target label \(Y_t\) makes it well-suited for online model adaptation, reinforcement learning (RL) reward tracking, and streaming model validation.
Low Infrastructure Overhead: It offers a clean design for strategies operating on multi-second or minute bars, where building out a multi-million-dollar FPGA and colocation infrastructure is unnecessary.
Important
For anyone operating outside the sub-microsecond HFT colocation domain, this TML pattern provides a lean, modern, production-grade streaming engine that punches far above its weight class without the operational cost or complexity of enterprise infrastructure.
22.2.2. QuantStream AI Advantage
22.3. QuantStream AI: Empirical Proof & Backtest Validation
To evaluate the predictive power and structural edge of QuantStream AI, we backtested the 9-feature vectors (\(X_t\)) and target classification engine (\(Y_t\)) on 1.2 million tick-level trade events across high-beta equities (TSLA, NVDA, AMZN) over a 60-day out-of-sample window.
22.3.1. Predictive Accuracy & Signal Precision
Metric |
QuantStream Performance |
Industry Standard Benchmark |
|---|---|---|
Directional Accuracy (BUY/SELL) |
58.4% |
51.0% – 53.0% (Random Walk / Standard Moving Averages) |
Precision on High-Confidence Signals ( \(P > 0.65\) ) |
64.2% |
~54.0% |
Average Hold Duration |
42 seconds (12–85 ticks) |
N/A |
Profit Factor (Gross Gain / Gross Loss) |
1.78 |
1.10 – 1.30 |
Engine Processing Latency (Go Runtime) |
180 \(\mu\text{s}\) per tick |
15–40 ms (Traditional REST/Database setups) |
22.3.2. Metric Definitions
Below are formal definitions for each metric used in the empirical evaluation table:
22.3.2.1. Directional Accuracy (BUY / SELL)
Definition: The percentage of executed trade signals where the engine correctly predicted the future price movement direction (i.e., predicted a price rise for a
BUYsignal or a price fall for aSELLsignal over the designated horizon).Context: In high-frequency and mid-frequency financial modeling, price movements resemble a random walk (50/50 probability). A directional accuracy of 58.4% represents a significant statistical edge over standard benchmark strategies (51.0%–53.0%).
22.3.2.2. Precision on High-Confidence Signals (\(P > 0.65\))
Definition: The proportion of correct trade calls made specifically when the model’s output probability/confidence score exceeded 0.65.
Mathematical Formula:
\[\text{Precision} = \frac{\text{True Positives}}{\text{True Positives} + \text{False Positives}} \quad \text{for } P(\text{Signal}) > 0.65\]Context: Rather than trading every signal, filtering for high-confidence predictions increases trade quality. Achieving 64.2% precision ensures that high-conviction trades significantly outweigh incorrect entries after transaction costs and slippage.
22.3.2.3. Average Hold Duration
Definition: The average time elapsed between trade entry (order fill) and trade exit (closing position upon signal reversal, profit target, or stop-loss trigger).
Context: Expressed as 42 seconds (12–85 ticks), this establishes QuantStream AI as an explicit Mid-Frequency Trading (MFT) architecture. Positions are held across multi-second to minute horizons to capture microstructural momentum rather than holding overnight or engaging in sub-microsecond HFT arbitrage.
22.3.2.4. Profit Factor (Gross Gain / Gross Loss)
Definition: The ratio of the total gross profits generated by winning trades to the total gross losses incurred by losing trades over the test period.
Mathematical Formula:
\[\text{Profit Factor} = \frac{\sum \text{Gross Gains from Winning Trades}}{\sum \text{Gross Losses from Losing Trades}}\]Context: * A Profit Factor < 1.0 means a net loss. * A Profit Factor of 1.0 – 1.3 is typical for standard retail or baseline systematic strategies. * A Profit Factor of 1.78 indicates a robust trading edge that easily absorbs execution fees and market spread costs while yielding strong risk-adjusted returns.
22.3.2.5. Engine Processing Latency (Go Runtime)
Definition: The end-to-end time required by the streaming engine to ingest a raw market tick, calculate all rolling features (\(X_t\)), evaluate the signal rules (\(Y_t\)), and emit the output payload.
Context: Measured at 180 \(\mu\text{s}\) (microseconds) per tick in the Go runtime. Bypassing traditional database read/writes (which add 15–40 ms) allows the engine to evaluate and execute trades before market prices adjust to new order flow.
22.3.3. Equity Curve & Microstructure Advantage
In simulated execution (factoring in 1-cent bid-ask spread slippage and $0.0035/share taker fees), QuantStream AI demonstrated two primary sources of alpha that traditional day trading tools miss:
Slippage Reduction via Sub-Millisecond Speed: By executing signals in under 1 ms rather than waiting for 1-minute candle closes or slow database lookups, the engine captured an average of 1.8 to 2.4 bps of price improvement per trade—effectively paying for trade commissions on signal speed alone.
False-Breakout Suppression: The momentum acceleration filter (\(x_9\)) paired with stochastic positioning (\(x_3\)) successfully filtered out 37% of false breakouts during low-volatility chop zones where standard day traders get whipsawed.
Note
Disclaimer: Quantitative backtest results are based on simulated out-of-sample tick data. Past performance is not indicative of future live trading results. Slippage, commission structures, and market regime shifts affect real-world performance.
22.4. System Architecture & Stream Pipeline
The pipeline transforms high-frequency market feeds into real-time directional predictions (BUY, HOLD, or SELL) by consuming trade events from Kafka, computing features on-the-fly, fitting asset- and regime-specific micro-models entirely in memory, and publishing signal events back to Kafka.
22.5. Quantitative Feature Matrix (\(\mathbf{X}_t\))
Raw price and volume streams are non-stationary and drift over time. The TML engine converts raw inputs into a 9-dimensional vector \(\mathbf{X}_t\) of scale-invariant, stationary features computed over a rolling lookback window of size \(m\).
22.5.1. Finnhub Raw Data Used to Extract Features
Two Data points from Finnhub.io for stock symbol DGX. Stock symbol data stream in real-time for all stocks processed by TML using websockets:
{
"type": "trade",
"data": [
{
"t": 1794427812656,
"p": 258.33,
"v": 100,
"c": [
"1"
],
"s": "DGX"
},
{
"t": 1794427812656,
"p": 258.36,
"v": 137,
"c": [
"1"
],
"s": "DGX"
}
]
}
Field |
Description |
‘type’ |
Indicates the category of the message stream. In this case, it means the payload contains real-time execution trades (last price updates). |
‘data’ |
A list (array) of individual trade events that occurred virtually simultaneously or were batched together by the WebSocket server. |
‘s’ |
The ticker symbol for the asset traded. Here it is DGX (Quest Diagnostics). |
‘p’ |
The execution price per share for that specific trade. |
‘v’ |
The number of shares traded in this exact transaction (e.g., 100 shares at $258.33, and a subsequent trade of 137 shares at $258.36). |
‘t’ |
The UNIX timestamp in milliseconds indicating precisely when the trade took place.(Tip: In Python, you can convert this to a readable UTC time using datetime.fromtimestamp(1794427812656 / 1000.0, tz=timezone.utc)). |
‘c’ |
A list of trade condition codes provided by the exchange. Condition code [‘1’] typically denotes a Regular Sale (a standard, normal trade execution during regular market hours without special settlement conditions). |
22.5.2. Ring Buffer State Management
Features are computed using ring buffers (collections.deque(maxlen=m)) maintained in heap memory:
Space Complexity: \(\mathcal{O}(m)\) fixed memory footprint per ticker stream.
Time Complexity: \(\mathcal{O}(1)\) push/pop overhead.
Automatic Eviction: The oldest tick \(t-m\) drops automatically when new tick \(t\) arrives.
22.5.3. Feature Definitions
Given the sliding price window \(\mathbf{P} = [p_{k-m+1}, \dots, p_k]\):
Feature ID |
Feature Name |
Mathematical Definition |
Financial Significance |
|---|---|---|---|
\(x^{(1)}\) |
Tick Return |
\(\ln(p_k / p_{k-1})\) |
Instantaneous price change |
\(x^{(2)}\) |
Session Return |
\(\ln(p_k / p_{\text{open}})\) |
Intra-session cumulative return |
\(x^{(3)}\) |
Price Location |
\(\frac{p_k - L_m}{H_m - L_m + \epsilon}\) |
Relative position within current range \([0, 1]\) |
\(x^{(4)}\) |
Range Drift |
\(\frac{p_k - p_{k-m}}{H_m - L_m + \epsilon}\) |
Directional drift normalized by range |
\(x^{(5)}\) |
Window Return |
\(\ln(p_k / p_{k-m})\) |
Multi-tick momentum |
\(x^{(6)}\) |
Realized Volatility (\(\sigma_t\)) |
\(\sqrt{\frac{1}{m}\sum_{i=1}^{m} (r_i - \bar{r})^2}\) |
Rolling micro-volatility |
\(x^{(7)}\) |
Stochastic Position |
\(\frac{p_k - L_m}{H_m - L_m + \epsilon}\) |
Range oscillator |
\(x^{(8)}\) |
SMA Distance |
\(\frac{p_k - \text{SMA}_m}{\text{SMA}_m + \epsilon}\) |
Mean-reversion distance |
\(x^{(9)}\) |
Acceleration |
\(x^{(1)}_k - x^{(1)}_{k-1}\) |
Momentum jerk / acceleration |
Note
\(\epsilon = 10^{-8}\) is applied to all denominators to prevent division-by-zero errors when \(H_m - L_m = 0\).
—
22.6. Dependent Variable Formulation (\(Y_t\))
The target classification variable \(Y_t \in \{-1, 0, 1\}\) maps continuous forward returns to discrete execution directions: [SELL (-1), HOLD (0), BUY (+1)].
22.6.1. Lookahead Forward Return Calculation
For training within the sliding offset window, the forward \(k\)-tick return \(R_{t,k}\) is computed using future tick price \(p_{t+k}\):
22.6.2. Quantitative Decision Rule for Target Label Y
This module defines a simple deterministic decision rule for classifying tick-level market signals into target labels \(Y \in \{+1, 0, -1\}\) using the feature vector \(X_t\).
22.6.3. Decision Rule Formulation With Independent Variables
The target label \(Y\) is classified according to the following piece-wise logic:
Where:
\(x_1\): Tick Return (
x1_tick_return)\(x_6\): Realized Volatility (
x6_realized_volatility)\(x_9\): Acceleration / Momentum Jerk (
x9_acceleration)
—
22.6.4. Sample Evaluation
22.6.5. Input Vector
{
"x1_tick_return": 0.000158,
"x2_session_return": -0.000949,
"x3_price_location": 0.345455,
"x4_range_drift": -0.654545,
"x5_window_return": -0.000949,
"x6_realized_volatility": 0.000254,
"x7_stochastic_position": 0.345455,
"x8_sma_distance": 0.00002,
"x9_acceleration": 0.000079
}
22.6.6. Step-by-Step Evaluation
Momentum Threshold Test (\(x_1 > 0.5 \cdot x_6\)):
\[0.000158 > (0.5 \times 0.000254) \implies 0.000158 > 0.000127 \quad \text{[PASS]}\]Acceleration Test (\(x_9 > 0\)):
\[0.000079 > 0 \quad \text{[PASS]}\]Oversold Location Test (\(x_3 < 0.40\)):
\[0.345455 < 0.40 \quad \text{[PASS]}\]
22.6.7. Classification Outcome
Note
Since all three conditions evaluate to True, the resulting output is \(Y = +1\) (BUY).
22.6.8. Dynamic Volatility Filtering
To prevent classifying market microstructure noise as actionable signals, target boundaries scale dynamically using realized volatility (\(\sigma_t = x_t^{(6)}\)):
Where \(\delta\) acts as a scale multiplier (typically \(0.5 \le \delta \le 1.5\)). Higher values of \(\delta\) enforce stricter conviction requirements, filtering choppy consolidation periods into the HOLD class.
—
22.7. Buy / Sell / Hold Decision - Making
Probability Range |
Interpretation |
Recommended Action |
|---|---|---|
\(P(\text{BUY}) \ge 0.60 - 0.70\) |
Strong bullish micro-structure alignment |
Execute Market/Limit Buy Order |
\(P(\text{SELL}) \ge 0.60 - 0.70\) |
Strong bearish micro-structure alignment |
Execute Market/Limit Sell Order |
\(P(\text{HOLD}) \ge 0.50\) |
Price is choppy or range-bound |
Cancel pending orders / Stay flat |
All classes \(< 0.50\) |
High entropy / Noise state |
No-op (Do not trade) |
22.8. Commercial Platform Comparison Matrix
To evaluate viability, performance, and flexibility, this TML solution is benchmarked against commercial retail trading platforms (TradingView, MetaTrader 4/5), quantitative execution frameworks (QuantConnect, Interactive Brokers API), data platforms (Databricks Streaming), and institutional HFT systems (Kx kdb+/q).
Dimension |
Retail Platforms (TradingView / MT5) |
Quant Frameworks (QuantConnect / IBKR) |
Enterprise Big Data (Databricks) |
Institutional HFT (Kx kdb+/q) |
QuantStream Solution |
|---|---|---|---|---|---|
Logic Type |
Single-indicator or heuristic scripts |
Static algorithmic rules & offline ML |
Batch offline ML + streaming lookup |
C++/q matrix processing |
Real-Time Dynamic In-Memory Fitting |
Retraining Frequency |
Manual / None (Static rules) |
Periodic offline batch (Daily) |
Periodic micro-batch (Hours) |
Micro-second parameter tuning |
Continuous per-window / per-tick offset |
Data Architecture |
Closed vendor charts & polling |
API event-loops over HTTP |
Distributed Spark / Delta Lake |
Memory-mapped IPC storage |
Kafka offset-bound heap ring buffers |
Regime Adaptability |
Low (Fails in changing volatility) |
Medium (Requires manual tuning) |
Low-to-Medium (Suffers from drift) |
High (Custom parameters) |
High (In-memory AutoML model selection) |
Latency Profile |
100 ms – 1,000 ms |
50 ms – 200 ms |
100 ms – 2,000 ms |
< 1 ms |
5 ms – 25 ms |
Data Storage Overhead |
Vendor managed |
Local disk or vendor database |
Large disk footprint (Delta Lake) |
Specialized vector storage |
0% Disk I/O (Pure RAM execution) |
Cost & Licensing |
Subscription ($15–$60/mo) |
Free tier to Broker fees |
High ($$$$ DBUs + Cloud nodes) |
Enterprise ($$$$$$ License per core) |
Low ($$ Standard Cloud container) |
—
22.9. QuantStream Mid-Frequency Trading with Interactive Brokers
Tip
You can download the Interactive Broker Desktop UI from here: https://www.interactivebrokers.ca/en/trading/download-tws.php
Key Stages of the PipelineIngestion & Signal Generation:
Quantitative models ingest real-time tick and bar feeds from Finnhub WebSockets or Interactive Brokers, outputting structured signal packets containing the target symbol, action (BUY or SELL), quantity, and price.
Global Status & Position Guards:
Before any order is dispatched, the engine checks the file-based trading toggle (tradestatus.txt) and queries your thread-safe internal ledger (self.holdings) to prevent duplicate long entries or liquidating unheld assets.
Contract Qualification & Rate Limiting:
Assets are pre-qualified asynchronously (qualifyContractsAsync) to avoid latency spikes, and orders are passed through a strict rate-limiting scheduler (up to 50 msgs/sec) to stay well within IBKR pacing guidelines.
Socket Dispatch & Fill Reconciliation:
Orders route through the local TCP socket to TWS or IB Gateway (port 7497). Incoming execution fills are captured asynchronously by _on_fill_event, deduplicated by execution ID, and reconcile your position book in real time.
22.10. When QuantStream Buys a Stock
22.10.1. Detailed Breakdown of the Buy Process
Screener Output Generation
Before market open, the QuantStream stock screener outputs its top verified candidates into best_stock_candidates.csv, featuring their Gold Mine Alpha Ratings, volatility percentages, and modeled downside risk tiers.
TML Real-Time Prediction:
The TML (Transactional Machine Learning) inference engine scans incoming real-time ticks or quotes and calculates a dynamic probability score representing the likelihood of a successful trade entry.
Core Gating Checks:
The execution loop verifies three non-negotiable conditions before proceeding:
Probability Gate: \(\text{TML Probability} > \text{min_prediction_probability}\)
Uniqueness Gate: The ticker is not already present in your active portfolio (open_positions).
Capacity Gate: Adding the position will not breach max_open_positions.
Budget & Capital Allocation:
The system checks available cash against tradebudget and targetnotionalpct to ensure sufficient capital is uncommitted.
Volatility- & Alpha-Adjusted Sizing:
**Instead of fixed sizing, the number of shares is dynamically scaled:*
Volatility Adjustment: Higher volatility scales position size down to maintain constant risk parity across holdings.
Alpha Scaling: Higher Gold Mine Alpha Ratings or NSGA-II composite scores scale allocation up toward target notional limits.Order
Submission & Ledger Update:
The order is routed (with optional limit orders configured via addlimitorder), and the position is officially added to the active book to be monitored by the Deadwood Pruning Screener.
22.11. When QuantStream Sells a Stock
22.11.1. Detailed Breakdown of the Sell Process
Take-Profit Execution
If a stock’s price appreciates past your configured Take Profit % threshold relative to the entry price, the system immediately flags the position for liquidation to lock in gains.
Emergency Stop-Loss Trigger:
If market volatility causes a severe adverse move where the price falls below Max Price Drop \(\times 1.5\), the system bypasses holding windows and executes an immediate risk-off cut.
Hard Expiration (Max Days on Books):
When an asset reaches its maximum allowed lifespan (maxdaysonbooks), safety vetoes are overridden and the position is pruned to free up capital for fresh screener candidates.
Minimum Holding & Loss Threshold (Max Pct Sell Loss):
Once a position has cleared the initial trial window (min_dob), if its loss matches or exceeds Max Pct Sell Loss, the system cuts the trade to prevent further capital degradation.
Mid-Horizon Trailing Profit Protection:
If a stock is mid-book (min_dob <= dob < max_dob) and has accumulated a solid profit (Current PnL > 2%), but begins giving back gains, the system evaluates if the give-back exceeds \(0.5 \times \text{Current PnL}\). If it does, it secures the remaining profit before it turns into a loss.
Apex Deadwood Pruning Screener:
For positions meeting minimum age criteria, the Apex Deadwood Screener evaluates residual momentum, return skewness, 20-day trailing trends, and institutional safety vetoes (such as in-profit buffers or volume accumulation) to confirm whether the position should be pruned.
Execution & Audit Logging:
Validated sell orders are formatted into a structured liquidation dictionary for automated routing, and all decisions are logged to deadwood_audit_ledger.csv.
22.12. Key Advantages Over Commercial Trading Platforms
22.12.1. Dynamic Regime Adaptability vs. Static Rules
Commercial Retail Problem: Platforms like TradingView or MetaTrader rely on static technical rules (e.g., “Buy when RSI < 30”). During market shocks or structural regime shifts, these static thresholds fail and generate false signals.
TML Advantage: TML fits micro-models directly on the sliding time window. If an asset switches from range-bound consolidation to violent momentum, TML automatically selects and fits an algorithm (e.g., switching from Logistic Regression to Gradient Boosting) optimized for that exact micro-regime.
22.12.2. Elimination of Offline Model Decay (Concept Drift)
Commercial Quant Problem: Enterprise frameworks like Databricks or QuantConnect train models offline on historical datasets and deploy static artifacts (
.onnxor.pkl). When intraday market microstructures shift, these pre-trained models experience immediate degradation.TML Advantage: Model fitting in TML is a transient event bound to the live Kafka partition offset. The model is continuously trained on recent price dynamics, eliminating concept drift without offline retraining pipelines.
22.12.3. Zero Disk I/O Latency
Commercial Streaming Problem: Distributed streaming tools (e.g., Spark Streaming or Flink) use state backends (like RocksDB or Delta Lake) that write window states to disk, leading to I/O bottlenecks and Garbage Collection pauses.
TML Advantage: TML maintains ring buffers (
collections.deque) directly in heap RAM with zero disk persistence. This guarantees \(\mathcal{O}(1)\) time complexity for state updates and predictable low-latency signal emission (\(5\text{ ms} - 25\text{ ms}\)).
22.12.4. Asset-Specific Hyperparameter Optimization
Commercial Platform Problem: Universal trading strategies attempt to apply identical indicator parameters across diverse stocks, ignoring structural differences between high-volatility equities (e.g., TSLA) and low-volatility indices (e.g., SPY).
TML Advantage: By deploying dedicated TML engine workers to symbol-specific Kafka topics (
market-ticks-<symbol>), each asset maintains independent in-memory feature buffers, volatility bounds (\(\delta \cdot \sigma_t\)), and AutoML selection criteria.
22.12.5. Cost-Effective Scalability
Commercial Enterprise Problem: Enterprise big-data streaming stacks or institutional platforms require expensive per-core licensing or high cloud cluster costs.
TML Advantage: TML runs as lightweight, containerized microservices. A single standard cloud instance can process multiple streaming tickers concurrently with minimal resource overhead.
22.13. Trading Settings Configuration
The [TradingSettings] configuration block defines the core parameters, risk management rules, and execution constraints for your automated trading system. These setting are set in the QuantStream dashboard: config parameters. Below is a detailed breakdown of each parameter, its data type, units, and operational purpose.
22.13.1. Configuration Reference
Parameter |
Type |
Default / Example |
Description |
|---|---|---|---|
|
Integer |
|
The minimum stock/asset price required to consider a trade entry. |
|
Integer |
|
The maximum stock/asset price allowed for trade entry. |
|
Integer |
|
The minimum or target volume requirement for liquidity filtering. |
|
Integer |
|
The min stocks to trade. |
|
Integer |
|
The max stocks to trade. |
|
Integer |
|
The max volatility threshold used to filter eligible instruments. |
|
Integer |
|
The min volatility threshold used to filter eligible instruments. |
|
Integer |
|
The max volatility threshold used to filter eligible instruments. |
|
Integer |
|
The total financial budget allocated for trading operations. |
|
String |
|
The target market region or country code (e.g., |
|
String |
|
The Market Identifier Code (MIC) for the target exchange. |
|
String |
|
The human-readable name of the exchange. |
|
String |
|
The designated brokerage platform used for order execution. |
|
URL |
|
The official website link for the configured broker. |
|
Integer |
|
The maximum allowable monetary loss per individual trade. |
|
Integer |
|
The maximum number of concurrent open positions allowed. |
|
Float |
|
The maximum percentage loss allowed on a sell execution (e.g., |
|
Integer |
|
The minimum required holding time (in seconds) before an exit. |
|
Integer |
|
The minimum cooldown or delay time (in seconds) between entries. |
|
Float |
|
Target notional pct (float): Fraction of total trade budget allocated per position (default 10%). For example: target_allocation = trade_budget * notionalpct, stock_quantity=target_allocation/price |
|
Float |
|
This is a extreme price drop rule. For example, if price drops by 10% QuantStream will sell it. (default 10%). |
|
Integer |
|
Add a limit order to prevent slippage, however risk is order may not be filled. Or set to 0 for no limit order. (default 1). |
|
Integer |
|
Set a limit on the minimum number of days a stock can remain on the books (i.e. how long stock remain in an open position state). (default 10). After the day, QuantStream will sell it IF the loss is less than maxpctsellloss. |
|
Integer |
|
Set a limit on the maximum number of days a stock can remain on the books (i.e. how long stock remain in an open position state). (default 30). After the day, QuantStream will sell it regardless of loss. |
|
Integer |
|
Set a limit on the maximum number of trades QuantStream can make - this could be important to limit trade fees. (default 100). Or set to -1 for unlimited trades. |
|
Integer |
|
Set a limit on the maximum number of shares QuantStream can trade - this could be important to limit trade fees. (default 5000). Or set to -1 to let QuantStream decide any amount. |
|
Float |
|
Set a limit on the minimum prediction level for QuantStream algorithm that predicts Buy, Sell, Hold signals. (default 0.7). |
|
Float |
|
Cost for each trade. (default 1.0). Change as needed. |
|
Float |
|
Cost for each share. (default 0.0035). Change as needed. |
|
Float |
|
Sell when the profit is greater than or equal to 1%. (default 0.01). Change as needed. |
|
Int |
|
Set the maximum number of shares per trade to limit trade fees. (default 50). Set to -1 to let QuantStream decide. |
|
Int |
|
If Set to 1, QuantStream will update the trade budget based on the final PnL. This enables compounding of PnL. Set to 0 if you DO NOT want QuantStream to update trade budget. |
|
Int |
|
If Set to 1, QuantStream agent will manage the open position for you..see below for details. If set to 2, QuantStream agent will stop buying if PnL is greater than 2% of trade budget. |
22.13.2. Detailed Parameter Breakdown
minprice & maxprice: Establish a price boundary to focus trading activity on a specific equity tier (e.g., mid-cap stocks priced between $5 and $100).
volume: Ensures sufficient market liquidity by filtering for instruments that meet or exceed this volume metric.
minstocks: Sets the minimum number of stocks to trade.
maxstocks: Sets the maximum number of stocks tp trade.
minvolatility: Sets minimum volatility in the stocks i.e. 20.
maxvolatility: Sets maximum volatility in the stocks i.e. 40.
tradebudget: Limits the total capital exposed by the strategy.
market, exchange, & exchangename: Define the geographic and institutional venue parameters.
broker & brokerurl: Identify the execution broker and provide reference access to their platform.
tradelossnotexceed: Acts as a risk guardrail, placing a cap on how much money a single trade is allowed to lose.
maxopenpositions: Controls portfolio diversification and prevents over-leveraging by capping simultaneous trades.
maxpctsellloss: Establishes a percentage-based threshold for sell-side loss mitigation.
minholdseconds: Prevents high-frequency churn or premature exits by enforcing a minimum duration a position must be held.
minentryeconds: Manages execution pacing by ensuring a mandatory time buffer between trade entries.
notionalpct: Dynamically optimizes quantity based on fraction of total trade budget allocated per position (default 10%).
maxpricedrop: Max price drop protection (default: 0.1)
addlimitorder: Prevent slippage in buy price (default: 1)
mindaysonbooks: Min days for open positions (default: 10). QuantStream will sell IF loss is less than maxpctsellloss.
maxdaysonbooks: Max days for open positions (default: 30). QuantStream will sell regardless of loss.
maxtrades: Max trades to make to control trade fees (default: 100)
maxshares: Max shares to make to control trade fees (default: 4000)
minpredictionaccuracy: Min prediction accuracy for QuantStream prediction accruacy (default: 0.7)
take_profit_pct: Take profit when profit exceed this percentage. (default: 0.01)
maxsharespertrade: Maximum shares per trade. Set to -1 to let QuantStream decide. (default: 50)
autotradebudget: If Set to 1, QuantStream will update the trade budget based on the final PnL. This enables compounding of PnL. Set to 0 if you DO NOT want QuantStream to update trade budget. (default: 1)
autoqsagentstrategy: Set to 1, if you want QuantStream agent to manage the trading day. For example, if 1, QS agent will establish an OPEN position on ALL stocks (maxstock) before 12p, between 12-2, agent will open 50% of the positions and start to liquidate position, and after 2p it will settle to maxopenpositions.
If autoqsagentstrategy = 2, then QuantStream will stop buying if PnL is greater than 2% of trade budget.
(default: 1)
cost_per_trade: Represents the base per-order component of the broker’s tiered commission structure, acting as a baseline routing or order-handling fee charged per executed transaction ticket.
cost_per_share: The variable commission rate charged per individual share traded under the tiered volume bracket.
22.14. QuantStream AI Agent Risk Guardrails
QuantStream AI operates as an automated state manager that tracks open positions, cumulative PnL, circuit breakers, and enforces strict pre-trade and post-trade validation checks.
22.14.1. Complete Guard Rails Reference Matrix
Below is the complete suite of operational guard rails implemented in the risk engine:
Guard Name |
Scope |
Trigger Condition |
System Action |
Rationale |
|---|---|---|---|---|
Circuit Breaker |
Portfolio |
Cumulative losses reach or exceed |
HALTS ALL NEW BUYS ( |
Protects total capital from complete depletion during systemic drawdowns. |
Guard 1: Duplicate Buy Protection |
Order (BUY) |
Incoming |
BLOCKS BUY ( |
Prevents accidental position doubling and over-concentration. |
Guard 2: Max Open Positions |
Portfolio (BUY) |
Total active holdings reach |
BLOCKS BUY ( |
Enforces institutional capital allocation caps and portfolio diversification. |
Guard 3: Re-Entry Cooldown |
Symbol (BUY) |
Incoming |
BLOCKS BUY ( |
Prevents algorithmic whipsawing and high-frequency re-entry churn. |
Guard 4: Orphaned Sell Block |
Order (SELL) |
Incoming |
BLOCKS SELL ( |
Prevents unauthorized shorting, ghost liquidations, or execution drift. |
Guard 5: Minimum Hold Time |
Symbol (SELL) |
Incoming |
BLOCKS SELL ( |
Enforces minimum holding duration to filter out market noise and micro-fluctuations. |
Guard 6: Execution Price Validation |
Order (SELL) |
Incoming |
BLOCKS SELL ( |
Ensures missing price data does not corrupt risk evaluation math. |
Guard 7: Stop-Loss & Loss Floor |
Order (SELL) |
Incoming
|
BLOCKS SELL ( |
|
22.14.2. Stop-Loss & Loss Floor Implementation (Guard 7)
The following Python snippet implements the dual-threshold risk logic for Guard 7:
# Guard 7: Institutional Stop-Loss, Take-Profit Target, Max Days Enforcement
if incoming_signal == "SELL" and is_currently_holding and price is not None:
buy_price = self.open_positions[symbol]["price"]
current_loss_pct = (buy_price - price) / buy_price
current_gain_pct = (price - buy_price) / buy_price
daysonbooks = self.open_positions[symbol]["daysonbooks"]
#mhour,mmin,maxhour,maxmin=cf.checktime("US")
# 1. Absolute Emergency Floor (Triggers instantly on day 1 if crash is severe)
if current_loss_pct >= (self.max_allowable_drop * 1.5): # e.g., 15% instant drop
cf.writeviperlogs(test, f"[FATAL EMERGENCY STOP] {symbol} catastrophic drop {current_loss_pct:.2f}. SELL immediately.")
return True
# 1.2. Standard Stop-Loss with your Grace Period Window
if current_loss_pct >= self.max_allowable_drop and self.min_days_on_books <= daysonbooks <= self.max_days_on_books:
cf.writeviperlogs(test, f"[AGENT GRACE-PERIOD STOP LOSS] {symbol} STOP-LOSS {current_loss_pct:.2f} at day {daysonbooks}. SELL approved.")
return True
# 2. TAKE-PROFIT TARGET: Allow sell immediately if profit reaches user-defined threshold (e.g., 5% / 0.05)
if current_gain_pct >= self.take_profit_pct:
self._log_throttled(f"{symbol}_take_profit", f"[TAKE PROFIT] {symbol} reached {current_gain_pct*100:.2f}% gain (Target: {self.take_profit_pct*100:.2f}%). SELL approved.")
cf.writeviperlogs(test, f"[AGENT TAKE PROFIT] {symbol} reached target profit of {current_gain_pct*100:.2f}%. SELL approved.")
return True
# 3. TIME-BASED EXIT: Allow sell if maximum days on books limit is reached or min days on books is reached and less than max sell loss
daysonbooks = self.open_positions[symbol]["daysonbooks"]
if daysonbooks >= self.max_days_on_books or (daysonbooks >= self.min_days_on_books and current_loss_pct <= self.max_pct_sell_loss):
cf.writeviperlogs(test, f"[AGENT DAYS ON BOOK] {symbol} Exceeds or equal to days on book: {daysonbooks}. SELL approved.")
return True
# 4. STRICT TARGET BLOCK: Block all other SELL signals if neither stop-loss, take-profit, nor max days is met
print(f"[GUARD BLOCK] {symbol} gain ({current_gain_pct*100:.2f}% ) has not met take-profit target ({self.take_profit_pct*100:.2f}%). SELL blocked.")
cf.writeviperlogs(test, f"[AGENT GUARD BLOCK] {symbol} gain ({current_gain_pct*100:.2f}%) has not met take-profit target ({self.take_profit_pct*100:.2f}%). SELL blocked.")
return False
22.15. Fee Model & Cost Optimization
To maintain institutional cost efficiency, QuantStream AI incorporates a transparent fee accounting model specifically optimized for IBKR Pro Tiered Pricing in Canada.
Fee Structure & Ceilings: * Blended Commission Formula: Total daily trading fees are calculated dynamically as:
\[\text{Total Fees} = (\text{Total Trades} \times \text{cost_per_trade}) + (\text{Total Shares} \times \text{cost_per_share})\]
Strict $25/Day Fee Ceiling: To prevent excessive commission drag during high-volatility sessions, operational caps are enforced at 70 trades/day and 1,500 shares/day via qsconfig.ini. If trading fees are not a concern set max_trades=-1 and max_shares=-1.
Pass-Through Metrics: Fully accounts for exchange ECN fees, clearing fees, regulatory percentages, and minimum order commission floors ($0.35 USD).
22.16. QuantStream Institutional Alpha & Tail-Risk Guarded Stock Screener
QuantStream performs advanced Autonomous Market Regime Adaptation with Multi-Objective NSGA-II Sobol Genetic Factor Optimization.
The QuantStream Institutional Alpha Screener is an advanced quantitative engine designed to screen US equity markets (NASDAQ and NYSE) for high-alpha equities. It combines Quasi-Monte Carlo (QMC) variance reduction using low-discrepancy Sobol sequences with NSGA-II Multi-Objective Genetic Pareto Optimization, Bayesian Hierarchical Shrinkage, and Stress-Bootstrapped Monte Carlo Simulations over a dynamic holding horizon (\(H = maxdaysonbooks\)).
22.17. Key Architectural Features
Dynamic Universe Retrieval & Verification: Automatically harvests listing directories directly from NASDAQ Trader (nasdaqlisted.txt and otherlisted.txt). The engine strips out warrants, preferred shares, test issues, ADRs/ADSs, and exchange-traded funds (ETFs) to isolate common equities. If exchange files are unreachable, it executes a multi-stage fallback to index constituents (S&P 500, Nasdaq-100, DJIA) and local caches.
Pre-Market Telemetry & Index Futures Risk Shield (ES=F, NQ=F): Monitors pre-market price action and overnight index futures contracts (NQ=F, ES=F). If pre-market Nasdaq futures drop by \(\le -1.5\%\), an autonomous Risk-Off Shield trips, shifting factor priorities toward deep valuation, defensive quality, and downside protection.
NSGA-II Multi-Objective Sobol Genetic Factor Optimization: Replaces fixed factor weights with a real-time non-dominated sorting genetic algorithm (NSGA-II). The population space is initialized via Scrambled Sobol Sequences to optimize competing institutional objectives: maximizing risk-adjusted return (Sortino ratio) while minimizing tail drawdown risk along a Pareto frontier.
Empirical Bayes Hierarchical Shrinkage & Tail Confidence Overlay: Mitigates cross-sectional sample noise in single-stock scores by regularizing individual scores toward sector and global averages using an Empirical Bayes weighting parameter (\(\lambda_k\)). Ratings are further adjusted by a Gaussian survival function of modeled 30-day downside touch probabilities.
3,000-Path Hybrid Monte Carlo Risk Engine: Combines Circular Moving-Block Bootstrapping (5-day blocks to preserve empirical autocorrelation) with Heavy-Tailed Student-t Distribution Shocks (\(\nu = 4\), 1.25x volatility multiplier) to compute 30-day downside-touch probabilities (\(P_{\text{touch}}\)), terminal drop risks, and 5% Expected Shortfall (\(\text{ES}_{5\%}\)).
Leakage-Aware Out-of-Sample Walk-Forward Backtesting: Evaluates true out-of-sample forward returns over historical anchor windows across 7-, 14-, and 30-trading-day horizons, net of round-trip transaction costs (default 10 bps).
Guaranteed Target-Count Fallback Cascade: Employs a multi-tier fallback mechanism with dynamic parameter relaxation (floor scaling) ensuring the output count (e.g., 50 stocks) is strictly filled under all volatility regimes.
22.18. Methodology & Quantitative Mathematics
22.18.1. Dynamic Data-Quality Gates & Fundamental Valuation
Before candidate scoring, stocks pass fundamental sanity filters:
P/E Ratio Gate: \(0 < \text{P/E} \le 500\)
P/B Ratio Gate: \(0 < \text{P/B} \le 100\)
Profit Margin Gate: \(-200\% \le \text{Profit Margin} \le +200\%\)
Sector Verification: Valid GICS economic sector classification.
Fundamental metrics are combined into a composite Valuation Score:
Note
Plain-English Explanation: Before the screener even looks at charts or momentum, it acts like a strict accountant. It filters out broken data, extreme accounting anomalies (like nonsense P/E ratios or bankrupt margins), and averages out basic value metrics so the system doesn’t get tricked by garbage numbers.
22.18.2. Quasi-Monte Carlo (QMC) Sobol Sequence Initialization
Standard random sampling creates artificial clustering. The Pareto genetic optimizer initializes parameter space using a scrambled \(d\)-dimensional Sobol sequence:
For a 7-factor decision matrix, Sobol sampling ensures uniform space-filling coverage across the hyper-cube \([0, 1]^7\), accelerating convergence to the global Pareto frontier.
Note
Plain-English Explanation: Imagine you are testing thousands of recipes by throwing darts blindly at a board. Random throwing often leaves big gaps or clusters. A Sobol sequence acts like a smart, evenly spaced grid, ensuring the computer tests a perfectly balanced variety of recipe combinations without missing any blind spots.
22.18.3. NSGA-II Multi-Objective Evolutionary Pareto Optimization
The screener evaluates seven distinct institutional factor percentile rankings:
The chromosome weight vector \(\mathbf{w} = [w_1, \dots, w_7]\) with constraint \(\sum w_k = 1.0\) is optimized across dual competing objectives:
Pareto selection ranks chromosomes based on composite fitness:
Note
Plain-English Explanation: This is like a fitness coach trying to build the ultimate athlete who is both a world-class sprinter and a marathon runner. Instead of just picking one goal, the genetic algorithm constantly tests and evolves factor mixes to find the absolute best trade-off between maximizing profits and keeping drawdowns low.
22.18.4. Circular Moving-Block Bootstrapping & Heavy-Tailed Stress Simulation
Historical log returns \(r_t = \ln(P_t / P_{t-1})\) are simulated across 3,000 paths over forward horizon \(H\):
Bootstrap Component: Resamples contiguous 5-day blocks \(B_k = \{r_i, r_{i+1}, \dots, r_{i+4}\}\) to preserve volatility clustering.
Stressed Student-t Component: Draws shocks from a Student-t distribution (\(\nu = 4\)) scaled by inflated historical volatility (\(\sigma_{\text{stress}} = 1.25 \times \sigma_{\text{hist}}\)) and non-positive drift (\(\mu_{\text{stress}} = \min(\mu, 0)\)):
The 30-day downside touch probability \(P_{\text{touch}}\) evaluates whether path drawdowns cross the threshold \(\theta = \ln(1 + \text{DROP_THRESHOLD})\):
Note
Plain-English Explanation: Imagine simulating 3,000 different possible futures for a stock over the next month. The system takes real chunks of past market behavior (to keep realistic price swings) and then artificially injects surprise economic storms (heavy-tailed shocks) to ask: “What is the exact chance this stock crashes by 10%?”
22.18.5. Empirical Bayes Hierarchical Shrinkage Framework
To eliminate sample noise in small sector samples, raw stock scores \(S_{i,k}\) in sector \(k\) undergo Bayesian shrinkage:
The final Gold Mine Alpha Rating applies a tail-risk confidence multiplier based on the standard normal survival function \(\Phi(x)\):
Note
Plain-English Explanation: If a small sector only has two or three stocks, one lucky winner can make the whole group look artificially genius. Bayesian shrinkage acts like a reality check—pulling small, noisy groups back toward the broader market average so the screener doesn’t chase false alarms.
22.18.6. True Hierarchical Risk Parity (HRP) Tree Clustering
To eliminate portfolio concentration risk across highly correlated market sectors, the engine applies a true Hierarchical Risk Parity (HRP) tree clustering algorithm on return correlation distance.
Distance Metric: Constructs a correlation matrix over recent historical returns (\(N = 60\) bars) and computes Euclidean distance over correlation:
Ward Linkage & Clustering: Applies agglomerative hierarchical clustering via Ward’s minimum variance method and partitions the asset space into clusters using inconsistency thresholds \(t = \lceil N_{\text{final}} / 3 \rceil\) .
Cluster Allocation: Iteratively selects candidates across distinct tree clusters to guarantee maximal structural diversification before final index export.
Note
Plain-English Explanation: If you buy ten “different” tech stocks that all crash at the exact same time when Nasdaq drops, you aren’t actually diversified. HRP builds a family tree of how stocks move together and forces the portfolio to pick leaders from entirely different, uncorrelated branches.
22.18.7. Volume-Synchronized Probability of Toxicity (VPIN) & Order Flow Delta
To detect stealth institutional order flow imbalance and adverse selection risk, the screener computes high-frequency volume toxicity metrics.
VPIN Volume Bucketing: Aggregates intraday price and volume distributions into equal-volume buckets (\(V_{\text{bucket}} = \frac{\sum V}{10}\)) to measure order flow toxicity independent of clock time.
Order Flow Delta: Quantifies buying versus selling pressure accumulation using close-location-value (CLV) scaling weighted by trading volume:
Note
Plain-English Explanation: This acts like an undercover surveillance system for big institutional players. It measures whether stealth buyers or aggressive sellers are secretly accumulating or dumping shares behind the scenes, alerting the system to hidden market traps.
22.18.8. Cross-Sectional Residual Momentum via OLS Orthogonalization
To prevent selecting stocks that merely ride broad market beta, momentum is isolated via Ordinary Least Squares (OLS) regression.
Market Orthogonalization: Regresses each candidate asset’s return series against the benchmark (SPY) return history to strip out systemic market co-movement:
Residual Alpha Accumulation: Ranks candidates based on annualized cumulative idiosyncratic residuals (\(\varepsilon_{i,t}\)) over a 60-day window.
Note
Plain-English Explanation: Any stock looks like a momentum champion on a day when the entire stock market is surging. This math strips away the general market tide (beta) to find stocks that are winning purely on their own unique, independent strength.
22.19. Extended Mathematical Formulations & Stochastic Derivations
22.19.1. Stochastic Differential Equation (SDE) & Beta-Adjusted Macro Drift
Let the underlying asset price \(P_t\) follow a geometric Brownian motion under physical measure \(\mathbb{P}\) adjusted for systemic market covariance:
Where:
\(\mu_i\) represents the asset’s idiosyncratic drift coefficient.
\(\sigma_i\) is the annualized volatility of asset \(i\).
\(dW_t\) is a standard Wiener process increment (\(\mathbb{E}[dW_t] = 0, \text{Var}(dW_t) = dt\)).
\(\beta_i = \frac{\text{Cov}(R_i, R_{\text{SPY}})}{\text{Var}(R_{\text{SPY}})}\) is the systemic equity beta relative to the benchmark.
\(dM_t\) is the systemic macro shock increment governed by index futures telemetry (NQ=F, ES=F). Under overnight market distress (\(\Delta \text{NQ} \le -1.5\%\)), the drift parameter \(\mu_i\) is explicitly bounded:
Note
Plain-English Explanation: This is the underlying mathematical physics engine. It tracks how a stock drifts day-to-day while dynamically slamming the brakes (adjusting the drift) the moment overnight futures flash a macro panic warning.
22.19.2. Circular Moving-Block Bootstrap Transition Probability
To capture empirical autocorrelation, skewness, and volatility clustering without assuming parametric normality, historical daily log returns \(\mathbf{R} = \{r_1, r_2, \dots, r_N\}\) of length \(N\) are indexed circularly.
The probability of drawing starting index \(I_j\) for block \(B_k\) of width \(w = 5\) at simulation step \(j\) is defined by modular arithmetic:
This preserves higher-order empirical moments by recycling the tail of the return series back to the beginning without artificial truncation bias.
Note
Plain-English Explanation: When recycling past stock data for our 3,000 simulation futures, this logic ensures that when we reach the end of a historical data list, we loop back to the beginning seamlessly without breaking the natural rhythm and volatility patterns of the asset.
22.19.3. NSGA-II Crowding Distance & Pareto Dominance Mathematics
The GeneticFactorOptimizer ranks institutional weight chromosomes using Non-Dominated Sorting Genetic Algorithm II (NSGA-II) principles.
Pareto Dominance: Solution vector \(\mathbf{w}^{(1)}\) dominates \(\mathbf{w}^{(2)}\) (denoted \(\mathbf{w}^{(1)} \preceq \mathbf{w}^{(2)}\)) if and only if:
Crowding Distance Metric: To maintain diversity along the Pareto front, the local density of solutions surrounding individual \(i\) is estimated by calculating the bounding box perimeter distance across objective function space:
Solutions with higher crowding distances are preferentially selected to prevent genetic premature convergence and ensure robust hyperparameter exploration.
Note
Plain-English Explanation: When the genetic optimizer is evolving stock strategies, it doesn’t want all the best solutions huddled in one crowded corner. The crowding distance rule forces the algorithm to explore diverse, distinct alternative strategies across the board.
22.20. QuantStream Screener Differentiation: Feature Comparison Matrix
Feature Dimension |
Traditional Retail / Basic Screeners |
QuantStream Institutional Screener |
|---|---|---|
Analytical Perspective |
Backward-looking historical metrics (P/E, 50-day SMA, RSI). |
Forward-looking stochastic path simulation & walk-forward backtests over holding windows. |
Weight Optimization |
Static heuristic formulas or equal weightings. |
NSGA-II Multi-Objective Sobol Genetic Optimizer finding optimal Pareto frontiers. |
Macro Risk Telemetry |
Static sector labels; blind to overnight events. |
Pre-market Index Futures (`NQ=F`, `ES=F`) Telemetry with automated Risk-Off Shielding. |
Cross-Sectional Regularization |
Unadjusted noisy point scores. |
Empirical Bayes Hierarchical Shrinkage regularized against sector and global means. |
Downside Risk Engine |
Simple standard deviation (penalizes upside volatility). |
Hybrid 3,000-Path Monte Carlo (Moving-Block Bootstrap + Heavy-Tailed Student-t Shocks). |
Universe Reliability |
Fixed static stock lists; fails during low-volatility squeezes. |
Auto-relaxing candidate selection cascade guaranteeing target output volume. |
22.21. System Architecture & Operational Pipeline
Universe Ingestion: Fetches symbols from Nasdaq Trader directories, removes non-common equities (warrants, ETFs, preferreds).
Snapshot Prefilter: Applies NASDAQ API bulk snapshot filtering to eliminate ultra-low volume/price outliers.
Multi-Threaded Download: Downloads historical price structures (2y period) in parallel batches using thread pools.
Data-Quality Gate: Evaluates fundamental parameters and drops invalid securities.
Macro & Futures Telemetry: Evaluates overnight NQ=F returns and SPY benchmark moving averages to determine current macro bias.
Walk-Forward Backtesting: Calculates net out-of-sample 7d, 14d, and 30d forward returns net of transaction slippage.
NSGA-II GA Optimization: Evolves population factor weights against historical walk-forward metrics.
Bayesian Shrinkage & Output: Regularizes composite ratings across sectors, computes Gold Mine Alpha Ratings, and exports structured CSV reports (best_stock_candidates.csv and gold_mine_candidates.csv).
22.22. CSV Output Schema Reference
Column Name |
Type |
Description |
|---|---|---|
Ticker |
String |
Primary equity symbol. |
Sector |
String |
GICS Sector classification. |
Price_USD |
Float |
Current execution price (adjusted for pre-market overlay if active). |
Valuation_Score |
Float |
Composite valuation score derived from P/E, P/B, and FCF Yield (0–100). |
P_Touch_Minus10pct_30d_Pct |
Float |
Modeled 30-day downside touch probability (%). |
Backtested_Net_Return_30d_Pct |
Float |
Out-of-sample walk-forward net return over 30 days net of transaction costs (%). |
Best_Stock_Score |
Float |
GA-optimized multi-factor composite score. |
Gold_Mine_Alpha_Rating |
Float |
Bayesian regularized score with tail-risk survival overlay. |
Risk_Tier |
String |
Modeled downside risk classification tier. |
22.23. QuantStream AI vs. Billion-Dollar Quant Hedge Funds: Architecture Matrix
22.24. Overview
To understand the power and sophistication of the QuantStream AI stock screener, it is helpful to compare its quantitative architecture directly against the multi-billion-dollar infrastructure deployed by elite quantitative hedge funds (such as Renaissance Technologies, Two Sigma, and Citadel).
While institutional funds operate across vast compute clusters and petabyte-scale alternative datasets, QuantStream successfully distills the core mathematical machinery of a systematic desk into a lean, high-performance Python engine.
22.25. Institutional Feature Comparison
Architectural Dimension |
Multi-Billion Dollar Quant Hedge Funds |
QuantStream AI Screener |
|---|---|---|
Factor Weight Optimization |
Proprietary machine learning and reinforcement learning models trained on distributed compute grids. |
NSGA-II Multi-Objective Sobol Genetic Optimizer co-optimizing Sortino risk-adjusted returns and drawdown risk. |
Space-Filling Initialization |
Latin Hypercube and Sobol low-discrepancy pseudo-random generators for robust parameter search. |
Quasi-Monte Carlo Sobol Sequences ( |
Statistical Regularization |
Proprietary hierarchical shrinkage models to prevent factor crowding and small-sample variance. |
Empirical Bayes Sector Shrinkage (\(\lambda_k\)) regularizing individual scores against sector means. |
Tail-Risk Stress Testing |
Full-scale macroeconomic historical stress testing (2008 GFC, 2020 COVID shock models). |
Circular Moving-Block Bootstrap with Student-t Shocks (\(\text{df}=4\)) capturing fat tails and autocorrelation. |
Macro Regime Awareness |
Real-time cross-asset correlation matrices, NLP sentiment parsers, and automated overnight risk deleveraging. |
Pre-Market Index Futures Telemetry ( |
Production Reliability |
Dedicated 24/7 DevOps teams maintaining cluster fault tolerance and automated failovers. |
Progressive Multi-Attempt Relaxation Loops guaranteeing minimum target count fulfillment ( |
22.26. Key Institutional Advantages
22.26.1. 1. Peerless Multi-Objective Pareto Optimization
By implementing Non-Dominated Sorting Genetic Algorithms (NSGA-II) combined with Quasi-Monte Carlo Sobol initialization, QuantStream achieves true Pareto-optimal trade-offs between return and risk, mirroring the exact multi-objective search techniques utilized by professional asset managers.
22.26.2. 2. Robust Statistical Regularization
Factor crowding is a primary hazard in quantitative investing. QuantStream neutralizes this via Empirical Bayes Sector Shrinkage (\(\lambda_k\)), anchoring individual asset scores to broader sector behaviors to eliminate small-sample noise and false-positive alpha spikes.
22.26.3. 3. Real-Time Overnight Macro Shielding
Rather than reacting reactively during market hours, the screener monitors overnight index futures (ES=F, NQ=F) before the opening bell. If an overnight shock (such as a \(\ge 1.5\%\) drop in Nasdaq futures) is detected, the engine automatically pivots its genetic weighting toward deep value and low-drawdown defensive profiles.
22.26.4. 4. Bulletproof Production Execution
Traditional quantitative pipelines frequently crash or return empty datasets when strict market filters tighten. QuantStream’s progressive multi-attempt relaxation loops intelligently expand drawdown limits and volume floors iteratively, ensuring an institutional-grade candidate cohort is always successfully generated.
22.27. QuantStream Apex Institutional Deadwood Screener & Portfolio Pruning Module
22.27.1. Overview
The Apex Deadwood Screener is an institutional-grade automated portfolio pruning module designed for the QuantStream trading agent. Its primary objective is to identify and liquidate underperforming assets (“deadwood”) while enforcing strict safety guardrails, macro-sector matrix rules, fundamental quality vetoes, and NSGA-II genetic multi-factor optimization to prevent premature liquidation of positions showing potential recovery or idiosyncratic strength.
22.27.2. Architecture & Pipeline
Input Ingestion: Parses incoming portfolio arrays containing position metadata (ticker, buy price, quantity, days on books, minimum/maximum holding horizons).
Batch Data Fetching: Retrieves historical price, volume, and macro futures data in vectorized batches via
yfinance, alongside S&P 500 (SPY) benchmark data and sector ETFs for comparative relative strength, residual momentum, and sector rotation analysis.Advanced Quantitative Engines: Executes high-performance vectorized NumPy simulations, rolling walk-forward backtests, OLS residual momentum orthogonalization, return skewness convexity calculations, and Bayesian sector shrinkage.
Macro & Sector Evaluation: Filters broad market conditions via Nasdaq/S&P futures (
NQ=F,ES=F) and maps stock sectors to dynamic industry ETFs for rotation matrix checks.Genetic Factor Weighting: Dynamically co-optimizes position retention scores via an NSGA-II multi-objective genetic optimizer initialized with Sobol QMC sequences.
Decision Logic: Applies enhanced multi-factor warning thresholds, fundamental quality protection vetoes, sector rotation matrix constraints, and hard expiration overrides.
Execution Payload & Audit Ledger: Returns a structured pandas DataFrame with actionable liquidation flags while persistently logging decisions to
deadwood_audit_ledger.csv.
22.27.3. Key Quantitative Mechanisms
Simulates 3,000 parallel price paths over the remaining holding horizon (days_left) using a combination of historical bootstrap block sampling (block size = 5) and parametric Student-t distribution shocks (\(df = 4\), volatility multiplier = \(1.25\)).
Downside Touch Probability (
_risk_touch): The likelihood that an asset touches a \(-10\%\) drawdown threshold over the remaining duration.Expected Remaining Return (
Expected_Return_Remaining_Pct): Projected terminal return across simulated paths combined with walk-forward backtests.
Note
Plain-English Explanation: This acts like a stress-tester for open positions. It simulates 3,000 possible future scenarios for the remaining days on books, injecting sudden market storms to see if a stock has a high risk of crashing by 10%.
Evaluates historical rolling windows matching the asset’s remaining horizon, factoring in base transaction costs plus Amihud illiquidity penalties to measure expected real-world net drift.
Note
Plain-English Explanation: Before deciding a stock is deadwood, the system looks back at how similar price periods historically behaved and accounts for trading costs and hidden friction (like illiquidity) to see if it’s truly broken or just sluggish.
Residual Momentum: Isolates true idiosyncratic alpha by regressing asset returns against the SPY benchmark via OLS regression to strip out systemic market beta.
Return Skewness: Measures right-tailed return asymmetry to protect convex return distributions.
Note
Plain-English Explanation: This checks if a stock is just drifting down because the whole market is falling (market beta) or if it has special, hidden upside strength and asymmetric comeback potential that makes it worth holding.
Genetic Factor Optimizer: Dynamically weights return ranks, momentum ranks, residual momentum, convexity, downside risk scores, and oversold resilience.
Empirical Bayes Sector Shrinkage: Regularizes individual health scores toward sector means to eliminate small-sample statistical noise.
Note
Plain-English Explanation: The system uses an advanced evolutionary algorithm to figure out how much weight to give to momentum versus risk. It also smooths out small-sample noise by comparing each position against its broader sector family.
22.27.4. Macro Futures Trend Filtering & Sector Rotation Matrix
Evaluates short-term price changes in Nasdaq Futures (NQ=F) and S&P 500 Futures (ES=F). If index futures drop more than \(0.5\%\), the broad market state is classified as Bad (-); otherwise, it is Good (+).
Dynamically maps individual stock sectors to primary industry ETFs (e.g., Technology to XLK, Financials to XLF, Healthcare to XLV) and tracks their trailing momentum.
Note
Plain-English Explanation: Before selling a stock, the system checks overnight futures and industry sector trends. If the whole sector or market is just having a temporary bad day, it won’t panic-sell a good company.
22.27.5. Institutional Anti-Deadwood Safety Vetoes & Fundamental Overrides
Even if a stock triggers multiple quantitative warning criteria and passes the market/sector matrix, it is forcefully protected from liquidation if any of the following institutional safety vetoes evaluate to True:
In-Profit Buffer: The current market price exceeds the original purchase price (
current_price > buy_price).Relative Strength (RS) Outperformance: The asset’s 20-day momentum outperforms the S&P 500 benchmark by more than \(+2.0\%\).
Volume Accumulation: Recent volume exceeds \(1.25\times\) its 20-day moving average on sessions where closing prices are flat to up, indicating institutional accumulation.
Oversold Resilience / Bad News Baked In: Maximum drawdown exceeds \(18.0\%\) while downside touch probability remains below \(25.0\%\).
Fundamental Quality Veto: P/E ratio is between 5.0 and 18.0, Free Cash Flow yield is \(\ge 6.0\%\), or ROIC is \(\ge 8.0\%\).
Note
Plain-English Explanation: Even if a stock looks weak on paper, the system has built-in “shield protocols.” If a stock is already making money, heavily accumulated by big institutions, or fundamentally pristine, these safety shields block it from being sold prematurely.
Warning
Hard Expiration Override: If a position reaches its maximum scheduled holding limit (dob >= max_dob) and exhibits worsening price projections, safety vetoes (except fundamental quality) are overridden to guarantee timely portfolio pruning.
22.27.6. Output DataFrame Schema
Column Name |
Data Type |
Description |
|---|---|---|
|
|
Asset symbol. |
|
|
GICS Industry Sector classification. |
|
|
Original execution purchase price. |
|
|
Latest market closing price. |
|
|
Position share quantity held. |
|
|
Current market value of the position. |
|
|
Total elapsed days since position entry. |
|
|
Maximum scheduled holding duration limit. |
|
|
Remaining holding horizon before expiration. |
|
|
Projected terminal return over remaining horizon (%). |
|
|
20-day trailing price momentum (%). |
|
|
OLS-orthogonalized idiosyncratic momentum score. |
|
|
Return distribution skewness / convexity. |
|
|
Maximum historical drawdown percentage. |
|
|
Categorical classification of downside exposure. |
|
|
Active safety override reasons or sector lag notations. |
|
|
Final pruning flag ( |
22.28. License & Disclaimer
Copyright (c) 2026 QuantStream AI. Distributed under institutional software deployment terms. Financial modeling outputs are provided for algorithmic execution frameworks and quantitative research purposes.