//◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘

//                   |||||||||||||||||||||||||||||||||||
//                   ||                               ||                                                                                                                
//                   ||     -= Highlow EA 5.05 =-     ||
//                   ||           24.08.2017          ||
//                   ||                               ||
//                   |||||||||||||||||||||||||||||||||||

#property copyright   "TradeLikeaPro.ru"
#property link        "http://forum.tradelikeapro.ru/index.php?topic=15142.0"
#property description "Very flexible EA, trade which is based on the intersection"
#property description "of extrema price of the previous candle."
#property description "----------------------------------------------------------"
#property description "Очень гибкий советник, торговля которого основана"
#property description "на пересечении цены экстремумов предыдущей свечи."

//◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘

enum iTrades   { Both_Sides = 0, Only_Long = 1, Only_Short = 2, Stop_Work = 4                  };

enum iMode     { Break = 1, Bounce = 2, GreenRed = 3                                           };

enum iTF       { M1  = PERIOD_M1,  M5  = PERIOD_M5, M15 = PERIOD_M15, 
                 M30 = PERIOD_M30, H1  = PERIOD_H1, H4  = PERIOD_H4, 
                 D1  = PERIOD_D1,  W1  = PERIOD_W1, MN1 = PERIOD_MN1,                          };  

enum iOn_Off   { Off = 0, On = 1                                                               }; 

enum iTypeSL   { Without_SL = 0, Fix_SL = 1, AR_HighLow = 2, AR_OpenClose = 3                  };

enum iTypeTP   { Without_TP = 0, Fix_TP = 1, AR_Highlow = 2, AR_Openclose = 3                  };

enum iTypeMA   { simple = 0, exponential = 1, smoothed = 2, linear = 3                         };

enum iPriceMA  { close = 0, open = 1, high = 2, low = 3, median = 4, typical = 5, weighted = 6 };

enum iColorCF  { No = 0, Positiv = 1, Negativ = 2                                              };

enum iArrange  { No_Arrange     = 0, Martingale    = 1, 
                 Multiply_minus = 2, Multiply_plus = 3,
                 Divide_minus   = 4, Divide_plus   = 5,                                        };

enum iTimes    { _00 = 00, _01 = 01, _02 = 02, _03 = 03, _04 = 04, _05 = 05, _06 = 06, 
                 _07 = 07, _08 = 08, _09 = 09, _10 = 10, _11 = 11, _12 = 12, _13 = 13, 
                 _14 = 14, _15 = 15, _16 = 16, _17 = 17, _18 = 18, _19 = 19, _20 = 20, 
                 _21 = 21, _22 = 22, _23 = 23, _24 = 24                                        };

//◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘

extern  string    Name_of_Set               = "Default 5.05"                       ;

extern  string    General_Settings          = "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" ;
extern  iTrades   Trades                    = 0                                    ;
extern  iTF       Time_Frame                = H4                                   ;
extern  iTimes    Start_Work_Hour           = 00                                   ;
extern  iTimes    Stop_Work_Hour            = 24                                   ;
extern  iOn_Off   One_Order_Candle          = 1                                    ;
extern  int       Max_Spread                = 5                                    ;

extern  string    Main_Settings             = "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" ;
extern  iMode     Mode_for_Trades           = 1                                    ;
extern  iColorCF  Color_Candle_Filt         = 0                                    ;
extern  double    Point_Open_Filt           = 0                                    ;

extern  string    SL_Settings               = "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" ;
extern  iTypeSL   Type_of_sl                = 1                                    ;
extern  int       AR_CountBars_sl           = 0                                    ;
extern  int       AR_Percent_sl             = 0                                    ; 
extern  double    Fix_sl                    = 30                                   ;

extern  string    TP_Settings               = "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" ;
extern  iTypeTP   Type_of_tp                = 0                                    ;
extern  int       AR_CountBars_tp           = 0                                    ;
extern  int       AR_Percent_tp             = 0                                    ; 
extern  double    Fix_tp                    = 0                                    ;

extern  string    TS_Settings               = "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" ;
extern  iOn_Off   Trailing_Work             = 1                                    ;
extern  double    Trailing_Start            = 10                                   ;
extern  double    Trailing_Step             = 2                                    ;
    
extern  string    Arrange_Settings          = "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" ;
extern  iArrange  Type_of_Arrange           = 0                                    ;
extern  double    Buster                    = 0                                    ;
extern  int       Max_Orders                = 0                                    ;
extern  double    Martingale_Step           = 0                                    ;

extern  string    Lot_MM                    = "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" ;
extern  iOn_Off   Auto_Lot                  = 0                                    ;
extern  double    Risk                      = 0                                    ;
extern  double    Count_Money               = 0                                    ;
extern  double    Fix_Lot                   = 0.01                                 ;
extern  double    Equity_SL                 = 100                                  ;
extern  double    Equity_TP                 = 100                                  ;

extern  string    Moving_Average            = "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" ;
extern  iOn_Off   MA_Work                   = 0                                    ;
extern  iTypeMA   MA_Type                   = 0                                    ;
extern  int       MA_Period                 = 0                                    ;
extern  int       MA_Shift                  = 0                                    ;
extern  iPriceMA  MA_Price                  = 0                                    ;
extern  int       MA_ShiftByCandle          = 0                                    ;

extern  string    Orders                    = "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" ;
extern  string    CommentText               = "Highlow EA 5.05"                    ;                             
extern  int       MagicNumber               = 540863443                            ;                                                     
extern  int       Slippage                  = 5                                    ;                          
extern  color     Color_Open_Buy            = clrGreen                             ;                           
extern  color     Color_Open_Sell           = clrRed                               ;                             
extern  color     Color_Close_Order         = clrBlack                             ;     

//◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘
                
datetime TimeStart = 0, NewCandle = 0;

bool Work_on_New_Candle = 0, ActiveOrder = 0; 
       
//◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘
       
void OnInit() 
   {
      TimeStart = iTime(Symbol(),Time_Frame,0); 
      
      if (Digits == 3 || Digits == 5) 
         {
            Fix_tp *=10; Martingale_Step *=10; Max_Spread    *=10; Slippage        *=10;
            Fix_sl *=10; Trailing_Start  *=10; Trailing_Step *=10; Point_Open_Filt *=10;  
         }
   }

//◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘

void OnTick()
   {
      double MA  = iMA (Symbol(),Time_Frame,MA_Period,MA_Shift,(int)MA_Type,(int)MA_Price,MA_ShiftByCandle);   // ← Объявили индикатор Скользящей средней.
      
      //-------------------------------------------------------------------------------------------------------------------------------------------------------------------
      
      bool Spread = 0;                                                                                         // ← Объявили рабочую переменную. 
      
      if (Ask-Bid <= Max_Spread*Point) Spread = 1;                                                             // ← Текущий спред ниже или равен указанному. Торгуем.
      if (Ask-Bid >  Max_Spread*Point) Spread = 0;                                                             // ← Текущий спред выше указанного. Не торгуем.
      
      //-------------------------------------------------------------------------------------------------------------------------------------------------------------------     
      
      if (iTime(Symbol(),Time_Frame,0) > TimeStart) Work_on_New_Candle = 1;                                    // ← Работа советника только на новой свече.
      
      //-------------------------------------------------------------------------------------------------------------------------------------------------------------------
            
      if (One_Order_Candle == 1 && NewCandle != iTime(Symbol(),Time_Frame,0))                                  // ← Торгуем только первое персечение предыдущей свечи. 
         {
            NewCandle = iTime(Symbol(),Time_Frame,0); 
            
            if (Count_Open_Orders() == 0) ActiveOrder = 0; 
         }
         
      if (One_Order_Candle == 0) { ActiveOrder = 0; }                                                          // ← Торгуем любое пересечение предыдущей свечи.
      
      //------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
           
      int Extrem = 0;                                                                                          // ← Объявили рабочую переменную.
      
      if (Bid >= iHigh(Symbol(),Time_Frame,1) + Point_Open_Filt*Point) Extrem = 1;                             // ← Цена-бид пересекла High предыдущей свече с учётом PointOpenFilt.          
      if (Bid <= iLow (Symbol(),Time_Frame,1) - Point_Open_Filt*Point) Extrem = 2;                             // ← Цена-бид пересекла Low  предыдущей свече с учётом PointOpenFilt.

      //-------------------------------------------------------------------------------------------------------------------------------------------------------------------
      
      int Prev_Candle = 0, Buy__with_Color = 0, Sell_with_Color = 0;                                           // ← Объявили рабочие переменные.
      
      if (iClose(Symbol(),Time_Frame,1) > iOpen(Symbol(),Time_Frame,1)) Prev_Candle = 1;                       // ← Предыдущая закрытая свеча - зелёная.
      if (iClose(Symbol(),Time_Frame,1) < iOpen(Symbol(),Time_Frame,1)) Prev_Candle = 2;                       // ← Предыдущая закрытая свеча - красная.
      
      if (Color_Candle_Filt == 1 && Prev_Candle == 1) Buy__with_Color = 1;                                     // ← Выбран позитив в цвете, предыдущая свеча - бычья.    Покупаем
      if (Color_Candle_Filt == 2 && Prev_Candle == 2) Buy__with_Color = 1;                                     // ← Выбран негатив в цвете, предыдущая свеча - медвежья. Покупаем.
      if (Color_Candle_Filt == 1 && Prev_Candle == 2) Sell_with_Color = 1;                                     // ← Выбран позитив в цвете, предыдущая свеча - медвежья. Продаём
      if (Color_Candle_Filt == 2 && Prev_Candle == 1) Sell_with_Color = 1;                                     // ← Выбран негатив в цвете, предыдущая свеча - бычья.    Продаём
      
      if (Color_Candle_Filt == 0) { Buy__with_Color = 1; Sell_with_Color = 1; }                                // ← Не выбрано согласования с цветом. Можем как Покупать так и Продавать.

      //------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
      
      int GreenRedMode = 0;                                                                                    // ← Объявили рабочую переменную.
      
      if (iOpen(Symbol(),Time_Frame,2) > iClose(Symbol(),Time_Frame,2) &&                                      // ← 2я свеча - красная и 
          iOpen(Symbol(),Time_Frame,1) < iClose(Symbol(),Time_Frame,1)) GreenRedMode = 1;                      // ← 1я свеча - зелёная, тогда покупаем на хае первой свечи.
          
      if (iOpen(Symbol(),Time_Frame,2) < iClose(Symbol(),Time_Frame,2) &&                                      // ← 2я свеча - зелёная и
          iOpen(Symbol(),Time_Frame,1) > iClose(Symbol(),Time_Frame,1)) GreenRedMode = 2;                      // ← 1я свеча - красная, тогда продаём на лоу первой свечи.
      
      //-------------------------------------------------------------------------------------------------------------------------------------------------------------------
           
      if (Count_Open_Orders() == 0 && Work_on_New_Candle == 1 && ActiveOrder == 0 && Spread == 1 && Trades != 4)
         {
            if (Hour() >= Start_Work_Hour && Hour() < Stop_Work_Hour)
               {
                  if ( Trades != 2 && Mode_for_Trades == 1 && Extrem == 1 && Buy__with_Color == 1 && MA_Work == 0             ) { SendBuy (); ActiveOrder = 1; }
                  if ( Trades != 1 && Mode_for_Trades == 1 && Extrem == 2 && Sell_with_Color == 1 && MA_Work == 0             ) { SendSell(); ActiveOrder = 1; }
                  if ( Trades != 1 && Mode_for_Trades == 2 && Extrem == 1 && Sell_with_Color == 1 && MA_Work == 0             ) { SendSell(); ActiveOrder = 1; }
                  if ( Trades != 2 && Mode_for_Trades == 2 && Extrem == 2 && Buy__with_Color == 1 && MA_Work == 0             ) { SendBuy (); ActiveOrder = 1; }
            
                  if ( Trades != 2 && Mode_for_Trades == 1 && Extrem == 1 && Buy__with_Color == 1 && MA_Work == 1 && Bid > MA ) { SendBuy (); ActiveOrder = 1; }
                  if ( Trades != 1 && Mode_for_Trades == 1 && Extrem == 2 && Sell_with_Color == 1 && MA_Work == 1 && Bid < MA ) { SendSell(); ActiveOrder = 1; }
                  if ( Trades != 1 && Mode_for_Trades == 2 && Extrem == 1 && Sell_with_Color == 1 && MA_Work == 1 && Bid < MA ) { SendSell(); ActiveOrder = 1; }
                  if ( Trades != 2 && Mode_for_Trades == 2 && Extrem == 2 && Buy__with_Color == 1 && MA_Work == 1 && Bid > MA ) { SendBuy (); ActiveOrder = 1; }
            
                  if ( Trades != 2 && Mode_for_Trades == 3 && Extrem == 1 && GreenRedMode    == 1 && MA_Work == 1 && Bid > MA ) { SendBuy (); ActiveOrder = 1; }
                  if ( Trades != 2 && Mode_for_Trades == 3 && Extrem == 1 && GreenRedMode    == 1 && MA_Work == 0             ) { SendBuy (); ActiveOrder = 1; }
                  if ( Trades != 1 && Mode_for_Trades == 3 && Extrem == 2 && GreenRedMode    == 2 && MA_Work == 1 && Bid < MA ) { SendSell(); ActiveOrder = 1; }
                  if ( Trades != 1 && Mode_for_Trades == 3 && Extrem == 2 && GreenRedMode    == 2 && MA_Work == 0             ) { SendSell(); ActiveOrder = 1; }
               }
         }
         
      //-------------------------------------------------------------------------------------------------------------------------------------------------------------------
             
      if (Type_of_Arrange == 1 && Fix_tp > 0) { Grid_Buy (); Grid_Sell(); }                                    // ← Работа Мартингейла.
      
      if (Trailing_Work == 1 && Count_Open_Orders() == 1) { Trailing(); }                                      // ← Работа ТрейлингСтопа.
      
      Equity_Stop_Loss(); Equity_Take_Profit();                                                                // ← Работа СтопЛосса и ТейкПрофита по эквити.
   }
   
//◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘

void SendBuy()
   { 
      double SLB, TPB;                                                                                         // ← Объявили ТейкПрофит и СтопЛосс.
      
      if (                        Type_of_tp == 1) TPB = NormalizeDouble(Bid+Fix_tp           *Point,Digits);  // ← Фиксированный ТП для покупок.
      if (                        Type_of_tp == 2) TPB = NormalizeDouble(Bid+AR_HighLow_tp  ()*Point,Digits);  // ← Плавающий ТП, от среднего HighLow.
      if (                        Type_of_tp == 3) TPB = NormalizeDouble(Bid+AR_OpenClose_tp()*Point,Digits);  // ← Плавающий ТП, от среднего OpenClose.
      if (                        Type_of_tp == 0) TPB = 0;                                                    // ← ТП - выключены.
      
      if (Type_of_Arrange != 1 && Type_of_sl == 1) SLB = NormalizeDouble(Bid-Fix_sl           *Point,Digits);  // ← Фиксированный СЛ для покупок.
      if (Type_of_Arrange != 1 && Type_of_sl == 2) SLB = NormalizeDouble(Bid-AR_HighLow_sl  ()*Point,Digits);  // ← Плавающий СЛ, от среднего HighLow.
      if (Type_of_Arrange != 1 && Type_of_sl == 3) SLB = NormalizeDouble(Bid-AR_OpenClose_sl()*Point,Digits);  // ← Плавающий СЛ, от среднего OpenClose.
      if (Type_of_Arrange == 1 || Type_of_sl == 0) SLB = 0;                                                    // ← CЛ - выключены.
      
      bool b = OrderSend (Symbol(), OP_BUY, Lot(), Ask, Slippage, SLB, TPB, CommentText, MagicNumber, 0, Color_Open_Buy);
   }
   
   
void SendSell() 
   { 
      double SLS, TPS;                                                                                         // ← Объявили ТейкПрофит и СтопЛосс.
      
      if (                        Type_of_tp == 1) TPS = NormalizeDouble(Ask-Fix_tp           *Point,Digits);  // ← ТП для продаж.
      if (                        Type_of_tp == 2) TPS = NormalizeDouble(Ask-AR_HighLow_tp  ()*Point,Digits);  // ← Плавающий ТП, от среднего HighLow.      
      if (                        Type_of_tp == 3) TPS = NormalizeDouble(Ask-AR_OpenClose_tp()*Point,Digits);  // ← Плавающий ТП, от среднего OpenClose.      
      if (                        Type_of_tp == 0) TPS = 0;                                                    // ← ТП - выключены.
      
      if (Type_of_Arrange != 1 && Type_of_sl == 1) SLS = NormalizeDouble(Ask+Fix_sl           *Point,Digits);  // ← Фиксированный СЛ для продаж.
      if (Type_of_Arrange != 1 && Type_of_sl == 2) SLS = NormalizeDouble(Ask+AR_HighLow_sl  ()*Point,Digits);  // ← Плавающий СЛ, от среднего HighLow.
      if (Type_of_Arrange != 1 && Type_of_sl == 3) SLS = NormalizeDouble(Ask+AR_OpenClose_sl()*Point,Digits);  // ← Плавающий СЛ, от среднего OpenClose.
      if (Type_of_Arrange == 1 || Type_of_sl == 0) SLS = 0;                                                    // ← CЛ - выключены.
      
      bool s = OrderSend (Symbol(), OP_SELL, Lot(), Bid, Slippage, SLS, TPS, CommentText, MagicNumber, 0, Color_Open_Sell);
   }
   
//◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘

double Lot()
   {
      double l; int a = 0; datetime b = 0; static int c = 0; 
      
      if (Auto_Lot == 0) l = Fix_Lot;
      
      if (Auto_Lot == 1 && Type_of_Arrange == 1 && Count_Money > 0) l = NormalizeDouble(AccountEquity()/Count_Money/100,2);
      
      if (Auto_Lot == 1 && Type_of_Arrange != 1 && Risk > 0) 
         {
            if (Type_of_sl == 1) l = NormalizeDouble((AccountEquity()/100*Risk)/Fix_sl           ,2);
            if (Type_of_sl == 2) l = NormalizeDouble((AccountEquity()/100*Risk)/AR_HighLow_sl  (),2);
            if (Type_of_sl == 3) l = NormalizeDouble((AccountEquity()/100*Risk)/AR_OpenClose_sl(),2);
         }
      
      if ((Type_of_Arrange == 2 || 3 || 4 || 5) && Buster > 0)
         {
            for(int i=OrdersHistoryTotal()-1;i>=0;i--){if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
            {if(OrderSymbol()==Symbol()&&(OrderMagicNumber()==MagicNumber)&&OrderCloseTime()>b){a=OrderTicket();b=OrderCloseTime();}}}

            if(Type_of_Arrange==2&&OrderSelect(a,SELECT_BY_TICKET)){if(OrderProfit()<0)
            {if(c<Max_Orders){l=NormalizeDouble(OrderLots()*Buster,2);c++;}
            if(c>=Max_Orders&&Auto_Lot==0){l=Fix_Lot;c=1;}
            if(c>=Max_Orders&&Auto_Lot==1&&Type_of_sl==1){l=NormalizeDouble((AccountEquity()/100*Risk)/Fix_sl           ,2);c=0;}
            if(c>=Max_Orders&&Auto_Lot==1&&Type_of_sl==2){l=NormalizeDouble((AccountEquity()/100*Risk)/AR_HighLow_sl  (),2);c=0;}
            if(c>=Max_Orders&&Auto_Lot==1&&Type_of_sl==3){l=NormalizeDouble((AccountEquity()/100*Risk)/AR_OpenClose_sl(),2);c=0;}}
            if(OrderProfit()>=0){if(Auto_Lot==0){l=Fix_Lot;c=1;}
            if(               Auto_Lot==1&&Type_of_sl==1){l=NormalizeDouble((AccountEquity()/100*Risk)/Fix_sl           ,2);c=0;}
            if(               Auto_Lot==1&&Type_of_sl==2){l=NormalizeDouble((AccountEquity()/100*Risk)/AR_HighLow_sl  (),2);c=0;}
            if(               Auto_Lot==1&&Type_of_sl==3){l=NormalizeDouble((AccountEquity()/100*Risk)/AR_OpenClose_sl(),2);c=0;}}}

            if(Type_of_Arrange==3&&OrderSelect(a,SELECT_BY_TICKET)){if(OrderProfit()>0)
            {if(c<Max_Orders){l=NormalizeDouble(OrderLots()*Buster,2);c++;}    
            if(c>=Max_Orders&&Auto_Lot==0){l=Fix_Lot                                                                       ;c=0;}
            if(c>=Max_Orders&&Auto_Lot==1&&Type_of_sl==1){l=NormalizeDouble((AccountEquity()/100*Risk)/Fix_sl           ,2);c=0;}
            if(c>=Max_Orders&&Auto_Lot==1&&Type_of_sl==2){l=NormalizeDouble((AccountEquity()/100*Risk)/AR_HighLow_sl  (),2);c=0;}
            if(c>=Max_Orders&&Auto_Lot==1&&Type_of_sl==3){l=NormalizeDouble((AccountEquity()/100*Risk)/AR_OpenClose_sl(),2);c=0;}} 
            if(OrderProfit()<=0){if(Auto_Lot==0){l=Fix_Lot;c=1;}
            if(               Auto_Lot==1&&Type_of_sl==1){l=NormalizeDouble((AccountEquity()/100*Risk)/Fix_sl           ,2);c=0;}
            if(               Auto_Lot==1&&Type_of_sl==2){l=NormalizeDouble((AccountEquity()/100*Risk)/AR_HighLow_sl  (),2);c=0;}
            if(               Auto_Lot==1&&Type_of_sl==3){l=NormalizeDouble((AccountEquity()/100*Risk)/AR_OpenClose_sl(),2);c=0;}}}
            
            if(Type_of_Arrange==4&&OrderSelect(a,SELECT_BY_TICKET)){if(OrderProfit()<0)
            {if(c<Max_Orders){l=NormalizeDouble(OrderLots()/Buster,2);c++;}
            if(c>=Max_Orders&&Auto_Lot==0){l=Fix_Lot;c=1;}               
            if(c>=Max_Orders&&Auto_Lot==1&&Type_of_sl==1){l=NormalizeDouble((AccountEquity()/100*Risk)/Fix_sl           ,2);c=0;}
            if(c>=Max_Orders&&Auto_Lot==1&&Type_of_sl==2){l=NormalizeDouble((AccountEquity()/100*Risk)/AR_HighLow_sl  (),2);c=0;}
            if(c>=Max_Orders&&Auto_Lot==1&&Type_of_sl==3){l=NormalizeDouble((AccountEquity()/100*Risk)/AR_OpenClose_sl(),2);c=0;}}
            if(OrderProfit()>=0){if(Auto_Lot==0){l=Fix_Lot;c=1;}
            if(               Auto_Lot==1&&Type_of_sl==1){l=NormalizeDouble((AccountEquity()/100*Risk)/Fix_sl           ,2);c=0;}
            if(               Auto_Lot==1&&Type_of_sl==2){l=NormalizeDouble((AccountEquity()/100*Risk)/AR_HighLow_sl  (),2);c=0;}
            if(               Auto_Lot==1&&Type_of_sl==3){l=NormalizeDouble((AccountEquity()/100*Risk)/AR_OpenClose_sl(),2);c=0;}}}
            
            if(Type_of_Arrange==5&&OrderSelect(a,SELECT_BY_TICKET)){if(OrderProfit()>0)
            {if(c<Max_Orders){l=NormalizeDouble(OrderLots()/Buster,2);c++;}
            if(c>=Max_Orders&&Auto_Lot==0){l=Fix_Lot;c=1;}
            if(c>=Max_Orders&&Auto_Lot==1&&Type_of_sl==1){l=NormalizeDouble((AccountEquity()/100*Risk)/Fix_sl           ,2);c=0;}
            if(c>=Max_Orders&&Auto_Lot==1&&Type_of_sl==2){l=NormalizeDouble((AccountEquity()/100*Risk)/AR_HighLow_sl  (),2);c=0;}
            if(c>=Max_Orders&&Auto_Lot==1&&Type_of_sl==3){l=NormalizeDouble((AccountEquity()/100*Risk)/AR_OpenClose_sl(),2);c=0;}}
            if(OrderProfit()<=0){if(Auto_Lot==0){l=Fix_Lot;c=1;}
            if(               Auto_Lot==1&&Type_of_sl==1){l=NormalizeDouble((AccountEquity()/100*Risk)/Fix_sl           ,2);c=0;}
            if(               Auto_Lot==1&&Type_of_sl==2){l=NormalizeDouble((AccountEquity()/100*Risk)/AR_HighLow_sl  (),2);c=0;}
            if(               Auto_Lot==1&&Type_of_sl==3){l=NormalizeDouble((AccountEquity()/100*Risk)/AR_OpenClose_sl(),2);c=0;}}}
          }

      if (l < MarketInfo(Symbol(),MODE_MINLOT)) l = MarketInfo(Symbol(),MODE_MINLOT);
      if (l > MarketInfo(Symbol(),MODE_MAXLOT)) l = MarketInfo(Symbol(),MODE_MAXLOT);
      
      return (l);
   }

//◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘

void Grid_Buy()

         {int c=0;for(int q=0;q<OrdersTotal();q++){if(OrderSelect(q,SELECT_BY_POS)==1&&OrderMagicNumber()==MagicNumber){c++;}} 
         double n=0,t;int b=0;c=0;int o;for(int j=0;j<OrdersTotal();j++){if(OrderSelect(j,SELECT_BY_POS)==1&&OrderMagicNumber()
         ==MagicNumber&&OrderSymbol()==Symbol()){if(OrderType()==o||c==0)c++;{b=OrderTicket();n=OrderLots();o=OrderType();}}}
         if(OrderSelect(b,SELECT_BY_TICKET)&&c<Max_Orders){if(OrderType()==OP_BUY&&OrderOpenPrice()>=Ask+Martingale_Step*Point) 
         {b=OrderSend(Symbol(),OP_BUY,NormalizeDouble(n*Buster,2),Ask,Slippage,0,NormalizeDouble(Ask+Fix_tp*Point,Digits),
         CommentText,MagicNumber,0,Color_Open_Buy);if(OrderSelect(b,SELECT_BY_TICKET)==1)t=OrderTakeProfit();for(int k=0;
         k<OrdersTotal();k++){if(OrderSelect(k,SELECT_BY_POS)==1&&OrderMagicNumber()==MagicNumber&&OrderSymbol()==Symbol()&&t!=0
         &&OrderTicket()!=b){bool z=OrderModify(OrderTicket(),OrderOpenPrice(),0,t,0,Color_Open_Buy);}}}}}
         
         
void Grid_Sell()

         {int c=0;for(int q=0;q<OrdersTotal();q++){if(OrderSelect(q,SELECT_BY_POS)==1&&OrderMagicNumber()==MagicNumber){c++;}}
         double n=0,t;int s=0;c=0;int O;for(int j=0;j<OrdersTotal();j++){if(OrderSelect(j,SELECT_BY_POS)==1&&OrderMagicNumber()
         ==MagicNumber&&OrderSymbol()==Symbol()){if(OrderType()==O||c==0)c++;{s=OrderTicket();n=OrderLots();O=OrderType();}}}
         if(OrderSelect(s,SELECT_BY_TICKET)&&c<Max_Orders){if(OrderType()==OP_SELL&&OrderOpenPrice()<=Bid-Martingale_Step*Point)
         {s=OrderSend(Symbol(),OP_SELL,NormalizeDouble(n*Buster,2),Bid,Slippage,0,NormalizeDouble(Bid-Fix_tp*Point,Digits),
         CommentText,MagicNumber,0,Color_Open_Sell);if(OrderSelect(s,SELECT_BY_TICKET)==1)t=OrderTakeProfit();for(int k=0;
         k<OrdersTotal();k++){if(OrderSelect(k,SELECT_BY_POS)==1&&OrderMagicNumber()==MagicNumber&&OrderSymbol()==Symbol()&&t!=0
         &&OrderTicket()!=s){bool z=OrderModify(OrderTicket(),OrderOpenPrice(),0,t,0,Color_Open_Sell);}}}}}


int Count_Open_Orders()
      
         {int c=0;for(int s=OrdersTotal()-1;s>=0;s--){if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
         {if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)c++;}}return(c);}
         

void Equity_Stop_Loss()

         {if(AccountEquity()<=AccountBalance()/100*(100-Equity_SL))
         {for(int i=OrdersTotal()-1;i>=0;i--){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         {if(OrderSymbol()==Symbol()&&OrderType()==OP_BUY&&OrderMagicNumber()==MagicNumber) 
         bool cb=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Color_Close_Order);
         if(OrderSymbol()==Symbol()&&OrderType()==OP_SELL&&OrderMagicNumber()==MagicNumber)
         bool cs=OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Color_Close_Order);}}}}
         
         
void Equity_Take_Profit()

         {if(AccountEquity()>=AccountBalance()/100*(100+Equity_TP))
         {for(int i=OrdersTotal()-1;i>=0;i--){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         {if(OrderSymbol()==Symbol()&&OrderType()==OP_BUY&&OrderMagicNumber()==MagicNumber) 
         bool cb=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Color_Close_Order);
         if(OrderSymbol()==Symbol()&&OrderType()==OP_SELL&&OrderMagicNumber()==MagicNumber) 
         bool cs=OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Color_Close_Order);}}}}
         
         
void Trailing()

         {for(int i=OrdersTotal()-1;i>=0;i--){if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){if(OrderSymbol()==Symbol())
         {if(OrderType()==OP_BUY&&OrderMagicNumber()==MagicNumber){if(Bid-OrderOpenPrice()>Trailing_Start*Point)
         {if(OrderStopLoss()<Bid-(Trailing_Start+Trailing_Step)*Point||OrderStopLoss()==0)
         {bool tb=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-Trailing_Start*Point,Digits),0,0);}}}
         if(OrderType()==OP_SELL&&OrderMagicNumber()==MagicNumber){if(OrderOpenPrice()-Ask>Trailing_Start*Point)
         {if(OrderStopLoss()>Ask+(Trailing_Start+Trailing_Step)*Point||OrderStopLoss()==0)
         {bool ts=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+Trailing_Start*Point,Digits),0,0);}}}}}}}
         
         
int AR_HighLow_sl()
   
         {int i;double hl=0;for(i=Bars;i>0;i--){if(AR_CountBars_sl>=i){hl+=MathAbs(High[i]-Low[i])/Point;}}
         double a=NormalizeDouble(hl/AR_CountBars_sl,0);double b=NormalizeDouble((a/100)*AR_Percent_sl,0);return(b);}
   
   
int AR_OpenClose_sl()
  
         {int i;double oc=0;for(i=Bars;i>0;i--){if(AR_CountBars_sl>=i){oc+=MathAbs(Open[i]-Close[i])/Point;}}
         double a=NormalizeDouble(oc/AR_CountBars_sl,0);double b=NormalizeDouble((a/100)*AR_Percent_sl,0);return(b);}
         
         
int AR_HighLow_tp()
   
         {int i;double hl=0;for(i=Bars;i>0;i--){if(AR_CountBars_tp>=i){hl+=MathAbs(High[i]-Low[i])/Point;}}
         double a=NormalizeDouble(hl/AR_CountBars_tp,0);double b=NormalizeDouble((a/100)*AR_Percent_tp,0);return(b);}
   
   
int AR_OpenClose_tp()
  
         {int i;double oc=0;for(i=Bars;i>0;i--){if(AR_CountBars_tp>=i){oc+=MathAbs(Open[i]-Close[i])/Point;}}
         double a=NormalizeDouble(oc/AR_CountBars_tp,0);double b=NormalizeDouble((a/100)*AR_Percent_tp,0);return(b);}  
       
//◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘