Array out of range when converting MLQ5 to MQL4

 

Hello there,

I'm converting the working MT5 version of my EA to MT4, but I get an array out of range error not matter what I try.

It's my first time working in MQL4 and I have tried many things but I can't solve the issue so hopefully someone can spot it and advice because I'm running out of idea... (and yes I did check MQL4 Reference and forums already!)

Here is the code (it is working in MQL5) :

// Function to save HLINE values to a file
void SaveHLINEs()
{
    // Open a file for writing
    const string fileName = _Symbol+"HLINEs.txt";
    int fileHandle = FileOpen(fileName, FILE_WRITE|FILE_TXT|FILE_ANSI);
    if(fileHandle != INVALID_HANDLE)
    {
        for(int i = 0; i < ObjectsTotal(0, 0, OBJ_HLINE); i++)
        {
            string lineName   = ObjectName(0, i, 0, OBJ_HLINE);
            double price      = (double)ObjectGetDouble(0, lineName, OBJPROP_PRICE);
            color  lineColor  = (color)ObjectGetInteger(0, lineName, OBJPROP_COLOR);
            int    width      = (int)ObjectGetInteger  (0, lineName, OBJPROP_WIDTH);
            int    style      = (int)ObjectGetInteger  (0, lineName, OBJPROP_STYLE);
            bool   selectable = (bool)ObjectGetInteger (0, lineName, OBJPROP_SELECTABLE);
            bool   selected   = (bool)ObjectGetInteger (0, lineName, OBJPROP_SELECTED);            
            

            // Write the HLINE data to the file
            FileWriteString(fileHandle, lineName + ";" + DoubleToString(price) + ";" + IntegerToString(lineColor) + 
                                                ";" + IntegerToString(width) + ";" + IntegerToString(style) + ";" +
                                                (selectable ? "1" : "0") + ";" + (selected ? "1" : "0") + "\r\n");
        }

        // Close the file handle
        FileClose(fileHandle);
    }
}

// Function to load HLINE values from a file
void LoadHLINEs()
{
    // Construct the filename with the current symbol
    const string fileName = _Symbol + "HLINEs.txt";

    // Check if the file exists and if its name contains the current symbol
    if (FileIsExist(fileName) == true)
    {
        // Open the file for reading
        int fileHandle = FileOpen(fileName, FILE_READ);
        if (fileHandle != INVALID_HANDLE)
        {
            // Read HLINE data from the file
            while (!FileIsEnding(fileHandle))
            {
               string lineData = FileReadString(fileHandle);
               string parts[];
               char sep = ';';
               StringSplit(lineData, sep, parts);
               
               string lineName  = parts[0];
               double price     = (double)StringToDouble(parts[1]); // This line is out of range
               color lineColor  = (color)StringToInteger(parts[2]);
               int   width      = (int)StringToInteger(parts[3]);
               int   style      = (int)StringToInteger(parts[4]);
               bool  selectable = parts[5] == "1";
               bool  selected   = parts[6] == "1";
               
               // Create HLINE using the retrieved properties
               ObjectCreate(0, lineName, OBJ_HLINE, 0, 0, price);
               ObjectSetInteger(0, lineName, OBJPROP_COLOR, lineColor);
               ObjectSetInteger(0, lineName, OBJPROP_WIDTH, width);
               ObjectSetInteger(0, lineName, OBJPROP_STYLE, style);
               ObjectSetInteger(0, lineName, OBJPROP_SELECTABLE, selectable);
               ObjectSetInteger(0, lineName, OBJPROP_SELECTED, selected);
               ObjectSetInteger(0, lineName, OBJPROP_TIMEFRAMES, OBJ_ALL_PERIODS);
               ObjectSetString (0, lineName, OBJPROP_TOOLTIP, "\n");
                
            }

            // Close the file handle
            FileClose(fileHandle);
            FileDelete(fileName);
        }
    }
}


And here is the content of the text file:

EURUSD_EntryPriceInvisibleLine_419614662;1.09601000;4294967295;2;0;0;0

InitialSLInvisibleLine_419614662;1.09603000;4294967295;2;0;0;0

Thank you in advance for your time and advices.

Documentation on MQL5: Python Integration / order_calc_margin
Documentation on MQL5: Python Integration / order_calc_margin
  • www.mql5.com
order_calc_margin - Python Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

While I don't have access to the contents of [...]"HLINES.txt" file, it's certain that when you call

StringSplit(lineData, sep, parts);

it does not fill the array parts with more than one substring (the file may be empty or doesn't contain any separator characters). As per the documentation, the StringSplit function returns the number of substrings that will fill the array. After you call it, check the return value and make sure it is at least equal to the number of substrings you expect it to be (in this case, you expect it to be 7 substrings). I would recommend you to debug your code aswell (set a breakpoint in this line and check if the array parts is filled correctly - although we already know it's not. Check which indexes are filled and see if it is expected they're filled with that data).


Also, idk if I missed something, but you need to read line by line. You are trying to read the entire file here:

string lineData = FileReadString(fileHandle);

Split it by lines and then split them by the separator character in loops. This may be causing an array out of range error aswell.

 
Just realized that FileReadString reads line by line. Ignore the second part of my post.
 
Emanuel Cavalcante Amorim Filho #:

While I don't have access to the contents of [...]"HLINES.txt" file, it's certain that when you call

it does not fill the array parts with more than one substring (the file may be empty or doesn't contain any separator characters). As per the documentation, the StringSplit function returns the number of substrings that will fill the array. After you call it, check the return value and make sure it is at least equal to the number of substrings you expect it to be (in this case, you expect it to be 7 substrings). I would recommend you to debug your code aswell (set a breakpoint in this line and check if the array parts is filled correctly - although we already know it's not. Check which indexes are filled and see if it is expected they're filled with that data).


Also, idk if I missed something, but you need to read line by line. You are trying to read the entire file here:

Split it by lines and then split them by the separator character in loops. This may be causing an array out of range error aswell.


Thank you for your feedback I really appreciated it!

The issue is fixed now, the FileOpen function wasn't set properly apparently.
I added the following to the FileOpen function and it worked :

FileOpen(fileName, FILE_READ|FILE_TXT|FILE_ANSI,";");

In MLQ5 it was working fine with just FILE_READ, but it seems that MQL4 is less flexible.
Lesson learned, I won't make the same mistake twice!

Thank you again.

Reason: