Forex Factory (https://www.forexfactory.com/forum.php)
-   Platform Tech (https://www.forexfactory.com/forumdisplay.php?f=69)
-   -   Using external file to store Variable content in case of MT crash (https://www.forexfactory.com/showthread.php?t=524656)

MikeFT Feb 3, 2015 6:45am | Post# 1

Using external file to store Variable content in case of MT crash
 
Hello folks,

I am wondering if there is a way to export some trade parameters from an EA to an external file. Maybe a txt?
I would like to save the target for an open Order, as an example.

In case MT4 crashes, the EA initialization will read this information and this way can make sure that the position is treated as if was meant to be treated!

Anybody any input/hints for me?

honestknave Feb 3, 2015 8:02am | Post# 2

Hello folks, I am wondering if there is a way to export some trade parameters from an EA to an external file. Maybe a txt? I would like to save the target for an open Order, as an example. In case MT4 crashes, the EA initialization will read this information and this way can make sure that the position is treated as if was meant to be treated! Anybody any input/hints for me?
You have a few options:

  1. Global Variables which have shelf life of 4 weeks from when they were last accessed.
  2. File functions which will let you read and write to an external file

You should find some examples and explanations through those links. Hope that helps


MikeFT Feb 3, 2015 11:19am | Post# 3

Thank you.

As far as I see it, GlobalVariables are not an option. In case the computer crashes and MT is opened on another machine, the other machine won't have access to the GlobalVariables values from the crashed machine. I don't think there is an option to utilize global variables between different terminals.

External file, using FileFunctions seems to be the way!

Does anybody have some existing code where I can get an idea, how the creation, storage and read from an external file works?

I basically will only need to store a few values and a TimeStamp. These values will be updated regularly, if a position is opened/changed. If a terminal initializes an expert, it will read from the file, if the TimeStamp is not older than a predefined amount to time.

MikeFT Feb 3, 2015 7:47pm | Post# 4

I did put together some code and I get the script to write something my my file (which is stored in the "Files" folder).

Inserted Code
//+------------------------------------------------------------------+
//|                                     ScriptsTextExternalFile1.mq4 |
//|                                               Copyright 2015 BBT |
//|                                                         https:// |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015 BBT"
#property link      "https://"
#property version   "1.00"
#property strict
//int                Int1[];
//int                Int2[];
int         Int1=1;
int         Int2=2;
double      Double1=1.1;      
double      Double2=2.2;
string      String1="String1";
datetime    DateTime= __DATE__;
 
void OnStart()
{
//FileName = test.csv;
int file_handle = FileOpen ( "test.csv" , FILE_WRITE | FILE_READ | FILE_CSV);
FileWrite(file_handle,Int1,Int2,Double1,Double2,String1,DateTime);
Alert(file_handle);
}

The question is now:
If MT crashes, how do I get the stored values from the file?

MikeFT Feb 15, 2015 2:14pm | Post# 5

Reading from the file is not too difficult (if you know how) ;-)

Inserted Code
int file_handle = FileOpen ( "test.csv" , FILE_READ | FILE_CSV);
   int int1ReadValue= StrToInt(FileReadString(file_handle));
   int Int2ReadValue=StrToInt(FileReadString(file_handle));
   double Double1ReadValue=StrToDouble(FileReadString(file_handle));
   //and so on for the remaining values
FileClose(file_handle);

Maybe it's of help for anybody!


© Forex Factory