This is because the magic number is used by the MetaTrader back end to know which expert advisor to send information to. For example, when a trade notification is received, MetaTrader will have stamped the order with the expert advisor’s magic number, and it therefore knows which expert advisor to send the trade notification to.

It can therefore also be used to find out which expert advisor sent a particular order.

]]>https://metaquotes5.wordpress.com/2012/08/11/csymbolinfo-obtain-information-about-a-symbol/feed/0skebangaTrading off a moving averagehttps://metaquotes5.wordpress.com/2012/07/28/trading-off-a-moving-average/
https://metaquotes5.wordpress.com/2012/07/28/trading-off-a-moving-average/#respondSat, 28 Jul 2012 23:46:40 +0000http://metaquotes5.wordpress.com/?p=31Continue reading →]]>Using the examples provided in the MQL5 source tree, I found one which traded using a moving average.

…\MQL5\Experts\Examples\Moving Average\Moving Average.mq5

I took this and played around with it in order to further understand the mechanics of the provided MQL5 object hierarchy.

The idea is simple: Apply a simple moving average to the chart in order to average out the noise and see an overall trend.

By shifting the moving average horizontally to the right, when price action crosses the average it signals a reverse in trend.

When a new bar is opened (ie: on the first tick of the new bar the tick volume in the bar is 1), we look at what happened in the previous bar.

If price crosses the trend moving upwards, this is a signal to go long.

// the previous bar opened below the average and closed above the average
if (rates[0].open < ma[0] && rates[0].close > ma[0])
order = ORDER_TYPE_BUY; // price crosses up - go long

If price crosses the trend moving downwards, this is a signal to go short.

// the previous bar opened above the average and closed below the average
if (rates[0].open > ma[0] && rates[0].close < ma[0])
order = ORDER_TYPE_SELL; // price crosses down - go short

I keep track of whether I have opened my first position. Once this has happened I double my lot size. The reason for this is so that when the trend reverses, I will enter an opposite position for double the number of lots; this will exit my previous position and enter a new position in the opposite direction.

]]>https://metaquotes5.wordpress.com/2012/07/28/trading-off-a-moving-average/feed/0skebangaDisplaying more informationhttps://metaquotes5.wordpress.com/2012/07/21/displaying-more-information/
https://metaquotes5.wordpress.com/2012/07/21/displaying-more-information/#respondSat, 21 Jul 2012 06:41:02 +0000http://metaquotes5.wordpress.com/?p=23The OnChartEvent callback returns a number of parameters. I wrote another small utility class to display the details of these parameters.

]]>https://metaquotes5.wordpress.com/2012/07/21/displaying-more-information/feed/0skebangaDisplaying some informationhttps://metaquotes5.wordpress.com/2012/07/21/displaying-some-information/
https://metaquotes5.wordpress.com/2012/07/21/displaying-some-information/#respondSat, 21 Jul 2012 06:29:02 +0000http://metaquotes5.wordpress.com/?p=14Continue reading →]]>I now add some information about the status of the EA, and the parameters passed to the callbacks.

The call to OnDeinit passes an integer value called ‘reason’. I wrote a simple class with a static function which will translate the integer into a string.

I opened the integrated MetaEditor IDE by clicking on Tools – MetaQuotes Language Editor [F4]

Once it had opened, I selected New – Expert Advisor (template). I chose “Test” as my first EA’s name, and selected all the available callback functions / event handlers.

I then added the following code to each event handler, which would show me which functions were called at what point when I ran the EA.

Print(__FUNCTION__);

If you then compile (F7), and run (F5), you will be returned to MetaTrader, and asked to confirm you wish to run the EA. Click on OK.

Now when you click on the chart, or a new tick arrives, etc, you will see the callback function’s name being printed (It is displayed in Experts tab of the Toolbox – if the toolbox is not visible, Ctrl-T will bring it up)

To stop the EA, either right click on the EA icon in the top right corner of the main chart window and click Remove, or return to MetaEditor (F4) and click on Stop debugging (Shift-F5).