how to retain variable if mt4 needs to restart?

 

Hi all,

What are the methods to retain a variable value if mt4 was restarted?

i can only think of either

1. storing the information in the comment of an order

2. save the info in a csv file and read it back later

Thanks 

 
bobo1974:

What are the methods to retain a variable value if mt4 was restarted?

Use global variables.
 

Global variables will be lost !

use

FileWrite()

To store your data then use FileRead(datatype) to read the data back into the EA. https://www.mql5.com/en/docs/files

A simple check call for:

ENUM_TERMINAL_INFO_INTEGER

Identifier

Description

TERMINAL_CONNECTED

Connection to a trade server


Can alert you when the connection drops.

*NOTE that comments can change !!, it's not a secure or reliable solution.

Documentation on MQL5: File Functions
Documentation on MQL5: File Functions
  • www.mql5.com
File Functions - Reference on algorithmic/automated trading language for MetaTrader 5
 
Marco vd Heijden:

Global variables will be lost !

use

To store your data then use FileRead(datatype) to read the data back into the EA. https://www.mql5.com/en/docs/files

A simple check call for:

ENUM_TERMINAL_INFO_INTEGER

Identifier

Description

TERMINAL_CONNECTED

Connection to a trade server


Can alert you when the connection drops.

*NOTE that comments can change !!, it's not a secure or reliable solution.

hi

 is this function going to write to an external file and read back later? 

 
bobo1974:

hi

 is this function going to write to an external file and read back later? 

No that will alert you that your terminal has lost it's connection.

Can be used in combination with

Alert() 
PlaySound()
MessageBox()
SendNotification()
SendMail()

And etc.

To store your data use 

FileWrite()

To read it

FileReadArray

Reads arrays of any type except for string from the file of the BIN type

FileReadBool

Reads from the file of the CSV type a string from the current position till a delimiter (or till the end of a text line) and converts the read string to a value of bool type

FileReadDatetime

Reads from the file of the CSV type a string of one of the formats: "YYYY.MM.DD HH:MM:SS", "YYYY.MM.DD" or "HH:MM:SS" - and converts it into a datetime value

FileReadDouble

Reads a double value from the current position of the file pointer

FileReadFloat

Reads a float value from the current position of the file pointer

FileReadInteger

Reads int, short or char value from the current position of the file pointer

FileReadLong

Reads a long type value from the current position of the file pointer

FileReadNumber

Reads from the file of the CSV type a string from the current position till a delimiter (or til the end of a text line) and converts the read string into double value

FileReadString

Reads a string from the current position of a file pointer from a file

FileReadStruct

Reads the contents from a binary file  into a structure passed as a parameter, from the current position of the file pointer


As per: https://www.mql5.com/en/docs/files

You have several options.

* Build an EA that simply scans all orders upon initialization, and handles it from there.

That will work anytime and with all positions, but you can not recover exact information about specific symbols, it means all orders will be handled to a set of pre defined values.

Unless you specify the settings, for each symbol, and load them upon detection.

* Store the current position data into a file then read that data back into the EA in case of a failure.

This requires that you FileWrite every parameter you want your EA to recover upon re-loading the terminal.

* Decentralized nodes.

In this case you run several terminals with the same software and they share a combined file.

If one terminal fails this is not a problem because there are more running on the same account.

This requires an even deeper or synchronized level of coding.(as Nodes)

Documentation on MQL5: File Functions
Documentation on MQL5: File Functions
  • www.mql5.com
File Functions - Reference on algorithmic/automated trading language for MetaTrader 5
 
Marco vd Heijden:

Global variables will be lost !

This is incorrect. Global variables are retained between restarts. They are just a simple way of storing data in a special file the terminal maintains for you.
 
Stanislav Korotky:
This is incorrect. Global variables are retained between restarts. They are just a simple way of storing data in a special file the terminal maintains for you.

Please restart your computer and try again.

Which files are they stored in?

Are you referring to a file on disk ?

or to a virtual memory location ?

It seems to me that Bob is looking for a solution that will work even in case his fuse trips because the dishwasher had an internal short circuit for example.

 
Marco vd Heijden:

Please restart your computer and try again.

Which files are they stored in?

Are you referring to a file on disk ?

or to a virtual memory location ?

It seems to me that Bob is looking for a solution that will work even in case his fuse trips because the dishwasher had an internal short circuit for example.

I don't have full technical details, except that the file with global variables is MetaTrader's work folder\profiles\gvariables.dat and updates promptly (I don't know exact timing). They exist in memory for quick access and constantly backed up on the disk. The GVs do definitely survive between sessions. You may address your in depth questions to MQ. In case when PC halts unexpectedly you can not guarantee that information will be saved by any means, including file writes suggested by you.
 
Marco vd Heijden:

Please restart your computer and try again.

Which files are they stored in?

Are you referring to a file on disk ?

or to a virtual memory location ?

It seems to me that Bob is looking for a solution that will work even in case his fuse trips because the dishwasher had an internal short circuit for example.

You can read this link :

Global variables are kept in the client terminal for 4 weeks since the last access, then they will be deleted automatically. An access to a global variable is not only setting of a new value, but reading of the global variable value, as well.

Reason: