As an MQL developer, I’ve spent years constructing buying and selling robots. All of us chase the identical factor: a system that’s each clever and sturdy. We depend on technical indicators, value motion, and sophisticated logic to search out an edge. For a very long time, Machine Studying felt like a “holy grail,” however one which was simply out of attain or an excessive amount of of a “black field.”
My fundamental hesitation was this: I do not need an EA that *simply* depends on a blind prediction. The market has context. A mannequin educated on historic information would possibly say “BUY,” however as a dealer, I do know that sign is nugatory if the unfold is 100 pips, volatility is zero, or a significant pattern on a better timeframe is screaming “SELL.”
So, I made a decision to construct one thing totally different: a Hybrid EA. An EA that makes use of a robust Machine Studying mannequin for its core sign however then validates that sign towards a gauntlet of confirmed, “commonsense” technical confluence filters.
Immediately, I wish to stroll you thru the precise course of I used to construct this, from a Python script to a completely purposeful MQL5 Knowledgeable Advisor.
Half 1: The ML Workflow – From Information to Mannequin
You’ll be able to’t simply “make” an AI. It’s important to prepare it. This whole a part of the method occurs exterior of MetaTrader, sometimes in Python utilizing libraries like TensorFlow (Keras) and Scikit-learn.
1. Information Preparation & Characteristic Engineering
First, I wanted information. Plenty of it. I exported historic information (Open, Excessive, Low, Shut, Tick_Volume) for my goal image. The important thing is not simply the information, however the way you body the issue. I am not attempting to foretell the *precise* subsequent value; I am attempting to foretell a easy binary consequence: “Will the following bar’s Shut be greater or decrease than the present bar’s Shut?”
I structured this as a “windowed” dataset. The mannequin would take a look at a sequence of 60 bars ( WINDOW_SIZE = 60 ) to foretell the end result of the 61st bar.
2. Normalization (The Essential Step)
Neural networks don’t love uncooked value information. A value of 2300.00 is only a “massive quantity” and might trigger the mannequin’s math to blow up. We should normalize all our options, normally to a spread between 0 and 1. I used a regular `MinMaxScaler`.
That is important: you will need to save the *precise* parameters (min, max, scale) used to normalize the coaching information. We are going to want them inside MQL5 to organize reside market information for the mannequin.
<!– PSEUDO-CODE: Saving the Scaler (Python) –> scaler = MinMaxScaler(feature_range=(0, 1)) X_train_scaled = scaler.fit_transform(X_train) # — Save the scaler parameters — # That is the “secret key” for our EA save_scaler_to_file(scaler, “my_scaler.pkl”)
3. Mannequin Coaching (Python/TensorFlow)
I used a easy however highly effective LSTM (Lengthy Brief-Time period Reminiscence) community. LSTMs are nice at understanding sequences, which is ideal for time-series information like charts.
<!– PSEUDO-CODE: Mannequin Coaching (Python) –> # ‘y_train’ is 1 if next_close > shut, else 0 mannequin = Sequential([ LSTM(units=50, input_shape=(60, 5)), # 60 bars, 5 features Dropout(0.2), Dense(units=1, activation=’sigmoid’) # Final output: 0.0 to 1.0 ]) mannequin.compile(optimizer=”adam”, loss=”binary_crossentropy”) mannequin.match(X_train_scaled, y_train, epochs=30) # Save the educated mannequin mannequin.save(“My_Gold_Model.h5”)
The `sigmoid` activation is vital. It means the mannequin’s output is not simply “BUY” or “SELL,” however a likelihood from 0.0 (100% likelihood of DOWN) to 1.0 (100% likelihood of UP). A price of 0.5 is impartial.
4. Conversion to ONNX
MetaTrader 5 cannot run TensorFlow fashions immediately. It runs fashions within the ONNX (Open Neural Community Alternate) format. This can be a easy conversion step utilizing a Python library.
<!– PSEUDO-CODE: Conversion (Shell) –> # This one-liner converts our Keras mannequin to ONNX !python -m tf2onnx.convert –keras My_Gold_Model.h5 –output My_Gold_Model.onnx
Now I’ve two important information: My_Gold_Model.onnx and the scaler parameters (which I exported to a easy CSV file).
Half 2: The MQL5 Integration – Constructing the Hybrid EA
That is the place the magic occurs. We deliver our educated mannequin into MQL5.
1. Loading the Mannequin and Scaler
I embed each the `.onnx` file and the scaler information immediately into the EA’s code utilizing #useful resource . In OnInit() , the EA hundreds the mannequin into reminiscence and parses the scaler values into a world array.
<!-- PSEUDO-CODE: MQL5 OnInit() -->
#useful resource "InformationMLModelsMy_Gold_Model.onnx" as const uchar Model_M5[]
#embody <Commerce/Commerce.mqh>
lengthy g_modelHandle = INVALID_HANDLE;
double g_scalerMin[5]; // open, excessive, low, shut, quantity
double g_scalerScale[5];
int OnInit()
{
// ... load scaler values from useful resource into g_scalerMin/g_scalerScale ...
// Load the ONNX mannequin from the useful resource buffer
g_modelHandle = OnnxCreateFromBuffer(Model_M5, ONNX_DEFAULT);
if(g_modelHandle == INVALID_HANDLE)
{
Print("Did not load ONNX mannequin!");
return(INIT_FAILED);
}
return(INIT_SUCCEEDED);
}
2. The Prediction Loop (OnTick)
On each tick (or new bar), the EA does the *very same course of* as our Python script:
- Will get the final 60 bars of information.
- Normalizes this information utilizing our saved g_scalerMin and g_scalerScale values.
- Passes the 60×5 normalized matrix to the ONNX mannequin.
- Will get a single float worth again (our likelihood).
<!– PSEUDO-CODE: MQL5 OnTick() Prediction –> void OnTick() { // 1. Get final 60 bars MqlRates charges[]; CopyRates(_Symbol, _Period, 0, 60, charges); // 2. Normalize information matrixf input_data(60, 5); for(int i=0; i<60; i++) { input_data[i][0] = (float)((charges[i].open – g_scalerMin[0]) * g_scalerScale[0]); input_data[i][1] = (float)((charges[i].excessive – g_scalerMin[1]) * g_scalerScale[1]); // … and so forth for low, shut, quantity … } // 3. Run prediction vectorf output_data(1); if(!OnnxRun(g_modelHandle, 0, input_data, output_data)) { Print(“OnnxRun failed!”); return; } // 4. Interpret end result double probability_of_up = output_data[0]; // Now… what to do with this? ProcessSignal(probability_of_up); }
Half 3: The “Secret Sauce” – My Confluence Filter
That is what separates a “toy” from an expert software. I do not commerce if probability_of_up > 0.5 . That is a rookie mistake.
As a substitute, I exploit the mannequin’s output as my main sign, which should then be confirmed by my confluence filter. This filter, impressed by my different EAs, is designed to reply one query: “Is that this a protected and logical time to commerce?”
Earlier than my new EA locations any commerce, it checks all of this:
- Unfold Verify: Is the present unfold beneath my InpMaxSpreadPips ? If not, no commerce.
- Threshold Verify: Is the likelihood sign robust sufficient? (e.g., > 0.55 or < 0.45, based mostly on InpMinPredictionDiff ).
- Multi-Timeframe EMA: Does the ML sign align with the EMA pattern on the present, earlier, AND subsequent timeframes?
- RSI Affirmation: Is RSI above 55 for a purchase or beneath 45 for a promote?
- MACD Affirmation: Is the MACD line on the proper aspect of the sign line?
- Volatility Filter: Is the market transferring? We test if ATR is inside a minimal and most pip vary.
- Pattern Power: Is the ADX worth above 20, confirming a pattern is even current?
Provided that the ML sign is robust AND the market context is logical does the commerce get positioned.
Pre-Launch Announcement: Ratio X Gold ML (ONNX)
This hybrid philosophy is the core of my brand-new Knowledgeable Advisor, the Ratio X Gold ML (ONNX), which I’ve simply completed creating.
It combines all the pieces I’ve mentioned above into one highly effective, professional-grade package deal. It is not only a blind predictor; it is an clever buying and selling assistant that fuses next-generation ML predictions with time-tested technical evaluation.
The important thing options embody:
- Pre-Educated ONNX Fashions: Laborious-coded fashions for M1, M5, M15, M30, H1, and H4, so you may commerce on any of those timeframes immediately.
- Full Confluence Filter: The precise multi-timeframe filter I described (EMA, RSI, MACD, ATR, ADX, Unfold) to make sure high-quality entries.
- Full Danger Administration Suite:
- Fastened Lot or Danger-Share Autolot sizing.
- ATR-based or Fastened Pips for Cease Loss and Take Revenue.
- Every day Revenue and Loss Targets (as % of steadiness).
- Buying and selling Time Filter.
- Breakeven and multi-function Trailing Cease logic.
- A sensible Margin Verify that auto-adjusts lot dimension if margin is low.
How one can Get Early Entry
I’m doing a particular, quiet pre-release of this EA for my MQL5 neighborhood associates earlier than the official launch.
If you’re on this hybrid buying and selling method and wish to be one of many first to make use of the Ratio X Gold ML, this is what to do:
1. Add me as a Buddy on MQL5.
2. Control my MQL5 wall.
I shall be posting it on my wall first with a particular introductory value solely for many who are following me. That is my most superior EA up to now, and I am excited to share it with a critical group of merchants first.
Thanks for studying, and I hope this offers you some new concepts to your personal improvement!

























