Algo trade: Trades disabled

 
My Bot can't take any trades on XAUUSD even the script likes correct.  What correction should I bring please. 


// global variables
input string POINT            = "====== Numbers are in Points, not Pips ======"; //=====================
input int    Magic_Number     = 241106;                                         //Magic Number                                      //Order Comment
input int    MaxSpread        = 400;                                              //Maximum Spread, 0 = Disabled
input int    Slippage         = 40;
input int    Max_Trades       = 3;                                               //Maximum Trades, 0 = Unlimited

void OnTick()
  {
...
         else
            if(MaxSpread != 0 && SymbolInfoInteger(Symbol(), SYMBOL_SPREAD) > MaxSpread)
              {
               AllowTrading = false;
               Show_Message("Spread: " + (string) SymbolInfoInteger(Symbol(), SYMBOL_SPREAD) + " is Higher than " + (string) MaxSpread + " - Trade Disabled!");
              }
            else
              {
               AllowTrading = true;
               ObjectDelete(0, Prefix + "NoTrade");
              }
...
  }
 

hi add this before on tick

bool AllowTrading = true;

add this bloc in on tick to allow trades

#include <Trade\Trade.mqh>
CTrade trade;

void OnTick()
  {
   // 1. Vérifie spread
   if(MaxSpread != 0 && SymbolInfoInteger(Symbol(), SYMBOL_SPREAD) > MaxSpread)
     {
      AllowTrading = false;
      Print("Spread: ", SymbolInfoInteger(Symbol(), SYMBOL_SPREAD), " > MaxSpread = ", MaxSpread, " → trading désactivé.");
      return;
     }
   else
      AllowTrading = true;

   // 2. Vérifie s'il y a déjà assez de trades ouverts
   int open_trades = 0;
   for(int i = 0; i < PositionsTotal(); i++)
     {
      if(PositionGetTicket(i) != -1)
        {
         if(PositionGetInteger(POSITION_MAGIC) == Magic_Number)
            open_trades++;
        }
     }

   if(Max_Trades != 0 && open_trades >= Max_Trades)
     {
      Print("Nombre max de trades atteints: ", open_trades);
      return;
     }

   // 3. Vérifie si trading est autorisé globalement
   if(!AllowTrading || !TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
     {
      Print("Trading non autorisé par le terminal.");
      return;
     }

   // 4. CONDITIONS DE STRATÉGIE (exemple simplifié)
   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
   double sl = ask - 200 * _Point;
   double tp = ask + 400 * _Point;

   // 5. Envoi d’ordre exemple
   if(trade.Buy(0.1, _Symbol, ask, sl, tp, "Buy Order"))
      Print("Trade envoyé avec succès !");
   else
      Print("Erreur en envoyant l’ordre : ", trade.ResultRetcode(), " - ", trade.ResultRetcodeDescription());
  }

Improperly formatted code edited by moderator. Please always and use the CODE button (Alt-S) when inserting code.

Code button in editor

 
Samuel Bedin #:

hi add this before on tick


add this bloc in on tick to allow trades

Improperly formatted code edited by moderator. Please always and use the CODE button (Alt-S) when inserting code.

Thanks a lot