Beyond Indicators: The New Era of Hybrid Expert Advisors (ONNX + LLMs)

Beyond Indicators: The New Era of Hybrid Expert Advisors (ONNX + LLMs)

20 November 2025, 18:02
Mauricio Vellasquez
0
347

Beyond Indicators: The New Era of Hybrid Expert Advisors (ONNX + LLMs)


This article explores the frontier of algorithmic trading development: the fusion of rapid quantitative models (via ONNX) with the contextual reasoning capabilities of Large Language Models (LLMs) accessed via API.

Expert Advisor (EA) development is undergoing a paradigm shift. The days of relying solely on combinations of Moving Averages and RSI are fading. The search for "alpha" (competitive advantage) now lies in the ability to process not just numerical data, but also market context.

The most promising approach currently is the creation of Hybrid AI Systems. These systems combine two distinct forces:

  1. The Quantitative Brain (ONNX): Traditional Machine Learning (ML) models (like Neural Networks, XGBoost, LSTMs) trained to detect numerical patterns at high speed.
  2. The Contextual Brain (LLM via API): Models like GPT-4, Claude, or Llama, capable of reading news, analyzing macroeconomic sentiment, and "reasoning" about the current state of the market.

This article details the architecture of this fusion and provides pseudo-algorithms to guide development.

Why This Hybrid Approach?

Traditional ML-based Expert Advisors (using only past prices) suffer from a critical problem: they are "blind" to the outside world. An LSTM model might predict a rise in EURUSD based on 10 years of chart patterns, but it doesn't know that the European Central Bank just announced an unexpected interest rate cut.

By integrating an LLM, we give the EA "eyes."

  • The Role of ONNX (Speed and Numerical Precision): The ONNX (Open Neural Network Exchange) standard allows training heavy models in Python (using PyTorch, TensorFlow, Scikit-learn) and running them highly optimized within the trading platform (like MQL5). It is responsible for the short-term tactical signal.
  • The Role of the LLM (Reasoning and Sentiment): Via an API, we send textual data (financial news headlines, economic calendar data) to an LLM. The LLM acts as a senior analyst, providing a macro sentiment score and a risk summary.

System Architecture

The hybrid EA doesn't make decisions based on just one output. It functions like an automated investment committee.

  1. Data Input: The EA collects price data (OHLC, Tick volume) and external data (News via RSS, Economic Calendar).
  2. Parallel Processing:
    • Path A (Fast): Price data is transformed into features and passed to the local ONNX model. Output: Bullish_Trend_Probability (0.85) .
    • Path B (Slow/Asynchronous): Recent news is packaged into a prompt and sent via API to the LLM. Output: Macro_Sentiment_Score (-0.6 Bearish) .
  3. Fusion Engine: The core of the EA receives both signals and applies conditional logic. If ONNX says "Strong Buy," but the LLM says "Extreme Macro Risk," the EA may choose not to trade or drastically reduce lot size.

Implementation Pseudo-Algorithms

Implementation is divided into two crucial phases: offline training and online execution in the EA.

Phase 1: Training and Preparation (Offline - Python)

In this phase, focus on creating a robust numerical model.

# PSEUDO-ALGORITHM: ONNX MODEL TRAINING # Target Language: Python (Scikit-learn / PyTorch / TensorFlow) BEGIN # 1. Data Collection and Feature Engineering Historical_Data = LoadOHLCData("EURUSD", "H1", Last_5_Years) Features = CreateFeatures(Historical_Data) # Ex: RSI, Price Deltas, Volatility Targets = CreateTargets(Historical_Data) # Ex: Will price rise in next 5 bars? (0 or 1) # 2. Data Splitting Train_X, Test_X, Train_Y, Test_Y = TrainTestSplit(Features, Targets) # 3. Model Training (Example: Random Forest or Lightweight Neural Net) ML_Model = RandomForestClassifier(n_estimators=100, max_depth=10) ML_Model.fit(Train_X, Train_Y) # 4. Evaluation Accuracy = Evaluate(ML_Model, Test_X, Test_Y) IF Accuracy < Acceptable_Threshold THEN: Adjust hyperparameters and return to step 3 # 5. Convert to ONNX (Critical for use in the EA) # Define the expected input shape for the model Input_Signature = DefineInputSignature(Features.shape) ONNX_Model = ConvertToONNX(ML_Model, Input_Signature) # 6. Save files SaveFile("MyQuantModel.onnx", ONNX_Model) SaveMetadata("Features_Scaling_Params.json") # Important for normalizing data in the EA END

Phase 2: Hybrid EA Execution (Online - MQL5/C++)

This phase occurs inside the trading platform. The challenge here is managing the latency of the LLM API call. We cannot call the LLM on every tick.

// PSEUDO-ALGORITHM: EA EXECUTION LOOP (MQL5 LIKE) # Global Variables ONNX_Model_Handle = INVALID_HANDLE; Last_LLM_Call_Time = 0; LLM_Call_Interval_Seconds = 300; // Call LLM every 5 minutes, for example Current_Macro_State = "NEUTRAL"; Macro_Score = 0.0; // --- INITIALIZATION FUNCTION --- Function OnInit(): // Load the quantitative brain ONNX_Model_Handle = OnnxCreate("MyQuantModel.onnx", ONNX_DEFAULT); IF ONNX_Model_Handle == INVALID_HANDLE THEN Return ERR_INIT; // Configure WebRequest for LLM API (Ex: OpenAI URL) AllowURL("https://api.openai.com/v1/chat/completions"); Return INIT_SUCCEEDED; EndFunction // --- MAIN LOOP (ON EVERY NEW BAR OR TICK) --- Function OnTick(): // 1. Basic Checks IF New_Bars == FALSE AND Tick_Mode == FALSE THEN Return; // ============================================ // SUBSYSTEM 1: THE QUANTITATIVE BRAIN (ONNX) // ============================================ // Collect recent chart data Recent_Data = CopyOHLCBuffer(Current_Symbol, Current_Period, 100_Bars); // IMPORTANT: Apply the SAME normalization used in Python training Feature_Vector = RealTimeFeatureEngineering(Recent_Data, Metadata_Scaling); // Run ONNX inference (Fast, local) ML_Signal_Probability = OnnxRun(ONNX_Model_Handle, Feature_Vector); // ============================================ // SUBSYSTEM 2: THE CONTEXTUAL BRAIN (LLM API) // ============================================ // Latency and cost management: Do not call API constantly IF (CurrentTime() - Last_LLM_Call_Time) > LLM_Call_Interval_Seconds THEN: // Collect recent news (ex: via integrated economic calendar or RSS) Headlines_Text = CollectLatestFinancialNews(Last_30_Minutes); IF Headlines_Text IS NOT EMPTY THEN: // Build Structured Prompt Prompt = "Analyze the following financial headlines for EURUSD: " + Headlines_Text + ". Respond ONLY in JSON format: {'sentiment': 'score from -1 to 1', 'reason_summary': 'short text'}."; // Send HTTP Request (Asynchronous if possible, synchronous in standard MQL5) JSON_Response = WebRequest("POST", API_URL, API_KEY, Prompt); // Process Response IF WebRequest_Success THEN: Macro_Score = ExtractScoreFromJSON(JSON_Response); // Ex: -0.7 Current_Macro_State = ExtractReasonFromJSON(JSON_Response); Last_LLM_Call_Time = CurrentTime(); Print("LLM Macro Update: Sentiment ", Macro_Score, " Reason: ", Current_Macro_State); ELSE: Print("Error calling LLM API. Maintaining previous state."); END_IF // ============================================ // FUSION AND DECISION ENGINE // ============================================ Final_Decision = "NEUTRAL"; // Example Fusion Logic: The LLM acts as a risk filter // BUY Scenario IF (ML_Signal_Probability > 0.80) AND (Macro_Score >= -0.2) THEN: // Quant model sees strong buy AND macro is not terribly negative Final_Decision = "BUY"; // SELL Scenario ELSE IF (ML_Signal_Probability < 0.20) AND (Macro_Score <= 0.2) THEN: // Quant model sees strong sell AND macro is not extremely positive Final_Decision = "SELL"; // CONFLICT/RISK Scenario (Example) ELSE IF (ML_Signal_Probability > 0.90) AND (Macro_Score < -0.5) THEN: Print("ALERT: Severe conflict. Quant indicates Strong Buy, but Macro is very negative. Avoiding trade."); Final_Decision = "NEUTRAL"; // Execution IF Final_Decision == "BUY" AND NoOpenPositions() THEN SendBuyOrder(); IF Final_Decision == "SELL" AND NoOpenPositions() THEN SendSellOrder(); EndFunction

The Shortcut to Disciplined Trading

Building complex hybrid systems like the one described above requires significant expertise in data science, programming, and infrastructure management. However, achieving professional-grade discipline and consistency doesn't always mean reinventing the wheel.

For traders seeking robust, ready-made solutions that prioritize discipline and adaptable strategies, Ratio X Trading Systems offers a powerful alternative. We provide a suite of sophisticated Expert Advisors designed to navigate various market conditions without the need for you to code complex AI integrations yourself.

MQL5 Community Exclusive: We are currently offering a special promotion for our friends in the MQL5 community to access the complete Ratio X Toolbox.

🚨 Urgent Update: Due to high demand, there are only 2 coupons remaining for 20% OFF lifetime access.

Don't miss this final opportunity to secure professional-grade tools at an unbeatable price.

➡️ Click here to claim one of the last 2 coupons

Challenges and Considerations in Hybrid Development

  1. API Latency: API calls to LLMs can take 1 to 5 seconds. Your EA must be designed to handle this delay without freezing the main tick flow, or use calls only to define strategic bias, not for high-frequency execution.
  2. Prompt Engineering: The quality of the LLM output depends entirely on the quality of your prompt. Instruct the model to be a skeptical financial analyst and provide structured outputs (like JSON) for easy parsing in the EA.
  3. API Costs: Frequent calls to models like GPT-4 can get expensive. Optimize call intervals and prompt sizes.
  4. Data Normalization: The most common error is forgetting to apply the exact same normalization (StandardScaler, MinMaxScaler) used in Python training to real-time data within the EA before sending it to the ONNX model.

Conclusion

The combination of local ONNX models for fast signal processing and cloud-based LLMs for contextual reasoning represents the future of sophisticated Expert Advisors. This architecture allows for the creation of systems that do not just react to price movements but "understand" the environment in which they are operating, more closely mimicking the decision-making process of a professional human trader.