//+------------------------------------------------------------------+
//| EA: Disable AutoTrading At Profit                                |
//| Author: GPT EA Builder                                           |
//+------------------------------------------------------------------+
#property strict

//--- Import Windows API
#import "user32.dll"
   void keybd_event(int bVk, int bScan, int dwFlags, int dwExtraInfo);
#import

//--- Key codes
#define  REL  0x0002
#define  CTRL 0x11
#define  E    0x45

//--- Action Options
enum enAction {
   Disable,    // Disable AutoTrading (toggle Ctrl+E)
   Remove,     // Remove this EA from chart
   Pause       // Pause trading for X seconds
};

input double Profit = 200;   // Profit in Account Currency
input bool CurrentSymbolOnly = true; // Count trades only on this symbol?
input enAction ActionAfterWins = Disable; // Action when target reached
input int PauseSeconds = 60;         // Pause duration if Pause selected

//+------------------------------------------------------------------+
//| Expert initialization                                            |
//+------------------------------------------------------------------+
int OnInit()
{
   Print("EA started. Action after ", DoubleToString(Profit,2), " profit: ", EnumToString(ActionAfterWins));
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{

   CheckProfit();
}

//+------------------------------------------------------------------+
//| Function: Check closed trades                                    |
//+------------------------------------------------------------------+
void CheckProfit()
{
   int total = OrdersTotal();

   for(int i = total-1; i >= 0; i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
            if(CurrentSymbolOnly && OrderSymbol() != Symbol())
               continue;

               double profit = OrderProfit();
               profit++;
               if(profit >= Profit)
               {
                  Print(">>> Target reached: ", DoubleToString(Profit,2), " profit. Executing action: ", EnumToString(ActionAfterWins));
                  TakeAction();
                  return;
               }
      }
   }
}

//+------------------------------------------------------------------+
//| Function: Take action based on user input                        |
//+------------------------------------------------------------------+
void TakeAction()
{
   switch(ActionAfterWins)
   {
      case Disable:
         if(TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
         {
            Print("AutoTrading is ON. Disabling with Ctrl+E...");
            keybd_event(CTRL,0,0,0);
            keybd_event(E,0,0,0);
            keybd_event(CTRL,0,REL,0);
            keybd_event(E,0,REL,0);
         }
         else
         {
            Print("AutoTrading already disabled. No action taken.");
         }
         break;

      case Remove:
         Print("Removing EA from chart...");
         ExpertRemove();
         break;

      case Pause:
         {
            int ms = PauseSeconds * 1000;  // convert to milliseconds
            Print("Pausing EA for ", PauseSeconds, " seconds (", ms, " ms).");
            Sleep(ms); // Blocks only this EA
         }
         break;
   }
}
//+------------------------------------------------------------------+
