//Version  January 7, 2007 Final
//+------------------------------------------------------------------+
//|                                                  Lite_EXPERT.mqh |
//|                             Copyright © 2006,   Nikolay Kositsin | 
//|                              Khabarovsk,   farria@mail.redcom.ru | 
//+------------------------------------------------------------------+
 
//+------------------------------------------------------------------+
//| StopCorrect()                                                    |
//+------------------------------------------------------------------+
int StopCorrect(int Stop)
  {
//----
   int CorrStop,Extrem_Stop;
   Extrem_Stop=MarketInfo(Symbol(),MODE_STOPLEVEL);
   if(Extrem_Stop<=0)return(-1);
   if(Stop<Extrem_Stop)
    {
     CorrStop=Extrem_Stop;
     if(IsOptimization())
      {
       Print("StopCorrect: Неверные стопы!!!");
       PlaySound(ALERT.WAV);
      }
     else Alert("StopCorrect: Неверные стопы!!!");
    }
   else CorrStop= Stop;
   return(CorrStop);
//----
  }
//+-------------------------------------------------------------------------------------------+
//| OpenBuyOrder()                                                                            |
//+-------------------------------------------------------------------------------------------+
bool OpenBuyOrder(bool BUY_Signal, double Money_Management, int STOPLOSS, int TAKEPROFIT)
 {
  if (!BUY_Signal)return(false); 
  int total=OrdersTotal();
  //---- Проверка на наличие открытой позиции по данной торговой паре в Buy направлении
  for(int ttt=total-1;ttt>=0;ttt--)     
      if (OrderSelect(ttt,SELECT_BY_POS,MODE_TRADES))
           if((OrderSymbol()==Symbol())&&(OrderType()==0))return(false); 
  //---- 
  double ask=NormalizeDouble(Ask,Digits); 
  double bid=NormalizeDouble(Bid,Digits);
  if (ask==0)return(false);   
  double LotVel;      
  double tickVel=MarketInfo(Symbol(),MODE_TICKVALUE);    
  if (tickVel==0)return(true);   
  if (Money_Management>0)LotVel=tickVel*AccountEquity()*Money_Management/10000.0;
  else LotVel=-tickVel*10000*Money_Management/10000.0;
  //----
  int Lot=NormalizeDouble(LotVel,1);   
  if (Lot<0.1)return(false);   
  //----+ Открываем позицию на покупку
  double Stoploss=NormalizeDouble(bid-STOPLOSS*Point,Digits);  
  double TakeProfit=NormalizeDouble(ask+TAKEPROFIT*Point,Digits);  
  int ticket=OrderSend(Symbol(),OP_BUY,Lot,ask,3,Stoploss,TakeProfit,NULL,0,0,CLR_NONE); 
  return(true);   
 }  
//+-------------------------------------------------------------------------------------------+
//| OpenSellOrder()                                                                           |
//+-------------------------------------------------------------------------------------------+
bool OpenSellOrder(bool SELL_Signal, double Money_Management, int STOPLOSS, int TAKEPROFIT)
 {
  if (!SELL_Signal)return(false); 
  int total=OrdersTotal();
  //---- Проверка на наличие открытой позиции по данной торговой паре в Sell направлении 
  for(int kkk=total-1;kkk>=0;kkk--)      
     if (OrderSelect(kkk,SELECT_BY_POS,MODE_TRADES))
           if((OrderSymbol()==Symbol())&&(OrderType()==1))return(false); 
  //---- 
  double bid=NormalizeDouble(Bid,Digits); 
  double ask=NormalizeDouble(Ask,Digits);
  if (bid==0.0)return(true);   
  double LotVel;      
  double tickVel=MarketInfo(Symbol(),MODE_TICKVALUE);
  if (tickVel==0.0)return(true);   
  if (Money_Management>0)LotVel=tickVel*AccountEquity()*Money_Management/10000.0;
  else LotVel=-tickVel*10000*Money_Management/10000.0;
  //----
  int Lot=NormalizeDouble(LotVel,1); 
  if (Lot<0.1)return(false);      
  //----+ Открываем позицию на продажу 
  double Stoploss=NormalizeDouble(ask+STOPLOSS*Point,Digits);         
  double TakeProfit=NormalizeDouble(bid-TAKEPROFIT*Point,Digits);
  int ticket=OrderSend(Symbol(),OP_SELL,Lot,bid,3,Stoploss,TakeProfit,NULL,0,0,CLR_NONE); 
  return(true);
 }              
//+-------------------------------------------------------------------------------------------+