
//+------------------------------------------------------------------+
//|                                 panelektrik@yandex.ru            |
//|                                 Starigin Konstantin              |
//+------------------------------------------------------------------+

#property show_inputs

extern int maxCLOSE=+100;
extern int minCLOSE=-100;


int objekt1(string Trade)
   {
    ObjectCreate("max",OBJ_LABEL,0,0,0);
    ObjectSetText("max","max  =  +"+Trade, 10,"Times New Roman",DodgerBlue);
    ObjectSet("max", OBJPROP_CORNER, 1);
    ObjectSet("max", OBJPROP_XDISTANCE, 20);
    ObjectSet("max", OBJPROP_YDISTANCE, 10);
   }
int objekt2(string Trade)
   {
    ObjectCreate("min",OBJ_LABEL,0,0,0);
    ObjectSetText("min","min  =   "+Trade, 10,"Times New Roman",Tomato);
    ObjectSet("min", OBJPROP_CORNER, 1);
    ObjectSet("min", OBJPROP_XDISTANCE, 20);
    ObjectSet("min", OBJPROP_YDISTANCE, 30);
   }   
   
int start()
{
 objekt1(maxCLOSE);
 objekt2(minCLOSE);
 PlaySound("Wait.wav");
  while(!IsStopped())
  {
  if((AccountEquity()-AccountBalance()>maxCLOSE)||(AccountEquity()-AccountBalance()<minCLOSE))
  if(close()==0)
  {
  PlaySound("Wait.wav");
  ObjectDelete("max");
  ObjectDelete("min");
  break;
  }
  Sleep(10000);
 }
}
//+------------------------------------------------------------------+
int close()
  {
   for(int i=OrdersTotal(); i+1>=0; i--)
   {
    RefreshRates();
    if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderType()==OP_BUY))
    OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),MarketInfo(Symbol(),MODE_SPREAD));
    if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderType()==OP_SELL))
    OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),MarketInfo(Symbol(),MODE_SPREAD));
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
    OrderDelete(OrderTicket());
   }
   return(OrdersTotal());
  }
//+------------------------------------------------------------------+
int deinit()
{
ObjectDelete("max");
ObjectDelete("min");
}