Nous utilisons cet Expert Advisor (EA) comme un outil de trading efficace.
Voici les 3 paramètres à définir :
- Objectif de Profit
- Limite de Perte
- Numéro Magique
extern double inTargetProfitMoney = 10; //Objectif de Profit (€) extern double inCutLossMoney = 0.0 //Limite de Perte (€) extern int inMagicNumber = 0 //Numéro Magique
Lorsque cet EA est exécuté, il appellera d'abord la fonction OnInit(). C'est ici que nous vérifions les paramètres et l'initialisation des variables.
int OnInit() { //--- if(inTargetProfitMoney <= 0) { Alert("Entrée invalide"); return(INIT_PARAMETERS_INCORRECT); } inCutLossMoney = MathAbs(inCutLossMoney) * -1; //--- return(INIT_SUCCEEDED); }
À chaque mouvement de prix (tick), la fonction OnTick() sera appelée.
void OnTick() { //--- double tFloating = 0.0; int tOrder = OrdersTotal(); for(int i=tOrder-1; i>=0; i--) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if(OrderMagicNumber() == inMagicNumber) { tFloating += OrderProfit()+OrderCommission() + OrderSwap(); } } } if(tFloating >= inTargetProfitMoney || (tFloating <= inCutLossMoney && inCutLossMoney < 0)) { fCloseAllOrders(); } }
Dans la fonction OnTick(), nous continuons à calculer le profit ou la perte totale. Ensuite, nous fermerons tous les ordres qui atteignent l'objectif ou la limite de perte maximale.
void fCloseAllOrders() { double priceClose = 0.0; int tOrders = OrdersTotal(); for(int i=tOrders-1; i>=0; i--) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if(OrderMagicNumber() == inMagicNumber && (OrderType() == OP_BUY || OrderType() == OP_SELL)) { priceClose = (OrderType()==OP_BUY)?MarketInfo(OrderSymbol(), MODE_BID):MarketInfo(OrderSymbol(), MODE_ASK); if(!OrderClose(OrderTicket(), OrderLots(), priceClose, slippage, clrGold)) { Print("AVERTISSEMENT : Échec de la fermeture"); } } } } }
Pour plus d'informations détaillées et de partage de code mql4, rejoignez notre groupe Telegram t.me/codeMQL
Articles connexes
- Générer des Signaux de Trading avec MQL5 Wizard : Étoiles du Matin/du Soir et RSI
- Utiliser MQL5 Wizard pour Créer un Expert Advisor Basé sur les Modèles de Chandeliers Englobants et MFI
- Générez des Signaux de Trading avec MQL5 Wizard : Dark Cloud Cover et Piercing Line
- Développez un Expert Advisor avec MQL5 : Signaux de Trading 3 Corbeaux Noirs / 3 Soldats Blancs + RSI
- AOCCI : Un Expert pour MetaTrader 5