extern double target = 3854.0;

int OnInit() { EventSetTimer(30);
   return(INIT_SUCCEEDED); }

void OnTick() { }

void OnDeinit(const int reason) {}

void OnTimer() {
   if (AccountEquity() >= target) Close_All_Trades(); }

void Close_All_Trades() { string temp;
  for (int i = OrdersTotal()-1; i >= 0; i --) {
    if (OrderSelect(i, SELECT_BY_POS)) { bool result = false;
       switch(OrderType()) {
          //Close opened long positions
          case OP_BUY  : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Pink);   // Slippage
                         break;
          //Close opened short positions
          case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Pink);   // Slippage
                         break;
          //Close pending orders
            case OP_BUYLIMIT  : result = OrderDelete( OrderTicket() );     break;
            case OP_BUYSTOP   : result = OrderDelete( OrderTicket() );     break;
            case OP_SELLLIMIT : result = OrderDelete( OrderTicket() );     break;
            case OP_SELLSTOP  : result = OrderDelete( OrderTicket() ); }
       if (result == false) Sleep(2000); } }

  temp = "***** Account Target Reached. All Trades Have Been Closed. (" + DoubleToStr(AccountBalance(), 2) + " *****";      Alert(temp); }
