//+------------------------------------------------------------------+
//|                                                    Nabivalka.mq4 |
//+------------------------------------------------------------------+
 extern int Magic = 6543532;
 extern double Lot = 0.1;         //Лот
 extern int Otstup_BUY_SELL=0;   //Отступ между отложками 

//+------------------------------------------------------------------+
int orders(int type,string com){int j=0;for(int i=0;i<OrdersTotal();i++){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
 if((OrderType()==type)&&(OrderComment()==com)&&(OrderMagicNumber()==Magic)&&(OrderSymbol()==Symbol()))j++;}return(j);}
//+------------------------------------------------------------------+
int orderss(string com){int j=0;for(int i=0;i<OrdersTotal();i++){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
 if((OrderType()==OP_BUY||OrderType()==OP_SELL)&&(OrderMagicNumber()==Magic)&&(OrderSymbol()==Symbol())&&(OrderComment()==com))j++;}return(j);}
//+------------------------------------------------------------------+
int Allorders(string com){int j=0;for(int i=0;i<OrdersTotal();i++){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
 if((OrderMagicNumber()==Magic)&&(OrderSymbol()==Symbol())&&(OrderComment()==com))j++;}return(j);}
//+------------------------------------------------------------------+  
void Send_buystop(double CENA){if(orders(OP_BUYSTOP,"UP")==0)OrderSend(Symbol(),OP_BUYSTOP,Lot,NormalizeDouble(CENA,Digits),10,0,0,"UP",Magic,0,Blue);}
void Send_selllimit(double CENA){if(orders(OP_SELLLIMIT,"UP")==0)OrderSend(Symbol(),OP_SELLLIMIT,Lot,NormalizeDouble(CENA,Digits),10,0,0,"UP",Magic,0,Red);}
//+------------------------------------------------------------------+ 
void Send_sellstop(double CENA){if(orders(OP_SELLSTOP,"DW")==0)OrderSend(Symbol(),OP_SELLSTOP,Lot,NormalizeDouble(CENA,Digits),10,0,0,"DW",Magic,0,Red);}
void Send_buylimit(double CENA){if(orders(OP_BUYLIMIT,"DW")==0)OrderSend(Symbol(),OP_BUYLIMIT,Lot,NormalizeDouble(CENA,Digits),10,0,0,"DW",Magic,0,Blue);}
//+------------------------------------------------------------------+  
void ALLOrderCloseBy(string com)
 {RefreshRates();int ticetBuy,ticetSell;for(int i=OrdersTotal();i+1>=0;i--)
 {if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderMagicNumber()==Magic)&&(OrderSymbol()==Symbol())&&(OrderComment()==com))
 {if(OrderType()==OP_BUY)ticetBuy=OrderTicket();
 if(OrderType()==OP_SELL)ticetSell=OrderTicket();}}
 if(orders(OP_BUY,"UP")+orders(OP_SELL,"UP")==2)OrderCloseBy(ticetBuy,ticetSell);
 if(orders(OP_BUY,"DW")+orders(OP_SELL,"DW")==2)OrderCloseBy(ticetBuy,ticetSell);}
//+------------------------------------------------------------------+
 int ALLCloseOtl(string com){RefreshRates();for(int i=OrdersTotal(); i+1>=0; i--){
 if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)&&(OrderComment()==com)){
 if(OrderType()==OP_BUYSTOP||OrderType()==OP_BUYLIMIT||OrderType()==OP_SELLSTOP||OrderType()==OP_SELLLIMIT)OrderDelete(OrderTicket());}}}
//+------------------------------------------------------------------+
int start()
  {
//----
 ALLOrderCloseBy("UP");
 ALLOrderCloseBy("DW");
//----
  }
//+------------------------------------------------------------------+