//+------------------------------------------------------------------+
//|                                           EqutyTrawlerXR_V00.mq4 |
//|                                 Copyright © 2009, XrustSolution. |
//|                                        http://www.xrust.ucoz.net |
//+------------------------------------------------------------------+
#property copyright "#Copyright © 2009, XrustSolution.#"
#property link      "#http://www.xrust.ucoz.net#"
extern double       EqutyPersent      =   1;
extern double       RepeatTimeinSec   =   1;
//+------------------------------------------------------------------+
void start(){double step=1;
  if(RepeatTimeinSec==0){RepeatTimeinSec=0.1;}
  while(!IsStopped()&&IsExpertEnabled()){
    Sleep(1000*RepeatTimeinSec);
    if(AccountEquity()>AccountBalance()){
      if(AccountProfit()>AccountEquity()/100*EqutyPersent*step){step++;}
      if(step>1){
        if(AccountProfit()<=AccountEquity()/100*EqutyPersent*(step-1)){
          CloseAll();
        }
      }
    }
  }
return;}
//+------------------------------------------------------------------+
// Закрывает все ордера на данном инструменте                        |
//+------------------------------------------------------------------+
void CloseAll(){
for(int n=OrdersTotal()+1;n>=0;n--){
  if(OrderSelect(n,SELECT_BY_POS,MODE_TRADES)){ 
    if(OrderType()<2){ 
      del(OrderTicket());
    }  
  }    
}  
return;    
}
//+------------------------------------------------------------------+
//Удаляет рыночный ордер с указанным ей тикетом                      |
//+------------------------------------------------------------------+
void del(int ticket){int err;
 for(int i=0;i<1;i++){
   GetLastError();//обнуляем ошику
   OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES);
   string symbol = OrderSymbol();
   if(OrderType()==OP_BUY){RefreshRates();
     double prise = MarketInfo(symbol,MODE_BID);
     if(!OrderClose(ticket,OrderLots(),prise,3,Green)){err = GetLastError();}}
   if(OrderType()==OP_SELL){RefreshRates();
     prise = MarketInfo(symbol,MODE_ASK);
     if(!OrderClose(ticket,OrderLots(),prise,3,Green)){err = GetLastError();}}
 if(err == 0){PlaySound("expert.wav");break;} 
 if(err != 0){PlaySound("timeout.wav");Print("Error for Close Funtion =",err);} 
 while(!IsTradeAllowed()){Sleep(5000);}// если рынок занят то подождем 5 сек 
 if (err==146) while (IsTradeContextBusy()) Sleep(1000*11);
 } 
}