Ajouter une étiquette 23 juil.
2025 à 04:49
//+------------------------------------------------------------------+
//| BBot_v2.mq5 |
//| Copyright 2023, MetaQuotes
Software Corp. |
//|
[Link] |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023,
MetaQuotes Software Corp."
#property link "[Link]
#property version "1.00"
//+------------------------------------------------------------------+
//| Input parameters |
//+------------------------------------------------------------------+
input double LotSize=0.2; // Taille de la
position (effet de levier)
input int BBPeriod=20; // Période des
Bandes de Bollinger
input double BBDeviation=2.0; // Déviation
des Bandes de Bollinger
input int RSIPeriod=14; // Période du RSI
input int StochKPeriod=5; // Période K du
Stochastique
input int StochDPeriod=3; // Période D du
Stochastique
input int StochSlowing=3; //
Ralentissement du Stochastique
input int StopLossPips=30; // Stop Loss
initial en pips
input int EntryDistance=3; // Distance
d'entrée après la bande (pips)
input int TrailingStopPips=3; // Trailing Stop
en pips
input int ProfitLockPips=10; // Distance pour
activer le trailing (pips)
input int TakeProfitPips=2; // Distance TP
avant la bande opposée (pips)
//+------------------------------------------------------------------+
//| Global variables |
//+------------------------------------------------------------------+
int handleBB, handleRSI, handleStoch;
double bbUpper[], bbMiddle[], bbLower[];
double rsi[];
double stochMain[], stochSignal[];
MqlTick lastTick;
ulong lastTradeTime=0;
ulong currentPositionTicket=0;
double initialStopLoss=0;
double initialTakeProfit=0;
double breakevenLevel=0;
bool trailingActivated=false;
//+------------------------------------------------------------------+
//| Expert initialization function
|
//+------------------------------------------------------------------+
int OnInit()
{
// Initialiser les indicateurs
handleBB=iBands(NULL,PERIOD_M5,BBPeriod,
0,BBDeviation,PRICE_CLOSE);
handleRSI=iRSI(NULL,PERIOD_M5,RSIPeriod,PRI
CE_CLOSE);
handleStoch=iStochastic(NULL,PERIOD_M5,Stoc
hKPeriod,StochDPeriod,StochSlowing,MODE_SM
A,STO_LOWHIGH);
if(handleBB==INVALID_HANDLE ||
handleRSI==INVALID_HANDLE ||
handleStoch==INVALID_HANDLE)
{
Print("Erreur lors de la création des
indicateurs");
return(INIT_FAILED);
}
// Définir les tableaux comme séries
temporelles
ArraySetAsSeries(bbUpper,true);
ArraySetAsSeries(bbMiddle,true);
ArraySetAsSeries(bbLower,true);
ArraySetAsSeries(rsi,true);
ArraySetAsSeries(stochMain,true);
ArraySetAsSeries(stochSignal,true);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function
|
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Libérer les handles des indicateurs
if(handleBB!=INVALID_HANDLE)
IndicatorRelease(handleBB);
if(handleRSI!=INVALID_HANDLE)
IndicatorRelease(handleRSI);
if(handleStoch!=INVALID_HANDLE)
IndicatorRelease(handleStoch);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Vérifier si c'est un nouveau tick
if(!SymbolInfoTick(_Symbol,lastTick)) return;
// Récupérer les données des indicateurs
if(CopyBuffer(handleBB,BASE_LINE,
0,3,bbMiddle)<3 ||
CopyBuffer(handleBB,UPPER_BAND,
0,3,bbUpper)<3 ||
CopyBuffer(handleBB,LOWER_BAND,
0,3,bbLower)<3 ||
CopyBuffer(handleRSI,0,0,3,rsi)<3 ||
CopyBuffer(handleStoch,0,0,3,stochMain)<3 ||
CopyBuffer(handleStoch,1,0,3,stochSignal)<3)
{
Print("Erreur lors de la copie des données des
indicateurs");
return;
}
// Gérer les positions ouvertes
ManageOpenPositions();
// Vérifier les nouvelles opportunités de trading
(seulement si pas de position ouverte)
if(!PositionSelect(_Symbol))
{
CheckForNewEntries();
}
}
//+------------------------------------------------------------------+
//| Vérifier les nouvelles opportunités d'entrée
|
//+------------------------------------------------------------------+
void CheckForNewEntries()
{
// Ne pas trader à chaque tick - vérifier le temps
if(lastTradeTime==[Link]) return;
lastTradeTime=[Link];
// Calculer la distance en points
double
entryDistancePoints=EntryDistance*_Point*10; //
Pour les paires avec 5 décimales
double
stopLossPoints=StopLossPips*_Point*10;
double
takeProfitPoints=TakeProfitPips*_Point*10;
// Conditions pour une position d'achat
if([Link]<=bbLower[0]+entryDistancePoints
&&
[Link]>bbLower[0] &&
rsi[0]>rsi[1] && rsi[1]>rsi[2] &&
stochMain[0]>stochMain[1] &&
stochMain[1]>stochMain[2] &&
stochMain[0]>stochSignal[0])
{
double sl=bbLower[0]-stopLossPoints;
double tp=bbMiddle[0]-takeProfitPoints;
OpenTrade(ORDER_TYPE_BUY,sl,tp);
}
// Conditions pour une position de vente
else
if([Link]>=bbUpper[0]-entryDistancePoints
&&
[Link]<bbUpper[0] &&
rsi[0]<rsi[1] && rsi[1]<rsi[2] &&
stochMain[0]<stochMain[1] &&
stochMain[1]<stochMain[2] &&
stochMain[0]<stochSignal[0])
{
double sl=bbUpper[0]+stopLossPoints;
double tp=bbMiddle[0]+takeProfitPoints;
OpenTrade(ORDER_TYPE_SELL,sl,tp);
}
}
//+------------------------------------------------------------------+
//| Gérer les positions ouvertes
|
//+------------------------------------------------------------------+
void ManageOpenPositions()
{
if(PositionSelect(_Symbol))
{
ulong
ticket=PositionGetInteger(POSITION_TICKET);
double
currentProfit=PositionGetDouble(POSITION_PRO
FIT);
double
openPrice=PositionGetDouble(POSITION_PRICE_
OPEN);
double
currentStopLoss=PositionGetDouble(POSITION_
SL);
ENUM_POSITION_TYPE
posType=(ENUM_POSITION_TYPE)PositionGetIn
teger(POSITION_TYPE);
// Calculer les distances en points
double
trailingStopPoints=TrailingStopPips*_Point*10;
double
profitLockPoints=ProfitLockPips*_Point*10;
// Vérifier si nous devons activer le trailing
stop
if(posType==POSITION_TYPE_BUY)
{
if([Link]>=openPrice+profitLockPoints
&& !trailingActivated)
{
breakevenLevel=openPrice+trailingStopPoints;
ModifyStopLoss(ticket,breakevenLevel);
trailingActivated=true;
}
else if(trailingActivated &&
[Link]>breakevenLevel+trailingStopPoints)
{
breakevenLevel=[Link]-trailingStopPoints;
ModifyStopLoss(ticket,breakevenLevel);
}
}
else if(posType==POSITION_TYPE_SELL)
{
if([Link]<=openPrice-profitLockPoints
&& !trailingActivated)
{
breakevenLevel=openPrice-trailingStopPoints;
ModifyStopLoss(ticket,breakevenLevel);
trailingActivated=true;
}
else if(trailingActivated &&
[Link]<breakevenLevel-trailingStopPoints)
{
breakevenLevel=[Link]+trailingStopPoints;
ModifyStopLoss(ticket,breakevenLevel);
}
}
}
else
{
// Réinitialis