How you save data? Simple or global variables, files, db?

 

Hi,

What methods do you use to store important data to make your MQL4 EAs resistant to platform crashes and other unexpected problems? I have read somewhere that global variables are not any better than simple variables and the data will not be saved if the platform crashes. Is this true? Currently I use a file based sql db to store all important values. Now I am less nervous about possibility of EA not being able to recover from crash, but constant checking and updating of variables affects the performance in a negative way. Considering this, it would be very interesting to hear about solutions that you use.

Regards,
Quaestus

 
quaestus:

Hi,

What methods do you use to store important data to make your MQL4 EAs resistant to platform crashes and other unexpected problems? I have read somewhere that global variables are not any better than simple variables and the data will not be saved if the platform crashes. Is this true? Currently I use a file based sql db to store all important values. Now I am less nervous about possibility of EA not being able to recover from crash, but constant checking and updating of variables affects the performance in a negative way. Considering this, it would be very interesting to hear about solutions that you use.

Regards,
Quaestus


Hi, I have resolved that problem using FileOpen(Name,FILE_BIN|FILE_WRITE);

Depending of the type of values you want to store, string/double/int/bool and the amount of these values you can use the functions FileWriteArray or FileWriteString/Double/Int.

Then you must choose when to write the values to hard disk, y choosed when orders were open or closed or in a better way, when something was changed in the terminal like some order propertys.

I will leave here some functions I did so you can take as an example.

To more information please read this section https://docs.mql4.com/files/FileOpen

Files:
 
quaestus:
  1. What methods do you use to store important data to make your MQL4 EAs resistant to platform crashes and other unexpected problems?
  2. I have read somewhere that global variables are not any better than simple variables and the data will not be saved if the platform crashes. Is this true?
  3. but constant checking and updating of variables affects the performance in a negative way.
  1. None - I recalculate everything when needed. What information must you save?
  2. On a power failure Global Variables will not be written to disk. Using files you must close or flush to be certain.
  3. Why are you constantly checking. Do your calculations then nothing changes until either price reaches a level or a new bar starts.
    double  Point_2;                                // Export to CallAgain
    :
    int     init(){                                                     
        Point_2 = Point / 2.;
        :
    }
    double  ca.below,   ca.above=0;                 // Import from CallAgain
    int     start(){                                
        #define INF 0x6FFFFFFF // Not quite infinite, Jul 2029, or 1,879,048,191
        static datetime Time0;
        if (Time0 != Time[0] || ca.below > Bid ||   ca.above < Bid){    // Important
            Time0 = Time[0];    ca.below = 0;       ca.above = INF;
            :
            double buyLevel = ...
            :
            if (Bid < buyLevel){ CallAgain(buyLevel, "buy"); return; }
            : OrderSend
        }
    }
    ///////
    void    CallAgain(double when, string why){
            /* Last call Printed nol(1.25741)<1.2579<noh(1.25839)
             * start: Print(
             * "Bid(",PriceToStr(Bid),"<cab(",PriceToStr(ca.below),")=",Bid<ca.below,
             * Printed: Bid(1.25741)<cab(1.25741)=1
             * < returned true but prices were equal. Thus Point_2*/
            if (when > Bid - Point_2 && ca.above > when){ ca.above = when + Point_2; }
            if (when < Bid + Point_2 && ca.below < when){ ca.below = when - Point_2; }
    /*      static string below="", above="";
            if (when > Bid - Point_2 && ca.above > when){ ca.above = when + Point_2;        above=why; }
            if (when < Bid + Point_2 && ca.below < when){ ca.below = when - Point_2;        below=why; }
            Print("cab(",PriceToStr(ca.below),")<Bid(",PriceToStr(Bid),")<",
                      "caa(",PriceToStr(ca.above),")");     //*/
    }
    

 
WHRoeder:
  1. None - I recalculate everything when needed. What information must you save?
  2. On a power failure Global Variables will not be written to disk. Using files you must close or flush to be certain.
  3. Why are you constantly checking. Do your calculations then nothing changes until either price reaches a level or a new bar starts.

Hi, i have a question for you, this type of code works in mql4? I can be wrong, but i think classes and structures aren't allowed in mql4, object oriented programming is only for MQL5.
 
nqueiros:

Hi, i have a question for you, this type of code works in mql4? I can be wrong, but i think classes and structures aren't allowed in mql4, object oriented programming is only for MQL5.
They are just variables, it's not any kind of dot notation.
 
WHRoeder:
  1. None - I recalculate everything when needed. What information must you save?
  2. On a power failure Global Variables will not be written to disk. Using files you must close or flush to be certain.
  3. Why are you constantly checking. Do your calculations then nothing changes until either price reaches a level or a new bar starts.

Thanks for valuable input, WHRoeder.

1. Do you always find a way to recalculate all data? I always recalculate everything that is possible. However, sometimes recalculating everything reliably is very difficult, especially when EA takes into account user input. I usually need to store data when EA logic includes decisions based on relationships between orders (like categorizing them or having a hierarchy). The only reliable internal MT4 way of saving some data is MagicNumber. If I need to have at least two impossible to recalculate data fields per order, I just need to store data in a db.

2. Thanks for confirming this. I remember having read this somewhere too, but was not 100% sure if this is true until now.

3. When I have multiple orders that have relationships, I loop through them on each tick and access additional data in db. Based on this I close some orders or open new ones. It might be possible to optimize this by monitoring changes to number of active trades and OrderHistory, but sometimes I think that this will just add additional level of complexity and might possibly result in more bugs.

If you have any ideas how to accomplish some part of this easier or more efficiently, I would be glad to hear.

Thanks.

Reason: