New MetaTrader 5 build 3620: Web Terminal improvements, ONNX support and fast matrix multiplications in MQL5 - page 2

 

actually what i coufused about onnx is why it has to specify the input and output data shapes for a model.

in example i posted above,  for a sample, the model always receives 25 features and outputs a scalar value of prediction probability. So input shape is always (1,25,1 or 1,1,25 or 1,25) and output shape always (1,1).   Since features count is unchanged, why is there a OnnxSetInputShape method? Can it work normally if I set it to 24?  and in the same way , the output shape is always (1,1) ,this is not what I set, it is determined by the shape of the training set when the model is trained.

Documentation on MQL5: ONNX models / OnnxSetInputShape
Documentation on MQL5: ONNX models / OnnxSetInputShape
  • www.mql5.com
OnnxSetInputShape - ONNX models - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Mihai Baciu #:

Could you please just tell what the error code 4024 mean? The developers must know, ask them. They wrote the code, but didn't document it here: https://www.mql5.com/en/docs/constants/errorswarnings/errorcodes

It is new error code ERR_INVALID_HANDLE (4024)

 
Sergey Golubev #:
use search (the search results are here).

The search results are mostly about MT4. This thread is about MT5.

According to what I've seen from the search, 4024 in MT4 means "Internal Error". If the error code has the same meaning in MT5 as it used to have in MT4, the error code is not really informative. Some additional information would be very helpful.

As I said above, in MT5 the error code 4024 is not documented: https://www.mql5.com/en/docs/constants/errorswarnings/errorcodes

 
Ilyas #:

It is new error code ERR_INVALID_HANDLE (4024)

Do you know why the error could occur during OnnxSetInputShape? Is it possible to get some additional explanation (other than the error code returned by  GetLastError)?

 
Mihai Baciu #:

Do you know why the error could occur during OnnxSetInputShape? Is it possible to get some additional explanation (other than the error code returned by  GetLastError)?

Seems to me the error is pretty clear, not ?

//--- create the model
   long handle=OnnxCreateFromBuffer(ExtModel,ONNX_DEBUG_LOGS);
//--- specify the shape of the input data
   if(!OnnxSetInputShape(handle,0,ExtInputShape))
     {
      Print("OnnxSetInputShape failed, error ",GetLastError());
      OnnxRelease(handle);
      return(-1);
     }

An invalid handle was used in OnnxSetInputShape(). The handle was created previously with ExtModel.

From the documentation example, ExtModel is :

#resource "Python/model.onnx" as uchar ExtModel[]                   // model as resource

So what model did you use ?

 
Alain Verleyen #:

Seems to me the error is pretty clear, not ?

An invalid handle was used in OnnxSetInputShape(). The handle was created previously with ExtModel.

From the documentation example, ExtModel is :

So what model did you use ?

My code looks like this:

int neuralNetworkHandle;

const long                             ExtOutputShape[] = {1,1};
const long                             ExtInputShape [] = {1,90,4};
#resource "model.onnx" as uchar ExtModel[]

int OnInit()
  {
   neuralNetworkHandle=OnnxCreateFromBuffer(ExtModel,ONNX_DEBUG_LOGS);
   if(neuralNetworkHandle==INVALID_HANDLE)
     {
      PrintFormat("Failed to read %d", GetLastError());
      return (INIT_FAILED);
     }
   PrintFormat("Successfully read the ONNX file");
   
   if(!OnnxSetInputShape(neuralNetworkHandle,0,ExtInputShape))
     {
      Print("OnnxSetInputShape failed, error ",GetLastError());
      OnnxRelease(neuralNetworkHandle);
      return(-1);
     }
...

As you can see, I tested that the handle is not INVALID_HANDLE.

My model is just a recurrent neural network written in Tensorflow2.

Can you see anything suspicious in my code?

 

Hello guys,

Thank you so much for the ONNX support!

In the example, only OLHC data is feed to the training. I suppose Volume and other metadata could easily be added as it might be relevant for the model.


I see, however 2 issues to be worked out:


1) The size of the model that can be embedded is somewhat small. (< 200Mb?) - Is there some way to go around this limitation?

2) I would like to feed indicators data to the model. Unfortunately the Python interface doesn't provide a way to extract native MQL indicators data as part of the dump used for training. The only (dirty) workaround I found would be to write an specific EA that would dump OHLCV data + selected indicator datas to CSV, which would then be used for the training - but that kind of defeat the point of having the Python interface.

Alternatively (what I am doing so far) is relying on native Python libs for adding indicator data (like TALib and others). This has the huge drawnback of needing to have different implementations of indicators (mql and python), which can drift apart and it's a nightmare to maintain.

Would it be possible somehow to export indicators data over the Python interface?

 
Emerson Gomes #:


1) The size of the model that can be embedded is somewhat small. (< 200Mb?) - Is there some way to go around this limitation?

Answering to my own question above:

When bundling the model as a Resource, it's maximum size is 128Mb. 

However, if you use OnnxCreate to load model from File, there's no such limit.

 

I am having a bad time trying to feed different data to the model. I am currently using the ONNX.Price.Prediction shared project samples

I have been trying to add volume data, by using something like that:

if (!rates.CopyRates(model.symbol, model.period, COPY_RATES_OHLC + COPY_RATES_VOLUME_TICK , 1, model.sample_size))


However I am a bit lost on the data normalization part. Normally in Python/Tensorflow you can just rely on scikit to scale the dataset for you, and it will take care of all columns with different datatypes, but seems that in the example code, mean and std are manually extracted from the training and input data.

However, the training mean and std dataset values are never passed to the runtime input dataset, so I am afraid that this is generating wrong values during prediction, since mean and std from training and prediction will differ - and this can be specially an issue in the case of outliners.


While for OLHC you can probably share the scaling values for all 4 fields, if you have a different value range like for volume I guess you would need to scale these values again. And that would be truth for any arbitary input columns. 

Handling column by column individually seems to be very troublesome.

I guess ideally we would need to have a common scaler implementation (like the one in scikit), for MQL5 and Python, in order to abstract such tasks.


That being said I was unable to add a different the volume to the input data so far - If anyone could provide a code adaption on top of the current samples, that would be greatly appreciated!

 

Again, answering my own question :)


I ended up implementing the scaling mechanism:


1) On python, extracting the stddev and mean for each input and output columns;

2) Store these values in a csv file;

3) On the EA, read these values and apply them back for input and output data.


This critical step is missing in the current code, meaning that if you have any large price fluctuation, between Min/Max values from the training dataset and your actual values, your prediction will surely come out wrong.