const long ExtOutputShape[] = {1,1}; // model output shape
const long ExtInputShape [] = {1,10,4}; // model input form
#resource "Python/model.onnx" as uchar ExtModel[] // model as resource
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
int OnStart(void)
{
matrix rates;
//--- get 10 bars
if(!rates.CopyRates("EURUSD",PERIOD_H1,COPY_RATES_OHLC,2,10))
return(-1);
//--- input a set of OHLC vectors
matrix x_norm=rates.Transpose();
vector m=x_norm.Mean(0);
vector s=x_norm.Std(0);
matrix mm(10,4);
matrix ms(10,4);
//--- fill in the normalization matrices
for(int i=0; i<10; i++)
{
mm.Row(m,i);
ms.Row(s,i);
}
//--- normalize the input data
x_norm-=mm;
x_norm/=ms;
//--- 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);
}
//--- specify the shape of the output data
if(!OnnxSetOutputShape(handle,0,ExtOutputShape))
{
Print("OnnxSetOutputShape failed, error ",GetLastError());
OnnxRelease(handle);
return(-1);
}
//--- convert normalized input data to float type
matrixf x_normf;
x_normf.Assign(x_norm);
//--- get the output data of the model here, i.e. the price prediction
vectorf y_norm(1);
//--- run the model
if(!OnnxRun(handle,ONNX_DEBUG_LOGS | ONNX_NO_CONVERSION,x_normf,y_norm))
{
Print("OnnxRun failed, error ",GetLastError());
OnnxRelease(handle);
return(-1);
}
//--- print the output value of the model to the log
Print(y_norm);
//--- do the reverse transformation to get the predicted price
double y_pred=y_norm[0]*s[3]+m[3];
Print("price predicted:",y_pred);
//--- completed operation
OnnxRelease(handle);
return(0);
};
|