Metatrader 101: How to Set Up Push Notifications in Metatrader

Push notifications in Metatrader can be a great tool – you can get alerts to your smart phone on the move and so always be in touch with what’s going on. Setting it up is fairly straight-forward – here’s how to do it.

1. Firstly you need to be aware of what a push notification is: it’s a message which is sent to the Metatrader app on your smart phone. So in order for this to work you need to have a smart phone on which you can install the Metatrader app. Go to the app store and install the Metatrader 4 app in the usual way.

2. Once you have done this you need to get your Metatrader ID. This is a unique ID that identifies your app installation on your phone. You can find it under the settings icon on your app. It’s going to be an eight digit/letter code that you need to make note of.

3. Next you need to go to your computer version of Metatrader which is going to be sending out the notifications. Go to the Tools | Options menu option to bring up the Options dialog box. Navigate to the Notifications tab.

4. Click on Enable Push Notifications and then fill in your Metatrader ID from step 2. Click on the Test button to try it out and you should get a message appear almost immediately on your phone.

5. That’s basically it! Notifications aren’t always immediate as they depend on the network over which they’re travelling but they’re usually pretty fast.

Metatrader 101: How to Set-up E-mail Alerts on Mt4

E-mail alerts from Metatrader can be a very useful tool. Indeed our own Mt4 Alert Indicators rely on it (and also phone push notifications) for sending information to users when they are away from their desks. But how does one set it up? This post describes the process

1. From within Metatrader select the Tools | Options menu option which will bring up the Options dialog box.

2. There are many tabs on this, select the Email tab and click the Enable tick box at the top in order to activate the various fields.

3. The way that it works is that you need a “relay” e-mail account which is used to send the messages to any other account that you want. By far the easiest way to do this is to set up a new gmail account. So you’ll need to have a new gmail e-mail address together with it’s password. For the purposes of this article lets assume that it’s called relay_address@gmail.com and that you eventually want to send these e-mails on to some destination address destination_address@anything.com. I’ve put anything.com as part of the destination address to emphasise that it doesn’t need to be a gmail address (though of course it can be if you want it to be).

Once you’ve created this gmail address then fill in the fields below like this:

The SMTP password is the password for your e-mail address. You can see that the SMTP login and the From fields are the same as the alert e-mail is going to come from this relay address and to be sent to your destination_address@anything.com address.

4. Now click on the Test button and look at the Journal tab closely to see what it says.If it says:

Mail: ‘Test message’ has been sent

or words to that effect then you know that it’s all working and you can go and check in your destination e-mail box for your message. You’ve now finished the set-up and can go to step 6

5. If on the other hand you get the message

Mail: login to smtp.gmail.com failed

then (assuming that you’ve correctly entered your account details in the dialog box) you’ve got one more step to do. Google have security settings for their accounts which by default restricts access from non-Google accounts so you need to change this setting. Go to your Google Account and you need to find the Apps with account access section (this changes location from time to time so you will need to navigate around until you find it). Switch on the less secure access part.

Once you’ve done this try your Test message again. You should now find that it all works.

6. That should be it. You shouldn’t ever need to access your relay gmail account again if you don’t want to and it will faithfully keep forwarding your messages on for you. Now that you’ve configured all this, any e-mail notifications that your Mt4 indicators or EA’s send will correctly work.

Metatrader 101: Installing a DLL Library to your Mt4 Installation

This is the one in a series of Metatrader 101 posts, partly so that I can have something to give to clients who are new to Mt4. It will cover the basics so people can get up and running quickly.

 

Some more advanced Metatrader code that you’ll come across will require a DLL (a Windows library file) to be installed as well as the actual .ex4 or .mq4 file. This is actually just as easy as installing any Metatrader code. Here’s how to do it.

 

1. A DLL file will have a .dll name tag to it so you’ll know that it needs to be treated differently from other Metatrader files.

 

2. Unfortunately, the location on your hard drive of your Mt4 software is rather complicated as it’s split up into two locations. The bit where you need to copy your new software to is hidden away deep within the AppData section and you won’t easily find it by exploring the various folders. To help you with this there is a menu option which will open the location for you. From your Mt4 software go to File | Open Data Folder in order to bring up Windows Explorer opened at the correct folder location.

 

The folders within the MQL4 main folder

 

3.  You’ll see an MQL4 folder which you should open to reveal a series of sub-folders (see image above). Unlike for .mq4 or .ex4 files, DLL’s need to go in the Libraries folder so navigate there and drop the file in the folder.

 

4. That’s basically it! Unlike for .mq4 or .ex4 files there’s no need to refresh or restart your Mt4 program explicitly for the DLL though if you’ve installed some code which uses the DLL then you’ll need to refresh or restart for that part. See the instruction on installing new Mt4 code for details on how to install mq4 or .ex4 files.

 

Importing Back-test Data in Metatrader4

Back-testing with Metatrader4 can be a bit tricky for the unwary as there are some quirks that you need to be aware of and if you’re not careful you can end up back-testing with random data rather than the good quality data that you thought you were. In this blog post I go through how to ensure that your back-test is what you think it is. I also walk you through the necessary steps for importing and converting your data to different time-frames. This is particularly relevant if you want to use good quality back-test data of your own and are worried about it being “contaminated” by data from other sources. Incidentally, you may wish to take a look at the Snapdragon Historic Data Service as a good source of clean historic data for back-testing.

Part 1 – Clearing Out Old History Data

The first thing to be aware of is that the data that is used in the Mt4 back-test is stored in the history folder. You can look at this folder by going to the File | Open Data Folder menu option which will bring up the Windows Explorer open at the root folder for your installation. If you then open the history folder and select your broker folder you will be able to see all the history files which will have names like EURUSD1.hst. These are the same files that you can see when you select the  File | Open offline menu option instead, but in that instance they are all listed in a dialog box and you can’t delete them. So that’s where the data is stored. What you also need to know is that when Mt4 closes down, it writes out all the history data to this folder. So if you want to clear all your history data for example then you need to do it in the following way:

1. Open up the folder in Windows Explorer by going to File | Open Data Folder
2. Now close down Mt4 (but leave Windows Explorer open)
3. Now to delete all the data for EURUSD say, navigate down to the appropriate files and delete all the EURUSD*.hst files.

If you’re importing your own data for a back-test then I recommend that you first clear out the old data using this method.

Part 2 – Importing Your Own Data

To import some data of your own into the history folder then this is done using the History Center dialog box, which is brought up using the Tools | History Center menu option (or just the F2 key). Once you bring up this dialog box you will see something like this

 

 

You can see down the left-hand side are listed all the markets and by drilling down into them you can see sub-folders for each of the available time-frames. One important thing to note is that in order to select one of these sub-folders you need to double-click on it – just selecting it is not enough. If you look in the title bar of the dialog box it shows you which folder is currently active.

 

A really important fact to note is that when you double-click on a folder Mt4 “helpfully” goes and populates it with some data of its own. So even after you’ve deleted all the history data for a pair as described in Part 1 it will still go and put some more in that it’s downloaded from its servers. The best way around this issue is to double-click on the M1 folder, let it populate the list with its own data and then to delete it again. To do this, highlight the top element, hold down the shift key and then scroll to the bottom and click on the last element. This should now highlight all the elements in the list. Now click on the Delete button to delete them all. This deletes all the entries in the list.

 

The next thing to do is to import your own data. You do this by clicking the Import button and then navigating to the file that you want to import. Once you’ve imported your own data, Mt4 won’t overwrite it, but it will still add stuff beyond the end of your data. There’s not a lot you can do about this but as long as you know what date your data goes up to and are careful only to test up to that point then it should be OK. You should now have your own imported data in the list.

You should now see your data in the dialog box. Finally to make sure that it’s saved to the history folder you should close down Mt4.

Part 3 – Populating Different Time-frames

Now, when running back-tests, the Strategy Tester sometimes pulls in data from different time-frames in order to construct the bar data that you are back-testing. The best way to ensure that you are only using your own data is to populate all the time-frames with your own data. Even if you’re only back-testing one time-frame it is still recommended that you populate all the others just to be on the safe side. Now you may only have 1 minute data so you how do you create M5, M15 etc from this? The answer is to use the Period Converter script. Below are the necessary steps for doing this

 

1. The first thing that you need to do is to change the Max bars in history and Max bars in chart options in your Mt4 to ensure that you can load in all your data without any problems. Go to Tools | Options and then select the Charts tab from the dialog box. Set the Max bars in history and the Max bars in chart options to as big a number as you can fit in there: I just keep putting 9’s in there until it won’t take any more.

 

2. Next you need to go to the  File | Open offline menu option which will bring up the dialog box showing all the history data. You should see the file for the symbol that you’ve imported already, so your EURUSD,M1 file for example. Select this and click on the Open button. This will bring up a chart and because of your Max bars settings it will load all that it can into the chart.

 

3. Next we will need to generate the other time-frames. To do this we can use the Period Converter script that comes with your Mt4 installation. Drop the script into the chart and set the Period multiplier factor to 5 first of all to generate the 5 minute data. Note that you won’t see anything happen on the chart but in the Experts tab it will say X records written.

 

4. Now repeat this process for all the time-frames that you are likely to need, so 15, 30, 60, 240 etc. I usually don’t bother with the daily if I’m not testing a daily strategy but will do all the intraday time-frames.

 

5. Once you’ve done this then if you bring up the Offline Chart dialog (File | Open offline) then you should see all the different time-frames now listed there.
6. Finally, remember to set your two Max bars settings back to something moderate, otherwise it will slow your Mt4 right down.
7. The last step is to close down Mt4. This will write out all the history data to the folder so it’s now backed up.

Part 4 – Using Your Data

That should be it! You have now created all the history data that you want populated with your own data source and you can go ahead and do your back-tests with the Strategy Tester. Just make sure that you specify the end date from the test so it stops at or before your own data does. Otherwise you’ll be using some data that Mt4 has provided.

 

Metatrader 101: How To Use Offline Charts

This is the one in a series of Metatrader 101 posts, partly so that I can have something to give to clients who are new to Mt4. It will cover the basics so people can get up and running quickly.

In Metatrader you only have a limited choice of charts: bar, candle or line and a limited choice of time-frames. But if you want to use different time-frames more exotic charts such as Renko, Point and Figure etc, is this possible? The answer is yes using what are called Offline Charts.

 

The basic principle is that you run a script on a “driver” chart which generates the necessary data for your new chart whether it’s something like a 3 minute chart or a Renko chart. This generated data is stored in the historic data folder in real-time as the driver chart updates. Then to see this generated chart you go to the File | Open Offline chart menu which shows all the historic data files. Scroll down to find your generated chart and select it and voila!



Step By Step

Here are some step by step instructions. We’ll use the PeriodConverter script to illustrate the process as this script comes with every Mt4 installation. We’ll use this script to create a 3 minute chart.

 

1. The first thing you need to do is to make sure that you have Autotrading enabled otherwise you’ll spend a lot of time scratching your head as to why it’s not working. Look for the  Autotrading icon in your toolbar at the top and make sure that it shows a green triangle not a red square.

 

 

 

 

 

 

2. For this example your driver market should be an M1 chart for the market that you’re interested in. We’ll use EURUSD as an illustration. So first bring up an M1 EURUSD chart. It doesn’t need any indicators etc. and it doesn’t matter what it looks like. You just need it to be up. The important thing to remember is that you need to keep this driver chart up the whole time that you are using the offline chart, otherwise the offline chart will stop updating.

 

 

 

3. Next find the PerdiodConverter script in the scripts section and drop it in your M1 EURUSD driver chart. The input dialog box for this script will appear with one input variable on the “Inputs” tab which is the Period multiplier factor. In our case we want an M3 chart so we want a multiplier factor of 3. Enter that in the input field and click OK. 

 

 

 

3. At first glance nothing will appear to have happened. However if you look on the Experts tab of the Terminal window (Ctrl + T if it’s not showing) then you’ll see a message looking something like this:
 
 
4. Next we need to go to the File | Open Offline menu option which will bring up a dialog box showing all the history data files. Scroll down until your find the one which is labelled EURUSD,M3 . Highlight this and then click on the Open button. This will open up the chart. 
 
5. That’s it! The new chart will update almost in real-time (it’s programmed in the script to update every two seconds) and you can do all the usual stuff like add studies, EA’s and trade off the chart etc.

 

The finished EURUSD M3 chart

Just remember, you need to keep the driver chart (EURUSD M1 in the example above) running for the Offline chart to update. 

 

Different Time Periods

If you wanted something like an H8 chart, instead of using EURUSD M1 as a driver you would use EURUSD H1 and then a multiplier factor of 8 etc. So just work out what driver time period makes the most sense for the chart period that you are after and then use the appropriate multiplier.



 

 

Metatrader 101: Installing New Metatrader Code in Mt4

This is the one in a series of Metatrader 101 posts, partly so that I can have something to give to clients who are new to Mt4. It will cover the basics so people can get up and running quickly.
One of the great things about Metatrader is the amazing range of free code out there for extending and customising your charts with no end of scripts, indicators and EA out there, many of them free. But one of the first things that you need to learn is how to install these third-party pieces of code within your set-up. Here’s how to do it.
1. When installing code it will either be a .mq4 file which is the source code, or an .ex4 file which is the compiled code – with just the latter you won’t be able to see the underlying code. If people supply both all you need is the .mq4 file as your software will automatically compile it. Sometimes there are additional support files such as DLL libraries which you’ll need as well.
2. Unfortunately, the location on your hard drive of your Mt4 software is rather complicated as it’s split up into two locations. The bit where you need to copy your new software to is hidden away deep within the AppData section and you won’t easily find it by exploring the various folders. To help you with this there is a menu option which will open the location for you. From your Mt4 software go to File | Open Data Folder in order to bring up Windows Explorer opened at the correct folder location.
The folders within the MQL4 main folder
3.  You’ll see an MQL4 folder which you should open to reveal a series of sub-folders (see image above). Now what you do next depends on what you’re installing. If it’s an EA then it needs to go in the Experts folder, a script goes in the Scripts folder and indicators go (you guessed it) in the Indicators folder. Sometimes if the code is complicated you might also need to install various support files such as Dll’s etc which will go in the Libraries folder.
4. That’s basically it but now you need to get your Mt4 program to recognise the new files. There are two ways of doing this. The easiest way is simply to shut down and re-start the Mt4 platform. You should then see the new code in the appropriate section of the Navigator. The other way of doing it is to refresh the appropriate Navigator pane. For example, if you’ve installed a new EA then you need to go to the EA section on the Navigator pane, right-click and select Refresh. This will re-scan for all available EA code and your new EA should now be listed.
The various sections of the Navigator pane – right-click and select Refresh
That’s it! You can now make use of all the amazing pieces of Mt4 code that are out there.

Metatrader 101: Adding an EA to your Chart

This is the first in a series of Metatrader 101 posts, partly so that I can have something to give to clients who are new to Mt4. It will cover the basics so people can get up and running quickly.

An EA (Expert Advisor) is an automate trading strategy that will trade your account for you and hopefully make you money. In order to apply one to your chart you need to do the following:

1. Go to the Tools | Options menu option to bring up the Options dialog. On the Expert Advisors tab make sure that Allow automated trading has been ticked. You’ll see that there are other options there which disable automated trading when either the account or profile have been changed. It’s good to leave these on but it does mean that if you’re changing accounts regularly then you’ll need to check that automated trading is switched on each time you get started. Click OK once you’ve done this to close the dialog box.

Note, there is an alternative way of doing the above which you may prefer to use instead. Above the chart area there is space for some toolbars. Depending on what settings you have selected (View | Toolbars menu option to change these) you might have the “Standard” toolbar open. If you do then you’ll see this button:

If there is a green arrow next to the “man in a hat” icon then your EA’s are switched on. If there’s a red square then they’re switched off. Clicking this button toggles the EA’s on and off.

2. To add the EA to the chart simply drag and drop it from the Navigator pane on the left-hand side. The EA will first show a dialog box so that you can set it’s inputs. Once you’ve done this you should see a smiley face in the top right-hand corner of the screen which shows that the EA is active and working (see arrow in chart below).

EA smiley face in the top right-hand corner
3. That should be it. If you don’t see a smiley face then you need to remove the EA again by right clicking in the chart area and selecting Expert Advisors | Remove. The reason is probably because you’ve forgotten to do step 1 above so check that first and then repeat step 2. If it’s still not working then you need to look at the Experts tab in the Terminal window where the EA will output any error messages.

4. Removing an EA: as mentioned in step 3 right click in the chart area and select Expert Advisors | Remove. The smiley face icon will then disappear.

Metatrader Tip: Code for Cancelling All Pending Orders

There’s a rather nasty “gotcha” that one has to look out for when working with MetaTrader. It’s to do with iterating through a list of orders when you’re actually making changes to the order list. Take a look at this code to cancel all pending open orders.

void CancelOpenOrders()
{
   for(int i = 0; i< OrdersTotal(); i++)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false) break;
           
      if ( OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP )
      {
         if (!OrderDelete( OrderTicket() ))
            Print(“Delete Open Order failed: “,GetLastError(), 

             ” => “,ErrorDescription( GetLastError() ) ); 
      }
   }
}

At first glance there doesn’t look to be anything wrong with this code but if you come to use it you’ll find that it won’t delete all your open orders.  The reason why it doesn’t work is rather subtle but as it crops up regularly it’s worth getting to recognise it. Think about what happens if there are two open orders so OrdersTotal() returns 2. The first iteration (i = 0) it deletes the first order successfully, and then at the head of the for loop then it increments i  so now i = 1 and does the test to see if i < OrdersTotal(). Since we’ve now deleted one order, OrdersTotal() now returns 1 as there is only one outstanding order. This means that the i < OrdersTotal() condition now fails and it finishes the loop without deleting the second order.

The correct way to do it is to save the original order total in a variable and to iterate downwards so that you’re deleting the last order first. This way you’re not affecting the remaining order indices as you delete orders.

void CancelOpenOrders()
{
   int origOT = OrdersTotal();
  
   for(int i= origOT-1; i>=0; i–)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false) break;
           
      if ( OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP )
      {
         if (!OrderDelete( OrderTicket() ))
            Print(“Delete Open Order failed: “,GetLastError(), 

          ” => “,ErrorDescription( GetLastError() ) ); 
      }
   }
}

As a matter of interest, this same issue crops up in other programming situations where you’re deleting or altering things as you iterate through them so it’s always worth be aware of when you’re doing this.

Metatrader Tip: Offsetting Hedged Orders

A quick tip for beginners who are using Metatrader 4. If you’ve got two opposing hedged orders, say long 1.0 lots of EURUSD and short 1.0 lots of EURUSD then of course you are hedged. However, if you want to close out both positions rather than legging out of them (i.e. closing each individually) which carries a certain risk if the market moves whilst you’re in the process of closing them out, then you can simply offset one order against the other. What’s more this way you won’t incur so much commission charges as the second order is now just treated as a closing out order so you only get charged for one round-turn instead of two.

 

To do this, double-click on one of the orders to bring up the order ticket. In the Type field select “Close By”, this will then give you a list of orders which will offset this first order. Select the appropriate order in the list and then click on the Close button and voilá, you’ve offset your two hedged orders.
Note that the offsetting orders don’t need to be the same size. You can be long 1.0 lots and short say 0.5 lots and you can still do this offsetting which will result in a single long position of +0.5 lots.

 

click on image to enlarge