FileOpen is failed sometimes in MQL4

FileOpen is failed sometimes in MQL4

Manage alerts

Loading saved threads...

Reza Nasimi · External communityPost link
External question — Stack Overflow Stack Exchange Author: Reza Nasimi Original post: https://stackoverflow.com/questions/68966315 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. In an EA I'm writing a couple of integer values (some OrderTickects) into a bin file. Then I have a TP/SL modification function that loops through active trades and applies the modifications. This function is called by each tick and opens the file and if the order ticket found in the file should ignore that order without modification. The problem is that it successfully opens the file and reads it but only for the first time. the rest of the time it gives me an error of 0. I have checked everything as much as I could and tracked the issue with putting lots of prints until eventually, I am pretty sure that the problem is only the failure of the FileOpen function. THIS HAPPENED ON TESTER . I'm not sure if it will happen on the live market as well or not, because, to catch the issue on live testing would be very hard with this EA. I should add that every time that I opened the file, I also closed it. My only guess is that maybe when I call the FileClose function before it closes the file successfully the next call of FileOpen arrives. Can it be the reason? I would highly appreciate any guidance or ideas. Do you think I can trust opening the file in the live market? Here are the related parts of the code. Save function is called so less often, but IsBE_Done() is called by every tick if there are one or more active trades. void Save(int ticket) { int handle = FileOpen("BE1.bin", FILE_BIN | FILE_WRITE | FILE_READ); if(handle != INVALID_HANDLE) { FileSeek(handle, 0, SEEK_END); FileWriteInteger(handle, ticket); //--- FileClose(handle); } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool IsBE_Done(int ticket) { int handle = FileOpen("BE1.bin", FILE_BIN | FILE_WRITE | FILE_READ); if(handle != INVALID_HANDLE) { FileSeek(handle, 0, SEEK_SET); while(!FileIsEnding(handle)) { int val = FileReadInteger(handle); if(ticket == val) return true; } //--- FileClose(handle); } //--- return false; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void TPSL_Modification() { //--- if(!Modifications) return; //--- for(int i = OrdersTotal() - 1; i >= 0; i--) { if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; //--- if(OrderMagicNumber() != MAGICMA || OrderSymbol() != _Symbol) continue; //--- if(IsBE_Done(OrderTicket())) //====>>> This is what opens the file continue; //---
Quote
Report

Post Reply

Checking account access…