Hi folks,
One of MetaTrader numerous features is the built-in DDE server.
The MetaTrader DDE server enables you to communicate with the terminal from your Visual basic or Excel application (or whatever the tool you use to write your applications) and get a real time data.
What's the DDE?
DDE stands for Dynamic Data Exchange. DDE enables two running applications to share the same data.
The program who we capture the data from called "DDE server" . And the program who captures the data from server called "DDE client".
For example, in our program today we will capture the data from MetaTrader and show it in Visual basic, whenever the MetaTrader data changes, the Visual basic data changes accordingly.
Our DDE client:
There's a DDE simple has shipped with MetaTrader wrote in Excel (DDE-Sample.xls).
I have converted it to Visual Basic with some improvements (Ex: a full pairs list) and I'll improve it upon your request.
Download (Executable and source code):
MM.zip (8.67 KB)
How to run the program:
1- From the MetaTrader Tools menu choose Options and in the Server tab check Enable DDE server option (Figure 1).
2- Run MM.exe (Figure 2).
3- Choose the currency pair you want to monitor (Figure 3).
Hi folks,
We have the tool to send keyboard keys to MetaTrader here: Send Keyboard keys to MetaTrader!
Actually this scripts sends keyboard strokes not only to MetaTrader from your MQL4 code but to any active window.
Anyway, we have to have the tool to Get keyboard keys to MetaTrader.
You can assign a hot key to your MQL4 program (give this article a look: http://www.metatrader.info/node/162) but this key will only able to run your program.
What if you want to assign a hot key to a function in your program; for example if the user pressed CTRL+0 close all the opening trades or when he presses CTRL+5 increase the stop loss value +5 pips. Are you dreaming? no! here's the code of your dream!
Our indicator today will not do anything. It just will tell us that the user has pressed the CTRL + 0 keys. It's a sample of a very wide range of usage.
Let's give the code a look:
//+------------------------------------------------------------------+
//| Keyboard.mq4 |
//| Codersguru |
//| http://www.meatrader.info |
//+------------------------------------------------------------------+
#property copyright "Codersguru"
#property link "http://www.meatrader.info"
#property indicator_chart_window
#import "user32.dll"
bool GetAsyncKeyState(int nVirtKey);
#import
#define KEYEVENTF_EXTENDEDKEY 0x0001
#define KEYEVENTF_KEYUP 0x0002
#define VK_0 48
#define VK_1 49
#define VK_2 50
#define VK_3 51
#define VK_4 52
#define VK_5 53
#define VK_6 54
#define VK_7 55
#define VK_8 56
#define VK_9 57
#define VK_A 65
#define VK_B 66
#define VK_C 67
#define VK_D 68
#define VK_E 69
#define VK_F 70
#define VK_G 71
#define VK_H 72
#define VK_I 73
#define VK_J 74
#define VK_K 75
#define VK_L 76
#define VK_M 77
#define VK_N 78
#define VK_O 79
#define VK_P 80
#define VK_Q 81
#define VK_R 82
#define VK_S 83
#define VK_T 84
#define VK_U 85
#define VK_V 86
#define VK_W 87
#define VK_X 88
#define VK_Y 89
#define VK_Z 90
#define VK_LBUTTON 1 //Left mouse button
#define VK_RBUTTON 2 //Right mouse button
#define VK_CANCEL 3 //Control-break processing
#define VK_MBUTTON 4 //Middle mouse button (three-button mouse)
#define VK_BACK 8 //BACKSPACE key
#define VK_TAB 9 //TAB key
#define VK_CLEAR 12 //CLEAR key
#define VK_RETURN 13 //ENTER key
#define VK_SHIFT 16 //SHIFT key
#define VK_CONTROL 17 //CTRL key
#define VK_MENU 18 //ALT key
#define VK_PAUSE 19 //PAUSE key
#define VK_CAPITAL 20 //CAPS LOCK key
#define VK_ESCAPE 27 //ESC key
#define VK_SPACE 32 //SPACEBAR
#define VK_PRIOR 33 //PAGE UP key
#define VK_NEXT 34 //PAGE DOWN key
#define VK_END 35 //END key
#define VK_HOME 36 //HOME key
#define VK_LEFT 37 //LEFT ARROW key
#define VK_UP 38 //UP ARROW key
#define VK_RIGHT 39 //RIGHT ARROW key
#define VK_DOWN 40 //DOWN ARROW key
#define VK_PRINT 42 //PRINT key
#define VK_SNAPSHOT 44 //PRINT SCREEN key
#define VK_INSERT 45 //INS key
#define VK_DELETE 46 //DEL key
#define VK_HELP 47 //HELP key
#define VK_LWIN 91 //Left Windows key (Microsoft® Natural® keyboard)
#define VK_RWIN 92 //Right Windows key (Natural keyboard)
#define VK_APPS 93 //Applications key (Natural keyboard)
#define VK_SLEEP 95 //Computer Sleep key
#define VK_NUMPAD0 96 //Numeric keypad 0 key
#define VK_NUMPAD1 97 //Numeric keypad 1 key
#define VK_NUMPAD2 98 //Numeric keypad 2 key
#define VK_NUMPAD3 99 //Numeric keypad 3 key
#define VK_NUMPAD4 100 //Numeric keypad 4 key
#define VK_NUMPAD5 101 //Numeric keypad 5 key
#define VK_NUMPAD6 102 //Numeric keypad 6 key
#define VK_NUMPAD7 103 //Numeric keypad 7 key
#define VK_NUMPAD8 104 //Numeric keypad 8 key
#define VK_NUMPAD9 105 //Numeric keypad 9 key
#define VK_MULTIPLY 106 //Multiply key
#define VK_ADD 107 //Add key
#define VK_SEPARATOR 108 //Separator key
#define VK_SUBTRACT 109 //Subtract key
#define VK_DECIMAL 110 //Decimal key
#define VK_DIVIDE 111 //Divide key
#define VK_F1 112 //F1 key
#define VK_F2 113 //F2 key
#define VK_F3 114 //F3 key
#define VK_F4 115 //F4 key
#define VK_F5 116 //F5 key
#define VK_F6 117 //F6 key
#define VK_F7 118 //F7 key
#define VK_F8 119 //F8 key
#define VK_F9 120 //F9 key
#define VK_F10 121 //F10 key
#define VK_F11 122 //F11 key
#define VK_F12 123 //F12 key
#define VK_F13 124 //F13 key
#define VK_NUMLOCK 144 //NUM LOCK key
#define VK_SCROLL 145 //SCROLL LOCK key
#define VK_LSHIFT 160 //Left SHIFT key
#define VK_RSHIFT 161 //Right SHIFT key
#define VK_LCONTROL 162 //Left CONTROL key
#define VK_RCONTROL 163 //Right CONTROL key
#define VK_LMENU 164 //Left MENU key
#define VK_RMENU 165 //Right MENU key
int start()
{
if (GetAsyncKeyState(VK_LCONTROL) && GetAsyncKeyState(VK_0))
Alert ("The 'ctrl+0' keys have been pressed, do you want me to do something?");
return(0);
}
The most of the code is very like the code of Send Keyboard keys to MetaTrader!, the new function is GetAsyncKeyState Which take the key you want to monitor (to know was it pressed or not). and returns true if has been pressed and false otherwise.
So, you can use this line of code as the example above (ctrl+0 combination) to execute any function you want in your indicator or expert advisor.
Note: You can not use this code in your script because the scripts run once and not hosted on the MetaTrader chart like the indicators and the expert advisors.
Have fun!
Coders' Guru
I would like to draw attention of the community for a real need in creating an expert for exact duplication of trades made on an account by expert or human to another account where thae expert is attached.
Thus wwe need two expert:
1. For parent account to put all the orders into txt file.
2. For replica account to read this txt files and trade.
Would be grateful if the comunity could work on this!
Serggry
Hi folks,
A lot of people asked me and MetaQoutes for a better file handling functions that's why I'm writing this article/tool.
The problem of the normal file handling functions was the limited directories you can use for your output file:
One of annoying feature of MQL4 file functions is the directories limitation; you can't work with files that outside one of these three directories:
Terminal_Install_Dir/HISTORY/<current broker>
Works with FileOpenHistory() function.
Terminal_Install_Dir/EXPERTS/FILES
The common directory for file saving and opening.
Terminal_Install_Dir/TESTER/FILES
The directory of testing files.
MetaTrader thinks it's safer to limit the directories you can access from the normal MQL4 program and give you the ability to write your MQL4 extension (dll) to do what do you want.
That's why our tool today is useful because it enables you to work with files outside the limited directories of MQL4.
Please download the full package which includes:
The source code and the compiled version (dll) of the mtguru1.dll which is a MetaTrader extension that wrote in Visual c++.
gFiles.mqh is the include file which have the declarations of the functions inside the dll.
FilesDemo.mq4 is a demo indicator to show you how to use the dll.
Extract all of the contain of zip file to an empty folder.
Copy the mtguru1.dll to "MetaTrader 4\experts\libraries" path.
Copy FilesDemo.mq4 to "MetaTrader 4\experts\indicators" path and compile it.
Copy gFiles.mqh to "MetaTrader 4\experts\include".
Load FilesDemo.mq4from your Indicators - don't forget to enable "Allow DLL Import"
This is a list of the functions the current version of the mtguru1.dll has:
int gFileOpen(string file_name,int mode);
bool gFileWrite(int handle,string data);
bool gFileClose(int handle);
string gFileRead(int handle,int length=0);
void gFileSeek(int handle,int offset, int mode);
bool gFileDelete(string file_name);
int gFileSize(int handle);
int gFileTell(int handle);
bool gFileFlush(int handle);
bool gFileCopy(string source,string distance,bool IfExists);
bool gFileMove(string source,string distance);
They are very like the normal MQL4 functions but you can write in any directory you want. Please play with them and tell me your comment!
Enjoy!
Coders' Guru
I found this little script very usefull for those of us spending a lot of hours at the LCD ;)You need your POP3 mail account configured at Tools > Email.Also an email account with SMS notification service (you get SMS when new email comes).Here goes the code: extern double alert_up = 0;
extern double alert_down = 0;
int start()
{
int digits=MarketInfo(Symbol(),MODE_DIGITS);
if ( alert_up > 0 )
{
if ( Bid >= alert_up )
{
SendMail( Symbol()+" UP "+NormalizeDouble(alert_up,digits), ".");
alert_up = 0;
}
}
if ( alert_down > 0 )
{
if ( Bid <= alert_down )
{
SendMail( Symbol()+" DOWN "+NormalizeDouble(alert_down,digits), ".");
alert_down = 0;
}
}
return(0);
} Have fun! ;)
Hi folks,
One of forex-tsd forum members asked me for a price of code to check if last [closed] trade was a win or lose, That's why I've wrote this script (you can copy-paste the function you want to the expert advisor you are wiring).
The script has 5 self-explained functions:
This is the function my friend has asked for, it returns the last closed trade profit or loss.
This function returns the biggest profit of the closed trades.
This function returns the biggest loss of the closed trades.
This function returns the number of profit trades of the closed trades.
This function returns the number of loss trades of the closed trades.
Hi folks,
I hope you find the tool of today a useful one.
Our tool today is how to send keyboard strokes to MetaTrader from your MQL4 code.
For example: You want to open the Option window from your script (CTRL+O). You want to shutdown MetaTrader (ALT+F4).
Or you maybe want to run an expert advisor or another script from your code by assigning a hotkey to that program and call it from our tool.
The scenarios are unlimited!
Our script has two only functions:
Use this function to send a key stroke to MetaTrader.
The first parameter is the key you want to send. You will find the list of all the keyboard keys in the top of the script.
The second parameter is an optional one. And you set it to true if you want to send the key and release it immediately.
Releasing the key is very important. Just imagine you have clicked the CTRL key and didn't release it. Every keystroke after that will be combined with CTRL key. So, don't forget to release every key you have sent.
Use this function to release the key you have sent if you didn't release it already using the second parameter of SendKey.
I hope you enjoy the tool and I'm waiting the scenarios you used the tool in.
Hi folks,
I have a tool today that I hope it's a useful for you as it for me!
MQL4 enable us easily to write to csv (Comma-separated values) files. But it's hard to write script that handling reading from csv files and it's hard to make it a fast operation (Just imagine you have a csv file with 100000 record).
That's why I've got a lot of requests asking my to write a csv reader dll in c++
Our dll today have 4 functions:
Use this function to get how many records in the csv file. You have to pass to it the path and the file name of the csv file.
The function will return the count of the records or -1 if there's an error!
Example:
Alert(gGetRecordsCount("C:\\demo.CSV"));
Use function to get a record (line) from a csv file. Just pass to it the path and file name of the csv file and the record (line) number.
This function returns the record as string in success. If it couldn't open the file it'll return "NF" and "NL" if the record is empty and "N/A" if the record not found.
Example:
Alert(gGetRecord("C:\\demo.CSV",1));
Use this function to get how many fields (columns) the csv has. Pass to the function the path and file name of the csv file and the delimiter character that separate the fields.
The function will return the count of the fields or -1 if there's an error!
Example:
Alert(gGetFieldsCount("C:\\demo.CSV",','));
Use this function to get a cell in a specified record and specified field in the csv file. Just pass to it the path and file name of the csv file, the record number, the field number and the delimiter character that separate the fields.
This function returns the cell as string in success. If it couldn't open the file it'll return "NF" and "NL" if the record is empty and "N/A" if the record not found.
Example:
Alert(gGetCell("C:\\demo.CSV",1,1,','));
I hope to see your comment and what's else you want me to add to this tool!
Hi folks,
I'm receiving tens of messages everyday -in the forum- asking me about how to compile the Expert Advisors, Indicators, Script, and Libraries?How to know the kind of the MQL4 Program?
I automatically answer:
1- Download the program (.mq4)
2- Copy it to the /experts folder if it was an expert advisor, and to the experts/indicators folder if it was an indicator, and to experts/scripts if it was a script and it was a library copy it to experts/libraries folder.
3- Open the file in MetaEditor (by double clicking it).
4- Hit F5 to compile the program.
We all were novices and I'm not bored from the answers, but it must be an easier method to compile the MQL4 program and tell the trader the type of the program (expert, indicator, script, or library).
Ok fans! That's EMC.
Saturday and Sunday are very boring to any forex lover, but today I opened my Visual Basic and played with it to create a little tool for you (and me) that easily compile the MQL4 programs.
The first time you download the program you have to open it to set the options of the program (Figure 1); these are the options available in the current version:
Figure 1 - EMC Options
Choose this option if you want the EMC to open the mq4 file in MetaEditor after compiling it.
Choose this option if you want the EMC to compile the mq4 file only.
Note: Whether you have chosen Compile & open in MetaEditor or Complie only the EMC will copy the mq4 file to the right MetaTrader folder (/experts folder if it was expert, /indicators folder if it was indictor, /scripts folder if it was script and /libraries folder if it was library).
In must case you download the mq4 program to your desktop or any other folder outside the MetaTrader folders, you can check this option to delete this file after coping it to the MetaTrader folder (experts folder if it was expert, indicators folder if it was indictor etc).
Note: If you compile an mq4 program inside MetaTrader folder this option will not work because it's not logical to delete the mq4 file from the MetaTrader folder.
Check this option if you want EMC to tell you the type of the complied file (expert, indicator, script, or library) (Figure 2).
Figure 2
Click this button to uninstall the Compile context menu (Figure 3). You can still open the EMC to install the menu again or you can drag the mq4 file to the EMC program icon.
Click this button to save the option you have set.
Click this button to exit the program without saving the options.
To compile any file has the extension .mq4 simple right click it and you will find the menu item Compile (Figure 3), just click it and that's all.
Figure 3 - Use EMC
What is the version of visual Basic do you use for Easy MQL4 Compiler ??
Hi folks,
The most of my time goes to the navigating between MetaTrader and Forex-tsd forum. I'm visiting the forum to view if there are new posts or not.
With my tool today I will save my time and my concentration. I just click the Forex-tsd script and it will tell me if there are new posts or not in the forum.
I hope you find it useful too.
Hi folks,
I've got a lot of requests from my friends the members of forex-tsd forum asking me to make a better version of MetaTrader FTP sending.
I hope you find this tool useful and better than the SendFTP() MQL4 function!
Our dll today have 5 functions:
You have to use this function to connect to the FTP server before uploading or downloading files to it.
The first parameter is the FTP server, the second parameter is the User name, the third parameter is the Password and the fourth parameter is the directory path on the ftp server you want to upload or download from.
Note: If you want to upload/download from the root of the ftp server you have to set path parameter to "ROOT".
The function will return a string, it returns the error message if there's any or it return "Connected" if there's no error!
Example:
string result = gConnect("FTP SERVER","USER NAME","PASSWORD","ROOT");
Now you have a connection to the FTP server. You can upload the file you want to the server using this function.
The only parameter of this function is the path and the name of the file you want to upload.
Example:
string result = gSendFile("C:\\image.jpg");
If you want to download a file from the FTP server you have to use this function (You have to connect to the server before using gSendFile and gGetFile functions).
The first parameter is the name of the file on the FTP server you want to download. The second parameter is the path and file name you want to save the downloaded file to.
Example:
string result = gGetFile("image.jpg","C:\\image.jpg");
When you finish your work with the FTP sever you have to use this function to close the connection to the FTP server.
Example:
string result = gClose();
To make the life easier I've added this function to connect and upload a file to the FTP server then close the connection.
So, You can use this function alone without gConnect and gClose.
The first parameter is the FTP server, the second parameter is the User name, the third parameter is the Password,the fourth parameter is the directory path on the ftp server you want to upload or download from and the fifth parameter is the path of the file you want to upload.
Example:
string result = gConnect("FTP SERVER","USER NAME","PASSWORD","ROOT","C:\\image.jpg");
Hi folks,
Any trader knows the importance of the Alerts in any MQL4 program.
For instance: The 10 days moving average has been crossed the 80 days moving average upward! You have to buy now! You have to be alert!
MetaTrader removed one of my favorite alerts method SpeechText! But don't worry I'll write a program to make it available again!
What about the MSN Style Popup alert? Yes!
My new alert method is a MSN Style pop alert.

The package contains 4 files and you have to put each of them in the proper folder:
Pop.exe and Popup.dll
You have to copy them to C: drive.
Pop.mqh
Copy this file to /experts/include folder.
Pop_Demo.mq4
This is a script to demonstrate how to use the program. Copy it to /experts/script folder.
Note: If you want to copy Pop.exe and Popup.dll to another folder and not the C: root you have to change the directory in the code of calling the Pop function.
The script Pop_Demo.mq4 is an example of using the program.
To call the alert you use this line:
pop ( string msg , string installed_dir)
For example: if you installed Pop.exe and Popup.dll to C: drive and want to pop the text “Hi world!” you can use this line of code in your mql4 program:
pop (“Hi world!”, "c:");
Hope you enjoy the tool and tell me your comment!
Hi folks,
No more headache trying to send emails from MQL4 and MetaTrader! Our tool today will send emails anywhere (hotmail, gmail, POP3 anywhere). Our tool today can send Attachments!
Our dll using your current default email profile installed in your PC. If you want to change it just execute your outlook and go to Tools -> Accounts and check the default mail profile you want to use sending your emails.
Don't shut down your Outlook yet.
You have to go to Tools -> Options menu and from the Options window choose Security tab then uncheck this option "Warn me when other applications try to send mail as me" (Figure 1).
Our dll has only one function:
This is the only function available in our dll and the only function I guess you need!
These are the parameters (all of them are required) of the gSendMail function:
profile:
The mail profile you are going to use, set it to "default" and the dll will use your default mail profile.
to:
The email address you want to send the email to.
subject:
The subject of the message.
body:
The message body.
attach:
The path + file name of the file you want to attach to your email
attach_title:
The name of the file as it appear to the receiver.
Note: To get a working example please download the SendMail.mq4 script!
Hope you find it useful and hope to hear your comments!
Hi folks,
Scenario 1:
The EURUSD went up, I want to tell the boss. What if the MetaTrader can open my email client!
Scenario 1:
The EURUSD went down. Could MetaTrader open the notepad to write a piece of note.
If you are a lazy person like me, or you have more useful ideas (scenarios) about running applications from MetaTrader!
Running a program from MetaTrader is not a hard thing anymore.
Just you this library.
And enjoy with the Shell function:
use this function to run any program you want from your MQL code.
FullPath (string) the full path and the file name
Parameters (string) any parameters you want to pass to the program
(int) the handle of the program in success and -1 in error
int res = Shell ("c:\\window\\notepad.exe", "");
Hi folks,
Welcome to a new MetaTrader tool! I hope you find it useful.
I was in my office yesterday till the 3 AM waiting a my expert advisor to open a position. And when my wife phoned me to return home I forced to Shut Down my computer.
When I returned to the office today morning and gave the chart a look I cried the trend I lost and the profit I didn't get.
No more wife calls any more , not more Shut Downs before the trends.
Now you can use this dll to Shut Down the computer at the event you want.
For example after the Expert Advisor opens a trade or at a specific time.
I hope you enjoy it.
Hi folks,
A lot of MetaTrader fans complain because the removal of SpeechText function from MQL4 langauge (The function has been omitted in Build 188 (12 Jan 2006).
If you one of SpeechText lover just download this dll:
Setup:
1- Extract the "speak.dll" to "MetaTrader 4\experts\libraries" path.
2- Extract "SpeakDemo.mq4" to "MetaTrader 4\experts\scripts" path and compile it.
3- Extract "gSpeak.mqh" to "MetaTrader 4\experts\include".
4- Load SpeakDem from your Scripts - don't forget to enbable "Allow DLL Import"
5- Enjoy.
Hi folks,
I want to thank you all because your interest in my SpeechText dll.
Upon your requests I have added these extra options:
Now you can set the volume of the voice (0 : -100).
Set the rate of the voice (-10 : 10).
Set the pitch of the voice ( -50 : 50).
That's beside the original function:
Speak the text.
Coders' guru