//+------------------------------------------------------------------+
//|                                              Multi_Ilan_2010.mq4 |
//|                                  Copyright © 2010, Mazur Gennady |
//|                                               gennady@mazuru.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Mazur Gennady"
#property link      "mailto: gennady@mazuru.com"

extern double      Lot=0.01;
extern double      KoefLots=1.66;
extern double      RiskProc=0.5;
extern double      TakeProfit0=60;
extern double      TakeProfitN=7;
extern double      Shag=25;
extern double      UrovenR=55;
extern double      ProfTralProc=70;
extern bool        RejimTral=true;
extern double      ProfBUProc=50;
extern bool        RejimBU=true;
extern bool        RejimTrend=true;
extern bool        RejimLock11=true;
extern int         NLock11=10;
extern bool        ProverHedg=true;
extern bool        Comments=true;
extern int         SdvigInfo=0;
extern bool        Alerts=true;
extern int         HourOp1Day=3;
extern int         HourCl5Day=22;
extern int         MaxNHedj=3;
extern bool        RejimDopProfit=true;
extern double      DopTral=10;
extern string      T1="--КОАЛИЦИЯ ПРОТИВ $ --";
extern string      Para11="EURUSD";
extern string      Para12="GBPUSD";
extern string      Para13="AUDUSD";
extern string      T2="--КОАЛИЦИЯ ЗА $ --";
extern string      Para21="USDCAD";
extern string      Para22="USDJPY";
extern string      Para23="USDCHF";

int dig,totalall;
double pip,minStop,minLot;
int deviat;
int MinuteT,HourT;

int total,totalpr,nBuy,nSell;
double SumSymbol,ppLot,LastLots,LastOpen;
double nLots,Lots;
string TypePoz="";
double ProfitBuyN=0,ProfitSellN=0,Zakupka=0;
double NPriceBuy[12];
double NPriceSell[12];
double NLotBuy[12];
double NLotSell[12];
string Para;
string ParaN[6];
int PriznakPari;
double minBalans;
// usd/cad usd/jpy usd/chf
// eur/usd gbp/usd aud/usd
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   dig=MarketInfo(Symbol(),MODE_DIGITS);
   if(dig==2 || dig==4){
     pip=1*Point;
     deviat=5;
     minStop=MarketInfo(Symbol(),MODE_STOPLEVEL);
     ppLot=MarketInfo(Symbol(),MODE_TICKVALUE);
   }
   else{
     pip=10*Point;
     deviat=50;
     minStop=MarketInfo(Symbol(),MODE_STOPLEVEL)/10;
     ppLot=10*MarketInfo(Symbol(),MODE_TICKVALUE);
   }
   minLot=MarketInfo(Symbol(),MODE_MINLOT);
   switch(minLot){
      case 0.01 : dig=2; break;
      case 0.1 : dig=1; break;
      case 1 : dig=0; break;
      default : dig=1;
   }
   if(Lot<minLot && Lot!=0) Lot=minLot;
   Para="GP_"+Symbol();
   if (GlobalVariableCheck(Para) ==false)   GlobalVariableSet(Para,100);
   else GlobalVariableSet(Para,100);
   ParaN[0]="GP_"+Para11;
   ParaN[1]="GP_"+Para12;
   ParaN[2]="GP_"+Para13;
   ParaN[3]="GP_"+Para21;
   ParaN[4]="GP_"+Para22;
   ParaN[5]="GP_"+Para23;
   if(Para11==Symbol() || Para12==Symbol() || Para13==Symbol()) PriznakPari=1;
   if(Para21==Symbol() || Para22==Symbol() || Para23==Symbol()) PriznakPari=-1;
   if(RiskProc>0.5) RiskProc=0.5;
   minBalans=NormalizeDouble(MarketInfo(Symbol(),MODE_MARGINREQUIRED)*minLot*AccountLeverage()/RiskProc,2);
   double minBlns=minBalans-minBalans*0.05;
   string TORG="------------УДАЧИ В ТОРГОВЛЕ!-----------";
   if(minBalans>AccountBalance()+AccountBalance()*0.05) TORG="ПОПОЛНИТЕ ДЕПОЗИТ ДО НЕОБХОДИМОЙ СУММЫ!";
   Alert("------------- В Н И М А Н И Е ! -------------"
   ,"\n"," Используется принцип Мартингейла "
   ,"\n"," Устанавливайте объем сделок с учетом "
   ,"\n"," данного принципа и Вашего плеча 1:",DoubleToStr(AccountLeverage(), 0)
   ,"\n"," Баланс Вашего счета  ",DoubleToStr(AccountBalance(),2)," единиц"
   ,"\n"," Минимальный необходимый баланс "
   ,"\n"," для торговли  ",Symbol()," ",minBlns," единиц"
   ,"\n"
   ,"\n",TORG
   );
   if(NLock11<6) NLock11=6;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   GlobalVariablesDeleteAll(Para);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   if(Bars<100){
      Print("Количество баров менее 100");
      return(0);
   }
   totalall=OrdersTotal();
   total=totalSymbol(totalall);
   if(total==0){
     ProfitBuyN=0;
     ProfitSellN=0;
     Zakupka=0;
   }
   if(CheckGlobal()!=true) return(0);
   if(Minute()!=MinuteT){
     if(total>0 && DayOfWeek()==5)
       if((Hour()==HourCl5Day && Minute()>50) || Hour()>HourCl5Day)
         if(SumSymbol>0) CloseSymbol(totalall);
     if(total==0){
       double RSI1=iRSI(NULL,5,14,0,1);
       double RSI2=iRSI(NULL,5,14,0,2);
       if(Lot==0) Lots=RaschLot(RiskProc);
       else Lots=Lot;
       if(total==0 && totalpr!=0 && Alerts==true) 
         Alert(Symbol()," Все позиции закрыты");
       totalpr=0;
       if(GlobalVariableGet(Para)==1) TypePoz="BUY";
       if(GlobalVariableGet(Para)==-1) TypePoz="SELL";
       if((DayOfWeek()==1 && Hour()>HourOp1Day) || DayOfWeek()>1)
         if((DayOfWeek()==5 && Hour()<HourCl5Day) || DayOfWeek()<5)
           if(GlobalVariableGet(Para)==0){
             if(RSI1>RSI2 && RSI1<100.0-UrovenR && iClose(NULL,5,1)>iOpen(NULL,5,1)){
               TypePoz="BUY";
             }
             if(RSI1<RSI2 && RSI1>UrovenR && iClose(NULL,5,1)<iOpen(NULL,5,1)){
               TypePoz="SELL";
             }
           }
     }
     else if(total<3){
            if(MathMod(Minute(),5)==0){
              if(nBuy>0) UsloviaOpen("BUY");
              if(nSell>0) UsloviaOpen("SELL");
            }
          }
      else if(total<5){
            if(MathMod(Minute(),15)==0){
              if(nBuy>0) UsloviaOpen("BUY");
              if(nSell>0) UsloviaOpen("SELL");
            }
          }
       else if(total<8){
              if(HourT!=Hour()){
                if(ProverHedg && total==5) Alert(Symbol(),"\n","Открыто ордеров ",total
                                          ,"\n","Проверьте остальные пары на предмет хеджирования данной пары!"
                                          ,"\n","Отключите те пары , что не страхуют убыток данной пары!");
                if(Alerts) Alert(Symbol(),"\n","Открыто ордеров ",total,"\n","Баланс по паре ",NormalizeDouble(SumSymbol,2));
                if(nBuy>1) UsloviaOpen("BUY");
                if(nSell>1) UsloviaOpen("SELL");
                if(RejimLock11 && total==NLock11){
                  if(total==NLock11 && nSell==0) OpenLook("SELL",nLots);
                  if(total==NLock11 && nBuy==0) OpenLook("BUY",nLots);
                }
              }
            }
         else if(MathMod(Hour(),4)==0){
                if(Alerts) Alert(Symbol(),"\n","Открыто ордеров ",total,"\n","Баланс по паре ",NormalizeDouble(SumSymbol,2));
                if(nBuy>1) UsloviaOpen("BUY");
                if(nSell>1) UsloviaOpen("SELL");
                if(RejimLock11 && total==NLock11){
                  if(total==NLock11 && nSell==0) OpenLook("SELL",nLots);
                  if(total==NLock11 && nBuy==0) OpenLook("BUY",nLots);
                }
              }
     if(TypePoz!="") OpenOrder(TypePoz,Lots);
     if(total>1 && total>totalpr){
       if(nBuy>1){
         RaschetProfit("BUY");
         Zakupka=ProfitBuyN-TakeProfitN*pip;
         if(RejimDopProfit) Zakupka=ProfitBuyN-TakeProfitN*2*pip;
         if(nBuy>=8) Zakupka=ProfitBuyN-3*pip;
       }
       if(nSell>1){
         RaschetProfit("SELL");
         Zakupka=ProfitSellN+TakeProfitN*pip;
         if(RejimDopProfit) Zakupka=ProfitSellN+TakeProfitN*2*pip;
         if(nSell>=8) Zakupka=ProfitSellN+3*pip;
       }
     }
     
     totalpr=total;
     MinuteT=Minute();
     HourT=Hour();
   }
   if(total==1) Modiftral(totalall);
   if(total>1) Modifikator(totalall);
   if(Comments==true) Commentary();
//----
   return(0);
  }
//+------------------------------------------------------------------+
void Commentary()
{
//  string koalic="";
//  if(PriznakPari==-1) koalic="Коалиция за $";
//  if(PriznakPari==1) koalic="Коалиция против $";
//  Comment(" --------------- "
//           ,"\n","Брокер: ",AccountCompany(),". Плечо: 1:",DoubleToStr(AccountLeverage(), 0)
//           ,"\n","Минимальный Стоп/Профит уровень  ",minStop," пп."
//           ,"\n","Стоимость 1 пункта 1 лота ",NormalizeDouble(ppLot,2)
//           ,"\n","Минимальный лот  ",minLot
//           ,"\n"," --------------- "
//           ,"\n","Количество открытых ордеров по паре ",total
//           ,"\n","Суммарный объем лота по паре ",nLots
//           ,"\n","Средняя цена открытия по паре ",Zakupka
//           ,"\n","Сумма баланса по паре ",NormalizeDouble(SumSymbol,2)
//           ,"\n","Прибыль/Убыток по счету ",AccountEquity()-AccountBalance()
//           ,"\n"," --------------- "
//           ,"\n",koalic
//           );
           
   ObjectCreate("Broker",OBJ_LABEL,0,0,0);
   ObjectSet("Broker",OBJPROP_XDISTANCE,200+SdvigInfo);
   ObjectSetText("Broker","Брокер: "+AccountCompany()+". Плечо: 1:"+DoubleToStr(AccountLeverage(), 0),12, "Arial Bold", White);

   ObjectCreate("KolOrder",OBJ_LABEL,0,0,0);
   ObjectSet("KolOrder",OBJPROP_XDISTANCE,200+SdvigInfo);
   ObjectSet("KolOrder",OBJPROP_YDISTANCE,80);
   if(nBuy>nSell) ObjectSetText("KolOrder","Количество открытых BUY позиций "+total,12, "Arial Bold", Blue);
   if(nBuy<nSell) ObjectSetText("KolOrder","Количество открытых SELL позиций "+total,12, "Arial Bold", Red);
   if(nBuy==0 && nSell==0) ObjectSetText("KolOrder","Открытых позиций нет",12, "Arial Bold", White);

   if(nBuy>0 || nSell>0){
     ObjectCreate("NLOTS",OBJ_LABEL,0,0,0);
     ObjectSet("NLOTS",OBJPROP_XDISTANCE,200+SdvigInfo);
     ObjectSet("NLOTS",OBJPROP_YDISTANCE,105);
     ObjectSetText("NLOTS","Суммарный объем лота по паре "+nLots,12, "Arial Bold", White);
   
     if(nBuy>1 || nSell>1){
       ObjectCreate("NPrice",OBJ_HLINE,0,0,0);
       ObjectSet("NPrice",OBJPROP_PRICE1,Zakupka);
       ObjectSet("NPrice",OBJPROP_COLOR,White);
       ObjectSet("NPrice",OBJPROP_STYLE,STYLE_DASH);
       
       double TochkaSliva;
       if(nBuy>nSell) TochkaSliva=Zakupka-(AccountBalance()-AccountBalance()*0.2)*pip/(ppLot*nLots);
       if(nBuy<nSell) TochkaSliva=Zakupka+(AccountBalance()-AccountBalance()*0.2)*pip/(ppLot*nLots);

       ObjectCreate("Sliv",OBJ_HLINE,0,0,0);
       ObjectSet("Sliv",OBJPROP_PRICE1,TochkaSliva);
       ObjectSet("Sliv",OBJPROP_COLOR,Red);
       ObjectSet("Sliv",OBJPROP_STYLE,STYLE_DASH);
     }
   
     ObjectCreate("SumBalans",OBJ_LABEL,0,0,0);
     ObjectSet("SumBalans",OBJPROP_XDISTANCE,200+SdvigInfo);
     ObjectSet("SumBalans",OBJPROP_YDISTANCE,130);
     if(SumSymbol>=0) ObjectSetText("SumBalans","Баланс по паре "+NormalizeDouble(SumSymbol,2),12, "Arial Bold", Blue);
     if(SumSymbol<0) ObjectSetText("SumBalans","Баланс по паре --"+NormalizeDouble(SumSymbol,2),12, "Arial Bold", Red);
   }
   
   ObjectCreate("Schet",OBJ_LABEL,0,0,0);
   ObjectSet("Schet",OBJPROP_XDISTANCE,200+SdvigInfo);
   ObjectSet("Schet",OBJPROP_YDISTANCE,155);
   ObjectSetText("Schet","Прибыль/Убыток по счету "+(AccountEquity()-AccountBalance()),12, "Arial Bold", White);
}
int totalSymbol(int ttl)
{
    int cn,NORDER;
    nBuy=0;
    nSell=0;
    nLots=0;
    LastLots=0;
    LastOpen=0;
    SumSymbol=0;
    ArrayInitialize(NPriceBuy,0);
    ArrayInitialize(NLotBuy,0);
    ArrayInitialize(NPriceSell,0);
    ArrayInitialize(NLotSell,0);
    for(cn=0;cn<ttl;cn++){
        OrderSelect(cn, SELECT_BY_POS, MODE_TRADES);
        if(OrderSymbol()==Symbol()){
          NORDER=NORDER+1;
          if(OrderType()==OP_BUY){
            NPriceBuy[nBuy]=OrderOpenPrice();
            NLotBuy[nBuy]=OrderLots();
            LastLots=OrderLots();
            LastOpen=OrderOpenPrice();
            nLots=nLots+LastLots;
            SumSymbol=SumSymbol+OrderSwap()+((Bid-OrderOpenPrice())/pip)*OrderLots()*ppLot;
            nBuy=nBuy+1;
          }
          if(OrderType()==OP_SELL){
            NPriceSell[nSell]=OrderOpenPrice();
            NLotSell[nSell]=OrderLots();
            LastLots=OrderLots();
            LastOpen=OrderOpenPrice();
            nLots=nLots+LastLots;
            SumSymbol=SumSymbol+OrderSwap()+((OrderOpenPrice()-Ask)/pip)*OrderLots()*ppLot;
            nSell=nSell+1;
          }
       }
    }
   if(NORDER==0 && GlobalVariableGet(Para)==100){
     GlobalVariableSet(Para,0);
     Sleep(3000);
   }
   if(NORDER>0 && GlobalVariableGet(Para)!=100) GlobalVariableSet(Para,100);
   return(NORDER);
}
void OpenOrder(string type,double Lt)
{
  int ticket=0;
  int cn=0;
  double SumPip=0;
  double SumLot=0;
  double tp=0;
  double profit=TakeProfitN;
  if(RejimDopProfit) profit=TakeProfitN*2;
  if(total>7) profit=3;
  if(type=="BUY"){
    if(nBuy==0) tp=Ask+TakeProfit0*pip;
    ticket=OrderSend(Symbol(),0,Lt,Ask,deviat,0,tp,"Ilan BUY "+Lt,123456789,0,Blue);
    if(ticket>0 && OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)){
      if(Alerts) Alert(Symbol(),"\n","Открыт новый ордер объемом ",Lt);
      if(GlobalVariableGet(Para)!=100) GlobalVariableSet(Para,100);
      totalall=totalall+1;
      TypePoz="";
      if(nBuy>0){
        SumLot=Lt;
        for(cn=0;cn<nBuy;cn++){
           SumPip=SumPip+(NPriceBuy[cn]-OrderOpenPrice())*NLotBuy[cn];
           SumLot=SumLot+NLotBuy[cn];
        }
        ProfitBuyN=OrderOpenPrice()+profit*pip+SumPip/SumLot;
      }
    }
  }
  if(type=="SELL"){
    if(nSell==0) tp=Bid-TakeProfit0*pip;
    ticket=OrderSend(Symbol(),1,Lt,Bid,deviat,0,tp,"Ilan SELL "+Lt,123456789,0,Red);
    if(ticket>0 && OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)){
      if(Alerts) Alert(Symbol(),"\n","Открыт новый ордер объемом ",Lt);
      if(GlobalVariableGet(Para)!=100) GlobalVariableSet(Para,100);
      totalall=totalall+1;
      TypePoz="";
      if(nSell>0){
        SumLot=Lt;
        for(cn=0;cn<nSell;cn++){
           SumPip=SumPip+(OrderOpenPrice()-NPriceSell[cn])*NLotSell[cn];
           SumLot=SumLot+NLotSell[cn];
        }
        ProfitSellN=OrderOpenPrice()-profit*pip-SumPip/SumLot;
      }
    }
  }        
  if(total>=1){
    if(GlobalVariableGet(Para)!=100) GlobalVariableSet(Para,100);
    Modifikator(totalall);
  }
}
void UsloviaOpen(string type)
{
  TypePoz="";
  int fr=5;
  if(total>2) fr=15;
  if(total>4) fr=60;
  if(total>7) fr=240;
  if(LastLots==0) return(0);
  Lots=NormalizeDouble(LastLots*KoefLots,dig);
  if(type=="BUY" && Ask<LastOpen-Shag*pip && LastOpen!=0){
    if(RejimTrend){
      if(iClose(NULL,fr,1)>=iOpen(NULL,fr,1)) TypePoz="BUY";
    }
    else TypePoz="BUY";
  }
  if(type=="SELL" && Bid>LastOpen+Shag*pip && LastOpen!=0){
    if(RejimTrend){
      if(iClose(NULL,fr,1)<=iOpen(NULL,fr,1)) TypePoz="SELL";
    }
    else TypePoz="SELL";
  }
}
void RaschetProfit(string type)
{
  double profit=TakeProfitN;
  if(RejimDopProfit) profit=TakeProfitN*2;
  if(total>7) profit=3;
  int cn=0;
  double SumPip=0;
  double SumLot=0;
  if(type=="BUY"){
        for(cn=0;cn<nBuy;cn++){
           SumPip=SumPip+(NPriceBuy[cn]-LastOpen)*NLotBuy[cn];
           SumLot=SumLot+NLotBuy[cn];
        }
        ProfitBuyN=LastOpen+profit*pip+SumPip/SumLot;
  }
  if(type=="SELL"){
        for(cn=0;cn<nSell;cn++){
           SumPip=SumPip+(LastOpen-NPriceSell[cn])*NLotSell[cn];
           SumLot=SumLot+NLotSell[cn];
        }
        ProfitSellN=LastOpen-profit*pip-SumPip/SumLot;
  }
}
void Modifikator(int ttl)
{
   int cnt;
   double TP,SL;
   for(cnt=0;cnt<ttl;cnt++){
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&  OrderSymbol()==Symbol()){
         if(OrderType()==OP_BUY){
            if(nSell==11 && nBuy==1){
              TP=0;
              if(OrderStopLoss()==0){
                SL=ProfitSellN;
                OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Lime);
              }
            }
            if(ProfitBuyN!=0 && OrderTakeProfit()!=ProfitBuyN && OrderStopLoss()==0){
               TP=ProfitBuyN;
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TP,0,Lime);
            }
            if(RejimDopProfit){
              if(OrderStopLoss()==0 && Bid-TakeProfitN*pip>Zakupka){
                TP=Zakupka+TakeProfit0*pip/2;
                SL=Zakupka+1*pip;
                OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Lime);
              }
              if(OrderStopLoss()!=0 && Bid-DopTral*pip>Zakupka)
                if(OrderStopLoss()+1*pip<Bid-DopTral*pip){
                  SL=Bid-DopTral*pip;
                  OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0,Lime);
                }
            }    
         }
         else{
            if(nBuy==11 && nSell==1){
              TP=0;
              if(OrderStopLoss()==0){
                SL=ProfitBuyN;
                OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Lime);
              }
            }
            if(ProfitSellN!=0 && OrderTakeProfit()!=ProfitSellN && OrderStopLoss()==0){ 
               TP=ProfitSellN;
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TP,0,Lime);
            }
            if(RejimDopProfit){
              if(OrderStopLoss()==0 && Ask+TakeProfitN*pip<Zakupka){
                TP=Zakupka-TakeProfit0*pip/2;
                SL=Zakupka-1*pip;
                OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Lime);
              }
              if(OrderStopLoss()!=0 && Ask+DopTral*pip<Zakupka)
                if(OrderStopLoss()-1*pip>Ask+DopTral*pip){
                  SL=Ask+DopTral*pip;
                  OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0,Lime);
                }
            }    
         }
      }
   }
//   return(0); 
}
bool CheckGlobal()
{
// usd/cad usd/jpy usd/chf
// eur/usd gbp/usd aud/usd
//Если не все переменные, то возврат
  bool yes=true;
  for(int i=0;i<6;i++)
    if(GlobalVariableCheck(ParaN[i])==false){
      Print("-- Пара ",ParaN[i]," не установлена --");
      Alert("-- Проверте установку советника на паре ",ParaN[i]);
      yes=false;
      break;
    }
  
  if(GlobalVariableGet(Para)==100 && total>=MaxNHedj){
    if(PriznakPari==1){
      if(nBuy>0){
        if(GlobalVariableGet(ParaN[0])!=100) GlobalVariableSet(ParaN[0],-1);
        if(GlobalVariableGet(ParaN[1])!=100) GlobalVariableSet(ParaN[1],-1);
        if(GlobalVariableGet(ParaN[2])!=100) GlobalVariableSet(ParaN[2],-1);
        if(GlobalVariableGet(ParaN[3])!=100) GlobalVariableSet(ParaN[3],1);
        if(GlobalVariableGet(ParaN[4])!=100) GlobalVariableSet(ParaN[4],1);
        if(GlobalVariableGet(ParaN[5])!=100) GlobalVariableSet(ParaN[5],1);
      }
      if(nSell>0){
        if(GlobalVariableGet(ParaN[0])!=100) GlobalVariableSet(ParaN[0],1);
        if(GlobalVariableGet(ParaN[1])!=100) GlobalVariableSet(ParaN[1],1);
        if(GlobalVariableGet(ParaN[2])!=100) GlobalVariableSet(ParaN[2],1);
        if(GlobalVariableGet(ParaN[3])!=100) GlobalVariableSet(ParaN[3],-1);
        if(GlobalVariableGet(ParaN[4])!=100) GlobalVariableSet(ParaN[4],-1);
        if(GlobalVariableGet(ParaN[5])!=100) GlobalVariableSet(ParaN[5],-1);
      }
    }
    if(PriznakPari==-1){
      if(nBuy>0){
        if(GlobalVariableGet(ParaN[0])!=100) GlobalVariableSet(ParaN[0],1);
        if(GlobalVariableGet(ParaN[1])!=100) GlobalVariableSet(ParaN[1],1);
        if(GlobalVariableGet(ParaN[2])!=100) GlobalVariableSet(ParaN[2],1);
        if(GlobalVariableGet(ParaN[3])!=100) GlobalVariableSet(ParaN[3],-1);
        if(GlobalVariableGet(ParaN[4])!=100) GlobalVariableSet(ParaN[4],-1);
        if(GlobalVariableGet(ParaN[5])!=100) GlobalVariableSet(ParaN[5],-1);
      }
      if(nSell>0){
        if(GlobalVariableGet(ParaN[0])!=100) GlobalVariableSet(ParaN[0],-1);
        if(GlobalVariableGet(ParaN[1])!=100) GlobalVariableSet(ParaN[1],-1);
        if(GlobalVariableGet(ParaN[2])!=100) GlobalVariableSet(ParaN[2],-1);
        if(GlobalVariableGet(ParaN[3])!=100) GlobalVariableSet(ParaN[3],1);
        if(GlobalVariableGet(ParaN[4])!=100) GlobalVariableSet(ParaN[4],1);
        if(GlobalVariableGet(ParaN[5])!=100) GlobalVariableSet(ParaN[5],1);
      }
    }
  }
  return(yes);
}
void Modiftral(int ttl)
{
   int cnt;
   double sl;
   for(cnt=0;cnt<ttl;cnt++){
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&  OrderSymbol()==Symbol()){
         if(OrderType()==OP_BUY){
            if(RejimBU==true && nBuy>0 && OrderStopLoss()==0)
               if(Bid-OrderOpenPrice()>(OrderTakeProfit()-OrderOpenPrice())*ProfBUProc/100.0){
                  sl=OrderOpenPrice()+(Ask-Bid);
                  OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,Lime);
               }
            if(RejimTral==true && nBuy>0 && 
               Bid-OrderOpenPrice()>(OrderTakeProfit()-OrderOpenPrice())*ProfTralProc/100.0){
                  sl=Bid-(OrderTakeProfit()-Bid);
                  if(Bid-sl<minStop*pip) sl=Bid-(minStop+1)*pip;
                  if(sl>OrderStopLoss())
                    OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,Lime);
            }
         }
         else{
            if(RejimBU==true && nSell>0 && OrderStopLoss()==0)
               if(OrderOpenPrice()-Ask>(OrderOpenPrice()-OrderTakeProfit())*ProfBUProc/100.0){
                  sl=OrderOpenPrice()-(Ask-Bid);
                  OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,Lime);
              }
            if(RejimTral==true && nSell>0 && 
               OrderOpenPrice()-Ask>(OrderOpenPrice()-OrderTakeProfit())*ProfTralProc/100.0){
                  sl=Ask+(Ask-OrderTakeProfit());
                  if(sl-Ask<minStop*pip) sl=Ask+(minStop+1)*pip;
                  if(sl<OrderStopLoss())
                    OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,Lime);
            }
         }
      }
   }
}
double RaschLot(double Risk)
{
  double Lt=0;
//  double rs=Risk/100.0;
  double balans=AccountBalance()-AccountBalance()*0.1;
  minBalans=NormalizeDouble(MarketInfo(Symbol(),MODE_MARGINREQUIRED)*minLot*AccountLeverage()/Risk,2);
  Lt=NormalizeDouble(minLot*balans/minBalans,dig);
//  Lt=NormalizeDouble(balans*rs/MarketInfo(Symbol(),MODE_MARGINREQUIRED),dig);
  if(Lt<minLot) Lt=minLot;
//  if(Lt>MaxLot) Lt=MaxLot;
  return(Lt);
}
void CloseSymbol(int ttl)
{
   bool   result;
   double price;
   int    c,cmd,error;
   for(c=0;c<ttl;c++){
      if(OrderSelect(c,SELECT_BY_POS,MODE_TRADES)){
         cmd=OrderType();
         if(OrderSymbol()==Symbol()){
               if(cmd==OP_BUY) price=Bid;
               else            price=Ask;
               result=OrderClose(OrderTicket(),OrderLots(),price,deviat,CLR_NONE);
               if(result==TRUE){
                  if(cmd==OP_BUY) nBuy=nBuy-1;
                  if(cmd==OP_SELL) nSell=nSell-1;
               }
         }
      }
   }
   Alert(Symbol()," Закрытие позиций");
}
void OpenLook(string type,double Lt)
{
  
//Стоп по нему равен профиту 10 поз
}

