//+------------------------------------------------------------------+//| ONNX.eurusd.H1.120.Prediction.mq5 |//| Copyright 2023, MetaQuotes Ltd. |//| https://www.mql5.com |//+------------------------------------------------------------------+#property copyright"Copyright 2023, MetaQuotes Ltd."#property link"https://www.mql5.com"#property version"1.08"#include <Trade\Trade.mqh>
inputdouble InpLots = 1.0; // Lots amount to open positioninputbool InpUseStops = true; // Use stops in tradinginputint InpTakeProfit = 500; // TakeProfit levelinputint InpStopLoss = 500; // StopLoss level#resource "/Files/DL ONNX/GRU/model.eurusd.H1.120.onnx"asuchar ExtModel[]
#define SAMPLE_SIZE 130long ExtHandle=INVALID_HANDLE;
int ExtPredictedClass=-1;
datetime ExtNextBar=0;
datetime ExtNextDay=0;
float ExtMin=0.0;
float ExtMax=0.0;
CTrade ExtTrade;
//--- price movement prediction#define PRICE_UP 0#define PRICE_SAME 1#define PRICE_DOWN 2//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
if(_Symbol!="EURUSD" || _Period!=PERIOD_H1)
{
Print("model must work with EURUSD,H1");
return(INIT_FAILED);
}
//--- create a model from static buffer
ExtHandle=OnnxCreateFromBuffer(ExtModel,ONNX_DEFAULT);
if(ExtHandle==INVALID_HANDLE)
{
Print("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 (only Close)constlong input_shape[] = {-1,SAMPLE_SIZE,1};
if(!OnnxSetInputShape(ExtHandle,ONNX_DEFAULT,input_shape))
{
Print("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 - series size, must match your desired size (e.g., -1 for variable size)//--- third index - number of series, must match the number of predicted pricesconstlong output_shape[] = {-1,-1,1};
if(!OnnxSetOutputShape(ExtHandle,0,output_shape))
{
Print("OnnxSetOutputShape error ",GetLastError());
return(INIT_FAILED);
}
//---return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+voidOnDeinit(constint reason)
{
if(ExtHandle!=INVALID_HANDLE)
{
OnnxRelease(ExtHandle);
ExtHandle=INVALID_HANDLE;
}
}
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+voidOnTick()
{
//--- check new dayif(TimeCurrent()>=ExtNextDay)
{
GetMinMax();
//--- set next day time
ExtNextDay=TimeCurrent();
ExtNextDay-=ExtNextDay%PeriodSeconds(PERIOD_D1);
ExtNextDay+=PeriodSeconds(PERIOD_D1);
}
//--- check new barif(TimeCurrent()<ExtNextBar)
return;
//--- set next bar time
ExtNextBar=TimeCurrent();
ExtNextBar-=ExtNextBar%PeriodSeconds();
ExtNextBar+=PeriodSeconds();
//--- check min and maxfloat close=(float)iClose(_Symbol,_Period,0);
if(ExtMin>close)
ExtMin=close;
if(ExtMax<close)
ExtMax=close;
//--- predict next price
PredictPrice();
//--- check trading according to predictionif(ExtPredictedClass>=0)
if(PositionSelect(_Symbol))
CheckForClose();
else
CheckForOpen();
}
//+------------------------------------------------------------------+//| Check for open position conditions |//+------------------------------------------------------------------+void CheckForOpen(void)
{
ENUM_ORDER_TYPE signal=WRONG_VALUE;
//--- check signalsif(ExtPredictedClass==PRICE_DOWN)
signal=ORDER_TYPE_SELL; // sell conditionelse
{
if(ExtPredictedClass==PRICE_UP)
signal=ORDER_TYPE_BUY; // buy condition
}
//--- open position if possible according to signalif(signal!=WRONG_VALUE && TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
{
double price,sl=0,tp=0;
double bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);
double ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
if(signal==ORDER_TYPE_SELL)
{
price=bid;
if(InpUseStops)
{
sl=NormalizeDouble(bid+InpStopLoss*_Point,_Digits);
tp=NormalizeDouble(ask-InpTakeProfit*_Point,_Digits);
}
}
else
{
price=ask;
if(InpUseStops)
{
sl=NormalizeDouble(ask-InpStopLoss*_Point,_Digits);
tp=NormalizeDouble(bid+InpTakeProfit*_Point,_Digits);
}
}
ExtTrade.PositionOpen(_Symbol,signal,InpLots,price,sl,tp);
}
}
//+------------------------------------------------------------------+//| Check for close position conditions |//+------------------------------------------------------------------+void CheckForClose(void)
{
//--- position should be closed by stopsif(InpUseStops)
return;
bool bsignal=false;
//--- position already selected beforelong type=PositionGetInteger(POSITION_TYPE);
//--- check signalsif(type==POSITION_TYPE_BUY && ExtPredictedClass==PRICE_DOWN)
bsignal=true;
if(type==POSITION_TYPE_SELL && ExtPredictedClass==PRICE_UP)
bsignal=true;
//--- close position if possibleif(bsignal && TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
{
ExtTrade.PositionClose(_Symbol,3);
//--- open opposite
CheckForOpen();
}
}
//+------------------------------------------------------------------+//| Get minimal and maximal Close for last 120 days |//+------------------------------------------------------------------+void GetMinMax(void)
{
vectorf close;
close.CopyRates(_Symbol,PERIOD_D1,COPY_RATES_CLOSE,0,SAMPLE_SIZE);
ExtMin=close.Min();
ExtMax=close.Max();
}
//+------------------------------------------------------------------+//| Predict next price |//+------------------------------------------------------------------+void PredictPrice(void)
{
staticvectorf output_data(1); // vector to get resultstaticvectorf x_norm(SAMPLE_SIZE); // vector for prices normalize//--- check for normalization possibilityif(ExtMin>=ExtMax)
{
Print("ExtMin>=ExtMax");
ExtPredictedClass=-1;
return;
}
//--- request last barsif(!x_norm.CopyRates(_Symbol,_Period,COPY_RATES_CLOSE,1,SAMPLE_SIZE))
{
Print("CopyRates ",x_norm.Size());
ExtPredictedClass=-1;
return;
}
float last_close=x_norm[SAMPLE_SIZE-1];
//--- normalize prices
x_norm-=ExtMin;
x_norm/=(ExtMax-ExtMin);
//--- run the inferenceif(!OnnxRun(ExtHandle,ONNX_NO_CONVERSION,x_norm,output_data))
{
Print("OnnxRun");
ExtPredictedClass=-1;
return;
}
//--- denormalize the price from the output valuefloat predicted=output_data[0]*(ExtMax-ExtMin)+ExtMin;
//--- classify predicted price movementfloat delta=last_close-predicted;
if(fabs(delta)<=0.00001)
ExtPredictedClass=PRICE_SAME;
else
{
if(delta<0)
ExtPredictedClass=PRICE_UP;
else
ExtPredictedClass=PRICE_DOWN;
}
}
//+------------------------------------------------------------------+
Why do I get that error? what does it mean? how do I fix this?
Hi, I'm desperate to finish this.
I have an onnx model
and can't make this EA work
Why do I get that error? what does it mean? how do I fix this?
Please help