//+------------------------------------------------------------------+ //| ProjectName | //| Copyright 2020, CompanyName | //| http://www.companyname.net | //+------------------------------------------------------------------+ #property copyright "Copyright 2024, eric TOLLENAERS" #property link "https://www.mql5.com" #property version "1.00" // Déclaration des paramètres d'entrée input int stochPeriod = 14; // Période du Stochastique input int overboughtLevel = 80; // Niveau de surachat input int oversoldLevel = 20; // Niveau de survente input double stopLossPercent = 0.10; // Pourcentage de perte maximal autorisé input double initialDeposit = 1000; // Dépôt initial input double riskPercent = 2; // Pourcentage de risque par trade // Déclaration des variables globales bool isTradeOpen = false; int tradeType = 0; // 0: pas de trade, 1: achat, -1: vente //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int OnInit() { return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnTick() { // Obtenir les valeurs actuelles du Stochastique double currentSlowK = iStochastic(NULL, 0, stochPeriod, 0, MODE_SMA, 0, MODE_SMA, 0, MODE_SMA, 0, MODE_MAIN, 0); double currentSlowD = iStochastic(NULL, 0, stochPeriod, 0, MODE_SMA, 0, MODE_SMA, 0, MODE_SMA, 0, MODE_SIGNAL, 0); // Calcul de la taille du lot en fonction du risque double riskAmount = initialDeposit * riskPercent / 100; double lotSize = riskAmount / (stopLossPercent * MarketInfo(Symbol(), MODE_POINT)); // Condition d'entrée if(!isTradeOpen) { if(currentSlowK >= overboughtLevel && currentSlowD >= overboughtLevel) { // Ouvrir un trade à la vente double stopLossPrice = NormalizeDouble(Ask + (stopLossPercent * MarketInfo(Symbol(), MODE_POINT)), Digits); double takeProfitPrice = 0; // Mettre le take profit selon votre stratégie if(OrderSend(Symbol(), OP_SELL, lotSize, Bid, 3, stopLossPrice, takeProfitPrice, "Sell Trade", 0, 0, Red) > 0) { isTradeOpen = true; tradeType = -1; } } else if(currentSlowK <= oversoldLevel && currentSlowD <= oversoldLevel) { // Ouvrir un trade à l'achat double stopLossPrice = NormalizeDouble(Bid - (stopLossPercent * MarketInfo(Symbol(), MODE_POINT)), Digits); double takeProfitPrice = 0; // Mettre le take profit selon votre stratégie if(OrderSend(Symbol(), OP_BUY, lotSize, Ask, 3, stopLossPrice, takeProfitPrice, "Buy Trade", 0, 0, Green) > 0) { isTradeOpen = true; tradeType = 1; } } } else { // Condition de sortie if((tradeType == -1 && (currentSlowK < overboughtLevel || currentSlowD < overboughtLevel)) || (tradeType == 1 && (currentSlowK > oversoldLevel || currentSlowD > oversoldLevel))) { // Fermer le trade for(int i = OrdersTotal() - 1; i >= 0; i--) { if(OrderSelect(i, SELECT_BY_POS) && OrderType() == (tradeType == -1 ? OP_SELL : OP_BUY) && OrderSymbol() == Symbol()) { OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 3); isTradeOpen = false; tradeType = 0; } } } } double CalculateLotSize() { double balance = AccountBalance(); double lotSize = NormalizeDouble((balance * 0.02) / MarketInfo(Symbol(), MODE_MARGINREQUIRED), 2); // 2% du solde if lotSize > MarketInfo(Symbol(), MODE_MAXLOT); lotSize = MarketInfo(Symbol(), MODE_MAXLOT); if lotSize < MarketInfo(Symbol(), MODE_MINLOT); lotSize = MarketInfo(Symbol(), MODE_MINLOT); } //+------------------------------------------------------------------+