Русский 中文 Español Deutsch 日本語 Português 한국어 Français Italiano Türkçe
preview
An example of how to ensemble ONNX models in MQL5

An example of how to ensemble ONNX models in MQL5

MetaTrader 5Machine learning | 12 April 2023, 10:48
4 932 5
MetaQuotes
MetaQuotes

Introduction

For stable trading, it is usually recommended to diversify both the traded instruments and the trading strategies. The same refers to machine learning models: it is easier to create several simpler models that one complex one. But it can be difficult to assemble these models into one ONNX model.

However, it is possible to combine several trained ONNX models in one MQL5 program. In this article, we will consider one of the ensembles called the voting classifier. We will show you how easy it is to implement such an ensemble.


Models for the project

For our example, we will use two simple models: a regression price prediction model and a classification price movement prediction model. The main difference between the models is that regression predicts the quantity, while classification predicts the class.

The first model is regression.

It is trained using EURUSD D1 data from 2003 to the end of 2022. Training is performed using series of 10 OHLC prices. To improve the model trainability, we normalize the prices and divide the average price in the series by the standard deviation in the series. Thus, we put a series into a certain range with a mean of 0 and a spread of 1, which improves convergence during training.

As a result, the model should predict the closing price for the next day.

The model is very simple. It is provided here for demonstration purposes only.

# Copyright 2023, MetaQuotes Ltd.
# https://www.mql5.com

from datetime import datetime
import MetaTrader5 as mt5
import tensorflow as tf
import numpy as np
import pandas as pd
import tf2onnx
from sklearn.model_selection import train_test_split
from tqdm import tqdm
from sys import argv

if not mt5.initialize():
    print("initialize() failed, error code =",mt5.last_error())
    quit()

# we will save generated onnx-file near the our script
data_path=argv[0]
last_index=data_path.rfind("\\")+1
data_path=data_path[0:last_index]
print("data path to save onnx model",data_path)

# input parameters
inp_model_name = "model.eurusd.D1.10.onnx"
inp_history_size = 10
inp_start_date = datetime(2003, 1, 1, 0)
inp_end_date = datetime(2023, 1, 1, 0)

# get data from client terminal
eurusd_rates = mt5.copy_rates_range("EURUSD", mt5.TIMEFRAME_D1, inp_start_date, inp_end_date)
df = pd.DataFrame(eurusd_rates)


#
# collect dataset subroutine
#
def collect_dataset(df: pd.DataFrame, history_size: int):
    """
    Collect dataset for the following regression problem:
    - input: history_size consecutive H1 bars;
    - output: close price for the next bar.

    :param df: D1 bars for a range of dates
    :param history_size: how many bars should be considered for making a prediction
    :return: features and labels
    """
    n = len(df)
    xs = []
    ys = []
    for i in tqdm(range(n - history_size)):
        w = df.iloc[i: i + history_size + 1]

        x = w[['open', 'high', 'low', 'close']].iloc[:-1].values
        y = w.iloc[-1]['close']
        xs.append(x)
        ys.append(y)

    X = np.array(xs)
    y = np.array(ys)
    return X, y
###


# get prices
X, y = collect_dataset(df, history_size=inp_history_size)

# normalize prices
m = X.mean(axis=1, keepdims=True)
s = X.std(axis=1, keepdims=True)
X_norm = (X - m) / s
y_norm = (y - m[:, 0, 3]) / s[:, 0, 3]

# split data to train and test sets
X_train, X_test, y_train, y_test = train_test_split(X_norm, y_norm, test_size=0.2, random_state=0)

# define model
model = tf.keras.Sequential([
    tf.keras.layers.LSTM(64, input_shape=(inp_history_size, 4)),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Dropout(0.1),
    tf.keras.layers.Dense(32, activation='relu'),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Dropout(0.1),
    tf.keras.layers.Dense(32, activation='relu'),
    tf.keras.layers.Dense(1)
])

model.compile(optimizer='adam', loss='mse', metrics=['mae'])

# model training for 50 epochs
lr_reduction = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, min_lr=0.000001)
history = model.fit(X_train, y_train, epochs=50, verbose=2, validation_split=0.15, callbacks=[lr_reduction])

# model evaluation
test_loss, test_mae = model.evaluate(X_test, y_test)
print(f"test_loss={test_loss:.3f}")
print(f"test_mae={test_mae:.3f}")

# save model to onnx
output_path = data_path+inp_model_name
onnx_model = tf2onnx.convert.from_keras(model, output_path=output_path)
print(f"saved model to {output_path}")

# finish
mt5.shutdown()
It is assumed that our regression model is executed, the resulting predicted price should be transformed into the following class: price goes down, price does not change, price goes up. This is required in order to organize the voting classifier.

The second model is the classification one.

It is trained on EURUSD D1 from 2010 to the end of 2022. Training is performed using series of 63 Close prices. One of three classes must be defined at the output: the price will go down, the price will stay within 10 points, or the price will go up. It is because of the second class that we had to train the model using data since 2010 — prior to that, in 2009, the markets switched from 4-digit to 5-digit accuracy. Thus, one old point became ten new points.

As in the previous model, the price is normalized. Normalization is the same: we divide the deviation from the average price in the series by the standard deviation in the series. The idea of this model was described in the article "Financial timeseries forecasting with MLP in Keras" (in Russian). This model is also designed for demonstration purposes only.

# Copyright 2023, MetaQuotes Ltd.
# https://www.mql5.com
#
# Classification model
# 0,0,1 - predict price down
# 0,1,0 - predict price same
# 1,0,0 - predict price up
#

from datetime import datetime
import MetaTrader5 as mt5
import tensorflow as tf
import numpy as np
import pandas as pd
import tf2onnx
from sklearn.model_selection import train_test_split
from tqdm import tqdm
from keras.models import Sequential
from keras.layers import Dense, Activation,Dropout, BatchNormalization, LeakyReLU
from keras.optimizers import SGD
from keras import regularizers
from sys import argv

# initialize MetaTrader 5 client terminal
if not mt5.initialize():
    print("initialize() failed, error code =",mt5.last_error())
    quit()

# we will save the generated onnx-file near the our script
data_path=argv[0]
last_index=data_path.rfind("\\")+1
data_path=data_path[0:last_index]
print("data path to save onnx model",data_path)

# input parameters
inp_model_name = "model.eurusd.D1.63.onnx"
inp_history_size = 63
inp_start_date = datetime(2010, 1, 1, 0)
inp_end_date = datetime(2023, 1, 1, 0)

# get data from the client terminal
eurusd_rates = mt5.copy_rates_range("EURUSD", mt5.TIMEFRAME_D1, inp_start_date, inp_end_date)
df = pd.DataFrame(eurusd_rates)


#
# collect dataset subroutine
#
def collect_dataset(df: pd.DataFrame, history_size: int):
    """
    Collect dataset for the following regression problem:
    - input: history_size consecutive H1 bars;
    - output: close price for the next bar.

    :param df: H1 bars for a range of dates
    :param history_size: how many bars should be considered for making a prediction
    :return: features and labels
    """
    n = len(df)
    xs = []
    ys = []
    for i in tqdm(range(n - history_size)):
        w = df.iloc[i: i + history_size + 1]
        x = w[['close']].iloc[:-1].values

        delta = x[-1] - w.iloc[-1]['close']
        if np.abs(delta)<=0.0001:
           y = 0, 1, 0
        else:
           if delta<0:
              y = 1, 0, 0
           else:
              y = 0, 0, 1

        xs.append(x)
        ys.append(y)

    X = np.array(xs)
    Y = np.array(ys)
    return X, Y
###


# get prices
X, Y = collect_dataset(df, history_size=inp_history_size)

# normalize prices
m = X.mean(axis=1, keepdims=True)
s = X.std(axis=1, keepdims=True)
X_norm = (X - m) / s

# split data to train and test sets
X_train, X_test, Y_train, Y_test = train_test_split(X_norm, Y, test_size=0.1, random_state=0)

# define model
model = Sequential()
model.add(Dense(64, input_dim=inp_history_size, activity_regularizer=regularizers.l2(0.01)))
model.add(BatchNormalization())
model.add(LeakyReLU())
model.add(Dropout(0.3))
model.add(Dense(16, activity_regularizer=regularizers.l2(0.01)))
model.add(BatchNormalization())
model.add(LeakyReLU())
model.add(Dense(3))
model.add(Activation('softmax'))

opt = SGD(learning_rate=0.01, momentum=0.9)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])

# model training for 300 epochs
lr_reduction = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.9, patience=5, min_lr=0.00001)
history = model.fit(X_train, Y_train, epochs=300, validation_data=(X_test, Y_test), shuffle = True, batch_size=128, verbose=2, callbacks=[lr_reduction])

# model evaluation
test_loss, test_accuracy = model.evaluate(X_test, Y_test)
print(f"test_loss={test_loss:.3f}")
print(f"test_accuracy={test_accuracy:.3f}")

# save model to onnx
output_path = data_path+inp_model_name
onnx_model = tf2onnx.convert.from_keras(model, output_path=output_path)
print(f"saved model to {output_path}")

# finish
mt5.shutdown()
The models were trained with data until the end of 2022, thus leaving the period to demonstrate their operation in the strategy tester.


An Ensemble of ONNX Models in the MQL5 Expert Advisor

Below is a simple Expert Advisor to demonstrate the possibilities of model ensembles. The main principles of using ONNX models in MQL5 were described in the second part of the previous article.

Forward declarations and definitions

#include <Trade\Trade.mqh>

input double InpLots = 1.0;              // Lots amount to open position

#resource "Python/model.eurusd.D1.10.onnx" as uchar ExtModel1[]
#resource "Python/model.eurusd.D1.63.onnx" as uchar ExtModel2[]

#define SAMPLE_SIZE1 10
#define SAMPLE_SIZE2 63

long     ExtHandle1=INVALID_HANDLE;
long     ExtHandle2=INVALID_HANDLE;
int      ExtPredictedClass1=-1;
int      ExtPredictedClass2=-1;
int      ExtPredictedClass=-1;
datetime ExtNextBar=0;
CTrade   ExtTrade;

//--- price movement prediction
#define PRICE_UP   0
#define PRICE_SAME 1
#define PRICE_DOWN 2

OnInit function

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   if(_Symbol!="EURUSD" || _Period!=PERIOD_D1)
     {
      Print("model must work with EURUSD,D1");
      return(INIT_FAILED);
     }

//--- create first model from static buffer
   ExtHandle1=OnnxCreateFromBuffer(ExtModel1,ONNX_DEFAULT);
   if(ExtHandle1==INVALID_HANDLE)
     {
      Print("First model OnnxCreateFromBuffer error ",GetLastError());
      return(INIT_FAILED);
     }
//--- since not all sizes defined in the input tensor we must set them explicitly
//--- first index - batch size, second index - series size, third index - number of series (OHLC)
   const long input_shape1[] = {1,SAMPLE_SIZE1,4};
   if(!OnnxSetInputShape(ExtHandle1,0,input_shape1))
     {
      Print("First model OnnxSetInputShape error ",GetLastError());
      return(INIT_FAILED);
     }
   
//--- since not all sizes defined in the output tensor we must set them explicitly
//--- first index - batch size, must match the batch size of the input tensor
//--- second index - number of predicted prices (we only predict Close)
   const long output_shape1[] = {1,1};
   if(!OnnxSetOutputShape(ExtHandle1,0,output_shape1))
     {
      Print("First model OnnxSetOutputShape error ",GetLastError());
      return(INIT_FAILED);
     }

//--- create second model from static buffer
   ExtHandle2=OnnxCreateFromBuffer(ExtModel2,ONNX_DEFAULT);
   if(ExtHandle2==INVALID_HANDLE)
     {
      Print("Second model OnnxCreateFromBuffer error ",GetLastError());
      return(INIT_FAILED);
     }
  
//--- since not all sizes defined in the input tensor we must set them explicitly
//--- first index - batch size, second index - series size
   const long input_shape2[] = {1,SAMPLE_SIZE2};
   if(!OnnxSetInputShape(ExtHandle2,0,input_shape2))
     {
      Print("Second model OnnxSetInputShape error ",GetLastError());
      return(INIT_FAILED);
     }

//--- since not all sizes defined in the output tensor we must set them explicitly
//--- first index - batch size, must match the batch size of the input tensor
//--- second index - number of classes (up, same or down)
   const long output_shape2[] = {1,3};
   if(!OnnxSetOutputShape(ExtHandle2,0,output_shape2))
     {
      Print("Second model OnnxSetOutputShape error ",GetLastError());
      return(INIT_FAILED);
     }
//--- ok
   return(INIT_SUCCEEDED);
  }

We will only run it with EURUSD, D1. This is because we use the data of the current symbol-period, while the models are trained using daily prices.

The models are included in the Expert Advisor as resources.

It is important to explicitly define the input and output data shapes.

OnTick function

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- check new bar
   if(TimeCurrent()<ExtNextBar)
      return;
//--- set next bar time
   ExtNextBar=TimeCurrent();
   ExtNextBar-=ExtNextBar%PeriodSeconds();
   ExtNextBar+=PeriodSeconds();

//--- predict price movement
   Predict();
//--- check trading according to prediction
   if(ExtPredictedClass>=0)
      if(PositionSelect(_Symbol))
         CheckForClose();
      else
         CheckForOpen();
  }

All trading operations are only performed at the beginning of the day.

Prediction function

//+------------------------------------------------------------------+
//| Voting classification                                            |
//+------------------------------------------------------------------+
void Predict(void)
  {
//--- evaluate first model
   ExtPredictedClass1=PredictPrice(ExtHandle1,SAMPLE_SIZE1);
//--- evaluate second model
   ExtPredictedClass2=PredictPriceMovement(ExtHandle2,SAMPLE_SIZE2);
//--- vote
   if(ExtPredictedClass1==ExtPredictedClass2)
      ExtPredictedClass=ExtPredictedClass1;
   else
      ExtPredictedClass=-1;
  }

A class is considered selected when both models have received the same class. This is a majority vote. And since there are only two models in the ensemble, voting by the majority means "unanimous".

Day Close price prediction from 10 previous OHLC prices

//+------------------------------------------------------------------+
//| Predict next price (first model)                                 |
//+------------------------------------------------------------------+
int PredictPrice(const long handle,const int sample_size)
  {
   static matrixf input_data(sample_size,4);    // matrix for prepared input data
   static vectorf output_data(1);               // vector to get result
   static matrix  mm(sample_size,4);            // matrix of horizontal vectors Mean
   static matrix  ms(sample_size,4);            // matrix of horizontal vectors Std
   static matrix  x_norm(sample_size,4);        // matrix for prices normalize

//--- prepare input data
   matrix rates;
//--- request last bars
   if(!rates.CopyRates(_Symbol,_Period,COPY_RATES_OHLC,1,sample_size))
      return(-1);
//--- get series Mean
   vector m=rates.Mean(1);
//--- get series Std
   vector s=rates.Std(1);
//--- prepare matrices for prices normalization
   for(int i=0; i<sample_size; i++)
     {
      mm.Row(m,i);
      ms.Row(s,i);
     }
//--- the input of the model must be a set of vertical OHLC vectors
   x_norm=rates.Transpose();
//--- normalize prices
   x_norm-=mm;
   x_norm/=ms;

//--- run the inference
   input_data.Assign(x_norm);
   if(!OnnxRun(handle,ONNX_NO_CONVERSION,input_data,output_data))
      return(-1);
//--- denormalize the price from the output value
   double predicted=output_data[0]*s[3]+m[3];
//--- classify predicted price movement
   int    predicted_class=-1;
   double delta=rates[3][sample_size-1]-predicted;
   if(fabs(delta)<=0.0001)
      predicted_class=PRICE_SAME;
   else
     {
      if(delta<0)
         predicted_class=PRICE_UP;
      else
         predicted_class=PRICE_DOWN;

     }

   return(predicted_class);
  }

The input data should be prepared following the same rules as when training the model. After the model is executed, the resulting value is converted back into the price. The class is calculated based on the difference between the last Close price in the series and the resulting price.

The price movement prediction based on a series of 63 daily Close prices:

//+------------------------------------------------------------------+
//| Predict price movement (second model)                            |
//+------------------------------------------------------------------+
int PredictPriceMovement(const long handle,const int sample_size)
  {
   static vectorf input_data(sample_size);    // vector for prepared input data
   static vectorf output_data(3);             // vector to get result

//--- request last bars
   if(!input_data.CopyRates(_Symbol,_Period,COPY_RATES_CLOSE,1,sample_size))
      return(-1);
//--- get series Mean
   float m=input_data.Mean();
//--- get series Std
   float s=input_data.Std();
//--- normalize prices
   input_data-=m;
   input_data/=s;

//--- run the inference
   if(!OnnxRun(handle,ONNX_NO_CONVERSION,input_data,output_data))
      return(-1);
//--- evaluate prediction
   return(int(output_data.ArgMax()));
  }

Prices are normalized using the same rules as in the first model. However, this time the code is more compact because the input is a vector, not a matrix. The class is selected by the maximum value of the three probabilities.

The trading strategy is simple. Trading operations are performed at the beginning of each day. If the prediction is "the price will go up", then we buy; if it is "the price will go down", we sell.

//+------------------------------------------------------------------+
//| Check for open position conditions                               |
//+------------------------------------------------------------------+
void CheckForOpen(void)
  {
   ENUM_ORDER_TYPE signal=WRONG_VALUE;
//--- check signals
   if(ExtPredictedClass==PRICE_DOWN)
      signal=ORDER_TYPE_SELL;    // sell condition
   else
     {
      if(ExtPredictedClass==PRICE_UP)
         signal=ORDER_TYPE_BUY;  // buy condition
     }

//--- open position if possible according to signal
   if(signal!=WRONG_VALUE && TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
      ExtTrade.PositionOpen(_Symbol,signal,InpLots,
                            SymbolInfoDouble(_Symbol,signal==ORDER_TYPE_SELL ? SYMBOL_BID:SYMBOL_ASK),
                            0,0);
  }
//+------------------------------------------------------------------+
//| Check for close position conditions                              |
//+------------------------------------------------------------------+
void CheckForClose(void)
  {
   bool bsignal=false;
//--- position already selected before
   long type=PositionGetInteger(POSITION_TYPE);
//--- check signals
   if(type==POSITION_TYPE_BUY && ExtPredictedClass==PRICE_DOWN)
      bsignal=true;
   if(type==POSITION_TYPE_SELL && ExtPredictedClass==PRICE_UP)
      bsignal=true;

//--- close position if possible
   if(bsignal && TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
     {
      ExtTrade.PositionClose(_Symbol,3);
      //--- open opposite
      CheckForOpen();
     }
  }


We have trained our model with the data until the beginning of 2023. So, let us set the testing interval from the beginning of the year.

Test settings

Here is the testing result based on the data since the beginning of the year.

Expert Advisor testing results


It would be interesting to know the testing results for each individual model.

To do this, let us modify the EA source code as follows:

enum EnModels
  {
   USE_FIRST_MODEL,    // Use first model only
   USE_SECOND_MODEL,   // Use second model only
   USE_BOTH_MODELS     // Use both models
  };
input EnModels InpModels = USE_BOTH_MODELS;  // Models using
input double   InpLots   = 1.0;              // Lots amount to open position

...

//+------------------------------------------------------------------+
//| Voting classification                                            |
//+------------------------------------------------------------------+
void Predict(void)
  {
//--- evaluate first model
   if(InpModels==USE_BOTH_MODELS || InpModels==USE_FIRST_MODEL)
      ExtPredictedClass1=PredictPrice(ExtHandle1,SAMPLE_SIZE1);
//--- evaluate second model
   if(InpModels==USE_BOTH_MODELS || InpModels==USE_SECOND_MODEL)
      ExtPredictedClass2=PredictPriceMovement(ExtHandle2,SAMPLE_SIZE2);

//--- check predictions
   switch(InpModels)
     {
      case USE_FIRST_MODEL :
         ExtPredictedClass=ExtPredictedClass1;
         break;
      case USE_SECOND_MODEL :
         ExtPredictedClass=ExtPredictedClass2;
         break;
      case USE_BOTH_MODELS :
         if(ExtPredictedClass1==ExtPredictedClass2)
            ExtPredictedClass=ExtPredictedClass1;
         else
            ExtPredictedClass=-1;
     }
  }

Enable the parameter "Use first model only".

Expert Advisor settings to use only the first model

First model testing results

First model testing results


Now let us test the second model. Here are the second model testing results.

Second model testing results

The second model turned out to be much stronger than the first one. The results confirm the theory that weak models need to be ensembled. However, this article was not about the theory of ensembling, but about the practical application.

Important note: Please be advised that the models used in the article are presented only to demonstrate how to work with ONNX models using the MQL5 language. The Expert Advisor is not intended for trading on real accounts.



Conclusion

We have presented a very simple yet illustrative example of an ensemble of two ONNX models. The number of models used simultaneously is limited and cannot exceed 256 models. However, even the use of more than two models will require a different approach to Expert Advisor programming, namely, it will require object-oriented programming.

But that is the topic for another article.

Translated from Russian by MetaQuotes Ltd.
Original article: https://www.mql5.com/ru/articles/12433

Attached files |
MQL5.zip (105.08 KB)
Last comments | Go to discussion (5)
mysticsoul
mysticsoul | 12 Apr 2023 at 12:49

I set the same date and the same settings, but the results came out differently.Does anyone know why?

First model testing results


First model testing results


second model testing results


second model testing results
Slava
Slava | 12 Apr 2023 at 15:06
mysticsoul #:I set the same date and the same settings, but the results came out differently.Does anyone know why?

It may be because your trade server is not MetaQuotes-Demo

Rasoul Mojtahedzadeh
Rasoul Mojtahedzadeh | 12 Apr 2023 at 23:04
mysticsoul #:

I set the same date and the same settings, but the results came out differently.Does anyone know why?

First model testing results

second model testing results
It could be due to a difference in the models weight initialization. Unfortunately, they don't use "seeding everything" technique to make their results reproducible.
linfo2
linfo2 | 13 Apr 2023 at 20:33

Firstly, thank you so much for putting this together, it is nice to look in different directions. It is easy to follow and well put together.

For me I get similar success rates and slightly lower number of trades with the demo account but when I use the meta trader demo account. With my trading account It only trades once . I am assuming it is time zone for the broker my broker is in Australia (GMT+10). The first transaction from demo account is; Core 1 2023.01.02 07:02:00   deal #2 sell 1 EURUSD at 1.07016 done (based on order #2) 

 The first transaction from My Broker  Australia (GMT+10) is;  Core 1 2023.01.03 00:00:00   failed market sell 1 EURUSD [Market closed] and not exactly certain how to resolve this. Possibly the whole model is time Zone dependent. If that was the case the times should be out in whole hours? but how does the start transaction 2023.01.02 07:02:00 become 2023.01.03 00:00:00?

Would appreciate any suggestions on the cause of this.



Too Chee Ng
Too Chee Ng | 19 Nov 2023 at 19:28
Slava #:
trade serve

Same here, I manage to reproduce very similar results with the original onnx files on my MetaQuates-Demo account.

Then, I manage to re-train the Python MLs to completion; though with the following warnings/errors which can be ignored:

D:\MT5 Demo1\MQL5\Experts\article_12433\Python>python ONNX.eurusd.D1.10.Training.py
2023-11-19 18:07:38.169418: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2023-11-19 18:07:38.169664: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
data path to save onnx model
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 5187/5187 [00:00<00:00, 6068.93it/s]
2023-11-19 18:07:40.434910: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2023-11-19 18:07:40.435070: W tensorflow/stream_executor/cuda/cuda_driver.cc:263] failed call to cuInit: UNKNOWN ERROR (303)
2023-11-19 18:07:40.437138: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: WIN-SSPXX7BO0B0
2023-11-19 18:07:40.437323: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: WIN-SSPXX7BO0B0
2023-11-19 18:07:40.437676: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Epoch 1/50
111/111 - 1s - loss: 1.6160 - mae: 0.9378 - val_loss: 2.7602 - val_mae: 1.3423 - lr: 0.0010 - 1s/epoch - 12ms/step
Epoch 2/50
111/111 - 0s - loss: 1.4932 - mae: 0.8952 - val_loss: 2.4339 - val_mae: 1.2412 - lr: 0.0010 - 287ms/epoch - 3ms/step
...

both ML scripts finish with:

111/111 - 0s - loss: 1.2812 - mae: 0.8145 - val_loss: 1.2598 - val_mae: 0.8142 - lr: 1.0000e-06 - 366ms/epoch - 3ms/step
Epoch 50/50
111/111 - 0s - loss: 1.3030 - mae: 0.8203 - val_loss: 1.2604 - val_mae: 0.8143 - lr: 1.0000e-06 - 365ms/epoch - 3ms/step
33/33 [==============================] - 0s 1ms/step - loss: 1.1542 - mae: 0.7584
test_loss=1.154
test_mae=0.758
2023-11-19 18:07:57.480814: I tensorflow/core/grappler/devices.cc:66] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 0
2023-11-19 18:07:57.481315: I tensorflow/core/grappler/clusters/single_machine.cc:358] Starting new session
2023-11-19 18:07:57.560110: I tensorflow/core/grappler/devices.cc:66] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 0
2023-11-19 18:07:57.560380: I tensorflow/core/grappler/clusters/single_machine.cc:358] Starting new session
2023-11-19 18:07:57.611678: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:354] MLIR V1 optimization pass is not enabled
saved model to model.eurusd.D1.10.onnx
24/24 - 0s - loss: 0.6618 - accuracy: 0.6736 - val_loss: 0.8993 - val_accuracy: 0.4759 - lr: 4.1746e-05 - 37ms/epoch - 2ms/step
Epoch 300/300
24/24 - 0s - loss: 0.6531 - accuracy: 0.6770 - val_loss: 0.8997 - val_accuracy: 0.4789 - lr: 4.1746e-05 - 39ms/epoch - 2ms/step
11/11 [==============================] - 0s 682us/step - loss: 0.8997 - accuracy: 0.4789
test_loss=0.900
test_accuracy=0.479
2023-11-19 18:07:19.838160: I tensorflow/core/grappler/devices.cc:66] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 0
2023-11-19 18:07:19.838516: I tensorflow/core/grappler/clusters/single_machine.cc:358] Starting new session
2023-11-19 18:07:19.872285: I tensorflow/core/grappler/devices.cc:66] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 0
2023-11-19 18:07:19.872584: I tensorflow/core/grappler/clusters/single_machine.cc:358] Starting new session
saved model to model.eurusd.D1.63.onnx

Next I re-compile the original ONNX.Price.Prediction.2M.D1.mq5 to use the new MLs I have trained.

The backtest results with the same MetaQuates-Demo account were much different from the original; which don't look good.

Would really appreciate to know what has gone wrong?
Many thanks.

How to detect trends and chart patterns using MQL5 How to detect trends and chart patterns using MQL5
In this article, we will provide a method to detect price actions patterns automatically by MQL5, like trends (Uptrend, Downtrend, Sideways), Chart patterns (Double Tops, Double Bottoms).
Category Theory in MQL5 (Part 6): Monomorphic Pull-Backs and Epimorphic Push-Outs Category Theory in MQL5 (Part 6): Monomorphic Pull-Backs and Epimorphic Push-Outs
Category Theory is a diverse and expanding branch of Mathematics which is only recently getting some coverage in the MQL5 community. These series of articles look to explore and examine some of its concepts & axioms with the overall goal of establishing an open library that provides insight while also hopefully furthering the use of this remarkable field in Traders' strategy development.
Population optimization algorithms: Harmony Search (HS) Population optimization algorithms: Harmony Search (HS)
In the current article, I will study and test the most powerful optimization algorithm - harmonic search (HS) inspired by the process of finding the perfect sound harmony. So what algorithm is now the leader in our rating?
Backpropagation Neural Networks using MQL5 Matrices Backpropagation Neural Networks using MQL5 Matrices
The article describes the theory and practice of applying the backpropagation algorithm in MQL5 using matrices. It provides ready-made classes along with script, indicator and Expert Advisor examples.