| / | Articles |
Articles
Features
Changing the External Parameters of MQL4 Programs without Restarting
To post a new article, please log in or register
|
Changing the External Parameters of MQL4 Programs without Restarting [ ru ]Introduction
|
| Type | Description | Analog in MQL | Value |
|---|---|---|---|
| 0 | Boolean or logical | bool | 0 – false, 1 - true |
| 1 | String | string | A set of characters. |
| 2 | Integer character | int | Boundary values correspond with the MQL4 rules. |
| 3 | Real character | double | Boundary values correspond with the MQL4 rules. Delimiter is the point. |
| 4 | Date | datetime | The format of recording corresponds with the MQL4 rules. |
| 5 | Action | no equivalent | 0 – no execution is required, 1 - execution is required. |
| Value | Description |
|---|---|
| 0 | Waiting. No processing is required. |
| 1 | Request for changing. The MQL4 program must response to this status value. |
| 2 | No processing is required. It is a value for variables that must be processed for an event other than direct changing the variable. |
Value – the value of the variable. The rules of recording the value depend on the variable type.
Parameters interaction.
External program. Changing the value.
If Status=0, then for changing the value of 'Value' the value of Status=1.
If Status=1, then for changing the value of 'Value' the value of 'Status' does not change.
If Status=2, then for changing the value of 'Value' the value of 'Status' does not change.
External program. Canceling changing the value.
There are situations where a value is changed by mistake or replaced with a wrong value, or the user changed his/her mind towards changing the value after having confirmed the changes. Let's consider the possibility to cancel this procedure. However, it must be taken into consideration that, if the MQL4 program has already processed the file containing changes, nothing can be conceled
If Status=1, then the 'Value' is changed for the previous one, and Status=0.
If Status=2, then the 'Value' is changed for the previous one, but the value of 'Status' does not change.
MQL4 program. Processing changes.
MQL4 program considers changes only if variable 'Status' is equal to 1. By definition, it doesn't matter what variable type has been changed, since we are calling to the processing function for each change. The difference is only in where the 'Value' will have a value or not, i.e., whether the variable change is a change or a request for execution of an action. Let's consider this in details.
If Status=1 and 'Type' ranges from 0 to 4, then use 'Value' and change the 'Status' for 0.
If Status=1 and Type=5, then execute an action and change the 'Status' for 0 and change the 'Value' for 0.
If in the course of executing the action we use additional values with Status=2, then the value of 'Status' does not change after use.
Let's consider some examples.
Example 1. Changing a Variable.
Let's have variable Var1 of the Integer type with the value of 10. The variable is in the "waiting" status.
Title=Var1
Type=2
Status=0
Value=10
In the external program, change the value of Var1 for 30. Now we have:
Title=Var1
Type=2
Status=1
Value=30
The value of 'Value' has changed, as well as the value of the 'Status' has changed for "request for changes processing".
The MQL4 program detects the changes, calls to the necessary function, and modifies the document:
Title=Var1
Type=2
Status=0
Value=30
The value of 'Value' has been accepted and the value of the 'Status' has changed for "waiting".
Example 2. Executing an action.
Let's have variable Var2 of the Action type. The variable is in the "waiting" status.
Title=Var2
Type=5
Status=0
Value=0
In the external program, change the value of Var2 for 1. Looking a bit ahead, we can say that, in order to avoid errors at assigning a wrong value to the action variable in the external program, we use a button, not an input field. Now we have:
Title=Var2
Type=5
Status=1
Value=1
The value of 'Value' has been changed, as well as the value of the 'Status' has changed for "request for changes processing".
The MQL4 program detects the changes, calls to the necessary function, and modifies the document:
Title=Var2
Type=5
Status=0
Value=0
The value of the 'Value' has changed for 0, while the value of the 'Status' has changed for "waiting".
We can see that, at executing an action, both the value of 'Status' and the value of 'Value' will change. If the variable is changed, only the value of the 'Status' will change, too, while the value of the variable remains the same.
Choosing a Transfer Protocol
Among many means of data transferring between an MQL4 program and an external program, we choose file. Using a file does not require the development of additional libraries for MT 4. The method is rather universal, small-footprint, and simple in its realization. It has a number of disadvantages that can be easily eliminated and has no critical limitations.
MQL4 program can response to the changes in the file of variables only upon a certain event, namely: upon changing of the current price on the current currency pair.
We can initiate checking the file of variables from an external program at any time. We should select optimal time intervals for checking, in order not to overload the resources of our PC and not to occupy the file for a long period of time, since it can be simultaneously used by the MQL4 program. The changes in the external program are mainly made by man, i.e., not so quickly as by a computer, so it would be enough to check the file not frequently than once half second to track the situation. This parameter, of course, must be adjustable, and you should empirically choose the time interval for yourself.
During the attempt to open the file with the MQL4 program, it can be opened by the external program for writing. Therefore, we should foresee such a situation and realize several attempts to call to the file within one processing, in order not to waste our time waiting for a new price change. The same relates to the external program: If the file is used by the MQL4 program, you should take several attempts to call to the file by certain time intervals.
Adaptation to MQL4
Variables
int ExVH_VarCnt; string ExVH_FileName; string ExVH_Project; string ExVH_Title[]; string ExVH_Type[]; string ExVH_Status[]; string ExVH_Value[];
Functions for User
bool ExVH_Open(string FileName) { bool Result=true; ExVH_FileName=FileName; if (!ExVH_i_Load()) Result=false; if (Result) if (!ExVH_i_GetVarCnt()) Result=false; if (Result) ExVH_i_Disassemble(); return(Result); } int ExVH_Close() { ExVH_i_Assemble(); ExVH_i_Save(); } int ExVH_GetStatus(int Id) { if ((Id<1)||(Id>ExVH_VarCnt)) return(-1); else return(StrToInteger(ExVH_Status[Id-1])); } int ExVH_SetStatus(int Id, int Status) { int Result; if ((Id<1)||(Id>ExVH_VarCnt)) Result=-1; else { Result=1; ExVH_Status[Id-1]=Status; } return(Result); } string ExVH_GetValue(int Id) { if ((Id<1)||(Id>ExVH_VarCnt)) return("N/A"); else return(ExVH_Value[Id-1]); }
Function for Internal Use
bool ExVH_i_Load() { bool Result=true; ExVH_Project=""; int i=0; int FS=0; int handle; int Buf[]; handle=FileOpen(ExVH_FileName,FILE_BIN|FILE_READ); if(handle>0) { FS=FileSize(handle); ArrayResize(Buf,FS); while(!FileIsEnding(handle)) { Buf[i] = FileReadInteger(handle, CHAR_VALUE); i++; } FileClose(handle); string Str=""; for (i=0;i<FS;i++) Str=Str+CharToStr(Buf[i]); ExVH_Project=Str; } else Result=false; return(Result); } bool ExVH_i_Save() { bool Result=true; int handle=FileOpen(ExVH_FileName,FILE_BIN|FILE_WRITE); if(handle>0) { FileWriteString(handle,ExVH_Project,StringLen(ExVH_Project)); FileClose(handle); } else Result=false; return(Result); } bool ExVH_i_GetVarCnt() { bool Result=true; string Value=ExVH_i_GetVarValue("Var_Cnt"); if (Value=="N/A") Result=false; else ExVH_VarCnt=StrToInteger(Value); return(Result); } void ExVH_i_Disassemble() { int i; ArrayResize(ExVH_Title, ExVH_VarCnt); ArrayResize(ExVH_Type, ExVH_VarCnt); ArrayResize(ExVH_Status, ExVH_VarCnt); ArrayResize(ExVH_Value, ExVH_VarCnt); for (i=0;i<ExVH_VarCnt;i++) { ExVH_Title[i]=ExVH_i_GetVarValue("Var"+(i+1)+"_Title"); ExVH_Type[i]=ExVH_i_GetVarValue("Var"+(i+1)+"_Type"); ExVH_Status[i]=ExVH_i_GetVarValue("Var"+(i+1)+"_Status"); ExVH_Value[i]=ExVH_i_GetVarValue("Var"+(i+1)+"_Value"); } } void ExVH_i_Assemble() { ExVH_Project="[ExVH 1.0]\r\n\r\n"; ExVH_Project=ExVH_Project+"Var_Cnt="+ExVH_VarCnt+"\r\n\r\n"; int i; for (i=0;i<ExVH_VarCnt;i++) { ExVH_Project=ExVH_Project+"Var"+(i+1)+"_Title="+ExVH_Title[i]+"\r\n"; ExVH_Project=ExVH_Project+"Var"+(i+1)+"_Type="+ExVH_Type[i]+"\r\n"; ExVH_Project=ExVH_Project+"Var"+(i+1)+"_Status="+ExVH_Status[i]+"\r\n"; ExVH_Project=ExVH_Project+"Var"+(i+1)+"_Value="+ExVH_Value[i]+"\r\n\r\n"; } } string ExVH_i_GetVarValue(string VarName) { string Result="N/A"; int Start,Stop; VarName=VarName+"="; Start=StringFind(ExVH_Project,VarName,0); if (Start!=-1) { Start=Start+StringLen(VarName); Stop=StringFind(ExVH_Project,CharToStr('\n'),Start); if (Stop!=-1) { Stop=Stop-1; Result=StringSubstr(ExVH_Project,Start,Stop-Start); } } return(Result); }
Adaptation for C++
The project was written in C++ Builder 2007. The archive of source codes named ExVH_CPP.zip is attached to the article.
Let's realize a small demonstrative example that will show most possibilities.
Thus, let's create a test document:
[ExVH 1.0]
Var_Cnt=5
Var1_Title=Boolean test
Var1_Type=0
Var1_Status=2
Var1_Value=0
Var2_Title=String test
Var2_Type=1
Var2_Status=2
Var2_Value=Hello world!
Var3_Title=Integer test
Var3_Type=2
Var3_Status=0
Var3_Value=12345
Var4_Title=Double test
Var4_Type=3
Var4_Status=0
Var4_Value=123.45
Var5_Title=Action test
Var5_Type=5
Var5_Status=0
Var5_Value=0
Let's save the document as ExVH.evh in folder [MetaTrader]/experts/files/.
Signature [ExVH 1.0] allows us to recognize the document when opening the file and informs about the version of the document structure used. If the document structure changes, the signature must be changed, too. This will allow us to avoid mixing, considering that the document file extension will remain unchanged.
Var_Cnt=5. This record informs us about that the document contains 5 variables.
Then the records of the same type follow that contain the information of each variable. The records are made according to the above specifications.
VarX_Title=
VarX_Type=
VarX_Status=
VarX_Value=
Thus, we have two variables (Var1 and Var2) that must be read as soon as an action appears (Var5), and two variables (Var3 and Var4), the changes of which must be considered independently.
The MQL4 program must display the corresponding message on the screen as soon as changes are made in the variables.
Test MQL4 Code
int init() { return(0); } int deinit() { return(0); } int start() { if (!ExVH_Open("ExVH.evh")) return(0); // Checking for action status if (ExVH_GetStatus(5)==1) { Alert("Actioned!"); string VarValue; if (ExVH_GetValue(1)=="1") VarValue="true"; else VarValue="false"; // Boolean variable value Alert("Boolean test variable="+VarValue); // String variable value Alert("String test variable="+ExVH_GetValue(2)); ExVH_SetStatus(5,0); } // Integer variable value if (ExVH_GetStatus(3)==1) { Alert("Integer test variable has been changed. New value="+ExVH_GetValue(3)); ExVH_SetStatus(3,0); } // Double variable value if (ExVH_GetStatus(4)==1) { Alert("Double test variable has been changed. New value="+ExVH_GetValue(4)); ExVH_SetStatus(4,0); } ExVH_Close(); return(0); }
Launching and Testing
It doesn't matter what part of the complex is launched first. Let it be MetaTrader 4, in this case. Preliminarily copy file ExVH_Demo.mq4 into folder [MetaTrader]/experts/ and then start Terminal. Document ExVH.evh has already been written in the program code. Launch the EA. Nothing should happen since the EA will be waiting for changes in the file.
Launch ExVH.exe preliminarily installed on the PC.

General View of the Program
Open the 'Project' and select the 'Open' item…

Opening the Document
The program loads variables and now we can change them.
In the 'Status' column, two values are reflected: idle (waiting) and Changed by ExVH (changes were made in an external program). This is how the screen looks after we have activated our 'Action'.

General View of the Program with Activated 'Action'
Changes for different types of variables are made in different ways.
For a boolean variable, the program gives one of the two messages:

For 'Action' activation, it give this:

For all other types of variables, a unified separate form.

Form for Changing the Values of Variables
After a number of changes, the 'Alert' window in the terminal may appear as follows:

Example Message Box during the Program Operation
Any good idea may have its advantages and disadvantages.
• This method cannot be actively used for back-testing.
• Changing the parameters can contradict the logic of your EA. You should process the changes in variables very carefully. It's rather a feature than a disadvantage.
• Execution of commands is only possible on the arrival of a new tick.
The functionality of the system can be enhanced in various directions. The field of action is practically boundless.
• Changing parameters according to different scenarios. For example, for passwords, you can have a possibility to hide the information entered.
• Storage of the file enciphered to be able to hide passwords.
• You can change the data transferring protocol making it more reliable and secured.
We considered the ways of changing the values of internal variables in MQL programs. Now we have an opportunity to manage external and other variables without restarting the program.
Files Attached to the Article
File
Description
ExVH_Demo.mq4 Test EA ExVH_Project.zip Prepared project file ExVH_CPP.zip Source codes of the external program ExVH_Install.zip External program installer
Note. ExVH_Install was made using the demo version of the Advanced Installer program.
Translated from Russian by MetaQuotes Software Corp.
Original article: http://articles.mql4.com/ru/661
![]() A Trader's Assistant Based on Extended MACD Analysis
Script 'Trader's Assistant' helps you to make a decision on opening positions, based on the extended analysis of the MACD status for the last three bars in the real-time trading on any timeframe. It can also be used for back testing. |
![]() Idleness is the Stimulus to Progress, or How to Work with Graphics Interacively
An indicator for interactive working with trend lines, Fibo levels, icons manually imposed on a chart. It allows you to draw the colored zones of Fibo levels, shows the moments of the price crossing the trend line, manages the "Price label" object. |
| Previous | Next |
Non standard workings add confusion to already confused environment ;)
Is not all 'knife n forking' done already? Live EA not item to be messed with when 'live'. If soooo desparate to fiddle with internals then edit+recompile Why? cuz EA bug. So fix it!
stitch + greg give very fair replys.
can't we do it much simpler? just create global vars for all parameters. you need them anyhow, to save the status during the period when you computer/network/etc is off, to restore/resume the trading and the analysis. then global vars can be easily edited directly from the terminal program, in case you stringently want to change the parameters of your expert, and a restart/reinit can not be carried on in that moment. On the other words, the idea is a bit odd... Your expert must be enough clever to handle interruption without losing its mind and tracks... i.e. without forgetting what he was doing and what orders he handled before interruption. That is because interruptions can occur in any moment, independent of you or your broker (market, net line, etc), and one EA forgetting old open orders or using strange variables after restart wont be so suitable to handle all your ecconomies.... The expert must be well written, and such an expert would probably handle any restart situation, and can be re-init in any moment. why we would need to change it's parameters without restart? It wouldn't be much simpler just to restart it?