Incomprehensible behavior on OnDeinit when saving objects...

 
Hello programmer fellows.

To preserve the objects on the chart when the settings of my EA or the timeframe is changed, my EA saves the objects on external files on OnDeinit(), and load the objects from the external files on OnInit().

It works just fine when I change the user inputs or recompile for example, but when I change the timeframe (when the deinit reason is CHARTCHANGE) it returns empty strings for the ObjectNames and 0 for the object type (for all objects regardless of their respective types) when saving the objects to the external file.


Here is the code I use, and the logs below:

int OnInit() {
    // Load HLINEs and other objects
    LoadHLINEs();
    LoadLabels();
    LoadRectangleTicket();

    return INIT_SUCCEEDED;
}

void OnDeinit(const int reason) {
    Print("Deinitializing with reason: ", reason);

    // Save HLINEs and other objects
    SaveHLINEs();
    SaveLabels();
    SaveRectangleTicket();

    app.Destroy(reason);

    Comment("");
    EventKillTimer();
}

void SaveHLINEs() {
    if (!MQLInfoInteger(MQL_TESTER)) {
        const string fileName = _Symbol + "HLINEs.txt";
        int fileHandle = FileOpen(fileName, FILE_WRITE | FILE_TXT | FILE_ANSI);
        if (fileHandle != INVALID_HANDLE) {
            int totalObjects = ObjectsTotal(0, 0, -1);  // Get the total number of objects in the chart
            Print("Total objects in the chart: ", totalObjects);

            for (int i = 0; i < totalObjects; i++) {
                string lineName = ObjectName(0, i);
                int objectType = (int)ObjectGetInteger(0, lineName, OBJPROP_TYPE);

                Print("Object index: ", i, " - Name: '", lineName, "' - Type: ", objectType);

                // Check if the object type is a horizontal line
                if (objectType == OBJ_HLINE) {
                    if (lineName == "" || lineName == NULL) {
                        Print("Skipping invalid object name at index: ", i);
                        continue; // Skip invalid object names
                    }

                    Print("Saving: ", lineName);
                    double price = 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, _Digits) + ";" + IntegerToString(lineColor) +
                                    ";" + IntegerToString(width) + ";" + IntegerToString(style) + ";" +
                                    (selectable ? "1" : "0") + ";" + (selected ? "1" : "0") + "\r\n");
                    Print("Saved: ", lineName);
                }
            }

            FileClose(fileHandle);
            Print("File closed: ", fileName);
        } else {
            Print("Error opening file for writing: ", fileName);
        }
    }
}



void LoadHLINEs() {
    if (!MQLInfoInteger(MQL_TESTER)) {
        const string fileName = _Symbol + "HLINEs.txt";
        if (FileIsExist(fileName)) {
            int fileHandle = FileOpen(fileName, FILE_READ | FILE_TXT | FILE_ANSI, ";");
            if (fileHandle != INVALID_HANDLE) {
                Print("Opened file for reading: ", fileName);
                while (!FileIsEnding(fileHandle)) {
                    string lineData = FileReadString(fileHandle);
                    string parts[];
                    StringSplit(lineData, ';', parts);

                    if (ArraySize(parts) < 7) continue; // Ensure valid data

                    string lineName = parts[0];
                    Print("Loading: ", lineName);
                    double price = StringToDouble(parts[1]);
                    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);
                    Print("Created: ", lineName);
                    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");
                }
                FileClose(fileHandle);
                Print("File closed after reading: ", fileName);
                // FileDelete(fileName); // Uncomment if you want to delete the file after reading
            } else {
                Print("Error opening file for reading: ", fileName);
            }
        } else {
            Print("File not found: ", fileName);
        }
    }
}
(I use other functions to save other object types but we don't need them here)


For example's sake it is supposed to save 2 HLINes.

Logs when changing the user input (when it works and saves the 2 lines that it is supposed to be saving):

// Save
2024.07.17 13:59:26.477 Manual Trading Interface (EURUSD,M4)    Deinitializing with reason: 5
2024.07.17 13:59:26.497 Manual Trading Interface (EURUSD,M4)    Total objects in the chart: 33
2024.07.17 13:59:26.497 Manual Trading Interface (EURUSD,M4)    Object index: 0 - Name: '32090Back' - Type: 110
2024.07.17 13:59:26.497 Manual Trading Interface (EURUSD,M4)    Object index: 1 - Name: '32090Border' - Type: 110
2024.07.17 13:59:26.497 Manual Trading Interface (EURUSD,M4)    Object index: 2 - Name: '32090Caption' - Type: 107
2024.07.17 13:59:26.497 Manual Trading Interface (EURUSD,M4)    Object index: 3 - Name: '32090ClientBack' - Type: 110
2024.07.17 13:59:26.497 Manual Trading Interface (EURUSD,M4)    Object index: 4 - Name: '32090Close' - Type: 106
2024.07.17 13:59:26.497 Manual Trading Interface (EURUSD,M4)    Object index: 5 - Name: '32090MinMax' - Type: 106
2024.07.17 13:59:26.497 Manual Trading Interface (EURUSD,M4)    Object index: 6 - Name: 'ButtonsCreatedForTicket_702277425' - Type: 101
2024.07.17 13:59:26.497 Manual Trading Interface (EURUSD,M4)    Object index: 7 - Name: 'ButtonsCreatedForTicket_702284750' - Type: 101
2024.07.17 13:59:26.498 Manual Trading Interface (EURUSD,M4)    Object index: 8 - Name: 'ButtonsCreatedForTicket_702289682' - Type: 101
2024.07.17 13:59:26.498 Manual Trading Interface (EURUSD,M4)    Object index: 9 - Name: 'CP_25%_Button_702289682_1.09316' - Type: 103
2024.07.17 13:59:26.498 Manual Trading Interface (EURUSD,M4)    Object index: 10 - Name: 'CP_50%_Button_702289682_1.09316' - Type: 103
2024.07.17 13:59:26.498 Manual Trading Interface (EURUSD,M4)    Object index: 11 - Name: 'CP_75%_Button_702289682_1.09316' - Type: 103
2024.07.17 13:59:26.498 Manual Trading Interface (EURUSD,M4)    Object index: 12 - Name: 'CP_BE_Button_702289682_1.09316' - Type: 103
2024.07.17 13:59:26.498 Manual Trading Interface (EURUSD,M4)    Object index: 13 - Name: 'CP_X_Button_702289682_1.09316' - Type: 103
2024.07.17 13:59:26.498 Manual Trading Interface (EURUSD,M4)    Object index: 14 - Name: 'Close BUY positions' - Type: 106
2024.07.17 13:59:26.498 Manual Trading Interface (EURUSD,M4)    Object index: 15 - Name: 'Close SELL positions' - Type: 106
2024.07.17 13:59:26.498 Manual Trading Interface (EURUSD,M4)    Object index: 16 - Name: 'EARectangle_BuyLine_702289682' - Type: 2
2024.07.17 13:59:26.498 Manual Trading Interface (EURUSD,M4)    Object index: 17 - Name: 'EARectangle_BuyLowerBottom_702289682' - Type: 20
2024.07.17 13:59:26.498 Manual Trading Interface (EURUSD,M4)    Object index: 18 - Name: 'EARectangle_BuyLowerTop_702289682' - Type: 20
2024.07.17 13:59:26.498 Manual Trading Interface (EURUSD,M4)    Object index: 19 - Name: 'EARectangle_BuyUpperBottom_702289682' - Type: 20
2024.07.17 13:59:26.498 Manual Trading Interface (EURUSD,M4)    Object index: 20 - Name: 'EARectangle_BuyUpperTop_702289682' - Type: 20
2024.07.17 13:59:26.498 Manual Trading Interface (EURUSD,M4)    Object index: 21 - Name: 'EAname' - Type: 102
2024.07.17 13:59:26.498 Manual Trading Interface (EURUSD,M4)    Object index: 22 - Name: 'EURUSD_entryPriceInvisibleLine_702289682' - Type: 1
2024.07.17 13:59:26.498 Manual Trading Interface (EURUSD,M4)    Saving: EURUSD_entryPriceInvisibleLine_702289682
2024.07.17 13:59:26.498 Manual Trading Interface (EURUSD,M4)    Saved: EURUSD_entryPriceInvisibleLine_702289682
2024.07.17 13:59:26.498 Manual Trading Interface (EURUSD,M4)    Object index: 23 - Name: 'InitialSLInvisibleLine_702289682' - Type: 1
2024.07.17 13:59:26.498 Manual Trading Interface (EURUSD,M4)    Saving: InitialSLInvisibleLine_702289682
2024.07.17 13:59:26.499 Manual Trading Interface (EURUSD,M4)    Saved: InitialSLInvisibleLine_702289682
2024.07.17 13:59:26.499 Manual Trading Interface (EURUSD,M4)    Object index: 24 - Name: 'OnTickTPLabel_702289682' - Type: 102
2024.07.17 13:59:26.499 Manual Trading Interface (EURUSD,M4)    Object index: 25 - Name: 'Open BUY position' - Type: 106
2024.07.17 13:59:26.499 Manual Trading Interface (EURUSD,M4)    Object index: 26 - Name: 'Open SELL position' - Type: 106
2024.07.17 13:59:26.499 Manual Trading Interface (EURUSD,M4)    Object index: 27 - Name: 'PosSelectionNumBG1' - Type: 102
2024.07.17 13:59:26.499 Manual Trading Interface (EURUSD,M4)    Object index: 28 - Name: 'PosSelectionNumBG2' - Type: 102
2024.07.17 13:59:26.499 Manual Trading Interface (EURUSD,M4)    Object index: 29 - Name: 'PosSelectionNumWhite' - Type: 102
2024.07.17 13:59:26.499 Manual Trading Interface (EURUSD,M4)    Object index: 30 - Name: 'Select Next position' - Type: 106
2024.07.17 13:59:26.499 Manual Trading Interface (EURUSD,M4)    Object index: 31 - Name: 'Select Previous position' - Type: 106
2024.07.17 13:59:26.499 Manual Trading Interface (EURUSD,M4)    Object index: 32 - Name: 'TakeProfitLabel_702289682' - Type: 102
2024.07.17 13:59:26.499 Manual Trading Interface (EURUSD,M4)    File closed: EURUSDHLINEs.txt

// Load
2024.07.17 13:59:26.517 Manual Trading Interface (EURUSD,M4)    Opened file for reading: EURUSDHLINEs.txt
2024.07.17 13:59:26.517 Manual Trading Interface (EURUSD,M4)    Loading: EURUSD_entryPriceInvisibleLine_702289682
2024.07.17 13:59:26.517 Manual Trading Interface (EURUSD,M4)    Created: EURUSD_entryPriceInvisibleLine_702289682
2024.07.17 13:59:26.517 Manual Trading Interface (EURUSD,M4)    Loading: InitialSLInvisibleLine_702289682
2024.07.17 13:59:26.517 Manual Trading Interface (EURUSD,M4)    Created: InitialSLInvisibleLine_702289682
2024.07.17 13:59:26.517 Manual Trading Interface (EURUSD,M4)    File closed after reading: EURUSDHLINEs.txt



And here are the logs when changing the timeframe (weird behavior):

// Save
2024.07.17 14:01:29.143 Manual Trading Interface (EURUSD,M4)    Deinitializing with reason: 3
2024.07.17 14:01:29.155 Manual Trading Interface (EURUSD,M4)    Total objects in the chart: 35
2024.07.17 14:01:29.170 Manual Trading Interface (EURUSD,M4)    Object index: 0 - Name: '' - Type: 0
2024.07.17 14:01:29.186 Manual Trading Interface (EURUSD,M4)    Object index: 1 - Name: '' - Type: 0
2024.07.17 14:01:29.206 Manual Trading Interface (EURUSD,M4)    Object index: 2 - Name: '' - Type: 0
2024.07.17 14:01:29.206 Manual Trading Interface (EURUSD,M4)    Object index: 3 - Name: '' - Type: 0
2024.07.17 14:01:29.206 Manual Trading Interface (EURUSD,M4)    Object index: 4 - Name: '' - Type: 0
2024.07.17 14:01:29.206 Manual Trading Interface (EURUSD,M4)    Object index: 5 - Name: '' - Type: 0
2024.07.17 14:01:29.206 Manual Trading Interface (EURUSD,M4)    Object index: 6 - Name: '' - Type: 0
2024.07.17 14:01:29.207 Manual Trading Interface (EURUSD,M4)    Object index: 7 - Name: '' - Type: 0
2024.07.17 14:01:29.207 Manual Trading Interface (EURUSD,M4)    Object index: 8 - Name: '' - Type: 0
2024.07.17 14:01:29.207 Manual Trading Interface (EURUSD,M4)    Object index: 9 - Name: '' - Type: 0
2024.07.17 14:01:29.209 Manual Trading Interface (EURUSD,M4)    Object index: 10 - Name: '' - Type: 0
2024.07.17 14:01:29.209 Manual Trading Interface (EURUSD,M4)    Object index: 11 - Name: '' - Type: 0
2024.07.17 14:01:29.211 Manual Trading Interface (EURUSD,M4)    Object index: 12 - Name: '' - Type: 0
2024.07.17 14:01:29.221 Manual Trading Interface (EURUSD,M4)    Object index: 13 - Name: '' - Type: 0
2024.07.17 14:01:29.222 Manual Trading Interface (EURUSD,M4)    Object index: 14 - Name: '' - Type: 0
2024.07.17 14:01:29.223 Manual Trading Interface (EURUSD,M4)    Object index: 15 - Name: '' - Type: 0
2024.07.17 14:01:29.223 Manual Trading Interface (EURUSD,M4)    Object index: 16 - Name: '' - Type: 0
2024.07.17 14:01:29.223 Manual Trading Interface (EURUSD,M4)    Object index: 17 - Name: '' - Type: 0
2024.07.17 14:01:29.223 Manual Trading Interface (EURUSD,M4)    Object index: 18 - Name: '' - Type: 0
2024.07.17 14:01:29.223 Manual Trading Interface (EURUSD,M4)    Object index: 19 - Name: '' - Type: 0
2024.07.17 14:01:29.224 Manual Trading Interface (EURUSD,M4)    Object index: 20 - Name: '' - Type: 0
2024.07.17 14:01:29.224 Manual Trading Interface (EURUSD,M4)    Object index: 21 - Name: '' - Type: 0
2024.07.17 14:01:29.224 Manual Trading Interface (EURUSD,M4)    Object index: 22 - Name: '' - Type: 0
2024.07.17 14:01:29.224 Manual Trading Interface (EURUSD,M4)    Object index: 23 - Name: '' - Type: 0
2024.07.17 14:01:29.224 Manual Trading Interface (EURUSD,M4)    Object index: 24 - Name: '' - Type: 0
2024.07.17 14:01:29.224 Manual Trading Interface (EURUSD,M4)    Object index: 25 - Name: '' - Type: 0
2024.07.17 14:01:29.224 Manual Trading Interface (EURUSD,M4)    Object index: 26 - Name: '' - Type: 0
2024.07.17 14:01:29.224 Manual Trading Interface (EURUSD,M4)    Object index: 27 - Name: '' - Type: 0
2024.07.17 14:01:29.224 Manual Trading Interface (EURUSD,M4)    Object index: 28 - Name: '' - Type: 0
2024.07.17 14:01:29.224 Manual Trading Interface (EURUSD,M4)    Object index: 29 - Name: '' - Type: 0
2024.07.17 14:01:29.224 Manual Trading Interface (EURUSD,M4)    Object index: 30 - Name: '' - Type: 0
2024.07.17 14:01:29.225 Manual Trading Interface (EURUSD,M4)    Object index: 31 - Name: '' - Type: 0
2024.07.17 14:01:29.225 Manual Trading Interface (EURUSD,M4)    Object index: 32 - Name: '' - Type: 0
2024.07.17 14:01:29.225 Manual Trading Interface (EURUSD,M4)    Object index: 33 - Name: '' - Type: 0
2024.07.17 14:01:29.225 Manual Trading Interface (EURUSD,M4)    Object index: 34 - Name: '' - Type: 0
2024.07.17 14:01:29.225 Manual Trading Interface (EURUSD,M4)    File closed: EURUSDHLINEs.txt

// Load
2024.07.17 14:01:29.235 Manual Trading Interface (EURUSD,M1)    Opened file for reading: EURUSDHLINEs.txt
2024.07.17 14:01:29.235 Manual Trading Interface (EURUSD,M1)    File closed after reading: EURUSDHLINEs.txt


What I have tried:
- To loop forward and backward through the objects.  (it didn't work)
- To save the objects on CHART_CHANGE and also on OnTimer() to also save them outside the OnDeinit function. (it didn't work)
- To use Sleep(100); before, and after the save function in the OnDeinit().  (it didn't work)
- To put the save function into a  "if (reason == REASON_CHARTCHANGE)"

I dont' have a lot of experience programming so I might be missing something very obvious but I would really appreciate some help as I don't know what to try anymore and I don't understnad the root of the issue even after exploring the forum.

Thank you in advance for your time.

 
Check erors (GetLastError()) after failed ObjectName and ObjectGetInteger 
 
Thank you for your suggestion.

Here is what I got for each object with empty strings and object type 0:

ERR_WRONG_STRING_PARAMETER

5040

Damaged parameter of string type


And the same code works just fine when the deinit reason is REASON_PARAMETERS.
 
Yoloic #:
Thank you for your suggestion.

Here is what I got for each object with empty strings and object type 0:

ERR_WRONG_STRING_PARAMETER

5040

Damaged parameter of string type


And the same code works just fine when the deinit reason is REASON_PARAMETERS.
Is this the error from ObjectName()?

Because if the name is wrong, you cant go on with accepting its type, surely you have a wrong string.

What is the LastError of ObjectName? And does it return true or false?
 
            int totalObjects = ObjectsTotal(0, 0, -1);  // Get the total number of objects in the chart
            Print("Total objects in the chart: ", totalObjects);

            for (int i = 0; i < totalObjects; i++) {
                string lineName = ObjectName(0, i);

Missing window argument.

 
Thank you for the feedbacks!

I have modified the code following your recommendations but I still get error 4103 and 4202 even after correcting:

ERR_CHART_NOT_FOUND

4103

Chart not found

ERR_OBJECT_NOT_FOUND

4202

Graphical object was not found



            int totalObjects = ObjectsTotal(0, 0, -1);  // Get the total number of objects in the chart
            Print("Total objects in the chart: ", totalObjects);

            for (int i = 0; i < totalObjects; i++) {
                string lineName = ObjectName(0, i, 0, -1);
                Print("ERROR = ", GetLastError());
                int objectType = (int)ObjectGetInteger(0, lineName, OBJPROP_TYPE);

                Print("Object index: ", i, " - Name: '", lineName, "' - Type: ", objectType);

2024.07.17 19:06:17.573 Manual Trading Interface (AUDJPY,M1)    Deinitializing with reason: 3
2024.07.17 19:06:17.588 Manual Trading Interface (AUDJPY,M1)    Total objects in the chart: 32
2024.07.17 19:06:17.603 Manual Trading Interface (AUDJPY,M1)    ERROR = 4103
2024.07.17 19:06:17.619 Manual Trading Interface (AUDJPY,M1)    Object index: 0 - Name: '32319Back' - Type: 0
2024.07.17 19:06:17.641 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.641 Manual Trading Interface (AUDJPY,M1)    Object index: 1 - Name: '' - Type: 0
2024.07.17 19:06:17.641 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.641 Manual Trading Interface (AUDJPY,M1)    Object index: 2 - Name: '' - Type: 0
2024.07.17 19:06:17.641 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.641 Manual Trading Interface (AUDJPY,M1)    Object index: 3 - Name: '' - Type: 0
2024.07.17 19:06:17.641 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.641 Manual Trading Interface (AUDJPY,M1)    Object index: 4 - Name: '' - Type: 0
2024.07.17 19:06:17.641 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.641 Manual Trading Interface (AUDJPY,M1)    Object index: 5 - Name: '' - Type: 0
2024.07.17 19:06:17.642 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.642 Manual Trading Interface (AUDJPY,M1)    Object index: 6 - Name: '' - Type: 0
2024.07.17 19:06:17.642 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.642 Manual Trading Interface (AUDJPY,M1)    Object index: 7 - Name: '' - Type: 0
2024.07.17 19:06:17.642 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.642 Manual Trading Interface (AUDJPY,M1)    Object index: 8 - Name: '' - Type: 0
2024.07.17 19:06:17.642 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.642 Manual Trading Interface (AUDJPY,M1)    Object index: 9 - Name: '' - Type: 0
2024.07.17 19:06:17.643 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.643 Manual Trading Interface (AUDJPY,M1)    Object index: 10 - Name: '' - Type: 0
2024.07.17 19:06:17.643 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.643 Manual Trading Interface (AUDJPY,M1)    Object index: 11 - Name: '' - Type: 0
2024.07.17 19:06:17.643 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.643 Manual Trading Interface (AUDJPY,M1)    Object index: 12 - Name: '' - Type: 0
2024.07.17 19:06:17.643 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.643 Manual Trading Interface (AUDJPY,M1)    Object index: 13 - Name: '' - Type: 0
2024.07.17 19:06:17.652 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.652 Manual Trading Interface (AUDJPY,M1)    Object index: 14 - Name: '' - Type: 0
2024.07.17 19:06:17.653 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.653 Manual Trading Interface (AUDJPY,M1)    Object index: 15 - Name: '' - Type: 0
2024.07.17 19:06:17.653 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.653 Manual Trading Interface (AUDJPY,M1)    Object index: 16 - Name: '' - Type: 0
2024.07.17 19:06:17.654 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.654 Manual Trading Interface (AUDJPY,M1)    Object index: 17 - Name: '' - Type: 0
2024.07.17 19:06:17.668 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.668 Manual Trading Interface (AUDJPY,M1)    Object index: 18 - Name: '' - Type: 0
2024.07.17 19:06:17.668 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.668 Manual Trading Interface (AUDJPY,M1)    Object index: 19 - Name: '' - Type: 0
2024.07.17 19:06:17.668 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.668 Manual Trading Interface (AUDJPY,M1)    Object index: 20 - Name: '' - Type: 0
2024.07.17 19:06:17.668 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.668 Manual Trading Interface (AUDJPY,M1)    Object index: 21 - Name: '' - Type: 0
2024.07.17 19:06:17.670 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.670 Manual Trading Interface (AUDJPY,M1)    Object index: 22 - Name: '' - Type: 0
2024.07.17 19:06:17.671 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.671 Manual Trading Interface (AUDJPY,M1)    Object index: 23 - Name: '' - Type: 0
2024.07.17 19:06:17.671 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.671 Manual Trading Interface (AUDJPY,M1)    Object index: 24 - Name: '' - Type: 0
2024.07.17 19:06:17.671 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.671 Manual Trading Interface (AUDJPY,M1)    Object index: 25 - Name: '' - Type: 0
2024.07.17 19:06:17.671 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.671 Manual Trading Interface (AUDJPY,M1)    Object index: 26 - Name: '' - Type: 0
2024.07.17 19:06:17.671 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.671 Manual Trading Interface (AUDJPY,M1)    Object index: 27 - Name: '' - Type: 0
2024.07.17 19:06:17.671 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.671 Manual Trading Interface (AUDJPY,M1)    Object index: 28 - Name: '' - Type: 0
2024.07.17 19:06:17.671 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.671 Manual Trading Interface (AUDJPY,M1)    Object index: 29 - Name: '' - Type: 0
2024.07.17 19:06:17.671 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.671 Manual Trading Interface (AUDJPY,M1)    Object index: 30 - Name: '' - Type: 0
2024.07.17 19:06:17.671 Manual Trading Interface (AUDJPY,M1)    ERROR = 4202
2024.07.17 19:06:17.671 Manual Trading Interface (AUDJPY,M1)    Object index: 31 - Name: '' - Type: 0
2024.07.17 19:06:17.672 Manual Trading Interface (AUDJPY,M1)    File closed: AUDJPYHLINEs.txt
2024.07.17 19:06:17.712 Manual Trading Interface (AUDJPY,M4)    Opened file for reading: AUDJPYHLINEs.txt
2024.07.17 19:06:17.712 Manual Trading Interface (AUDJPY,M4)    File closed after reading: AUDJPYHLINEs.txt
 
UP!

Does anyone has any idea or had the same issue? I'm still struggling with this issue...
 
Yoloic #:
UP!

Does anyone has any idea or had the same issue? I'm still struggling with this issue...
You would need to provide a complete code (mq5 file) to reproduce the issue, as well as the details of what you do exactly. I tried to reproduce it with the code you posted and I had no problem at all.
 
Yoloic:
Hello programmer fellows.

To preserve the objects on the chart when the settings of my EA or the timeframe is changed, my EA saves the objects on external files on OnDeinit(), and load the objects from the external files on OnInit().

It works just fine when I change the user inputs or recompile for example, but when I change the timeframe (when the deinit reason is CHARTCHANGE) it returns empty strings for the ObjectNames and 0 for the object type (for all objects regardless of their respective types) when saving the objects to the external file.


Here is the code I use, and the logs below:

(I use other functions to save other object types but we don't need them here)


For example's sake it is supposed to save 2 HLINes.

Logs when changing the user input (when it works and saves the 2 lines that it is supposed to be saving):



And here are the logs when changing the timeframe (weird behavior):


What I have tried:
- To loop forward and backward through the objects.  (it didn't work)
- To save the objects on CHART_CHANGE and also on OnTimer() to also save them outside the OnDeinit function. (it didn't work)
- To use Sleep(100); before, and after the save function in the OnDeinit().  (it didn't work)
- To put the save function into a  "if (reason == REASON_CHARTCHANGE)"

I dont' have a lot of experience programming so I might be missing something very obvious but I would really appreciate some help as I don't know what to try anymore and I don't understnad the root of the issue even after exploring the forum.

Thank you in advance for your time.

The problem is, you are using an EA. The EA will be notified of the deinit event after the chart has been destroyed by the terminal.

What you need to do is save each object when it gets created. Don't save them at deinit. It's too late then.

Best is to use a sqlite database to save your objects. Because then you can easily handle the saved objects when they need updating.

Using a text file will be cumbersome.

Edit:
Using this approach will save you from deinit time limit as well. Deinit only has 2.5 seconds of runtime granted by the terminal. After that, you will get a forceful removal with a message telling you "abnormal termination".
 
Alain Verleyen #:
You would need to provide a complete code (mq5 file) to reproduce the issue, as well as the details of what you do exactly. I tried to reproduce it with the code you posted and I had no problem at all.
Do you suspect it to be a bug?
 
Dominik Egert #:
The problem is, you are using an EA. The EA will be notified of the deinit event after the chart has been destroyed by the terminal.

What you need to do is save each object when it gets created. Don't save them at deinit. It's too late then.

Best is to use a sqlite database to save your objects. Because then you can easily handle the saved objects when they need updating.

Using a text file will be cumbersome.

Edit:
Using this approach will save you from deinit time limit as well. Deinit only has 2.5 seconds of runtime granted by the terminal. After that, you will get a forceful removal with a message telling you "abnormal termination".

I do agree the main problem is the design itself, it's a bad idea to save objects only in OnDeinit().

Though about the OP issue itself, I am wondering what is the source of this statement : "The EA will be notified of the deinit event after the chart has been destroyed by the terminal. " ?

Certainly when the user said the issue happened when changing the timeframe, so the chart should not be destroyed at all.

Dominik Egert #:
Do you suspect it to be a bug?
I don't know, I am staying open as long as there is not a clear explanation.