How to create automated Screenshots in MQL4

When I started with trading, I was told to create a trading plan and a trading diary. The diary was neccesary to write down the reasons for a trade, the open price and the closing price.

With automated trading systems for Metatrader4 we do have all these data in the journal – and we can even get a little more insight without much effort.

MQL4 comes with predefined methods for a lot of useful things. One of these things is the ability to create a screenshot.

This is very easy to do, the only thing you need to do it is a name and the resolution of your screen.

For example my netbook screen resolution is 1024 * 600. So that was easy. The name can be any kind of text.

To create your screenshot, you can now call the function WindowScreenShot() that is already integrated into MQL4.

The description in the MQL4 language reference is very detailed, but we will make it very easy.

Now we have to tell the MQL4 System what it should do and when it should do it.

To do this we start with the Word WindowScreenShot followed by a round opening bracket.
This is a function, so it is case sensitive. Please do not forget the capital letters. The first parameter is the file name.

So after the round opening bracket we put in the name we want for our screenshot file followed by the extension “.gif”. That gif extension will tell Windows that it is an image file.

You can use any kind of text in quotation marks for the file name. Even calculated values are possible if you convert them to a string value first.

The next parameter is the width of the screen. For my netbook that is 1024. And the third parameter is the height of 600 pixels.

If you use a bigger screen, you can use your screen settings for a better resolution, but that will result in a bigger file.

To separate the filename, the width and the height, we use a comma. And to tell the compiler that we are finished, we put a closing round bracket and a semicolon at the end of the line.

So our complete command would be a single line of code.

WindowScreenShot (“Screenshotname”,1024,600);

If you want to see the example, you will find it in the show notes.

Okay, now we have told Metatrader what to do. The next question is: when to do it? To keep things simple we will use an event, that is already predefined. It is called OnInit().

The event OnInit() is the first thing that happens when you drop your Expert Advisor on a chart.

Metatrader will try to do everything that is within the curly brackets and it will tell the rest of the program if that was successful.

It does this with the line


And all we have to do ist to put our code line after the opening curly bracket and we are done.

Now the complete program is finished:

int OnInit()
 WindowScreenShot ("Screenshotname.gif",1024,600);

Okay, now we have created a 100 percent fully functional Expert Advisor, that will run every time you drop it on the chart. You can find the compiled version and the source code in the show notes. You can drop the ex4 file on any chart in Metatrader4 and it will create a screenshot in the files folder of your Metatrader installation.

And you can load the mq4 file in your Metaeditor (simply press F4 in Metatrader) and adjust it to your own needs.

Imagine, what you can do with your own modular system and these precoded, ready to use modules.

I think, even if you are a discretionary trader, 5 minutes a day is well invested to improve your trading experience with MQL4 and Metatrader.

What do you think?

MQL4 Tutorial searches: