Exemple de code mql4 sur la stratégie des fractales
//+------------------------------------------------------------------+
//| FRACTAL.mq4 |
//| Copyright 2022, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
//variables
int buyFractal, sellFractal;
double stopLoss, Lot=0.01, takeProfit;
int magicNumber;
// Initialisation de l'indicateur fractal
void OnInit()
{
IndicatorSetString(INDICATOR_SHORTNAME,"Fractal");
}
// Mise à jour de l'indicateur fractal à chaque tick
void OnTick()
{
double fractalPrice = detectFractal();
if(fractalPrice != 0)
{
if(buyFractal != 0)
{
stopLoss = Ask-25;
takeProfit = Ask+50;
magicNumber = 1234;
PlaceOrder(OP_BUYSTOP, fractalPrice, stopLoss, takeProfit, magicNumber);
}
else
{
stopLoss = Bid+25;
takeProfit = Bid-50;
magicNumber = 1235;
PlaceOrder(OP_SELLSTOP, fractalPrice, stopLoss, takeProfit, magicNumber);
}
}
}
void PlaceOrder(int cmd, double price, double stopLoss, double takeProfit, int magicNumber)
{
if(OrdersTotal() == 0)
{
OrderSend(Symbol(),cmd,Lot,price,3,stopLoss,takeProfit,"My
order",magicNumber,0,cmd==OP_BUYSTOP?Green:Red);
}
}
double detectFractal()
{
for(int i=0; i<10; i++)
{
buyFractal = iFractals(Symbol(),PERIOD_CURRENT,MODE_UPPER,i);
sellFractal = iFractals(Symbol(),PERIOD_CURRENT,MODE_LOWER,i);
if(buyFractal != 0)
return buyFractal;
else if(sellFractal != 0)
return sellFractal;
}
return 0;
}
Exemple de code mql4 sur la stratégie des fractales / Part II
//+------------------------------------------------------------------+
//| FRACTAL2.mq4 |
//| Copyright 2022, MetaQuotes Software Corp.|
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
//variables
int buyFractal, sellFractal;
double stopLoss, Lot=0.01, takeProfit;
int magicNumber;
extern int periodeSupport=420;
extern int periodeResistance=420;
extern int periodMmShort=10;
extern int periodMmLong=20;
// Initialisation de l'indicateur fractal
void OnInit()
{
IndicatorSetString(INDICATOR_SHORTNAME,"Fractal");
}
// Mise à jour de l'indicateur fractal à chaque tick
void OnTick()
{
CheckBuyConditions();
CheckSellConditions();
}
// Fonction de détection de fractale bull=> retourne la dernière fractale depuis 10 minutes
double detectBullishFractal()
{
for(int i=0; i<10; i++)
{
double fractalPrice = iFractals(Symbol(), PERIOD_CURRENT, MODE_UPPER, i);
if(fractalPrice != 0)
return fractalPrice;
}
return 0;
}
// Fonction de détection de fractale bear=> retourne la dernière fractale depuis 10 minutes
double detectBearishFractal()
{
for(int i=0; i<10; i++)
{
double fractalPrice = iFractals(Symbol(), PERIOD_CURRENT, MODE_LOWER, i);
if(fractalPrice != 0)
return fractalPrice;
}
return 0;
}
// Fonction de vérification de la tendance : ma10 > ma20 => tendance haussière
bool isTrendUp(int periodeShort, int periodeLong)
{
double ma10 = iMA(Symbol(), PERIOD_CURRENT, periodeShort, 0, MODE_SMA, PRICE_CLOSE, 0);
double ma20 = iMA(Symbol(), PERIOD_CURRENT, periodeLong, 0, MODE_SMA, PRICE_CLOSE, 0);
if(ma10 > ma20)
return true;
else
return false;
}
// Fonction de vérification de la position par rapport au support, détection du points bas depuis x
minutes
bool isAboveSupport(int period)
{
int support = iLowest(Symbol(), PERIOD_CURRENT, MODE_LOW, period, 0);
if(Bid > Low[support] )
return true;
else
return false;
}
// Fonction de vérification de la position par rapport a la resistance, détection du point haut depuis
x minutes
bool isBelowResistance(int period)
{
int resistance = iHighest(Symbol(), PERIOD_CURRENT, MODE_HIGH, period, 0);
if (Ask < High[resistance])
return true;
else
return false;
}
//fonction qui determine la tendance journalière
bool isBelowDailyMA()
{
double ma = iMA(Symbol(), PERIOD_D1, 25, 0, MODE_SMA, PRICE_CLOSE, 0);
if (Ask < ma)
return true;
else
return false;
}
// Fonction pour placer les ordres
void PlaceOrder(int cmd, double price, double stopLoss, double takeProfit, int magicNumber)
{
if(OrdersTotal() == 0)
{
OrderSend(Symbol(),cmd,Lot,price,3,stopLoss,takeProfit,"My
order",magicNumber,0,cmd==OP_BUYSTOP?Green:Red);
}
}
// Vérifier si les conditions sont remplies pour acheter
void CheckBuyConditions()
{
if(isTrendUp(periodMmShort,periodMmLong) && isAboveSupport(periodeSupport)&& isBelowDailyMA()
)
{
double fractalPriceBull = detectBullishFractal();
if(fractalPriceBull != 0 && Ask < fractalPriceBull)
{
double stopLoss = fractalPriceBull-25;
double takeProfit = fractalPriceBull+150;
int magicNumber = 1234;
PlaceOrder(OP_BUYSTOP, fractalPriceBull, stopLoss, takeProfit, magicNumber);
}
}
}
// Vérifier si les conditions sont remplies pour vendre
void CheckSellConditions()
{
if(!isTrendUp(periodMmShort,periodMmLong)&& isBelowResistance(periodeResistance)
&&!isBelowDailyMA() )
{
double fractalPriceBear = detectBearishFractal();
if(fractalPriceBear != 0 && Bid >fractalPriceBear)
{
double stopLoss = fractalPriceBear+25;
double takeProfit = fractalPriceBear-150;
int magicNumber = 1235;
PlaceOrder(OP_SELLSTOP, fractalPriceBear, stopLoss, takeProfit, magicNumber);
}
}
}
Exemple de code pour shorter un marché suracheté
//+------------------------------------------------------------------+
//| venteSurachat.mq4 |
//| Copyright 2022, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
//more information in my site : www.mql4easytrading.com
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if (CompteurBougieHausse(7)>=6)
if ( moyenneRSI(10)>59 && moyenneRSI(10)<71)
vendre(20,180);
}
//+------------------------------------------------------------------+
// compteur de bougie bleu
int CompteurBougieHausse(int NbreBougie) {
int BougieHausse=0;
for (int i=1; i<=NbreBougie;i++) {
double ouverture = Open[i];
double cloture = Close [i];
double amplitude = cloture-ouverture;
if (amplitude>5 )
BougieHausse ++;
}
return BougieHausse;
}
// fonction pour vendre
void vendre(int stop, int profit){
int magic= 23;
double lot=0.01;
if (OrdersTotal()<1)
OrderSend (Symbol(), 5, lot, Bid-2,3,Ask+stop,Ask-profit,"VenteSurAchat",magic,
TimeCurrent()+999,Green);
}
// fonction qui calcule la moyenne du rsi sur les x dernieres bougies
double moyenneRSI (int NbreBougieRSI) {
double sumRSI=0;
for (int x=1; x<NbreBougieRSI+1;x++) {
double RSIx = iRSI(NULL,0,14,PRICE_CLOSE,x);
sumRSI += RSIx;
}
return sumRSI/NbreBougieRSI;
}
Exemple de code pour créer un indicateur personnalisé FIBONACCI
//+------------------------------------------------------------------+
//| SuperFibo.mq4 |
//| Copyright 2022, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_chart_window
extern double FiboLevel = 0.618; // Niveau de Fibonacci à choisir
extern double TimeForFibo = 180;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
// Obtenez le plus haut dans les 180 dernières barres
int highestBar = iHighest(NULL, 0, MODE_HIGH, TimeForFibo, 1);
double highestPrice = high[highestBar];
// Obtenez le plus bas dans les 180 dernières barres
int lowestBar = iLowest(NULL, 0, MODE_LOW, TimeForFibo, 1);
double lowestPrice = low[lowestBar];
// Créer l'objet Fibonacci
string fiboName = "Fibo";
ObjectDelete(0, fiboName);
if (ObjectCreate(0, fiboName, OBJ_FIBO, 0, Time[highestBar], highestPrice, Time[lowestBar],
lowestPrice))
{
// Définir les niveaux de Fibonacci
double levels[] = {0.0, 0.236, 0.382, 0.5, 0.618, 0.786, 1.0, 1.618, 2.618};
for(int i=0; i<ArraySize(levels); i++)
{
ObjectSetDouble(0, fiboName, OBJPROP_LEVELVALUE, i, levels[i]);
ObjectSetInteger(0, fiboName, OBJPROP_LEVELCOLOR, i, clrBlue);
ObjectSetString(0, fiboName, OBJPROP_LEVELTEXT, i, DoubleToString(levels[i], 2));
}
// Fix the Fibo object in the chart
ObjectSetInteger(0, fiboName, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, fiboName, OBJPROP_BACK, false);
ObjectSetInteger(0, fiboName, OBJPROP_HIDDEN, false);
}
return(rates_total);
}
Exemple de code pour entrer et cloturer la position sur le Stochastique
//+------------------------------------------------------------------+
//| testStoka.mq4 |
//| Copyright 2022, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
int magic = 6227;
double lot = 0.01;
extern double stochLow=10;
extern double stochHigh=90;
void OnTick() {
// condition pour entrer en position
double stochK= iStochastic(NULL,0,5,3,3,MODE_SMA,1,MODE_MAIN,0);
double stochD= iStochastic(NULL,0,5,3,3,MODE_SMA,1,MODE_SIGNAL,0);
double stochK1= iStochastic(NULL,0,5,3,3,MODE_SMA,1,MODE_MAIN,1);
double stochD1= iStochastic(NULL,0,5,3,3,MODE_SMA,1,MODE_SIGNAL,1);
// condition pour entrer en position
if (stochK < stochLow && stochD < stochLow && stochK1 < stochD1 && stochK>stochD && OrdersTotal() < 1)
int ticket = OrderSend(Symbol(), OP_BUY, lot, Ask, 3, Ask - 55, Ask + 34, "TESTSTOCH.2", magic, 0, Green);
//verification des conditions de fermeture
for (int i=OrdersTotal()-1; i>=0; i--) {
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)
&& OrderMagicNumber() == magic && OrderSymbol() == Symbol()) {
Print ("Ordre selectionné, ticket :", OrderTicket());
if (OrderType()== OP_BUY && ((OrderProfit() > 0.7 && stochD > stochHigh && stochK>stochHigh) )
|| (OrderProfit() < -1.7 && stochD < stochLow && stochK < stochLow)){
Print ("condition de cloture ok du ticket :", OrderTicket());
bool closeResult=OrderClose(OrderTicket(), OrderLots(), Bid,3,CLR_NONE);
}
}
}
}
Exemple de code pour entrer et cloturer la position sur un croisement de moyennes mobiles
//+------------------------------------------------------------------+
//| testMM.mq4 |
//| Copyright 2022, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
int magic = 6228;
double lot = 0.01;
extern double periodeShort=20;
extern double periodeLong=50;
extern bool closeResult=true;
void OnTick() {
// condition pour entrer en position
double mmS1 = iMA(Symbol(), PERIOD_CURRENT, periodeShort, 0, MODE_SMA, PRICE_CLOSE, 1);
double mmL1 =iMA(Symbol(), PERIOD_CURRENT, periodeLong, 0, MODE_SMA, PRICE_CLOSE, 1);
double mmS2 = iMA(Symbol(), PERIOD_CURRENT, periodeShort, 0, MODE_SMA, PRICE_CLOSE, 2);
double mmL2 = iMA(Symbol(), PERIOD_CURRENT, periodeLong, 0, MODE_SMA, PRICE_CLOSE, 2);
// condition pour entrer en position croissement a la hausse de la mmS avec la mmL
if (mmS2 < mmL2 && mmS1 > mmL1 && OrdersTotal() < 1)
int ticket = OrderSend(Symbol(), OP_BUY, lot, Ask, 3, Ask - 55, Ask + 34, "TESTMM", magic, 0, Green);
for (int i=OrdersTotal()-1; i>= 0; i--) {
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)
&& OrderMagicNumber() == magic && OrderSymbol() == Symbol()) {
Print ("Ordre selectionné, ticket :", OrderTicket());
if (OrderType()== OP_BUY)
if (mmS2 > mmL2 && mmS1 < mmL1)
{
Print ("condition de cloture ok du ticket :", OrderTicket());
if (closeResult)
OrderClose(OrderTicket(), OrderLots(), Bid,3,CLR_NONE);
}
}
}
}
Exemple de code pour trader l'open trade
//+------------------------------------------------------------------+
//| OpenTrade.mq4 |
//| Copyright 2022, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
double highValue=0;
double lowValue=0;
double stop;
double tp;
bool valuesCalculated = false;
bool linesDrawn = false;
int speed = 1000;
int ticket = -1;
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
datetime currentTime = TimeLocal();
string timeString = TimeToString( currentTime, TIME_MINUTES);
// si l'heure actuelle est entre 16h05 et 16h06 et que les valeurs n'ont pas été calculées
if ( StringSubstr (timeString,0,5) >= "16:05" && StringSubstr (timeString,0,5) <= "16:06" && !valuesCalculated)
{
highValue = findHighest();
lowValue = findLowest() ;
stop = (highValue - lowValue) / 2;
tp = (highValue - lowValue) * 2;
valuesCalculated = true;
}
if (valuesCalculated && !linesDrawn)
// tracer les lignes du range de l'ouvertur sur le graphique
{
ObjectCreate(0, "HighLine", OBJ_HLINE,0,0,highValue);
ObjectSetInteger(0,"HighLine", OBJPROP_COLOR,clrGreen);
ObjectCreate(0, "LowLine", OBJ_HLINE,0,0,lowValue);
ObjectSetInteger(0,"LowLine", OBJPROP_COLOR,clrRed);
linesDrawn = true;
}
//placer l'ordre d'achat si les prix cassent les plus hauts (highValue)
if (linesDrawn && Bid > highValue && OrdersTotal() == 0)
{
ticket = OrderSend(Symbol(), OP_BUY, 0.01, Ask, 3, Ask - stop, Ask + tp, "NasdaqOpenTrade", 23, 0, Green);
if (ticket < 0)
Print ("Erreur lors de la création de l'ordre : ", GetLastError());
else
Print ("Ordre d'achat placé à " , Ask);
}
}
//+------------------------------------------------------------------+
double findHighest () {
int highestIndex = iHighest(NULL,0,MODE_HIGH,37,0);
return High[highestIndex];
}
double findLowest() {
int lowestIndex = iLowest(NULL,0,MODE_LOW,37,0);
return Low[lowestIndex];
}
Exemple de code pour la stratégie multiscalper
//+------------------------------------------------------------------+
//| multiscalperV1.mq4 |
//| Copyright 2022, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
// declaration des parametres
input int magic = 666;
input double lot = 0.01;
extern double stopLoss = 20;
extern double tp = 170;
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
bool tendanceVente = true;
for (int i = 0; i < 3; i++) {
double mm8_H1 = movingAverage(8,60,i);
double mm21_H1 = movingAverage(21,60,i);
double high_H1 = iHigh (NULL,60,i);
double low_H1 = iLow (NULL,60,i);
// verifier la tendance sur le 60 minutes => la MM8 est en dessous de la MM21 et que la bougie H1 (en cours) est en dessous de la mm8
if (!(mm8_H1 < mm21_H1 && high_H1 < mm8_H1 && low_H1 < mm8_H1)) {
tendanceVente = false ;
break;
}
}
if (tendanceVente) {
// verifier les conditions pour le 5 minutes
double mm8_M5 = movingAverage(8,5,0);
double mm13_M5 = movingAverage(13,5,0);
double mm21_M5 = movingAverage(21,5,0);
// mm8 < mm13 < mm21
bool setUpVenteM5 = (mm8_M5 < mm13_M5 && mm13_M5 < mm21_M5);
// si toutes les conditions sont réunies
if (!OrdersTotal() /* NbreTradeMagic( magic) < 1 à retrouver dans les formations
*/ && setUpVenteM5 ) {
// determiner la trigger bar
double open_0 = iOpen(NULL,5,0);
double close_0 = iClose(NULL,5,0);
if (close_0 > mm8_M5 && open_0 < mm8_M5) {
// on doit determiner les plus bas sur les 5 dernières bougies (de 1 a 5)
double plusBas = iLow(NULL,5,1);
for (int i=2; i <= 5; i++) {
double low=iLow(NULL,5,i);
if (low < plusBas) {
plusBas=low;
}
}
// placer notre ordre de vente stop
double ventePrix = plusBas-3;
int ticket = OrderSend(NULL,OP_SELLSTOP,lot,ventePrix,3,ventePrix+stopLoss,ventePrix-tp,"multiScalper",magic,TimeCurrent()+1999,Red);
// verifier si l'ordre a bien été passé
if (ticket < 0) {
Print ("Erreur lors de l'exécution du trade", GetLastError());
} else {
Print ("Ordre de vente envoyé, ticket n° = " , ticket);
}
}
}
}
}
//+------------------------------------------------------------------+
//fonctions de calcul de moyennes mobiles simples
double movingAverage (int period, int timeframe, int shift) {
return iMA(NULL, timeframe, period,0,MODE_SMA,PRICE_CLOSE,shift);
}