//+------------------------------------------------------------------+
//| EA: Disable AutoTrading After Winning Trades                     |
//| 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 int WinningTradesToStop = 2;   // Number of consecutive winning trades before action
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

int consecutiveWins = 0;

//+------------------------------------------------------------------+
//| Expert initialization                                            |
//+------------------------------------------------------------------+
int OnInit()
{
   Print("EA started. Action after ", WinningTradesToStop, " wins: ", EnumToString(ActionAfterWins));
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   static datetime lastCheck = 0;
   if(TimeCurrent() == lastCheck) return;
   lastCheck = TimeCurrent();

   CheckClosedTrades();
}

//+------------------------------------------------------------------+
//| Function: Check closed trades                                    |
//+------------------------------------------------------------------+
void CheckClosedTrades()
{
   consecutiveWins = 0;
   int total = OrdersHistoryTotal();

   for(int i = total-1; i >= 0; i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
      {
         if(OrderCloseTime() > 0) // Closed trade
         {
            if(CurrentSymbolOnly && OrderSymbol() != Symbol())
               continue;

            if(OrderProfit() > 0) // Winning trade
            {
               consecutiveWins++;
               if(consecutiveWins >= WinningTradesToStop)
               {
                  Print(">>> Target reached: ", consecutiveWins, " winning trades. Executing action: ", EnumToString(ActionAfterWins));
                  TakeAction();
               }
            }
            else
            {
               // Reset streak if a losing trade is found
               break;
            }
         }
      }
   }
}

//+------------------------------------------------------------------+
//| 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;
   }
}
//+------------------------------------------------------------------+
