Hello,
I am trying to store one small value into file.
Simple, Rule
If file exist, read the value else create new file and store the value
When OnTick condition executed, Rewrite the value.
But i am getting an error 5011 and when i open the .txt file, it always empty and blank. How to solve this?
I have already seen that URL and some forum post but still i did not understand how can i code this. Can you help with sample code or more explanation, how can i achieve
I have already seen that URL and some forum post but still i did not understand how can i code this. Can you help with sample code or more explanation, how can i achieve
FileOpen(MM_File, FILE_READ |FILE_TXT );
Like this FILE_READ | FILE_TXT and FILE_WRITE | FILE_TXT?
extern string inc_MoneyManagement = "***Money Management***"; extern bool Money_Management = FALSE; enum MoneyManagement_enum {Fixed_Money, Balance_Percentage}; extern MoneyManagement_enum MoneyManagement_Type = Balance_Percentage; extern double Target_Revenue = 1000.0; double gTargeted_Revenue; // File to store gTargeted_Revenue value string MM_File = _Symbol+"targeted_revenue.txt"; int OnInit() { // Load gTargeted_Revenue value from file if it exists int fileHandle = FileOpen(MM_File, FILE_READ |FILE_TXT); if (fileHandle != INVALID_HANDLE) { gTargeted_Revenue = FileReadDouble(fileHandle); FileClose(fileHandle); Print("Loaded gTargeted_Revenue from file: ", gTargeted_Revenue); } else { // Create a new file and calculate gTargeted_Revenue ResetLastError(); fileHandle = FileOpen(MM_File, FILE_WRITE |FILE_TXT); if (fileHandle != INVALID_HANDLE) { if(MoneyManagement_Type == Balance_Percentage) gTargeted_Revenue = (1 + Target_Revenue/100) * AccountBalance(); else gTargeted_Revenue = Target_Revenue + AccountBalance(); FileWriteDouble(fileHandle, gTargeted_Revenue); FileClose(fileHandle); Print("Created new file and wrote gTargeted_Revenue: ", gTargeted_Revenue); Print(" File creation Error : " + GetLastError()); } else { Print("Failed to create file: ", MM_File); // Handle file creation failure here } } } void OnTick() { if(Money_Management && AccountEquity() >= gTargeted_Revenue) { for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS)==false) continue; if (OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE) == false) Print("Order close failed for ticket ", OrderTicket()); } Print("All The Trade Closed due to it exceeded defined Money Management (Target Revenue) : " + AccountEquity()); // Update gTargeted_Revenue value if(MoneyManagement_Type == Balance_Percentage) gTargeted_Revenue = (1 + Target_Revenue/100) * AccountBalance(); else gTargeted_Revenue = Target_Revenue + AccountBalance(); // Save gTargeted_Revenue value to file int fileHandle = FileOpen(MM_File, FILE_WRITE |FILE_TXT); if (fileHandle != INVALID_HANDLE) { FileWriteDouble(fileHandle, gTargeted_Revenue); FileClose(fileHandle); } else { Print("Failed to open file for writing: ", MM_File); // Handle file writing failure here } } }
Like this FILE_READ | FILE_TXT and FILE_WRITE | FILE_TXT?
My EA is MQL4 EA and i am working on MT4 Platform. I am getting this error on MT4
I saw your code based on it. i made an edit and posted next to your post. Is my code is correct?
My EA is MQL4 EA and i am working on MT4 Platform. I am getting this error on MT4
I saw your code based on it. i made an edit and posted next to your post. Is my code is correct?
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
I am trying to store one small value into file.
Simple, Rule
If file exist, read the value else create new file and store the value
When OnTick condition executed, Rewrite the value.
But i am getting an error 5011 and when i open the .txt file, it always empty and blank. How to solve this?