//+------------------------------------------------------------------+ //| keras_model_v1.mq5 | //| Copyright 2024, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ // Define global variables // model features datetime lastBarTime = 0; #define EXPERT_MAGIC 123456 long SessionHandle = 0; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ void OnStart() { SessionHandle=OnnxCreate("testmodel.onnx",ONNX_DEBUG_LOGS); } int OnInit() { //--- return(INIT_SUCCEEDED); //--- } void OnTick() { //--- // get the time of the current bar datetime currentBarTime = iTime(_Symbol, PERIOD_H1, 0); // check if new bar started if (currentBarTime > lastBarTime) { // close recent trade code here // update the last known bar's time lastBarTime = currentBarTime; // create array then fill array with predictors double X[11]; X[0] = iVolume(_Symbol, _Period, 0); X[1] = iMA(_Symbol, PERIOD_H1, 5, 0, MODE_EMA, PRICE_CLOSE); X[2] = iMA(_Symbol, PERIOD_H1, 20, 0, MODE_EMA, PRICE_CLOSE); X[3] = iMA(_Symbol, PERIOD_H1, 50, 0, MODE_EMA, PRICE_CLOSE); X[4] = iRSI(_Symbol, PERIOD_H1, 14, PRICE_CLOSE); X[5] = iMACD(_Symbol, PERIOD_H1, 12, 26, 9,PRICE_CLOSE); X[6] = 1.0; X[7] = iOpen(_Symbol, 0, 0); X[8]= iHigh(_Symbol, 0, 0); X[9] = iLow(_Symbol, 0, 0); X[10] = iClose(_Symbol, 0, 0); StandardizeArray(X); vectorf input_data = {X[0],X[1],X[2],X[3],X[4],X[5],X[6],X[7],X[8],X[9],X[10]}; vectorf output(1); //float prediction // get model output code bool success=OnnxRun(SessionHandle,ONNX_DEBUG_LOGS,input_data,output); float prediction = output[1]; // make trade based on model output if (prediction>0.5) { //--- declare and initialize the trade request and result of trade request MqlTradeRequest request={}; MqlTradeResult result={}; //--- parameters of request request.action =TRADE_ACTION_DEAL; request.symbol =Symbol(); request.volume =0.01; request.type =ORDER_TYPE_BUY; request.price =SymbolInfoDouble(Symbol(),SYMBOL_ASK); request.deviation=5; // allowed deviation from the price request.magic =EXPERT_MAGIC; // MagicNumber of the order request.sl = request.price + 1; request.tp = request.price - 1; //--- send the request if(!OrderSend(request,result)) PrintFormat("OrderSend error %d",GetLastError()); // if unable to send the request, output the error code //--- information about the operation PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order); } else { //--- declare and initialize the trade request and result of trade request MqlTradeRequest request={}; MqlTradeResult result={}; //--- parameters of request request.action =TRADE_ACTION_DEAL; request.symbol =Symbol(); request.volume =0.01; request.type =ORDER_TYPE_SELL; request.price =SymbolInfoDouble(Symbol(),SYMBOL_BID); request.deviation=5; request.magic =EXPERT_MAGIC; request.sl = request.price - 1; request.tp = request.price + 1; //--- send the request if(!OrderSend(request,result)) PrintFormat("OrderSend error %d",GetLastError()); // if unable to send the request, output the error code //--- information about the operation PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order); } } } //+------------------------------------------------------------------+ //| Trade function | //+------------------------------------------------------------------+ void OnTrade() { //--- } //+------------------------------------------------------------------+ //| Close last trade | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Code to derive features //+------------------------------------------------------------------+ void StandardizeArray(double& array[]) { int arraySize = ArraySize(array); // Calculate the mean double mean = 0; for (int i = 0; i < arraySize; i++) { mean += array[i]; } mean /= arraySize; // Calculate the standard deviation double variance = 0; for (int i = 0; i < arraySize; i++) { variance += MathPow(array[i] - mean, 2); } double stdDev = MathSqrt(variance / arraySize); // Standardize the array for (int i = 0; i < arraySize; i++) { array[i] = (array[i] - mean) / stdDev; } }