Download MetaTrader 5

Features of Experts Advisors

18 August 2005, 16:57
MetaQuotes Software Corp.
0
3 740

Creation of expert advisors in the MetaTrader trading system has a number of features.

  • Before opening a position, you should check if there is money available on the account. If there is not enough money on the account, the operation of opening a position will not be successful. The "FreeMargin" value must not be less than 1000 only during tests because the price of one lot is 1000 during tests.

    if(AccountFreeMargin() < 1000) return(0);    // not enough money
  • You can access history data by using the predefined arrays Time, Open, Low, High, Close, Volume. Due to historical reasons, index in these arrays increases from the end to the beginning. It means that the latest data have index 0. Index 1 indicates data shifted one period backwards, index 2 means data shifted two periods backwards, 3 is three periods backwards, etc.

    // if Close on the previous bar is less than 
    // Close on the bar before previous
    if(Close[1] < Close[2]) return(0);
  • It is also possible to access historical data using other time intervals and even using other currency pairs. To get such data, it is necessary to define a one-dimensional array first and perform a copy operation using the "ArrayCopySeries" function. Mind that, while calling the function, it is possible to pass a less number of parameters and do not specify default parameters.

    double eur_close_m1[];
    int number_copied = ArrayCopySeries(eur_close_m1, MODE_CLOSE, "EURUSD", PERIOD_M1);
  • In the process of writing an expert advisor, as well as any other software, it is sometimes necessary to get some additional debugging information. The MQL4 language provides several methods for getting such information.

    • The "Alert" function displays a dialog box with some data defined by the user.

      Alert("FreeMargin grows to ", AccountFreeMargin(), "!");
    • The "Comment" function displays data defined by the user in the upper-left corner of the chart. The character sequence "\n" is used to start a new line.

      Comment("FreeMargin is ", AccountFreeMargin(), ".");
    • The "Print" function saves data defined by the user to the system log.

      Print("FreeMargin is ", AccountFreeMargin(), ".");
  • To get the information about errors in the program, the "GetLastError" function is very useful. For example, an operation with an order always returns the ticket number. If the ticket number equals 0 (some error has occurred in the process of performing the operation), it is necessary to call the "GetLastError" function to get additional information about the error:

    int iTickNum = 0;
    int iLastError = 0;
    ...
    iTickNum = OrderSet (OP_BUY, g_Lots, Ask, 3, 0, Ask + g_TakeProfit * g_Points, Red);
    if (iTickNum <= 0) 
       {
         iLastError = GetLastError();
         if (iLastError != ERROR_SUCCESS) Alert("Some Message");
       }

    You should remember that calling the "GetLastError" function displays the code of the last error and resets its value. That is why calling this function once again in a row will always return value 0.

  • How to define the beginning of a new bar? (It can be necessary to find out that the previous bar has just been finished.) There are several methods to do it.


    The first method is based on checking the number of bars:

    int prevbars = 0;
    ...
    if(prevbars == Bars) return(0);
    prevbars = Bars;
    ...

    This method can fail to work while loading the history. That is, the number of bars is changed while the "previous" one has not been finished yet. In this case you can make checking more complicated by introducing a check for difference between the values equal to one.

    The next method is based on the fact that the "Volume" value is generated depending on the number of ticks that have come for each bar and the first tick means that the "Volume" value of a new bar equals 1:

    if( Volume > 1) return(0);
    ...

    This method can fail to work when there are a lot of incoming price ticks. The matter is that incoming price tricks are processed in a separate thread. And if this thread is busy when the next tick comes, this new incoming tick is not processed to avoid overloading the processor! In this case you can also make checking more complicated by saving the previous "Volume" value.

    The third method is based on the time a bar is opened:

    datetime prevtime=0;
    ...
    if(prevtime == Time[0]) return(0);
    prevtime = Time[0];
    ...

    It is the most reliable method. It works in all cases.

  • An example of working with a file of the "CSV" type:

    int h1;
    h1 = FileOpen("my_data.csv", MODE_CSV | MODE_WRITE, ";");
    if(h1 < 0)
      {
       Print("Unable to open file my_data.csv");
       return(false);
      }
    FileWrite(h1, High[1], Low[1], Close[1], Volume[1]);  
    FileClose(h1);

    Some explanations to the code. The file of the "CSV" format is opened first. In case there occurs an error while opening the file, the program is exited. In case the file is successfully opened, its content gets cleared, data are saved to the file and the file is closed. If you need to keep the content of the file being opened, you should use the MODE_READ opening mode:

    int h1;
    h1 = FileOpen("my_data.csv", MODE_CSV | MODE_WRITE | MODE_READ, ";");
    if(h1 < 0)
      {
       Print("Unable to open file my_data.csv");
       return(false);
      }
    FileSeek(h1, 0, SEEK_END);
    FileWrite(h1, High[1], Low[1], Close[1], Volume[1]);  
    FileClose(h1);

    In this example, data are added to the end of the file. To do it, we used the "FileSeek" function right after it was opened.

Translated from Russian by MetaQuotes Software Corp.
Original article: https://www.mql5.com/ru/articles/1494

Step on New Rails: Custom Indicators in MQL5 Step on New Rails: Custom Indicators in MQL5

I will not list all of the new possibilities and features of the new terminal and language. They are numerous, and some novelties are worth the discussion in a separate article. Also there is no code here, written with object-oriented programming, it is a too serous topic to be simply mentioned in a context as additional advantages for developers. In this article we will consider the indicators, their structure, drawing, types and their programming details, as compared to MQL4. I hope that this article will be useful both for beginners and experienced developers, maybe some of them will find something new.

Here Comes the New MetaTrader 5 and MQL5 Here Comes the New MetaTrader 5 and MQL5

This is just a brief review of MetaTrader 5. I can't describe all the system's new features for such a short time period - the testing started on 2009.09.09. This is a symbolical date, and I am sure it will be a lucky number. A few days have passed since I got the beta version of the MetaTrader 5 terminal and MQL5. I haven't managed to try all its features, but I am already impressed.

False trigger protection for Trading Robot False trigger protection for Trading Robot

Profitability of trading systems is defined not only by logic and precision of analyzing the financial instrument dynamics, but also by the quality of the performance algorithm of this logic. False trigger is typical for low quality performance of the main logic of a trading robot. Ways of solving the specified problem are considered in this article.

Using text files for storing input parameters of Expert Advisors, indicators and scripts Using text files for storing input parameters of Expert Advisors, indicators and scripts

The article describes the application of text files for storing dynamic objects, arrays and other variables used as properties of Expert Advisors, indicators and scripts. The files serve as a convenient addition to the functionality of standard tools offered by MQL languages.