//+------------------------------------------------------------------+
//|                                                          ãýï.mq4 |
//+------------------------------------------------------------------+

 int magic_buy    =   04356;
 int magic_sell   =   76432; 

 extern double lot =0.1;
 extern int sl =200;
 extern int tp =800; 
 extern int ãýï = 10;
 extern bool zakritie_po_secund = true;  
 extern int secund = 10000000;
 extern int ChasSTART = 2; 
 extern int ChasFINISH = 22; 
 int t;

//+------------------------------------------------------------------+
void Send_buy()
  {if(orders_buy()+orders_sell()==0)OrderSend(Symbol(),OP_BUY,lot,Ask,10*Point,Bid-sl*Point,Ask+tp*Point,"BUY",magic_buy,0,Blue);}
//+------------------------------------------------------------------+  
void Send_sell()
  {if(orders_buy()+orders_sell()==0)OrderSend(Symbol(),OP_SELL,lot,Bid,10*Point,Bid-tp*Point,0,"SELL",magic_sell,0,Red);}
//+------------------------------------------------------------------+
int orders_buy()
  {int j=0;for (int i=0;i<OrdersTotal();i++)
  {if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
  if((OrderType()==OP_BUY)&&(OrderMagicNumber()==magic_buy)&&(OrderSymbol()==Symbol()))j++;
  }return(j);}
//+------------------------------------------------------------------+
int orders_sell()
  {int j=0;for (int i=0;i<OrdersTotal();i++)
  {if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
  if((OrderType()==OP_SELL)&&(OrderMagicNumber()==magic_sell)&&(OrderSymbol()==Symbol()))j++;
  }return(j);}
//+------------------------------------------------------------------+
int ALLClosE()
   {while(!IsStopped()){RefreshRates();
   for(int i=OrdersTotal(); i+1>=0; i--){
   if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderType()==OP_BUY)&&(OrderMagicNumber()==magic_buy)&&(OrderSymbol()==Symbol()))
   OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),MarketInfo(Symbol(),MODE_SPREAD));
   if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderType()==OP_SELL)&&(OrderMagicNumber()==magic_sell)&&(OrderSymbol()==Symbol()))
   OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),MarketInfo(Symbol(),MODE_SPREAD));}
   if(orders_buy()+orders_sell()==0){break;}Sleep(1000);}}
//+------------------------------------------------------------------+
int start()
  {
 if(orders_buy()+orders_sell()==0)t=0;
 if((Hour()>ChasSTART)&&(Hour()<=ChasFINISH)){
 if((t==0)&&(Open[0]-Close[1]>ãýï*Point)){Send_buy();t=Time[0];}
 if((t==0)&&(Close[1]-Open[0]>ãýï*Point)){Send_sell();t=Time[0];}}
 
 if((t>0)&&(zakritie_po_secund==true)&&(TimeCurrent()-t>secund))ALLClosE();
 if((t>0)&&(zakritie_po_secund==false)&&(t!=Time[0]))ALLClosE();
  }
//+------------------------------------------------------------------+