Exness-MT5Real30
//+------------------------------------------------------------------+
//| MA_Cross_EA.mq5 |
//| EA : croisement de moyennes mobiles |
//+------------------------------------------------------------------+
#property copyright "Exemple"
#property version "1.00"
#include <Trade\Trade.mqh>
CTrade trade;
//--- Paramètres (modifiables dans les inputs de MT5)
input int MaRapidePeriode = 10; // Période MA rapide
input int MaLentePeriode = 30; // Période MA lente
input ENUM_MA_METHOD MaMethode = MODE_EMA; // Type de MA
input double Lot = 0.10; // Volume
input int StopLossPoints = 500; // Stop Loss (points)
input int TakeProfitPoints= 1000; // Take Profit (points)
input ulong MagicNumber = 123456; // Identifiant EA
int handleMaRapide = INVALID_HANDLE;
int handleMaLente = INVALID_HANDLE;
datetime derniereBarre = 0;
//+------------------------------------------------------------------+
int OnInit()
{
handleMaRapide = iMA(_Symbol, PERIOD_CURRENT, MaRapidePeriode, 0, MaMethode, PRICE_CLOSE);
handleMaLente = iMA(_Symbol, PERIOD_CURRENT, MaLentePeriode, 0, MaMethode, PRICE_CLOSE);
if(handleMaRapide == INVALID_HANDLE || handleMaLente == INVALID_HANDLE)
{
Print("Erreur création handles MA");
return INIT_FAILED;
}
trade.SetExpertMagicNumber(MagicNumber);
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
IndicatorRelease(handleMaRapide);
IndicatorRelease(handleMaLente);
}
//+------------------------------------------------------------------+
void OnTick()
{
// Traiter uniquement à l'ouverture d'une nouvelle barre
datetime barreActuelle = iTime(_Symbol, PERIOD_CURRENT, 0);
if(barreActuelle == derniereBarre) return;
derniereBarre = barreActuelle;
// Valeurs des MA : index 1 = barre précédente clôturée, 2 = avant-dernière
double maRapide1[2], maLente1[2];
if(CopyBuffer(handleMaRapide, 0, 1, 2, maRapide1) < 2) return;
if(CopyBuffer(handleMaLente, 0, 1, 2, maLente1) < 2) return;
// CopyBuffer retourne du plus ancien au plus récent
double rapPrec = maRapide1[0], rapAct = maRapide1[1];
double lenPrec = maLente1[0], lenAct = maLente1[1];
bool croisementHaussier = (rapPrec <= lenPrec) && (rapAct > lenAct);
bool croisementBaissier = (rapPrec >= lenPrec) && (rapAct < lenAct);
// Fermer les positions existantes si signal opposé
if(PositionsTotal() > 0 && PositionSelect(_Symbol))
{
ENUM_POSITION_TYPE type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
if((type == POSITION_TYPE_BUY && croisementBaissier) ||
(type == POSITION_TYPE_SELL && croisementHaussier))
trade.PositionClose(_Symbol);
}
// Ouvrir une position si aucune n'est ouverte
if(!PositionSelect(_Symbol))
{
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double point = _Point;
if(croisementHaussier)
{
double sl = (StopLossPoints > 0) ? ask - StopLossPoints * point : 0;
double tp = (TakeProfitPoints > 0) ? ask + TakeProfitPoints * point : 0;
trade.Buy(Lot, _Symbol, ask, sl, tp, "MA Cross Buy");
}
else if(croisementBaissier)
{
double sl = (StopLossPoints > 0) ? bid + StopLossPoints * point : 0;
double tp = (TakeProfitPoints > 0) ? bid - TakeProfitPoints * point : 0;
trade.Sell(Lot, _Symbol, bid, sl, tp, "MA Cross Sell");
}
}
}