//+------------------------------------------------------------------+
//|                                              ТРЕЙЛЕР_МОРЖОВА.mq4 |
//|                               Copyright © 2008,  my son          |
//|                                                 k-morzhov@ya.ru  |
//////////////////////////////////////////////////////////////////////
// 


extern double Lots = 0.1;
extern int Step = 70;
extern int slippage = 3;
extern int TakeProfit = 62;
extern double TralingProfit = 15; // профит в центах, при котором ордер начинаем тралить
extern int TS = 30;// значение трала  пп.





string com = "тр_М";
int Magic = 75; 
double LABUDABUY = 0;
double LABUDASELL = 10000;
datetime last_opened_buystop = 0;
datetime last_opened_sellstop = 0;





int start()
{











   int i,buys = 0,sells = 0;
   int buystop = 0,sellstop = 0;
   double buystop_open, sellstop_open,takeprof;
   double buys1,sells1;
   double SL;
   
   for(i = 0; i < OrdersTotal(); i ++)
   {
      OrderSelect(i, SELECT_BY_POS);
      if(OrderMagicNumber() != Magic) continue;
      
      if(OrderType() == OP_BUYSTOP)
      {
         buystop = OrderTicket();
         buystop_open = OrderOpenPrice();
      }
      if(OrderType() == OP_SELLSTOP)
      {
         sellstop = OrderTicket();
         sellstop_open = OrderOpenPrice();
      }
      
      if(OrderType() == OP_BUY)
         buys = OrderTicket();
         
    
      if(OrderType() == OP_SELL)
         sells = OrderTicket();         
   }
   
   if(buystop == 0 && TimeCurrent() - last_opened_buystop > 60)
            {   
                buystop_open = Ask+Step*Point;
          
                
                
                if(!IsTradeAllowed())
                
                return(-1);
                   
    
           
    
      
                  buystop = OrderSend(Symbol(),OP_BUYSTOP,Lots,buystop_open,slippage,0,buystop_open+TakeProfit*Point,com,Magic,0,Blue);
      
                  last_opened_buystop = TimeCurrent();
             }
   if(sellstop == 0 && TimeCurrent() - last_opened_sellstop > 60)
             {
                  sellstop_open = Bid-Step*Point;
                  
           
                  if(!IsTradeAllowed())
                
                  return(-1);
                     
    
     
    
                  
                  sellstop = OrderSend(Symbol(),OP_SELLSTOP,Lots,sellstop_open,slippage,0,sellstop_open-TakeProfit*Point,com,Magic,0,Crimson);   
                  last_opened_sellstop = TimeCurrent();
             }   
      
   if(buys != 0 || sells != 0)
            {
               for(i = 0; i < OrdersTotal(); i ++)
                      {
                         OrderSelect(i, SELECT_BY_POS);
                         if(OrderMagicNumber() != Magic || OrderProfit() < TralingProfit) continue;
                                                                                                                
          
                                                                                 
   if(OrderType() == OP_BUY)
      {
               buys1 = TS*Point;
              
               SL = Bid - buys1;
               
                  if(SL >= LABUDABUY)
                     {
                        LABUDABUY = SL;
                    
                            
                        if(!IsTradeAllowed())
                          
                        return(-1);
                          
                  
                        
                  
                         OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0,MediumSpringGreen);
                      }
      }
   
                      
           
   if(OrderType() == OP_SELL )
   
      {
               sells1 = TS*Point;         
               
               SL = Ask + sells1;
               
                  if(SL <= LABUDASELL)
                     {
                         LABUDASELL = SL;
               
                    
               
                         if(!IsTradeAllowed())
                          
                         return(-1);
                            
                  
                       
                  
                  
                         OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0,Orange);
                      }   
      }
            
                      }      
            }  
  
   
 
       
         
         
   if(LABUDABUY != 0 && sellstop_open != LABUDABUY && LABUDABUY > sellstop_open)
   
      {        
      
            takeprof = LABUDABUY-TakeProfit*Point;
       
            if(!IsTradeAllowed())
              
            return(-1);
                
                     
                
                     

                   OrderModify(sellstop,LABUDABUY,0,takeprof,0,Orange);
      }
      
   if(LABUDABUY == 0 && Bid-TS*Point > sellstop_open)
                                                    
       {
       
            takeprof = Bid-TS*Point-TakeProfit*Point;
                   
            if(!IsTradeAllowed())
               
            return(-1);
               
                     
               
                   
                   OrderModify(sellstop,Bid-TS*Point,0,takeprof,0,Orange);
                   
        }
      
      
      
      
      
   if(LABUDASELL !=10000 && buystop_open != LABUDASELL && LABUDASELL < buystop_open)
       {
             takeprof = LABUDASELL+TakeProfit*Point;
       
             if(!IsTradeAllowed())
                 {
            
                   return(-1);
                 }
                     
                  
     
     
                                       
                   OrderModify(buystop,LABUDASELL,0,takeprof,0,MediumSpringGreen);
                                     
       }
         
         
   if(LABUDASELL == 10000 && Ask+TS*Point < buystop_open && buystop!=0)
                                                        
       {
       
            takeprof = Ask+TS*Point+TakeProfit*Point;
                   
            if(!IsTradeAllowed())
                {
        
                   return(-1);
                }
                     
                    
                   
                   OrderModify(buystop,Ask+TS*Point,0,takeprof,0,MediumSpringGreen);
        }
             
             
             
                       
      
        
        
        
        
       
             
                
   return(0);
}
 