Expert advisor for MT4 for one evening. Evgeny Zhdan

Чтение книги онлайн.

Читать онлайн книгу Expert advisor for MT4 for one evening - Evgeny Zhdan страница 2

Expert advisor for MT4 for one evening - Evgeny Zhdan

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

i <100; i++)

      {

      count something 100 times.

      }

      int i = 0 – assign a variable to work within this cycle; i <100 – the cycle runs 100 times, from 0 to 99; i ++ (increment) means that with each run (iteration) of the cycle, the variable i will be incremented by 1.

      bool x = false; // assign value false for a variable x of bool type

      while (x==false) // while x is false. Two equal symbols – == – mean comparison

      {

      /*

      some conditions will come true here.

      As soon as x is true, the cycle will stop.

      */

      //for example

      x = true;// set x to true after the first run

      //and thus the cycle stops

      }

      In the process of writing an adviser, we will use both of these cycles, and you will puzzle them out easily.

      DESIGN 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

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