We often encounter the need to count the number of hands in historical transaction records, or to obtain information about the last historical transaction order. Such a function is not difficult for us, here is a simple code example:
double lot = 0;
for(int i=0;i < OrdersHistoryTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) == false) continue;
lot += OrderLots();
}
Print("Total lot:",lot);
This code is to count all the historical lot numbers through the loop. In fact, this code is just to change the code that loops through all the existing orders. Change the OrdersTotal() function to OrdersHistoryTotal(), and add a parameter to the OrderSelect function: MODE_HISTORY. So is there a problem with such a simple program? The answer is yes.
Let's run this EA program, the result displayed in the terminal is:
The total number of hands is actually 0! It's incredible, obviously I have a lot of transaction records.
Now let’s do some operations with the editor, and then run the program to see what the final result is
Right-click in the account history, select all transaction records, and now all historical records of previous transactions appear in the account history
Then we run the EA program and try it out, and the result will be normal:
This is a "pit" of obtaining historical records through the program. In MT4, the software will only display the historical records of the current month by default, and the program will only search based on the current historical records of the software when obtaining historical records. Since I have no Do any transactions, so when you count the total number of hands, you get 0, and if all the transaction records are displayed in the software, the program can finally work normally.
The reason why we seldom find this problem is that the timeliness of general MT4 platform demo accounts is very short. After registering a demo account, it expires soon, so few people can find this problem. Therefore, it is very important to have a platform that can open a long-term simulated account. We recommend using the xxx platform, which can provide a simulated account for up to xx days.
The long-term simulated user itself is very helpful for us to develop and test EA programs. As we all know, it is not enough to use historical simulation after developing a new EA program. It is also necessary to hang up the test. If the test expires within a few days, it will be like this It is very troublesome for developing EA.
The second "pit" about this historical record is that if we want to get the historical record, we can only use the EA program to obtain it. If we write a script program, we will not be able to get the historical record, even if we have already displayed it in the account history. all records.
So how can we avoid this problem? Because the customer is not aware of this problem when using our EA program, if the customer does not know or will not do this at all, how can we avoid this problem in the program? A relatively simple way is to store all order records in a file during the running of your EA program, so that the historical record information can be obtained even if the customer does not perform any operations. The following is a simple code example for saving files :
//content is the content to be stored
void setfile(string content)
{
string filename = "The file name to store";
int filehandle = FileOpen(filename,FILE_WRITE|FILE_TXT);//It can also be saved as a CSV file and replace FILE_TXT with FILE_CSV, so that multiple pieces of information can be stored
if(filehandle == INVALID_HANDLE)//judging whether there is an error in reading the file
{
FileClose(filehandle);
Print("An error occurred in saving the file");
}
else//Open normally, you can write content
{
FileWriteString(filehandle, content);
FileClose(filehandle);
}
}
Through the above methods, everyone must have understood, and our program will be more perfect and robust in future development.