Скачать книгу

SPECIFICATION

      Let’s describe what our future adviser should do and when:

      The trading signals will be generated by two standard indicators – the Envelopes and ZigZag. These indicators are built into MetaTrader4 and you do not need to download them separately. I chose these two indicators because their values are being requested in different ways. In case of Envelopes – by using the standard iEnvelopes function, and in case of ZigZag – by the iCustom function – you need to research it yourself (it sounds dramatic, I know) so that sometime later you’ll be able to request the data of almost any non-standard indicators of MetaTrader4.

      Let’s make a brief design specification:

      – If the upper peak of the ZigZag indicator (hereinafter – ZZ) is generated above the upper line of the Envelopes indicator (with the parameter Shift = 10, with the standard others), we should make a sell order with a fixed lot set in the Advisor’s settings.

      – If the lower ZZ peak is generated below the lower Envelopes one – a buy order (i.e., vice versa compared to the buy signal).

      – By modification (we’ll examine later while writing the code, why we should do it this way and not immediately by installing the order), the adviser should be able to set Stop-Losses and Take-Profits over the orders.

      – to add the option of closing the orders when the price reaches the opposite line of Envelopes. This function can be turned off in the settings.

      If you are reading this book, I hope that you already have a MetaTrader4 trading terminal on your computer and you know how to set a demo account. If not, you need to install this terminal by pre-registering with any broker supporting MetaTrader4.

      And now, translate your terminal into English! If you set your sights on pursuing programming, get used to English, no way round it! Leave the MetaEditor code editor in Russian, because when you translate it into English, its Help (F1) would also be in English. It’s not everyone’s cup of tea.

      NOW WE GET THE INDICATORS” DATA

      Open your MetaTrader4 and press F4 on the keyboard, or left click here:

      In the popped up code editor, click New (Create), then the Expert Advisor (template), then Next, in the Name field after Experts\ add MyFirstEA – this will be the name of your first Expert Advisor. You’ll get Experts\MyFirstEA. We do not need the Autor and link fields for this test advisor. Click the Next button. The Event Handles of the Expert Advisor window should pop up. There is nothing to set here and just click Next. The Tester event handless of the Expert Advisor window will pop up, once more, do not select anything there and click Finish. Now we get a working area where our trading bot will soon be born.

      In the comments on the image below, we indicated which blocks are responsible for what.

      To know the price values of indicators, we need to assign the global variables of the double type for the top and bottom lines of the Envelopes indicator. Let’s call them enveUP and enveDW. You can call them yourself. The same should be done to get the price value of the ZZ indicator. Let’s call this variable ZZ. Why the global variables, of all of them? In order for these values to be requested anywhere in the program (namely, in the advisor). The fact is that we will request the indicator values not with each incoming tick, but once for each candle. This will significantly improve performance because the terminal would not need to perform the same operation for each tick. If we enclose the request for our indicators in curly braces, while assigning their values NOT for the global variables, then these values will be visible only within those curly braces. And outside of them, we will get an error. I’ll try to describe the specifics in the picture below.

      Copy this code into your editor:

      //+ – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – +

      //| MyFirstEA.mq4 |

      //| Copyright 2017, |

      //+ – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – +

      #property copyright “Copyright 2017”

      #property link””

      #property version “1.00”

      #property strict

      //+ – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – +

      double enveUP, enveDW, ZZ;

      datetime open;

      //+ – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – — – +

      int OnInit ()

      {

      return (INIT_SUCCEEDED);

      }

      void OnDeinit (const int reason)

      {

      }

      void OnTick ()

      {

      if (Open [0]!= open)

      {

      enveUP = iEnvelopes (NULL,0,13,MODE_SMA,10,PRICE_CLOSE,0.2,MODE_UPPER,1);

      enveDW = iEnvelopes (NULL,0,13,MODE_SMA,10,PRICE_CLOSE,0.2,MODE_LOWER,1);

      ZZ = iCustom (Symbol (),0,“ZigZag”, 0,1);

      if (enveUP> 0 && enveDW> 0 && ZZ> 0) open = Open [0];

      }

      }

      Let’s examine what each line means.

      In global variables, except the variables for indicator values, we assigned a variable of datetime type with the open name. Now it set to 0.

      IMPORTANT: Position the cursor on the word datetime and press the F1 on the keyboard – the HELP window will pop up with a description of what the datetime type is. You can do it for all built-in commands!

      if (Open [0]!= open): If the Zero Candle Open Time IS NOT EQUAL open (i.e., it is set to zero), the code in curly braces will be executed. The Open [0] command means the Zero Candle Open Time (i.e., the time of the current, not yet closed candle). Again, position the cursor on Open and press F1 – read about this command.

      EnveUP = iEnvelopes (NULL,0,13,MODE_SMA,10,PRICE_CLOSE,0,2,MODE_UPPER,1); – click on iEnvelopes and see what data should be indicated and in which order:

      double iEnvelopes (

      – string symbol, // the symbol name

      – int timeframe, // timeframe

      – int ma_period, // period

      – int ma_method, // the averaging method

      – int ma_shift, // the average shift

      – int applied_price, // the price type

      – double deviation, // the deviation (as %)

      – int mode, // the line index

      – int

Скачать книгу