Error 5002 and 5004

 

Hello, I have problems when I run my code in the expert tab it gives me errors 5002 and 5004 2024.08.03 13:06:14.710 LunariaRNNPest (Step Index,H1) Verifying the directory: Common\Files\2 2024.08.03 13:06:14.711 LunariaRNNPrueba (Step Index,H1) Directory verified/created successfully: Common\Files\2 2024.08.03 13:06:14.711 LunariaRNNPreba (Step Index,H1) Trying to create the flag file in: Common\Files\2\Step Index_run_flag.txt 2024.08.03 13:06:14.713 LunariaRNNPrail (Step Index,H1) Error creating flag file for: Step Index. Error: 5004

I am creating this neural network for various symbols. I appreciate your support please

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    for(int symIdx = 0; symIdx < ArraySize(symbolsToTrade); symIdx++)
    {
        string symbolToTrade = symbolsToTrade[symIdx];
        string model_path = model_paths[symIdx];
        string scaler_path = scaler_paths[symIdx];
        string data_path = data_paths[symIdx];
        string predictions_file_path = prediction_paths[symIdx];
        string flag_path = flag_paths[symIdx];
        
        // Verificar si el directorio existe, si no, crearlo
        string directory = "Common\\Files\\2";
        Print("Verificando el directorio: ", directory);
        if (!CreateDirectoryIfNotExist(directory))
        {
            Print("Error al crear el directorio de bandera para: ", symbolToTrade);
            continue; // Si no se puede crear el directorio, saltar al siguiente símbolo
        }

        // Intentar crear el archivo de bandera
        string filename = directory + "\\" + symbolsToTrade[symIdx] + "_run_flag.txt";
        Print("Intentando crear el archivo de bandera en: ", filename);

        // Eliminar el archivo si ya existe
        if (FileIsExist(filename))
        {
            Print("El archivo ya existe. Eliminando: ", filename);
            FileDelete(filename);
        }

        int filehandle = FileOpen(filename, FILE_WRITE|FILE_TXT|FILE_COMMON);
        if (filehandle != INVALID_HANDLE)
        {
            FileClose(filehandle);
            Print("Archivo de bandera creado exitosamente para: ", symbolToTrade);
        }
        else
        {
            Print("Error al crear el archivo de bandera para: ", symbolToTrade, ". Error: ", GetLastError());
        }
    }
}
//+------------------------------------------------------------------+
Neural Networks: From Theory to Practice
Neural Networks: From Theory to Practice
  • www.mql5.com
Nowadays, every trader must have heard of neural networks and knows how cool it is to use them. The majority believes that those who can deal with neural networks are some kind of superhuman. In this article, I will try to explain to you the neural network architecture, describe its applications and show examples of practical use.
 
        string directory = "Common\\Files\\2";
You don't need to specify the Common\Files.
 
Alain Verleyen #:
You don't need to specify the Common\Files.


Thank you very much for your correction and helping me.  I did what you told me and created the flag file. But now it tells me trying to open the predictions file. 

But it doesn't open it, I don't understand why it doesn't run it.

 
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    for(int symIdx = 0; symIdx < ArraySize(symbolsToTrade); symIdx++)
    {
        string symbolToTrade = symbolsToTrade[symIdx];
        string model_path = model_paths[symIdx];
        string scaler_path = scaler_paths[symIdx];
        string data_path = data_paths[symIdx];
        string predictions_file_path = prediction_paths[symIdx];
        string flag_path = flag_paths[symIdx];
        
        // Verificar si el directorio de banderas existe, si no, crearlo
        string flag_directory = "2";
        Print("Verificando el directorio: ", flag_directory);
        if (!CreateDirectoryIfNotExist(flag_directory))
        {
            Print("Error al crear el directorio de bandera para: ", symbolToTrade);
            continue; // Si no se puede crear el directorio, saltar al siguiente símbolo
        }

        // Intentar crear el archivo de bandera
        string flag_filename = flag_directory + "\\" + symbolsToTrade[symIdx] + "_run_flag.txt";
        Print("Intentando crear el archivo de bandera en: ", flag_filename);

        // Eliminar el archivo si ya existe
        if (FileIsExist(flag_filename))
        {
            Print("El archivo ya existe. Eliminando: ", flag_filename);
            FileDelete(flag_filename);
        }

        int flag_filehandle = FileOpen(flag_filename, FILE_WRITE|FILE_TXT|FILE_COMMON);
        if (flag_filehandle != INVALID_HANDLE)
        {
            FileClose(flag_filehandle);
            Print("Archivo de bandera creado exitosamente para: ", symbolToTrade);
        }
        else
        {
            Print("Error al crear el archivo de bandera para: ", symbolToTrade, ". Error: ", GetLastError());
            continue;
        }

        // Ajustar la ruta del directorio de predicciones
        string predictions_directory = "1";
        string predictions_file_path_corrected = predictions_directory + "\\" + symbolsToTrade[symIdx] + "_predictions.csv";
        
        // Leer las predicciones desde el archivo correspondiente
        double prediction;
        Print("Intentando abrir el archivo de predicciones: ", predictions_file_path_corrected);
        int prediction_handle = FileOpen(predictions_file_path_corrected, FILE_READ|FILE_CSV|FILE_COMMON|FILE_WRITE|FILE_TXT);
        if(prediction_handle != INVALID_HANDLE)
        {
            prediction = FileReadNumber(prediction_handle);
            FileClose(prediction_handle);

            // Lógica para decidir si comprar o vender basado en la predicción
            if(prediction > 0)
            {
                Buy(symbolToTrade); // Ejecutar operación de compra
            }
            else if(prediction < 0)
            {
                Sell(symbolToTrade); // Ejecutar operación de venta
            }
        }
        else
        {
            Print("Error al abrir el archivo de predicciones para: ", symbolToTrade, ". Ruta: ", predictions_file_path_corrected, ". Error: ", GetLastError());
        }
    }
}

// Funciones de compra y venta
void Buy(string symbol)
{
    trade.Buy(lot_size, symbol, 0, 0, 0, "Compra automatizada");
}

void Sell(string symbol)
{
    trade.Sell(lot_size, symbol, 0, 0, 0, "Venta automatizada");
}
 
Sarah Vera #:
Alain Verleyen #:
You don't need to specify the Common\Files.


Thank you very much for your correction and helping me.  I did what you told me and created the flag file. But now it tells me trying to open the predictions file. 

But it doesn't open it, I don't understand why it doesn't run it.

 

It opens it otherwise you would get an error. But it certainly read a prediction=0.

        int prediction_handle = FileOpen(predictions_file_path_corrected, FILE_READ|FILE_CSV|FILE_COMMON|FILE_WRITE|FILE_TXT);

It must be CSV or TXT but not both.

 
Alain Verleyen #:

It opens it otherwise you would get an error. But it certainly read a prediction=0.

It must be CSV or TXT but not both.

I have deleted text and it still keeps telling me that it is trying to open the predictions file


 
Sarah Vera #:

I have deleted text and it still keeps telling me that it is trying to open the predictions file


Of course it is trying to open the predictions files, and it opens it. It's what your code is "saying" to do.

Your problem is unclear.

 
Alain Verleyen #:

Of course it is trying to open the predictions files, and it opens it. It's what your code is "saying" to do.

Your problem is unclear.

I made some changes to my code with the suggestion you made for the flag file and everything was perfect. But now it shows me error 5004 when trying to run the predictions file, with the previous code it says that my predictions are 0 which I don't understand because it would be equal to 0 if the predictions file has all its values ​​correct

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    for(int symIdx = 0; symIdx < ArraySize(symbolsToTrade); symIdx++)
    {
        string symbolToTrade = symbolsToTrade[symIdx];
        string predictions_file_path = "1" + symbolsToTrade[symIdx] + "_predictions.csv";
        string flag_filename = "2" + symbolsToTrade[symIdx] + "_run_flag.txt";
        
        // Verificar si el archivo de bandera existe, si no, crearlo
        if (!FileIsExist(flag_filename))
        {
            int flag_filehandle = FileOpen(flag_filename, FILE_WRITE|FILE_TXT|FILE_COMMON);
            if (flag_filehandle != INVALID_HANDLE)
            {
                FileClose(flag_filehandle);
                Print("Archivo de bandera creado exitosamente para: ", symbolToTrade);
            }
            else
            {
                Print("Error al crear el archivo de bandera para: ", symbolToTrade, ". Error: ", GetLastError());
                continue;
            }
        }

        // Leer las predicciones desde el archivo correspondiente
        double prediction = 0.0;
        Print("Intentando abrir el archivo de predicciones: ", predictions_file_path);
        int prediction_handle = FileOpen(predictions_file_path, FILE_READ|FILE_CSV|FILE_COMMON);
        if(prediction_handle != INVALID_HANDLE)
        {
            // Leer la primera línea del archivo y obtener el valor de predicción
            if(!FileIsEnding(prediction_handle))
            {
                string line = FileReadString(prediction_handle);
                prediction = StringToDouble(line);
                Print("Predicción leída: ", prediction);
            }
            FileClose(prediction_handle);

            // Lógica para decidir si comprar o vender basado en la predicción
            if(prediction > 0)
            {
                Buy(symbolToTrade); // Ejecutar operación de compra
            }
            else if(prediction < 0)
            {
                Sell(symbolToTrade); // Ejecutar operación de venta
            }
        }
        else
        {
            Print("Error al abrir el archivo de predicciones para: ", symbolToTrade, ". Ruta: ", predictions_file_path, ". Error: ", GetLastError());
        }
    }
}

// Funciones de compra y venta
void Buy(string symbol)
{
    trade.Buy(lot_size, symbol, 0, 0, 0, "Compra automatizada");
}

void Sell(string symbol)
{
    trade.Sell(lot_size, symbol, 0, 0, 0, "Venta automatizada");
}
 
Sarah Vera #:

I made some changes to my code with the suggestion you made for the flag file and everything was perfect. But now it shows me error 5004 when trying to run the predictions file, with the previous code it says that my predictions are 0 which I don't understand because it would be equal to 0 if the predictions file has all its values ​​correct

You are now missing the "\\" in the file path.

I have no idea what is the content of your prediction file, so I can't say if getting a value of 0 is normal or not.

 
Alain Verleyen #:

You are now missing the "\\" in the file path.

I have no idea what is the content of your prediction file, so I can't say if getting a value of 0 is normal or not

This is the document I am trying to open to run predictions for this asset. It is not normal for the prediction to be 0. The expert advisor should execute the purchases and sales of the asset

 
Sarah Vera #:

This is the document I am trying to open to run predictions for this asset. It is not normal for the prediction to be 0. The expert advisor should execute the purchases and sales of the asset

// Leer la primera línea del archivo y obtener el valor de predicción

The first line of your file is :

Open,High,Low,Close,Volume

 
Alain Verleyen #:

The first line of your file is :

Hi, it's me again. I modified the code but in the expert tab it tells me the following: 2024.08.06 18:50:55.354 LunariaRNNPrueba (Step Index,H1) Trying to open the predictions file: 1\Step Index_predictions.csv, I put in the code so that it does not read the header of the first row, but it does not make the prediction. If you can guide me on what I am doing wrong, it is my first neural network. I appreciate your support

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    string base_predictions_directory = "1";
    string flag_directory = "2";
    
    for(int symIdx = 0; symIdx < ArraySize(symbolsToTrade); symIdx++)
    {
        string symbolToTrade = symbolsToTrade[symIdx];
        string model_path = model_paths[symIdx];
        string scaler_path = scaler_paths[symIdx];
        string data_path = data_paths[symIdx];
        string predictions_file_path = base_predictions_directory + "\\" + symbolToTrade + "_predictions.csv";
        string flag_path = flag_directory + "\\" + symbolToTrade + "_run_flag.txt";
        
        // Crear el directorio de bandera si no existe
        if (!CreateDirectoryIfNotExist(flag_directory))
        {
            Print("Error al crear el directorio de bandera para: ", symbolToTrade);
            continue;
        }
        
        // Intentar crear el archivo de bandera
        if (!FileIsExist(flag_path))
        {
            int flag_handle = FileOpen(flag_path, FILE_WRITE|FILE_TXT|FILE_COMMON);
            if(flag_handle != INVALID_HANDLE)
            {
                FileClose(flag_handle);
                Print("Archivo de bandera creado exitosamente para: ", symbolToTrade);
            }
            else
            {
                Print("Error al crear el archivo de bandera para: ", symbolToTrade, ". Error: ", GetLastError());
                continue;
            }
        }

        // Verificar si el archivo de predicciones existe antes de abrirlo
        Print("Verificando existencia del archivo de predicciones: ", predictions_file_path);
        if (!FileIsExist(predictions_file_path))
        {
            Print("El archivo de predicciones no existe para: ", symbolToTrade, ". Ruta: ", predictions_file_path);
            continue;
        }

        // Leer las predicciones desde el archivo correspondiente
        double prediction = 0.0;
        Print("Intentando abrir el archivo de predicciones: ", predictions_file_path);
        int prediction_handle = FileOpen(predictions_file_path, FILE_READ|FILE_CSV|FILE_COMMON);
        if(prediction_handle != INVALID_HANDLE)
        {
            FileSeek(prediction_handle, 0, SEEK_SET); // Asegurarse de que el archivo se lea desde el principio
            string header = FileReadString(prediction_handle); // Leer y descartar la cabecera
            if (FileIsEnding(prediction_handle) == false)
            {
                string line = FileReadString(prediction_handle);
                if (StringLen(line) > 0)
                {
                    string values[];
                    StringSplit(line, ',', values);
                    if (ArraySize(values) > 1)
                    {
                        prediction = StringToDouble(values[1]);
                        Print("Predicción obtenida: ", prediction);
                    }
                }
            }
            FileClose(prediction_handle);
        }
        else
        {
            Print("Error al abrir el archivo de predicciones para: ", symbolToTrade, ". Ruta: ", predictions_file_path, ". Error: ", GetLastError());
            continue;
        }
        // Lógica para decidir si comprar o vender basado en la predicción
        if(prediction > 0)
        {
            Print("Ejecutando operación de compra para: ", symbolToTrade);
            Buy(symbolToTrade); // Ejecutar operación de compra
        }
        else if(prediction < 0)
        {
            Print("Ejecutando operación de venta para: ", symbolToTrade);
            Sell(symbolToTrade); // Ejecutar operación de venta
        }
    }
}


void Buy(string symbol)
{
    double price = SymbolInfoDouble(symbol, SYMBOL_ASK);
    double sl = price - (sl_pips * _Point);
    double tp = price + (tp_pips * _Point);
    trade.Buy(lot_size, symbol, price, sl, tp);
}

void Sell(string symbol)
{
    double price = SymbolInfoDouble(symbol, SYMBOL_BID);
    double sl = price + (sl_pips * _Point);
    double tp = price - (tp_pips * _Point);
    trade.Sell(lot_size, symbol, price, sl, tp);
}