Mql Newbie Book Modificado

Embed Size (px)

Citation preview

  • 8/9/2019 Mql Newbie Book Modificado

    1/24

  • 8/9/2019 Mql Newbie Book Modificado

    2/24

    X-TRADE BROKERS S.A.

    MQL Newbie Book 

    A Short guide for Automted Trdi!g

    Michał Zabielski

    2010-04-12

  • 8/9/2019 Mql Newbie Book Modificado

    3/24

    Table of ContentsTable of Contents .................................................................................................................................... 3

    Introduction ............................................................................................................................................ 4

    What is automated trading? ............................................................................................................... 4

    What is MQL? ............................................................................................................................... ...... 4

    How can I recognie MQL !rogram? ............................................................................................... 4

    "or what can I use it? .......................................................................................................................... 4

    T#!es of MQL !rograms .......................................................................................................................... $

    %&!ert 'd(isors ................................................................................................................................... $

    Indicators ............................................................................................................................................ $

    )cri!ts ................................................................................................................................................. $

    Installation .............................................................................................................................................. *

    Co!#ing to folder ................................................................................................................................ *

    I am +ista,Win- user and I ha(e a !roblem. What can I do? .......................................................... *

    Installer based method of installation ................................................................................................ *

    unning .................................................................................................................................................. -

    /re!arations ....................................................................................................................................... -

    Caution notice .................................................................................................................................... -

    )etting !ro!erties of !rogram ............................................................................................................ -

    )tarting the !rogram .......................................................................................................................... -

    %&!ert 'd(isors ............................................................................................................................... -

    Indicators ........................................................................................................................................ 0

    )cri!ts ............................................................................................................................................. 0

    )to!!ing the !rogram ......................................................................................................................... 0

    Testing the !rogram .............................................................................................................................. 12

    Testing %&!ert 'd(isor ...................................................................................................................... 12

    eall# im!ortant ........................................................................................................................... 12

    actest testing ............................................................................................................................ 12

    5emo account testing ................................................................................................................... 11

    eal account testing ...................................................................................................................... 11

    Testing )cri!ts ................................................................................................................................... 11

  • 8/9/2019 Mql Newbie Book Modificado

    4/24

    Introduction

    What is automated trading?

    'utomated trading is a set of methods which allow com!uter aided trading. That not meanse&ecuting orders b# com!uter and internet connection6 but rather enhancements that mae trading

    easier6 more accurate or com!letel# automatic. In 'utomated Trading com!uter can !lace the orders

    in manner #ou want6 in e&act maret conditions #ou ha(e chosen. There is no need to sta# and wait6

    looing on chart for !ro!er trading condition. Thans to 'utomated Trading com!uter can trade

    without human !resence. 7f course if #ou want be in charge all the time6 then com!uter ma# onl#

    alert #ou6 when the certain condition has occurred.

    Tired waiting for crossing two mo(ing a(erages? 8se 'utomated Trading.

    ut that9s not all. With 'utomated Trading solutions #ou can ha(e com!letel# new technical anal#sisindicators. If #ou s!end hours to calculate #our own technical indicator6 wh# not use com!uter? Wh#

    not let the com!uter draw the indicator directl# on chart? emember6 when #ou sa(e time6 #ou also

    sa(e mone#.

    What is MQL?

    MQL is a !rogramming language which e&tends the ca!abilities of the :;Trader /latform. The

    language s#ntacticall# is (er# similar to widel# used languages such lie C and descendants. This

    feature of the MQL maes eas# to learn and use for !rofessional and amateur !rogrammers. With

    !ro!er !re!arations one can learn how to !rogram in MQL within the wees6 not months. It 9s e(en

    !ossible to write sim!le %&!ert 'd(isor on first da# of learning MQL. Isn9t it im!ressi(e?

    7f course more so!histicated %&!ert 'd(isors needs more nowledge6 but there is nothing for free6

    right?

    How can I recognize MQL program?

    The MQL files ha(e two !ossible e&tensions

    • MQ4 @ for source code

    • %:4 @ for com!iled !rograms

    There is significant difference between files with these two e&tensions. The first one is the

    source file. It can be modified or re(ised b# !erson who nows MQL language. This is the source form

    of !rogram. The second one is the com!iled file. In this form the !rogram can used b# the

    !latform6 but it cannot be read b# human. Commercial MQL !rograms are distributed in this form.

    The !rocess of changing human;readable form of MQ4 file to com!uter;readable %:4 form called

    Com!ilation will be described later.

  • 8/9/2019 Mql Newbie Book Modificado

    5/24

    For what can I use it?

    "or trading6 of course= The !rograms written in MQL can enhance trading in man# wa#s. Countless

    new technical anal#sis indicators can be written in MQL6 man# automation scri!ts6 which do the most

    of the wor6 and last but not least %&!ert 'd(isors6 which can control the !ositions6 o!en new6 and

    close !ositions. With 'utomated Trading there is no need to loo on the chart all the time. If #ou wait

    for certain condition to !lace #our order #ou can commit it to com!uter or let the com!uter to

    inform #ou.

  • 8/9/2019 Mql Newbie Book Modificado

    6/24

    Tpes of MQL programs

    !"pert #d$isors%&!ert 'd(isors are the most interesting MQL scri!ts. The# !ro(ide abilit# to find best maret entr#

    !oints . The %&!ert 'd(isors can !lace the order on

    behalf of in(estor6 show or send !ro!er information6 or do both. It de!ends on what the in(estor

    reall# needs.

    The MQL %&!ert 'd(isors bases mostl# on technical anal#sis indicators6 but there is a (ariet# of

    strategies which can be im!lemented as 'd(isor and ha(e nothing in common with classical technical

    anal#sis.

    "or e&am!le %&!ert 'd(isor can calculate correlation between two instruments6 and o!en tradesaccordingl# to changes of correlation in time. MQL gi(es abilit# to write %&!ert 'd(isor that finds

    !atterns between man# instruments. It9s e(en !ossible to use neural networs and other

    so!histicated methods of data anal#sis.

    Indicators

    Technical 'nal#sis Indicators are s!ecial scri!ts which draws the indicator obBects directl# on chart. 7f course there is calculation algorithm beneath. The algorithm is res!onsible

    for (alues of indicator in each ste! of time. "or e&am!le Mo(ing '(erage algorithm taes !rices for

    gi(en time !eriod6 sum the !rices6 and then di(ides b# number of !eriods. "or ne&t !eriod the

    algorithm will do the same6 but the data which will be taen for calculations will be mo(ed b# one!eriod. The result is continuous line which re!resents (alues of mo(ing a(erage during the time

    !eriod co(ered b# chart.

    %cripts

    )cri!ts are the !rograms which allow to do sim!le6 re!etiti(e o!erations automaticall#. ormall#6

    when user run the scri!t6 the scri!t will do their wor6 and then it will turn off. "or e&am!le6 the

    scri!ts can be used for

    ; Closing all !ositions

    ; Calculate data

    ; Write data on chart

    ; Chec connection6 trading a(ailabilit#6 etc.

    ; Con(erting and sa(ing data from charts

    )cri!ts are es!eciall# good for maintenance o!erations which done manuall# will tae time.

  • 8/9/2019 Mql Newbie Book Modificado

    7/24

  • 8/9/2019 Mql Newbie Book Modificado

    8/24

    InstallationMost of scri!ts written in MQL are single files. More so!histicated scri!t can contain more files. This

    cha!ter co(ers all methods of installing the scri!ts #ou alread# ha(e on !latform.

    In order to start using the MQL !rogram6 no matter if it is scri!t6 indicator or e&!ert ad(isor6 #ou need

    to !lace the !rogram files in the !ro!er !latform folder. "or our :;Trader !latform the folder is usuall#

    !laced in c:\Program Files folder.

    Coping to folder

    Co!#ing MQL files to the !ro!er folder is the most crude form of installing. Howe(er it also a!!ears to

    be the most comfortable wa# of installing for more e&!erienced in(estors. If #ou are read# to co!#

    MQL files of some !rogram which #ou ha(e found on our website or found on one of man# other

    internet sites6 then Bust follow the below sim!le ste!s

    ; Chec in the descri!tion of the MQL !rogram #ou want to install whether it is scri!t6

    indicator or e&!ert ad(isor. Dou ma# find useful descri!tion abo(e which highlight the

    differences between MQL !rogram t#!es. It9s a (er# im!ortant ste!6 because each

    t#!e of scri!t has their own folder inside the !latform folder. /utting the MQL

    !rogram into wrong folder will cause inabilit# to run the MQL !rogram.

    ; )elect the files and select co!# o!eration from right;clic menu.

  • 8/9/2019 Mql Newbie Book Modificado

    9/24

    ; Eo to the folder where the t#!e of MQL !rograms #ou want to install is stored.

    o %&!ert 'd(isors #ou ha(e to !aste the MQL /rogram file of this ind into folder

     platform_folder\experts\

  • 8/9/2019 Mql Newbie Book Modificado

    10/24

    o Indicators #ou ha(e to !aste the MQL /rogram file of this ind into folder

     platform_folder\experts\indicators . It9s the subfolder for the folder where %&!ert

    'd(isors are stored.

    o )cri!ts #ou ha(e to !aste the MQL /rogram file of this ind into folder

  • 8/9/2019 Mql Newbie Book Modificado

    11/24

     platform_folder\experts\scripts . It9s the subfolder for the folder where %&!ert

    'd(isors are stored.

    I am &ista'Win( user and I ha$e a problem) What can I do?

    8nfortunatel# Microsoft Windows +ista has default set of (er# strict rules for changing data in

    !rogram files folders . 8nfortunatel# if #ou want use these

    method for install the scri!ts6 then #ou ha(e to as #our s#stem administrator to change the

    !ermissions for entire !latform folder and the subfolders. Dou ha(e to be allowed to read and write in!latform folder. If #ou are the user of #our own s#stem6 then #ou ha(e to change these !ermissions.

    Installer based method of installation

    The sim!lest wa# to install the MQL !rogram is to use dedicated installer. :;Trade roers !ro(ide

    installers for all MQL !rograms !ublished on their websites. 8ser ha(e to run the installer and follow

    the instructions on the screen. If the !latform has been installed in non;standard folder6 then

    choosing destination folder needs additional care.

  • 8/9/2019 Mql Newbie Book Modificado

    12/24

    *unning

    +reparationsThe (er# first !lace where e(er#thing need to be !ro!erl# set in order to use MQL /rograms is )etting

    Menu. The menu is a(ailable under CtrlF7 combination of e#s. Choose %&!ert 'd(isors tab. If an#

    settings has not been changed in !latform since installation then the window with settings set lie

    below should a!!ear

  • 8/9/2019 Mql Newbie Book Modificado

    13/24

    In order to use MQL /rograms in comfortable wa#6 it is needed to change settings. The changed

    settings are in red rectangles.

    'fter !ressing the 7G button6 setting should be !reser(ed.

    Caution notice

    Wh# #ou should be e&tra cautious when running !rogram from unnown source. Wh# it is better to

    use scri!ts from trusted sources.

    %etting properties of program

    Most of the MQL /rograms ha(e !ro!erties which can be set before starting the !rogram. It9s

    e&tremel# im!ortant to chec and understand these !ro!erties. )ome !ro!erties can change totall#

    !rogram beha(ior. It 9s !ossible6 for e&am!le6 that the e&!ert ad(isor will enter the !osition when

    com!letel# o!!osite maret conditions will a!!ear.

    %tarting the program

    %(er# !rogram in MQL is attached to the chart on which is started. This means that some !ro!erties

    are taen directl# from the chart to which is attached. It also means that user can6 and ha(e to

    choose the chart on which the MQL /rogram will be e&ecuted. I need to note here two im!ortant

    things. Changing the chart will cause the restart of the MQL

    /rogram. This is imminent and cannot be !re(ented. There are some scri!ts that ha(e Instrument and

    timeframe hardcoded. This means that no matter on what chart such !rogram will be e&ecuted. It will

    do e&actl# the same wor. .

  • 8/9/2019 Mql Newbie Book Modificado

    14/24

    !"pert #d$isors

    %&!ert 'd(isor can be run b# double clicing selected %&!ert 'd(isor in a(igator window in %&!ert

    'd(isors subtree.

    Then the !ro!erties window will a!!ear

  • 8/9/2019 Mql Newbie Book Modificado

    15/24

    The window has got two tabs Common tab and In!uts tab. Let9s focus on Common tab. The

    most im!ortant !ro!erties which ha(e to be set are maret b# the red rectangles. 8ser can choose

    how inde!endent the %&!ert 'd(isor is. To! left red rectangle shows o!tion there can be chosen

    whether the %&!ert 'd(isor is able to o!en Long /ositions6 )hort /ositions or both. It is useful if user

    want to limit the %&!ert 'd(isor to onl# one t#!e of trade. 'llow li(e trading is the most im!ortant

    setting for %&!ert 'd(isors. When this o!tion is set6 then the %&!ert 'd(isor is able to mani!ulate

    orders . 's manual confirmation o!tion is

    additional to the o!tion abo(e. )etting this o!tion forces manual confirmation of the user for each

    action regarding orders started b# %&!ert 'd(isor. The !rogram wors in ad(ise onl# mode. Most

    users turns this o!tion off.

    The last o!tion6 which is (er# im!ortant for users of commercial %&!ert 'd(isors is 'llow 5LL

    im!orts o!tion. )etting this o!tion allows %&!ert 'd(isor using e&ternal libraries named 5LL files.

  • 8/9/2019 Mql Newbie Book Modificado

    16/24

    When e(er#thing is set and read# to run6 then clic 7G. If e(er#thing is in order6 then name of the

    %&!ert 'd(isor and smiled face should a!!ear in to!;right corner of the chart. If instead of smiled

    face6 sad face is !resent6 then %&!ert 'd(isor is woring but from some reason it cannot trade. The

    reason could be unset !ro!ert# 'llow li(e trading or closed maret for the instrument on chart. If

    instead of smiles crossed lines has a!!eared in to!;left corner of the chart6 then it means that it is

    needed to turn on %&!ert 'd(isors in 7!tion window described earlier.

    Indicators

    Indicator can be run b# double clicing selected Indicator in a(igator window in Custom Indicatorssubtree. The following window should a!!ear

    If Indicator uses e&ternal 5LL files6 then o!tion 'llow 5LL im!orts H'+% T7 be turned on.

  • 8/9/2019 Mql Newbie Book Modificado

    17/24

    In!uts tab shows !arameters that can be set for chosen indicator. The number of settings and the

    meaning of each one de!ends on !rogrammer who coded the indicator. "or e&am!le the indicator

    which is shown on window abo(e ha(e onl# one settings @ !eriod for which each calculation of

    indicator is made. The rest of tabs are res!onsible for (isual !ro!erties of indicator and therefore will

    be omitted.

    /ress 7G for starting the indicator. The indicator should be now !laced on chart.

    %cripts

    Indicator can be run b# double clicing selected )cri!t in a(igator window in )cri!ts subtree.

    %topping the program

    %ach t#!e of !rogram ha(e their own wa# of sto!!ing e&ecution. The scri!ts should end their wor

    without user inter(ention. In case this not ha!!ens the scri!t can be sto!!ed b# right;clicing the

    chart to which the scri!t is attached and clicing emo(e )cri!t o!tion.

  • 8/9/2019 Mql Newbie Book Modificado

    18/24

    In order to remo(e indicator from the chart user need remo(e the indicator from the Indicators List

    of the chart. To achie(e that right;clic on the chart6 choose Indicators List o!tion. In window which

    has been shown choose the indicator #ou want to delete6 and clic the emo(e button. Close the

    window. The indicator should disa!!ear.

  • 8/9/2019 Mql Newbie Book Modificado

    19/24

  • 8/9/2019 Mql Newbie Book Modificado

    20/24

    In order to sto! %&!ert 'd(isor user ha(e two !ossibilities

    ; 8se !anic button which turns off all %&!ert 'd(isors on the !latform

    ; Turn off one %&!ert 'd(isor b# ight;clic on chart to which %&!ert 'd(isor is attached

    and choose emo(e from %&!ert 'd(isors submenu

  • 8/9/2019 Mql Newbie Book Modificado

    21/24

  • 8/9/2019 Mql Newbie Book Modificado

    22/24

    Testing the program

    Testing !"pert #d$isor

    *eall important 

    What is reall# im!ortant during testing the strateg#? Wh# the strateg# testing is im!ortant? The

    answer is sim!le. Testing !ro(ides the !ossibilit# to chec if all assum!tions included into strateg# are

    right6 if the code of the strateg# does e&actl# what was meant b# maer and if there are no

    !otentiall# dangerous errors.

    In :T;Trader !latform three le(els of testing %&!ert 'd(isors can be a!!lied

    ; actesting @ testing on historical data6 some assum!tions described below are taen

    ; 5emo account testing @ testing on demo account with (irtual mone#

    ; eal account testing @ testing on real account6 with real mone#6 but under user

    ad(isor#

    "irst test should be made on actesting6 because it could be done in minutes. The second is running

    strateg# on demo6 which s!ends more time. )ometimes it taes a wee of woring for %&!ert 'd(isor

    on demo account. If e(er#thing is woring as it was in assum!tions6 then there is time for real

    account tests6 where the %&!ert 'd(isor will be tested on battlefield. 'fter that we could be sure

    that our %&!ert 'd(isor %&!ert ad(isor @ for choosing e&!ert ad(isor to be tested.

  • 8/9/2019 Mql Newbie Book Modificado

    23/24

    J> )#mbol @ instrument on which %&!ert 'd(isor will be tested.

    3> Model @ t#!e of data modeling used for testing the %&!ert 'd(isor. It9s (er# im!ortant

    to understand each method of modeling and their im!act to testing results. 'll three

    methods of modeling will be described later.

    4> 8se data @ allows to choose time !eriod for which the %&!ert will be used.

    $> +isual mode @ allows to chec on chart how the %&!ert 'd(isor beha(es during

    !re(iousl# set !eriod of time.

    *> /eriod @ allows to choose on which chart !eriod the %&!ert 'd(isor will be ran

    -> %&!ert !ro!erties @ this button o!ens the window where (arious !arameters can be

    changed. This window will be described later.

    0> )#mbol !ro!erties @ this button shows window with current !ro!erties of chosens#mbol.

    K> )tart @ this button starts the testing

    .emo account testing

    Testing on demo account is Bust running the %&!ert 'd(isor on account with (irtual mone#. The

    %&!ert 'd(isor ha(e to be left woring for some time. There is no difference between real account

    testing and demo testing in the functional matters. The onl# difference is that there is no ris for real

    mone#. If something is going wrong6 Bust turn off %&!ert 'd(isor and chec the ournal Tab in

    Terminal subwindow

    'n# error which were caused b# %&!ert 'd(isor will be noticed here.

    *eal account testing

    Testing on real account needs far more caution than actesting or demo account testing. The reason

    is ob(ious @ real mone# is in(ol(ed. 7f course6 after bactesting and demo account testing we can be

    almost one hundred !ercent sure6 that the %&!ert 'd(isor is woring fine. Howe(er6 the res!onsible

    in(estor should chec on their own if %&!ert 'd(isor is not doing what he doesn9t su!!ose to do on

    real account.

  • 8/9/2019 Mql Newbie Book Modificado

    24/24

    The !rocedure is sim!le run %&!ert 'd(isor and watch if all mo(es done b# %&!ert 'd(isor are done

    right. If something is going wrong6 do the same as for demo account testing !rocedure.

    Testing %cripts

    How to test scri!t is doing what it should do? The rule of the thumb for scri!ts sa#s if the scri!t

    wors as it should and ends in less than minute6 then we could assume that is woring fine. 7f

    course6 first test of the scri!t should be made on demo account. This is es!eciall# (er# im!ortant for

    the scri!ts which o!erate on orders.