Set as homepage

Why Automated Trading in XTB?

  • Switch your emotions off – be automatic
  • High Frequency Trading – be faster than the rest
  • Save your time – let the computer do your work

What is Automated Trading?

  • Automated trading is a set of methods which allow computer aided trading. It allows you to take advantage of enhancements to make your trading easier, more precise or, if you choose, to fully automate your trading system.
  • Thanks to Automated Trading, you can now trade markets without even having to sit at your computer if you wish. You can simply program your computer to monitor markets based on your trading rules, and also to execute trades once the techincals meet your criteria.
  • Automated trading will save your time and work, helping you to focus on making the right trade. Take advantage of XTB's advanced functionality.
  • At XTB we have developed an easy to use Option Wizard to help you quickly & easily design option strategy to meet your market view and trading time frame.

 

Download

 

  • Documents
    • MQL book
      MQL book contains all necessary data to run and use MQL programs in the XTB-Trader platform. The book covers installation, running and testing MQL programs. All common problems are described along with simple and working solutions.
      The file is in PDF format.
      Download - MQL Newbie Book

 

Education

  • Introduction

    What is automated trading?

    Automated trading is a set of methods which allow computer aided trading. That not means executing orders by computer and internet connection, but rather enhancements that make trading easier, more accurate or completely automatic. In Automated Trading computer can place the orders in manner you want, in exact market conditions you have chosen. There is no need to stay and wait, looking on chart for proper trading condition. Thanks to Automated Trading computer can trade without human presence. Of course if you want be in charge all the time, then computer may only alert you, when the certain condition has occurred.
    Tired waiting for crossing two moving averages? Use Automated Trading.
    But that’s not all. With Automated Trading solutions you can have completely new technical analysis indicators. If you spend hours to calculate your own technical indicator, why not use computer? Why not let the computer draw the indicator directly on chart? Remember, when you save time, you also save money.

    What is MQL?

    MQL is a programming language which extends the capabilities of the X-Trader Platform. The language syntactically is very similar to widely used languages such like C and descendants. This feature of the MQL makes easy to learn and use for professional and amateur programmers. With proper preparations one can learn how to program in MQL within the weeks, not months. It’s even possible to write simple (working!) Expert Advisor on first day of learning MQL. Isn’t it impressive?
    Of course more sophisticated Expert Advisors needs more knowledge, but there is nothing for free, right?

    How can I recognize MQL program?

    The MQL files have two possible extensions:

    • MQ4 – for source code
    • EX4 – for compiled programs

    There is significant difference between files with these two extensions. The first one (mq4) is the source file. It can be modified or revised by person who knows MQL language. This is the source form of program. The second one (ex4) is the compiled file. In this form the program can used by the platform, but it cannot be read by human. Commercial MQL programs are distributed in this form.
    The process of changing human-readable form of MQ4 file to computer-readable EX4 form called Compilation will be described later.

    For what can I use it?

    For trading, of course! The programs written in MQL can enhance trading in many ways. Countless new technical analysis indicators can be written in MQL, many automation scripts, which do the most of the work, and last but not least: Expert Advisors, which can control the positions, open new, and close positions. With Automated Trading there is no need to look on the chart all the time. If you wait for certain condition to place your order you can commit it to computer or let the computer to inform you.

    more less
  • Types of MQL programs

    Expert Advisors

    Expert Advisors are the most interesting MQL scripts. They provide ability to find best market entry points (of course thanks to pre – programmed logic). The Expert Advisors can place the order on behalf of investor, show or send proper information, or do both. It depends on what the investor really needs.
    The MQL Expert Advisors bases mostly on technical analysis indicators, but there is a variety of strategies which can be implemented as Advisor and have nothing in common with classical technical analysis.
    For example Expert Advisor can calculate correlation between two instruments, and open trades accordingly to changes of correlation in time. MQL gives ability to write Expert Advisor that finds patterns between many instruments. It’s even possible to use neural networks and other sophisticated methods of data analysis.

    Indicators

    Technical Analysis Indicators are special scripts which draws the indicator objects (lines, dots, marks, etc.) directly on chart. Of course there is calculation algorithm beneath. The algorithm is responsible for values of indicator in each step of time. For example Moving Average algorithm takes prices for given time period, sum the prices, and then divides by number of periods. For next period the algorithm will do the same, but the data which will be taken for calculations will be moved by one period. The result is continuous line which represents values of moving average during the time period covered by chart.

    Scripts

    Scripts are the programs which allow to do simple, repetitive operations automatically. Normally, when user run the script, the script will do their work, and then it will turn off. For example, the scripts can be used for:

    • Closing all (pending) positions
    • Calculate data (minimal amount of money needed to open position, etc.)v
    • Write data on chart
    • Check connection, trading availability, etc.
    • Converting and saving data from charts

    Scripts are especially good for maintenance operations which done manually will take time.

    more less
  • Installation

    Most of scripts written in MQL are single files. More sophisticated script can contain more files. This chapter covers all methods of installing the scripts you already have on platform. In order to start using the MQL program, no matter if it is script, indicator or expert advisor, you need to place the program files in the proper platform folder. For our X-Trader platform the folder is usually placed in c:\Program Files folder.

    Copying to folder

    Copying MQL files to the proper folder is the most crude form of installing. However it also appears to be the most comfortable way of installing for more experienced investors. If you are ready to copy MQL files of some program which you have found on our website or found on one of many other internet sites, then just follow the below simple steps:

    • Check in the description of the MQL program you want to install whether it is script, indicator or expert advisor. You may find useful description above which highlight the differences between MQL program types. It’s a very important step, because each type of script has their own folder inside the platform folder. Putting the MQL program into wrong folder will cause inability to run the MQL program.
    • Select the files and select copy operation from right-click menu.
    • Go to the folder where the type of MQL programs you want to install is stored.
    • Expert Advisors: you have to paste the MQL Program file of this kind into folder platform_folder\experts\

    • Indicators: you have to paste the MQL Program file of this kind into folder platform_folder\experts\indicators . It’s the subfolder for the folder where Expert Advisors are stored.
    • Scripts: you have to paste the MQL Program file of this kind into folder platform_folder\experts\scripts . It’s the subfolder for the folder where Expert Advisors are stored.

    I am Vista/Win7 user and I have a problem. What can I do?

    Unfortunately Microsoft Windows Vista has default set of very strict rules for changing data in program files folders (it also includes our Platform folder). Unfortunately if you want use these method (copying) for install the scripts, then you have to ask your system administrator to change the permissions for entire platform folder and the subfolders. You have to be allowed to read and write in platform folder. If you are the user of your own system, then you have to change these permissions.

    Installer based method of installation

    The simplest way to install the MQL program is to use dedicated installer. X-Trade Brokers provide installers for all MQL programs published on their websites. User have to run the installer and follow the instructions on the screen. If the platform has been installed in non-standard folder, then choosing destination folder needs additional care.

    more less
  • Running

    Preparations

    The very first place where everything need to be properly set in order to use MQL Programs is Setting Menu. The menu is available under Ctrl+O combination of keys. Choose Expert Advisors tab. If any settings has not been changed in platform since installation then the window with settings set like below should appear:

    In order to use MQL Programs in comfortable way, it is needed to change settings. The changed settings are in red rectangles.

    After pressing the OK button, setting should be preserved.

    Caution notice

    Why you should be extra cautious when running program from unknown source. Why it is better to use scripts from trusted sources. (Like us or our affiliates)

    Setting properties of program

    Most of the MQL Programs have properties which can be set before starting the program. It’s extremely important to check and understand these properties. Some properties can change totally program behavior. It’s possible, for example, that the expert advisor will enter the position when completely opposite market conditions will appear.

    Starting the program

    Every program in MQL is attached to the chart on which is started. This means that some properties are taken directly from the chart to which is attached. It also means that user can, and have to choose the chart on which the MQL Program will be executed. I need to note here two important things. Changing the chart (i.e. changing the period of the chart) will cause the restart of the MQL Program. This is imminent and cannot be prevented. There are some scripts that have Instrument and timeframe hardcoded. This means that no matter on what chart such program will be executed. It will do exactly the same work. (first important attention still occurs for this case).

    Expert Advisors

    Expert Advisor can be run by double clicking selected Expert Advisor in Navigator window in Expert Advisors subtree.

    Then the properties window will appear:

    The window has got two tabs: “Common” tab and “Inputs” tab. Let’s focus on ”Common” tab. The most important properties which have to be set are market by the red rectangles. User can choose how independent the Expert Advisor is. Top left red rectangle shows option there can be chosen whether the Expert Advisor is able to open Long Positions, Short Positions or both. It is useful if user want to limit the Expert Advisor to only one type of trade. “Allow live trading” is the most important setting for Expert Advisors. When this option is set, then the Expert Advisor is able to manipulate orders (opening orders, closing orders and modifying orders). “Ask manual confirmation” option is additional to the option above. Setting this option forces manual confirmation of the user for each action regarding orders started by Expert Advisor. The program works in advise only mode. Most users turns this option off.
    The last option, which is very important for users of commercial Expert Advisors is “Allow DLL imports” option. Setting this option allows Expert Advisor using external libraries named DLL files.

    When everything is set and ready to run, then click OK. If everything is in order, then name of the Expert Advisor and smiled face should appear in top-right corner of the chart. If instead of smiled face, sad face is present, then Expert Advisor is working but from some reason it cannot trade. The reason could be unset property “Allow live trading” or closed market for the instrument on chart. If instead of smiles crossed lines has appeared in top-left corner of the chart, then it means that it is needed to turn on Expert Advisors in Option window described earlier.

    Indicators

    Indicator can be run by double clicking selected Indicator in Navigator window in Custom Indicators subtree. The following window should appear:

    If Indicator uses external DLL files, then option Allow DLL imports HAVE TO be turned on.

    “Inputs” tab shows parameters that can be set for chosen indicator. The number of settings and the meaning of each one depends on programmer who coded the indicator. For example the indicator which is shown on window above have only one settings – period for which each calculation of indicator is made. The rest of tabs are responsible for visual properties of indicator and therefore will be omitted.
    Press OK for starting the indicator. The indicator should be now placed on chart.

    Scripts

    Indicator can be run by double clicking selected Script in Navigator window in Scripts subtree.

    Stopping the program

    Each type of program have their own way of stopping execution. The scripts should end their work without user intervention. In case this not happens the script can be stopped by right-clicking the chart to which the script is attached and clicking “Remove Script” option.

    In order to remove indicator from the chart user need remove the indicator from the “Indicators List” of the chart. To achieve that right-click on the chart, choose “Indicators List” option. In window which has been shown choose the indicator you want to delete, and click the “Remove” button. Close the window. The indicator should disappear.


    In order to stop Expert Advisor user have two possibilities:

    • Use “panic button” which turns off all Expert Advisors on the platform:
    • Turn off one Expert Advisor by Right-click on chart to which Expert Advisor is attached and choose “Remove” from “Expert Advisors” submenu:
    more less
  • Testing the program

    Testing Expert Advisor

    Really important

    What is really important during testing the strategy? Why the strategy testing is important? The answer is simple. Testing provides the possibility to check if all assumptions included into strategy are right, if the code of the strategy does exactly what was meant by maker and if there are no potentially dangerous errors.
    In XTB-Trader platform three levels of testing Expert Advisors can be applied:

    • Backtesting – testing on historical data, some assumptions described below are taken
    • Demo account testing – testing on demo account with virtual money
    • Real account testing – testing on real account, with real money, but under user advisory

    First test should be made on Backtesting, because it could be done in minutes. The second is running strategy on demo, which spends more time. Sometimes it takes a week of working for Expert Advisor on demo account. If everything is working as it was in assumptions, then there is time for real account tests, where the Expert Advisor will be tested “on battlefield”. After that we could be sure that our Expert Advisor (or Expert Advisor we want to use) is reliable piece of software, and can be used for profitable Automated Trading.

    Backtest testing

    Backtesting of the strategy is very important. Testing on historical data gives information how the strategy behaves in historical conditions. That should always be the very first step of testing Expert Advisor. In few seconds validity of the Expert Advisor will be checked. The implications are obvious. Good, profitable Strategy can be easily recognized, and losing strategy (or even strategy with errors) can be easily discarded or sent back to programming stage. In most cases backtesting helps in correcting small, yet important errors.
    In XTB-Trader platform special functionality for backtesting is available under the key combination Ctrl+R. The Tester subwindow should appear.


    This window has several properties:

    1. Expert advisor – for choosing expert advisor to be tested.
    2. Symbol – instrument on which Expert Advisor will be tested.
    3. Model – type of data modeling used for testing the Expert Advisor. It’s very important to understand each method of modeling and their impact to testing results. All three methods of modeling will be described later.
    4. Use data – allows to choose time period for which the Expert will be used.
    5. Visual mode – allows to check on chart how the Expert Advisor behaves during previously set period of time.
    6. Period – allows to choose on which chart period the Expert Advisor will be ran
    7. Expert properties – this button opens the window where various parameters can be changed. This window will be described later.
    8. Symbol properties – this button shows window with current properties of chosen symbol.
    9. Start – this button starts the testing

    Demo account testing

    Testing on demo account is just running the Expert Advisor on account with virtual money. The Expert Advisor have to be left working for some time. There is no difference between real account testing and demo testing in the functional matters. The only difference is that there is no risk for real money. If something is going wrong, just turn off Expert Advisor and check the Journal Tab in Terminal subwindow:


    Any error which were caused by Expert Advisor will be noticed here.

    Real account testing

    Testing on real account needs far more caution than Backtesting or demo account testing. The reason is obvious – real money is involved. Of course, after backtesting and demo account testing we can be almost one hundred percent sure, that the Expert Advisor is working fine. However, the responsible investor should check on their own if Expert Advisor is not doing what he doesn’t suppose to do on real account.
    The procedure is simple: run Expert Advisor and watch if all moves done by Expert Advisor are done right. If something is going wrong, do the same as for demo account testing procedure.

    Testing Scripts

    How to test script is doing what it should do? The rule of the thumb for scripts says: if the script works as it should and ends in less than minute, then we could assume that is working fine. Of course, first test of the script should be made on demo account. This is especially very important for the scripts which operate on orders.

    more less