Table Of ContentPROGRAMMER EN MQL4
Apprenez à automatiser vos stratégies sur MetaTrader 4
3
© 2011 Henri Baltzer. Tous droits réservés.
Veuillez excuser par avance les nombreuses erreurs qui subsistent probablement dans les
pages suivantes malgré de nombreuses relectures et corrections. L’éditeur et l’auteur
n’endossent par conséquent aucune responsabilité pour l’exactitude ou la précision des
concepts étudiés dans ce livre et ne pourront pas être tenus pour responsables de toutes
pertes éventuelles fictives ou réelles, que l’utilisation des informations contenues dans ce livre
pourraient engendrer.
La programmation étant ce qu’elle est – le reflet de la façon de penser du programmeur -, il est
fort probable qu’au fur et à mesure voire même au début de votre apprentissage, vous
imaginiez des algorithmes (suite d'instructions, qui une fois exécutée correctement, conduit à
un résultat donné) ou des méthodes plus simples pour arriver au même résultat que les codes
qui figurent dans ce livre. Si tel est le cas, ce livre aura déjà rempli l’un de ses objectifs : vous
inciter à utiliser les concepts du livre dans d’autres situations ou de manière différente.
« MetaTrader 4 », « MetaTrader », « MQL4 », « MQL » sont des marques de commerce de
MetaQuotes Software Corp. MetaQuotes Software Corp n’est en aucun cas associé à cet
ouvrage et n’endosse aucune responsabilité en relevant.
Pour obtenir plus d’informations sur ce livre ainsi que des mises à jour et les codes sources de
l’expert consultant et de l’indicateur, rendez-vous sur http://www.eole-trading.com.
Pour contacter l’auteur, vous pouvez écrire à henri@eole-trading.com
ISBN : 978-1-4477-1407-1
4
Sommaire
PRÉFACE...............................................................................................................................11
INTRODUCTION....................................................................................................................12
PLATEFORME..............................................................................................................................12
CONCEPT DE RENVOI.....................................................................................................................13
ICÔNES ET CONVENTIONS DE RÉDACTION...........................................................................................13
TERMINOLOGIE ANGLOPHONE........................................................................................................13
1 LES TYPES DE DONNÉES.....................................................................................................14
LES ENTIERS (INT).........................................................................................................................14
LES RÉELS (DOUBLE).....................................................................................................................15
LES BOOLÉENS (BOOL)...................................................................................................................15
LES CHAÎNES DE CARACTÈRES (STRING).............................................................................................16
LA COULEUR (COLOR)....................................................................................................................16
DATE ET HEURE (DATETIME)...........................................................................................................17
2 LES INSTRUCTIONS............................................................................................................19
3 LES VARIABLES ET LES CONSTANTES..................................................................................20
DÉCLARER UNE VARIABLE...............................................................................................................20
AFFECTATION ET INITIALISATION......................................................................................................22
LOCALES, GLOBALES OU EXTERNES...................................................................................................22
4 EXPRESSIONS ET OPÉRATEURS..........................................................................................25
OPÉRATEURS ARITHMÉTIQUES........................................................................................................26
OPÉRATEURS D’INCRÉMENTATION ET DE DÉCRÉMENTATION..................................................................27
OPÉRATEURS D’AFFECTATION.........................................................................................................28
OPÉRATEURS DE COMPARAISON......................................................................................................29
OPÉRATEURS LOGIQUES................................................................................................................30
OPÉRATEURS AU NIVEAU DU BIT......................................................................................................31
OPÉRATEUR VIRGULE....................................................................................................................32
OPÉRATEUR D’APPEL DE FONCTION ET OPÉRATEUR RETURN..................................................................33
OPÉRATEURS D’INDEXATION DE TABLEAU..........................................................................................33
LA PRIORITÉ DES OPÉRATEURS.........................................................................................................33
5
5 LES FONCTIONS.................................................................................................................35
DÉFINITION ET DÉCLARATION..........................................................................................................35
APPEL DE FONCTION.....................................................................................................................37
OPÉRATEUR RETURN.....................................................................................................................38
IMBRIQUER DES FONCTIONS...........................................................................................................39
FONCTIONS SPÉCIALES...................................................................................................................40
Fonction spéciale int init()...................................................................................................40
Fonction spéciale int start()................................................................................................40
Fonction spéciale int deinit()...............................................................................................40
6 LES INSTRUCTIONS CONDITIONNELLES..............................................................................43
INSTRUCTION CONDITIONNELLE IF….................................................................................................43
INSTRUCTION CONDITIONNELLE IF… ELSE…........................................................................................44
MULTIPLES INSTRUCTIONS IF… ET IF… ELSE….....................................................................................44
INSTRUCTION CONDITIONNELLE SWITCH............................................................................................45
7 LES BOUCLES.....................................................................................................................47
BOUCLE FOR...............................................................................................................................47
BOUCLE WHILE............................................................................................................................49
8 PRÉPROCESSEUR...............................................................................................................51
DIRECTIVE #DEFINE......................................................................................................................51
DIRECTIVE #PROPERTY..................................................................................................................52
DIRECTIVE #INCLUDE....................................................................................................................53
DIRECTIVE #IMPORT.....................................................................................................................53
9 STRUCTURE.......................................................................................................................55
10 COMMENTAIRES..............................................................................................................57
COMMENTAIRES SUR UNE SEULE LIGNE.............................................................................................57
COMMENTAIRES MULTI-LIGNES.......................................................................................................57
COMMENTAIRES DANS « VOTRE PREMIER EXPERT CONSULTANT ».........................................................58
11 VOTRE PREMIER EXPERT CONSULTANT #0 : PRÉPARER LE TERRAIN.................................59
12 VOTRE PREMIER EXPERT CONSULTANT #1 : CRÉATION DU FICHIER..................................61
LANCER METAEDITOR...................................................................................................................61
CRÉER UN NOUVEL EXPERT CONSULTANT...........................................................................................62
COMPILER SON EXPERT..................................................................................................................63
13 VOTRE PREMIER EXPERT CONSULTANT #2 : OBTENTION DU PLUS HAUT/BAS..................66
14 FONCTIONS SUR LES BARRES...........................................................................................71
6
FONCTIONS BARS ET IBARS............................................................................................................71
FONCTIONS CLOSE[] ET ICLOSE().....................................................................................................73
FONCTIONS OPEN[] ET IOPEN()......................................................................................................75
FONCTIONS HIGH[] ET IHIGH().......................................................................................................75
FONCTIONS LOW[] ET ILOW().........................................................................................................76
FONCTIONS TIME[] ET ITIME()........................................................................................................77
FONCTIONS VOLUME[] ET IVOLUME()..............................................................................................78
FONCTIONS IHIGHEST() ET ILOWEST()..............................................................................................78
15 FONCTIONS TEMPORELLES..............................................................................................81
FONCTIONS DAY(), DAYOFWEEK() ET DAYOFYEAR()..........................................................................81
FONCTIONS HOUR(), MINUTE(), SECONDS(), MONTH() ET YEAR()........................................................82
FONCTIONS TIMEDAY(), TIMEDAYOFWEEK() ET TIMEDAYOFYEAR().....................................................84
FONCTIONS TIMEHOUR(), TIMEMINUTE(), TIMESECONDS(), TIMEMONTH() ET TIMEYEAR().....................85
FONCTIONS TIMELOCAL() ET TIMECURRENT()...................................................................................86
16 VOTRE PREMIER EXPERT CONSULTANT #3 : FILTRE HORAIRE...........................................88
17 FONCTIONS GRAPHIQUES................................................................................................91
CRÉER UN OBJET..........................................................................................................................91
PARAMÉTRER UN OBJET.................................................................................................................93
EXEMPLES..................................................................................................................................95
18 VOTRE PREMIER EXPERT CONSULTANT #4 : CRÉATION D’OBJETS GRAPHIQUES.............101
19 FONCTIONS DE TRADING...............................................................................................104
FONCTION DE PASSAGE D’ORDRE...................................................................................................104
FONCTION DE GESTION DES ORDRES...............................................................................................106
OrderClose()......................................................................................................................106
OrderCloseBy()..................................................................................................................106
OrderDelete()....................................................................................................................106
OrderModify()...................................................................................................................107
FONCTIONS POUR SÉLECTIONNER ET OBTENIR DE L’INFORMATION SUR LES POSITIONS..............................108
Fonction de sélection........................................................................................................108
Fonctions pour obtenir de l’information sur les positions.................................................109
20 VARIABLES PRÉDÉFINIES................................................................................................112
21 VOTRE PREMIER EXPERT CONSULTANT #5 : FONCTIONS DE PASSAGE D’ORDRES...........116
22 GESTION DES RISQUES...................................................................................................122
23 FONCTIONS DE CONVERSION.........................................................................................125
7
FONCTION CHARTOSTR()............................................................................................................125
FONCTION DOUBLETOSTR().........................................................................................................125
FONCTION NORMALIZEDOUBLE()..................................................................................................126
FONCTION STRTOINTEGER()........................................................................................................126
FONCTION STRTODOUBLE().........................................................................................................126
FONCTION STRTOTIME().............................................................................................................127
FONCTION TIMETOSTR().............................................................................................................127
24 LA GESTION DES ERREURS..............................................................................................129
25 FONCTIONS COMMUNES...............................................................................................133
FONCTION VOID ALERT().............................................................................................................133
FONCTION VOID COMMENT().......................................................................................................134
FONCTION INT GETTICKCOUNT()..................................................................................................134
FONCTION DOUBLE MARKETINFO()...............................................................................................135
FONCTION INT MESSAGEBOX().....................................................................................................135
FONCTION VOID PLAYSOUND().....................................................................................................138
FONCTION VOID PRINT().............................................................................................................139
FONCTION BOOL SENDFTP()........................................................................................................139
FONCTION VOID SENDMAIL().......................................................................................................140
FONCTION VOID SLEEP()..............................................................................................................141
26 FONCTIONS DE VÉRIFICATION........................................................................................142
FONCTION INT GETLASTERROR()...................................................................................................142
FONCTION BOOL ISCONNECTED()..................................................................................................142
FONCTION BOOL ISDEMO()..........................................................................................................142
FONCTION BOOL ISDLLSALLOWED()...............................................................................................143
FONCTION BOOL ISEXPERTENABLED()............................................................................................143
FONCTION BOOL ISLIBRARIESALLOWED()........................................................................................144
FONCTION BOOL ISTESTING().......................................................................................................144
FONCTION BOOL ISOPTIMIZATION()...............................................................................................145
FONCTION BOOL ISVISUALMODE()................................................................................................145
FONCTION BOOL ISSTOPPED()......................................................................................................145
FONCTION BOOL ISTRADEALLOWED()............................................................................................146
BOOL ISTRADECONTEXTBUSY().....................................................................................................146
27 VOTRE PREMIER EXPERT CONSULTANT #6 : GESTION DU RISQUE ET DES ERREURS........147
28 STOP SUIVEUR ET SEUIL DE RENTABILITÉ.......................................................................153
STOP SUIVEUR...........................................................................................................................153
SEUIL DE RENTABILITÉ.................................................................................................................155
8
29 NUMÉRO MAGIQUE.......................................................................................................157
30 VOTRE PREMIER EXPERT CONSULTANT #7 : STOP SUIVEUR............................................158
31 VARIABLES GLOBALES AU NIVEAU DE LA PLATEFORME..................................................162
FONCTION DATETIME GLOBALVARIABLESET()..................................................................................162
FONCTION BOOL GLOBALVARIABLECHECK()....................................................................................162
FONCTION DOUBLE GLOBALVARIABLEGET()....................................................................................163
FONCTION BOOL GLOBALVARIABLEDEL()........................................................................................163
FONCTION STRING GLOBALVARIABLENAME()..................................................................................163
FONCTION BOOL GLOBALVARIABLESETONCONDITION()....................................................................164
FONCTION INT GLOBALVARIABLESDELETEALL()................................................................................164
FONCTION INT GLOBALVARIABLESTOTAL ().....................................................................................164
EXEMPLES D’APPLICATION DES VARIABLES GLOBALES.........................................................................165
Premier exemple...............................................................................................................165
Deuxième exemple............................................................................................................166
32 VOTRE PREMIER EXPERT CONSULTANT #8 : VARIABLES GLOBALES................................168
33 LE TRAITEMENT DES TABLEAUX EN MQL4......................................................................174
ACCÈS AUX DONNÉES D’UN TABLEAU..............................................................................................175
INITIALISATION D’UN TABLEAU......................................................................................................176
FONCTIONS DE TRAITEMENT DE TABLEAUX......................................................................................176
Fonction int ArrayBsearch()..............................................................................................176
Fonction int ArrayCopy()...................................................................................................177
Fonction int ArrayCopyRates()..........................................................................................178
Fonction int ArrayCopySeries().........................................................................................178
Fonction int ArrayDimension()..........................................................................................179
Fonction int ArrayGetAsSeries()........................................................................................179
Fonction int ArrayInitialize().............................................................................................179
Fonction int ArrayIsSeries()...............................................................................................179
Fonction int ArrayMaximum()..........................................................................................180
Fonction int ArrayMinimum()...........................................................................................180
Fonction int ArrayRange().................................................................................................181
Fonction int ArrayResize().................................................................................................181
Fonction bool ArraySetAsSeries()......................................................................................182
Fonction int ArraySize().....................................................................................................182
Fonction int ArraySort()....................................................................................................182
CALCUL DE FRÉQUENCES RELATIVES...............................................................................................183
34 LES INDICATEURS...........................................................................................................186
9
LES INDICATEURS PERSONNALISÉS..................................................................................................186
Fonction void IndicatorBuffers().......................................................................................189
Fonction int IndicatorCounted()........................................................................................189
Fonction void IndicatorDigits()..........................................................................................189
Fonction void IndicatorShortName ()................................................................................189
Fonction void SetIndexStyle()............................................................................................190
Fonction void SetIndexArrow()..........................................................................................191
Fonction bool SetIndexBuffer().........................................................................................192
Fonction void SetIndexDrawBegin()..................................................................................192
Fonction void SetIndexEmptyValue()................................................................................192
Fonction void SetIndexLabel()...........................................................................................192
Fonction void SetIndexShift()............................................................................................193
Fonction void SetLevelValue()...........................................................................................193
Fonction void SetLevelStyle()............................................................................................193
INTÉGRATION INDICATEUR-EXPERT.................................................................................................196
35 LES FONCTIONS MATHÉMATIQUES................................................................................198
36 LA GESTION DES FICHIERS..............................................................................................199
LOCALISATION DES FICHIERS.........................................................................................................199
FONCTION DE GESTION DES FICHIERS..............................................................................................200
Fonction int FileOpen().....................................................................................................200
Fonction int FileOpenHistory()..........................................................................................200
Fonction int FileReadArray(), double FileReadDouble(), int FileReadInteger(), double
FileReadNumber() et string FileReadString()....................................................................201
Fonction int FileWrite(), int FileWriteArray(), int FileWriteDouble(), int FileWriteInteger()
et int FileWriteString()......................................................................................................201
Fonction void FileClose()...................................................................................................203
Fonction void FileDelete().................................................................................................203
Fonction void FileFlush()...................................................................................................203
Fonction bool FileIsEnding()..............................................................................................203
Fonction bool FileIsLineEnding().......................................................................................203
Fonction bool FileSeek()....................................................................................................204
Fonction int FileSize()........................................................................................................204
Fonction int FileTell()........................................................................................................204
EXEMPLES................................................................................................................................204
Écriture de données diverses dans un fichier....................................................................205
Obtenir et utiliser un fichier depuis Internet.....................................................................206
37 LES FONCTIONS SUR LES CHAÎNES DE CARACTÈRES........................................................211
10
Description:Apprenez à automatiser vos stratégies sur MetaTrader 4. Cet ouvrage est le premier guide de programmation en MQL4 en français. Grâce à ce manuel, vous pourrez apprendre à programmer vos stratégies, modifier des codes existants ou encore tout simplement comprendre la logique derrière certains