Reaper_v2.8

Author: 2021.05.12, Vasilij Fedotov.
Profit factor:
0.00
11 Views
0 Downloads
0 Favorites
Reaper_v2.8
ÿþ//+------------------------------------------------------------------+

//|                                                  Reaper_v2.8.mq4 |

//|                           Copyright 2021.05.12, Vasilij Fedotov. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "2021.05.12, Vasilij Fedotov."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict



// ?@>25@>G=K9 20@80=B: ?@>1C5< 2K1@0BL >@45@ 87 8AB>@88 ?> <038:C

//  A>25B=8:5 Reaper_v2.5 2K1>@:0 ;>B0 87 8AB>@88 4;O C25;8G5=8O ;>B0 A;54CNI59 A45;:8 ?@>872>48BAO

// ?> <038:C 87 8AB>@88  8 @07;8G=> 4;O B09< D@59<0 M15, M30 8 H1



//---- input parameters

extern string 01 = "2>4=K5 ?0@0<5B@K";

extern string c1 = "Reaper_v2.1";

extern string b1 = "==========================";

extern string 02 = "0AB@>9:8 5=546<5=B0";

extern double    Lots        = 0.1;     // Lots;           1J5< >B:@K205<>9 ?>78F88

extern bool      UseLots     = False;   // UseLots;        @07@5H8BL C25;8G5=8O >1L5<0 >B:@K205<>9 ?>78F88

extern double    maxlots     = 1.0;     // maxlots;        <0:A8<0;L=K9 ;>B

extern double    MultiPlier  = 1;       // MultiPlier;     <=>68B5;L 4;O C25;8G5=8O ;>B0



extern string 03 = "A=>2=K5 =0AB@>9:8";

extern bool      UseTF_H1       = True;

extern int       TakeProfit_H1  = 42;       // TakeProfit;     @>D8B 2 ?C=:B0E. A;8 0, B> =5 CAB0=02;8205BAO

extern int       StopLoss_H1    = 54;       // StopLoss;       !B>? 2 ?C=:B0E. A;8 0, B> =5 CAB0=02;8205BAO

extern bool      UseTF_M30      = False;

extern int       TakeProfit_M30 = 42;       // TakeProfit;     @>D8B 2 ?C=:B0E. A;8 0, B> =5 CAB0=02;8205BAO

extern int       StopLoss_M30   = 54;       // StopLoss;       !B>? 2 ?C=:B0E. A;8 0, B> =5 CAB0=02;8205BAO

extern bool      UseTF_M15      = False;

extern int       TakeProfit_M15 = 42;       // TakeProfit;     @>D8B 2 ?C=:B0E. A;8 0, B> =5 CAB0=02;8205BAO

extern int       StopLoss_M15   = 54;       // StopLoss;       !B>? 2 ?C=:B0E. A;8 0, B> =5 CAB0=02;8205BAO

int              Direct     = 10;       // Direct;         >AB>O==0O 25;8G8=0 8A?>;L7C5<0O ?@8 @0AG5B0E



extern string 04 = "0AB@>9:8 B@59;8=3-AB>?0";

extern bool      UseTrailihg     = True;    // UseTrailihg;    @07@5H8BL 8A?>;L7>20=8O B@59;8=3-AB>?0 4;O 70:@KB8O >@45@>2

extern int       TrailingStop_H1 = 24;      // TrailingStop_H1;   ;0AA8G5A:89 B@59;8=3-AB>?

extern int       TrallStep       = 6;       // TrallStep_H1;      H03 B@59;8=3-AB>?

extern int       NoLoss_H1       = 18;      // NoLoss_H1;         ?5@52>4 2 157C1KB>:

extern int       TrailingStop_M30 = 24;     // TrailingStop_M30;   ;0AA8G5A:89 B@59;8=3-AB>?

extern int       NoLoss_M30      = 18;      // NoLoss_M30;         ?5@52>4 2 157C1KB>:

extern int       TrailingStop_M15 = 24;     // TrailingStop_M15;   ;0AA8G5A:89 B@59;8=3-AB>?

extern int       NoLoss_M15      = 18;      // NoLoss_M15;         ?5@52>4 2 157C1KB>:



//---------"0AB@>9:8 8=48:0B>@0"------------------------------

extern string    A10 = "0AB@>9:8 8=48:0B>@0 Alligator";

extern bool      UseAlligator = True;

extern int       HardAll_H1 = 1;     //HardAll_H1;    1-8A?>;L7>20BL c8;L=K9 A83=0;; 2-8A?-BL A@54=89 A83=0;; 3--8A?-BL A;01K9 A83=0;;

extern int       HardAll_M30 = 1;    //HardAll_M30;           // 4-@525@A8@>20BL A;01K9 A83=0; (allsignal = 3); 5-@525@A8@>20BL 2A5 A83=0;K

extern int       HardAll_M15 = 1;    //HardAll_M15;



extern string    A12 = "0AB@>9:8 ?@545;5=8O B@5=40";

extern bool      UseTrendAll = False;

extern int       HardTrend_H1 = 1;   //HardTrend_H1;   8A?>;L7>20BL: 1-65AB:89 B@5=4, 2- A@54=89 B@5=4, 3-A;01K9 B@5=4, 4-B@5=4 >?@545;O5BAO ?> 8=4. ma

extern int       HardTrend_M30 = 1;  //HardTrend_M30;

extern int       HardTrend_M15 = 1;  //HardTrend_M15;

extern int       AllGator  = 1;

extern int       TF_Alligator = 240;

extern string    A13 = "==========================";



extern string    A14 = " @>G85 ?0@0<5B@K ";

extern string    OpenOrderSound = "ok.wav";                   // 2C: 4;O >B:@KB8O >@45@0

extern int       Magic_H1  = 16018 ;

extern int       Magic_M30 = 13018 ;

extern int       Magic_M15 = 11518 ;

extern string    A15 = "==========================";



bool Activate, FreeMarginAlert, FatalError;

double Tick, Spread, StopLevel, MinLot, MaxLot, LotStep;

datetime LastBar,  LastMess;

int Signal, Trend, LastBuySignal, LastSellSignal, UseInd;

string Comm;



double TP;

double SL;



//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

  {

   Activate = False;

// 1 - !1>@ 8=D>@<0F88 >1 CA;>28OE B>@3>2;8 ========================================

   Tick = MarketInfo(Symbol(), MODE_TICKSIZE);                         // <8=8<0;L=K9 B8:

   Spread = ND(MarketInfo(Symbol(), MODE_SPREAD)*Point);                  // B5:I89 A?@M4

   StopLevel = ND(MarketInfo(Symbol(), MODE_STOPLEVEL)*Point);  // B5:CI89 C@>25=L AB>?>2

   MinLot = MarketInfo(Symbol(), MODE_MINLOT);    // <8=8<0;L=K9 @07@5H5==K9 >1J5< A45;:8

   MaxLot = MarketInfo(Symbol(), MODE_MAXLOT);   // <0:A8<0;L=K9 @07@5H5==K9 >1J5< A45;:8

   LotStep = MarketInfo(Symbol(), MODE_LOTSTEP);          // H03 ?@8@0I5=8O >1J5<0 A45;:8



// 2 - @82545=85 >1J5<0 A45;:8 : 4>?CAB8<><C 8 ?@>25@:0 :>@@5:B=>AB8 >1J5<0 =======

   Lots = MathRound(Lots/LotStep)*LotStep; // >:@C3;5=85 >1J5<0 4> 1;8609H53> 4>?CAB8<>3>

   if(Lots < MinLot || Lots > MaxLot) // >1J5< A45;:8 =5 <5=LH5 MinLot 8 =5 1>;LH5 MaxLot

     {

      Comment("0@0<5B@>< Lots 1K; 7040= =5?@028;L=K9 >1J5< A45;:8! !>25B=8: >B:;NG5=!");

      return(0);

     }



// 3 -  @82545=85 ?5@5<<5==KE ?@8 ?OB87=0G=KE :0B8@>2:0E ===========================

   int dig=(int)MarketInfo(Symbol(),MODE_DIGITS);

   if(dig == 5|| dig == 3)

     {

      TakeProfit_H1    = TakeProfit_H1* 10;

      StopLoss_H1      = StopLoss_H1* 10;

      TrailingStop_H1  = TrailingStop_H1* 10;

      TrallStep        = TrallStep* 10;

      NoLoss_H1        = NoLoss_H1* 10;



      TakeProfit_M30   = TakeProfit_M30* 10;

      StopLoss_M30     = StopLoss_M30* 10;

      TrailingStop_M30 = TrailingStop_M30* 10;

      NoLoss_M30       = NoLoss_M30* 10;



      TakeProfit_M15   = TakeProfit_M15* 10;

      StopLoss_M15     = StopLoss_M15* 10;

      TrailingStop_M15 = TrailingStop_M15* 10;

      NoLoss_M15       = NoLoss_M15* 10;



      Direct           = Direct* 10;

     }

//-----------------------------------------

   UseInd = UseAlligator + UseTrendAll;

   Comm = "K1@0=K A;54CNI85 8=48:0B>@K:\n";



   if(UseAlligator)

      Comm = Comm + "Alligator\n";

   if(UseTrendAll)

      Comm = Comm + "UseTrendAll\n";

   if(UseTF_H1)

      Comm = Comm + StringFormat("">@3>2;O ?> H1 = %d\n",UseTF_H1);

   if(UseTF_M30)

      Comm = Comm + StringFormat("">@3>2;O ?> M30 = %d\n",UseTF_M30);

   if(UseTF_M15)

      Comm = Comm + StringFormat("">@3>2;O ?> M15 = %d",UseTF_M15);

   Comment(Comm);



   LastBuySignal = 0;

   LastSellSignal = 0;

   LastBar = 0;



   Activate = True; // A5 ?@>25@:8 CA?5H=> 7025@H5=K, 2>72>48< D;03 0:B82870F88 M:A?5@B0

//------------------------------------------------------

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {



   Comment("");

  }



//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {

   int ding = 0; // G8A;>- ?@8>@5B5B >?8@0F88, >7=0G05B GB> >?5@0F8O C65 2K?>;=5=0

   double lots = 0;

   int TakeProfit = 0;

   int StopLoss = 0;

   int Magic = 0;

   int TrailingStop = 0;

   int NoLoss = 0;

   int Alligator_TF = 0;



// 1 -  07@5H5=> ;8 A>25B=8:C @01>B0BL? ===========================================

   if(!Activate || FatalError)           // B:;NG05BAO @01>B0 A>25B=8:0, 5A;8 DC=:F8O

      return;            //  init 7025@H8;0AL A >H81:>9  8;8 8<5;0 <5AB> D0B0;L=0O >H81:0



// 2 - >=B@>;L 2@5<5=8 8 2K7>2 D-F88 B@59;8=3 AB>?0 ==============================

   if(LastMess + 300 < TimeCurrent())

      Comment(Comm);



   if(UseTF_H1)

     {

      if(UseTrailihg && CountOfOrders(Magic_H1) > 0)

        {

         TrailingStopDoubleLevel(TrailingStop_H1, NoLoss_H1, Magic_H1);

        }

     }

   if(UseTF_M30)

     {

      if(UseTrailihg && CountOfOrders(Magic_M30) > 0)

        {

         TrailingStopDoubleLevel(TrailingStop_M30, NoLoss_M30, Magic_M30);

        }

     }

   if(UseTF_M15)

     {

      if(UseTrailihg && CountOfOrders(Magic_M15) > 0)

        {

         TrailingStopDoubleLevel(TrailingStop_M15, NoLoss_M15, Magic_M15);

        }

     }



// 3 - >=B@>;L >B:@KB8O =>2>3> 10@0 ==============================================

   if(LastBar == Time[0])   //A;8 2@5<O ?>AG8B0==>3> 10@0 @02=> 2@5<5=8 B5:CI53> 10@0

      return;                         // B> MB> >7=0G05B, GB> =>2K9 10@ 5I5 =5 >B:@K;AO



// 4 - !1>@ 8=D>@<0F88 >1 CA;>28OE B>@3>2;8 ========================================

   Spread = ND(MarketInfo(Symbol(), MODE_SPREAD)*Point);               // B5:CI89 A?@M4

   StopLevel = ND(MarketInfo(Symbol(), MODE_STOPLEVEL)*Point);// B5:CI89 C@>25=L AB>?>2



// 5 -  0AG5B B5:CI53> A83=0;0 =====================================================

   if(ding == 0 && UseTF_H1)

     {

      Signal = 0;

      LastBuySignal = 0;

      LastSellSignal = 0;

      Alligator_TF = 60;

      GetSignal(Alligator_TF, HardAll_H1, HardTrend_H1);

      if(Signal != 0 && CountOfOrders(Magic_H1) == 0)

        {

         TakeProfit = TakeProfit_H1;

         StopLoss = StopLoss_H1;

         Magic = Magic_H1;

         ding = 1;

        }

     }

   if(ding == 0 && UseTF_M30)

     {

      Signal = 0;

      LastBuySignal = 0;

      LastSellSignal = 0;

      Alligator_TF = 30;

      GetSignal(Alligator_TF, HardAll_M30, HardTrend_M30);

      if(Signal != 0 && CountOfOrders(Magic_M30) == 0)

        {

         TakeProfit = TakeProfit_M30;

         StopLoss = StopLoss_M30;

         Magic = Magic_M30;

         ding = 1;

        }

     }

   if(ding == 0 && UseTF_M15)

     {

      Signal = 0;

      LastBuySignal = 0;

      LastSellSignal = 0;

      Alligator_TF = 15;

      GetSignal(Alligator_TF, HardAll_M15, HardTrend_M15);

      if(Signal != 0 && CountOfOrders(Magic_M15) == 0)

        {

         TakeProfit = TakeProfit_M15;

         StopLoss = StopLoss_M15;

         Magic = Magic_M15;

         ding = 1;

        }

     }



// 6 - ================ 1@01>B:0 A83=0;0 BUY =========================================

   double Price = 0;

   int Type = -1;

   int Ticket = 0;



   if(LastBuySignal > 0)

     {

      Ticket = CountOfOrders(Magic); // ?@>25@:0 >B:@KB>3>  >@45@0



      if(Ticket == 0)   // 5A;8 =5B >B:@KBKE ?>78F89

        {

         RefreshRates();

         Price = ND(Ask);

         TP = NP(IF(TakeProfit == 0, 0, Price + TakeProfit*Tick));

         SL = NP(IF(StopLoss == 0, 0, Price - StopLoss*Tick));

         Type = OP_BUY;

        }

      if(Ticket > 0)

         return;              // =5 C40;>AL 70:@KBL ?@>B82>?>;>6=CN ?>78F8N

     }



// 7 - ============ 1@01>B:0 A83=0;0 SELL ============================================



   if(LastSellSignal < 0)

     {

      Ticket = CountOfOrders(Magic);  // ?@>25@:0 >B:@KB>3>  >@45@0



      if(Ticket == 0)   // 5A;8 =5B >B:@KBKE ?>78F89

        {

         RefreshRates();

         Price = ND(Bid);

         TP = ND(IF(TakeProfit == 0, 0, Price - TakeProfit*Tick));

         SL = ND(IF(StopLoss == 0, 0, Price + StopLoss*Tick));

         Type = OP_SELL;

        }

      if(Ticket > 0)

         return;              // =5 C40;>AL 70:@KBL ?@>B82>?>;>6=CN ?>78F8N

     }



//     Print("===="!"==== 0@0<5B@ 4;O ?@>25@:8. AB@ 380 Tick=", Tick);

//     Print("===="!"==== 0@0<5B@ 4;O ?@>25@:8. AB@ 381 Point=", Point);



// 9 - ;>:  (5=46<5=B) ========================================================

   lots = 0;

   if(UseLots)

     {

      if(Magic == Magic_H1 || Magic == Magic_M30 || Magic == Magic_M15)

        {

         if(Ticket == 0)

           {

            if(lots == 0)

               lots = (double)NormalizeDouble((RaiseMultiPlier(Magic)*0.01),2);

            if(lots == 0)

               lots = Lots;

           }

        }

     }

   if(!UseLots)

      lots = Lots;

//     Print("===="!"==== 0@0<5B@ 4;O ?@>25@:8. AB@ 357 lots=", lots);

//     Print("===="!"==== 0@0<5B@ 4;O ?@>25@:8. AB@ 358 tiket=", tiket);

//     Print("===="!"==== 0@0<5B@ 4;O ?@>25@:8. AB@ 365 Magic=", Magic);



// - 9 - == :>=G0=85 1;>:0 ============================================================

   if(Magic == Magic_H1 || Magic == Magic_M30 || Magic == Magic_M15)

     {

      if(Type >= 0)



         Ticket = OpenOrderCorrect(Type, lots, Price, SL, TP, Magic);

      if(Ticket > 0)

         return;

     }

   LastBar = Time[0];



   return;

  }



//+--------------------------------------------------------------------------------------+



//+--------------------------------------------------------------------------------------+

//| 2CEC@>2=52K9 B@59;8=3-AB>?                                                          |

//| NoLoss - 2KAB02;O5BAO >48= @07 ?>A;5 4>AB865=8O =C6=>3> :>;-20 ?C=:B>2 ?@81K;8       |

//| TrailingStop - 4286CI89AO. :;NG05BAO ?>A;5 >B@01>B:8 B@59;8=3-AB>?0 ?5@2>3> C@>2=O  |

//+--------------------------------------------------------------------------------------+

void TrailingStopDoubleLevel(int TrailingStop, int NoLoss, int magic)

  {

   double sl = 0;



   for(int i = 0; i < OrdersTotal(); i++)

      if(OrderSelect(i, SELECT_BY_POS))

         if(OrderSymbol() == Symbol() && OrderMagicNumber() == magic && OrderType() < 2)

           {

            if(!WaitForTradeContext())

               break;

            // ;>: 1. >@<0;870F8O >A=>2=KE E0@0:B5@8AB8: ?>78F88 -----------------------------------

            RefreshRates();

            double OpPrice = ND(OrderOpenPrice());   // @82545=85 F5=K >B:@KB8O : =C6=>9 B>G=>AB8

            SL = ND(OrderStopLoss());          // @82545=85 AB>?-?@8:070 : =C6=>9 B>G=>AB8

            double NBid = ND(Bid);

            double NAsk = ND(Ask);



            // ;>: 2. ;0AA8G5A:89 B@59;8=3 4;8==>9 ?>78F88 -----------------------------------------

            if(OrderType() == OP_BUY)

               if(SL >= OpPrice && TrailingStop != 0)           // A;8 AB>? ?5@5=5A5= 2 157C1KB>:

                 {

                  if(NBid > ND(Tick*(TrailingStop + TrallStep)+SL) &&  // A;8 BID 1>;LH5 AB>?0 =0 TrailingStop

                     TrailingStop > StopLevel/Point)       //  TrailingStop 1>;LH5 C@>2=O AB>?>2

                     if(WaitForTradeContext())                 // 45< >A2>1>645=85 B>@3>2>3> ?>B>:0

                        if(!OrderModify(OrderTicket(), 0,

                                        ND(NBid-TrailingStop*Tick), OrderTakeProfit(), 0))         // >48D8:0F8O

                           Print("5 C40;>AL 87<5=8BL >@45@ BUY. "@59;8=3-AB>@. H81:0 !",

                                 GetLastError(), ", !B0@K9: ", SL, ", >2K9: ",

                                 NBid-TrailingStop*Tick);          //  0725@=CB>5 A>>1I5=85 >1 >H81:5

                 }



            // ;>: 3. 5@5=>A AB>?0 2 157C1KB>: 4;8==>9 ?>78F88 -------------------------------------

               else

                 {

                  if(ND(OpPrice+NoLoss*Tick) < NBid &&      // A;8 BID 1>;LH5 F5=K >B:@KB8O

                     NoLoss > StopLevel/Point)     // =0 TrailingStop1 ?C=:B>2 8 40;LH5, G5<

                     if(WaitForTradeContext())                                     // C@>25=L AB>?>2

                        if(!OrderModify(OrderTicket(), 0, OpPrice,

                                        OrderTakeProfit(), 0))           // <>48D8:0F8O

                           Print("5 C40;>AL 87<5=8BL >@45@ BUY. 5@2K9 B@0;. H81:0 !", // A>>1I5=85

                                 GetLastError(), ", !B0@K9: ", SL, ", >2K9: ", OpPrice);// 2 6C@=0;

                 }



            // ;>: 4. ;0AA8G5A:89 B@59;8=3 :>@>B:>9 ?>78F88 ----------------------------------------

            if(OrderType()==OP_SELL)                 // ;O :>@>B:>9 ?>78F88

               if(OpPrice >= SL && SL != 0 && TrailingStop != 0) // A;8 AB>? ?5@5=5A5= 2 157C1KB>:

                 {

                  if(TrailingStop > StopLevel/Point &&        // TrailingStop 1>;LH5 C@>2=O AB>?>2

                     SL > ND(Tick*(TrailingStop + TrallStep)+NAsk))  // 8 ASK =865 AB>?0 =0 TrailingStop ?C=:B>2

                     if(WaitForTradeContext())               // 68405< >A2>1>645=8O B>@3>2>3> ?>B>:0

                        if(!OrderModify(OrderTicket(), 0, ND(NAsk+TrailingStop*Tick),

                                        OrderTakeProfit(), 0))                                       // <>48D8:0F8O

                           Print("5 C40;>AL 87<5=8BL >@45@ SELL. "@59;8=3-AB>@. H81:0 !",

                                 GetLastError(), ", !B0@K9: ", SL, ", >2K9: ", NAsk+TrailingStop*Tick);

                 }



            // ;>: 5. 5@5=>A AB>?0 2 157C1KB>: :>@>B:>9 ?>78F88 ------------------------------------

               else

                 {

                  if(OpPrice > ND(NAsk+Tick*NoLoss) &&   //ASK =865 >B:@KB8O =0 TrailingStop1

                     NoLoss > StopLevel/Point)                      // 8 =0 StopLevel ?C=:B>2

                     if(WaitForTradeContext())                              // 68405< B>@3>2K9 ?>B>:

                        if(!OrderModify(OrderTicket(), 0, OpPrice, OrderTakeProfit(), 0))//<>48D8:0F8O

                           Print("5 C40;>AL 87<5=8BL >@45@ SELL. 5@2K9 B@0;. H81:0 !",

                                 GetLastError(), ", !B0@K9: ", SL, ", >2K9: ", OpPrice);

                 }



           }

   return;

  }

//+-------------------------------------------------------------------------------------+

//|  0AG5B A83=0;0                                                                      |

//|             A;8 7=0G5=85 Signal 1>;LH5 =C;O, 7=0G8B Buy                            |

//|             A;8 7=0G5=85 Signal <5=LH5 =C;O, 7=0G8B Sell                           |

//+-------------------------------------------------------------------------------------+

void GetSignal(int AlligatorTF, int HardAll, int HardTrend)

  {

   Signal = 0;

   int trend  = 0;

   int signal = 0;

   int allsignal = 0;



// - 6 - ============== =48:0B>@ Alligator ============================================

   if(UseAlligator)

     {

      int lips = 0;

      double atr = iATR(Symbol(),AlligatorTF,21,1);



      double Lips0 = iAlligator(Symbol(), AlligatorTF, 13, 8, 8, 5, 5, 3, MODE_SMMA, PRICE_MEDIAN, MODE_GATORLIPS, 0);

      double Lips1 = iAlligator(Symbol(), AlligatorTF, 13, 8, 8, 5, 5, 3, MODE_SMMA, PRICE_MEDIAN, MODE_GATORLIPS, 1);

      double Lips2 = iAlligator(Symbol(), AlligatorTF, 13, 8, 8, 5, 5, 3, MODE_SMMA, PRICE_MEDIAN, MODE_GATORLIPS, 2);

      double Lips3 = iAlligator(Symbol(), AlligatorTF, 13, 8, 8, 5, 5, 3, MODE_SMMA, PRICE_MEDIAN, MODE_GATORLIPS, 3);



      double close0 = iClose(Symbol(),AlligatorTF,0);

      double close1 = iClose(Symbol(),AlligatorTF,1);

      double close2 = iClose(Symbol(),AlligatorTF,2);

      double close3 = iClose(Symbol(),AlligatorTF,3);

      double open0 = iOpen(Symbol(),AlligatorTF,0);

      double open1 = iOpen(Symbol(),AlligatorTF,1);

      double open2 = iOpen(Symbol(),AlligatorTF,2);

      double open3 = iOpen(Symbol(),AlligatorTF,3);



      // >1@01>B:0 A83=0;0 4;O Buy >@45@0

      if(close1-open1 < atr*4)  // $8;LB@ =0 70?@8I5=8O >B:@KB8O A45;:8 ?@8 35?5 8 35?>2>9 A25G5

        {

         /*      double e1 = MathRound(MathAbs(close1-open1)/(Point*10));  // >?@545;O5< 25;8G8=C A25G8

               double e2 = MathRound(MathAbs(close2-open2)/(Point*10));

               double e3 = MathRound(MathAbs(close3-open3)/(Point*10));

               double c1 = MathRound(((open1+close1)/2)/(Point*10));      // >?@545;O5< A@54=5N F5=C A25G8

               double c2 = MathRound(((open2+close2)/2)/(Point*10));

               double c3 = MathRound(((open3+close3)/2)/(Point*10));



               if (!((e1==e2 && e2==e3 && e1==e3) && (e1<Direct*Point && e2<Direct*Point && e3<Direct*Point) && (c1<=c2 || c2<=c3 || c1<=c3))) // $8;LB@ ?> D;5BC

         */

           {

            if(close1>open1 && close1>Lips1 && open1<Lips1 && close2<Lips2 && open2<Lips2 && Ask > Lips0 && Ask < Lips0 + atr*3)

               lips = 1;

            if(close1>open1 && close1>Lips1 && open1>Lips1 && close2<Lips2 && open2<Lips2 && Ask > Lips0 && Ask < Lips0 + atr*3)

               lips = 2;

            if(close1>open1 && close1>Lips1 && close2<Lips2 && open2<close1 && open2>=Lips2 && Ask > Lips0 && Ask < Lips0 + atr*3)

               lips = 3;

           }

        }

      // >1@01>B:0 A83=0;0 4;O Sell >@45@0

      if(open1-close1<atr*4)  // $8;LB@ =0 70?@8I5=8O >B:@KB8O A45;:8 ?@8 35?5 8 35?>2>9 A25G5

        {

         /*      double e1 = MathRound(MathAbs(open1-close1)/(Point*10));  // >?@545;O5< 25;8G8=C A25G8

               double e2 = MathRound(MathAbs(open2-close2)/(Point*10));

               double e3 = MathRound(MathAbs(open3-close3)/(Point*10));

               double c1 = MathRound(((open1+close1)/2)/(Point*10));      // >?@545;O5< A@54=5N F5=C A25G8

               double c2 = MathRound(((open2+close2)/2)/(Point*10));

               double c3 = MathRound(((open3+close3)/2)/(Point*10));



               if (!((e1==e2 && e2==e3 && e1==e3) && (e1<Direct*Point && e2<Direct*Point && e3<Direct*Point) && (c1>=c2 || c2>=c3 || c1>=c3))) // $8;LB@ ?> D;5BC

         */

           {

            if(close1<open1 && close1<Lips1 && open1>Lips1 && close2>Lips2 && open2>Lips2 && Bid < Lips0 && Bid > Lips0 - atr*3)

               lips = -1;

            if(close1<open1 && close1<Lips1 && open1<Lips1 && close2>Lips2 && open2>Lips2 && Bid < Lips0 && Bid > Lips0 - atr*3)

               lips = -2;

            if(close1<open1 && close1<Lips1 && close2>Lips2 && open2>close1 && open2<=Lips2 && Bid < Lips0 && Bid > Lips0 - atr*3)

               lips = -3;

           }

        }

      if(lips != 0)

         allsignal = lips;



      /*    if (lips == 1) allsignal++;                                        // Buy signal

          if (lips == 2) allsignal = 2;

          if (lips == 3) allsignal = 3;

          if (lips == -1) allsignal--;                                        // Sell signal

          if (lips == -2) allsignal = -2;

          if (lips == -3) allsignal = -3;

      */

     }



// - 7 - ============== ?@545;5=8O B@5=40 =============================================

   if(UseTrendAll)

     {

      int t_bar = 0;

      int ma_bar = 0;

      int t_ma = 0;

      int all_gator = 0;



      double ma_bar1 = iMA(Symbol(),TF_Alligator,3,0,MODE_LWMA,PRICE_WEIGHTED,0);

      double ma_bar2 = iMA(Symbol(),TF_Alligator,3,0,MODE_LWMA,PRICE_WEIGHTED,1);

      double ma_bar3 = iMA(Symbol(),TF_Alligator,3,0,MODE_LWMA,PRICE_WEIGHTED,2);



      if(AllGator == 1)

         all_gator = MODE_GATORLIPS;

      if(AllGator == 2)

         all_gator = MODE_GATORTEETH;

      if(AllGator == 3)

         all_gator = MODE_GATORJAW;

      double all_lips1 = iAlligator(Symbol(),TF_Alligator,13,8,8,5,5,3,MODE_SMMA,PRICE_MEDIAN,all_gator,1);

      double all_lips2 = iAlligator(Symbol(),TF_Alligator,13,8,8,5,5,3,MODE_SMMA,PRICE_MEDIAN,all_gator,2);



      if(ma_bar1>=ma_bar2 && ma_bar2>ma_bar3)

         ma_bar = 1;



      if(ma_bar > 0 && all_lips1 < Bid-(Direct/2)*Point && all_lips1>all_lips2 && t_bar == 0)

         t_bar = 1;

      if(ma_bar > 0 && all_lips1 < Bid-(Direct/2)*Point && all_lips1<all_lips2 && t_bar == 0)

         t_bar = 2;

      if(ma_bar > 0 && all_lips1 > Bid-(Direct/2)*Point && all_lips1<all_lips2 && t_bar == 0)

         t_bar = 3;

      if(ma_bar > 0 && all_lips1 > Bid && t_bar == 0)

         t_bar = 4;



      if(ma_bar1<=ma_bar2 && ma_bar2<ma_bar3)

         ma_bar = -1;



      if(ma_bar < 0 && all_lips1 > Ask+(Direct/2)*Point && all_lips1<all_lips2 && t_bar == 0)

         t_bar = -1;

      if(ma_bar < 0 && all_lips1 > Ask+(Direct/2)*Point && all_lips1>all_lips2 && t_bar == 0)

         t_bar = -2;

      if(ma_bar < 0 && all_lips1 < Ask+(Direct/2)*Point && all_lips1>all_lips2 && t_bar == 0)

         t_bar = -3;

      if(ma_bar < 0 && all_lips1 < Ask && t_bar == 0)

         t_bar = -4;



      if(t_bar ==1)

         trend = 1;                                      // Buy signal

      if((t_bar ==2 || t_bar ==3 || t_bar ==4) && HardTrend == 4)

         trend = 3;  // ?@8 7=0G5=88 HardTrend = 4, B@5=4 >?@545;O5BAO ?> 8=4. ma

      if((t_bar ==2 || t_bar ==3) && HardTrend == 3)

         trend = 3;

      if(t_bar ==2 && HardTrend == 2)

         trend = 2;

      if(t_bar ==2 && HardTrend == 1)

         trend = 0;  // 7=0G5=8O 1=65AB:89 >B1>@; 2=A@54=89; 3=C<5@5==K9 4;O Buy >@45@>2



      if(t_bar == -1)

         trend = -1;                                    // Sell signal

      if((t_bar ==-2 || t_bar ==-3 || t_bar ==-4) && HardTrend == 4)

         trend = -3;  // ?@8 7=0G5=88 HardTrend = 4, B@5=4 >?@545;O5BAO ?> 8=4. ma

      if((t_bar ==-2 || t_bar ==-3) && HardTrend == 3)

         trend = -3;

      if(t_bar ==-2 && HardTrend == 2)

         trend = -2;

      if(t_bar == -2 && HardTrend == 1)

         trend = 0;  // 7=0G5=8O -1=65AB:89 >B1>@; -2=A@54=89; -3=C<5@5==K9  4;O Sell >@45@>2

     }



// - 8 - ======== 1@>1>B:0 A83=0;>2 8=48:0B>@0 8 B@5=40 ===============================

   if(allsignal != 0)

     {

      if(allsignal > 0)

        {

         if(allsignal == 1 && HardAll == 1)

            signal++;

         if((allsignal == 1 || allsignal == 2) && (HardAll == 2 || HardAll == 4))

            signal++;

         if((allsignal == 1 || allsignal == 2 || allsignal == 3) && HardAll == 3)

            signal++;

         if((allsignal == 1 || allsignal == 2 || allsignal == 3) && HardAll == 5)

            signal--;

         if(allsignal == 3 && HardAll == 4)

            signal--;

        }

      if(allsignal < 0)

        {

         if(allsignal == -1 && HardAll == 1)

            signal--;

         if((allsignal == -1 || allsignal == -2) && (HardAll == 2 || HardAll == 4))

            signal--;

         if((allsignal == -1 || allsignal == -2 || allsignal == -3) && HardAll == 3)

            signal--;

         if((allsignal == -1 || allsignal == -2 || allsignal == -3) && HardAll == 5)

            signal++;

         if(allsignal == -3 && HardAll == 4)

            signal++;

        }

      if(UseTrendAll)

        {

         if(signal > 0 && (trend == 1 || trend == 2 || trend == 3))

            Signal = signal+1;

         if(signal < 0 && (trend == -1 || trend == -2 || trend == -3))

            Signal = signal-1;



         //    if (signal > 0 && trend == -3 && HardAll == 3) Signal = signal+1; // @525@A A45;:8 ?@8 A;01>< A83=0;5

         //    if (signal < 0 && trend == 3 && HardAll == 3) Signal = signal-1;  // @525@A A45;:8 ?@8 A;01>< A83=0;5



        }

      if(!UseTrendAll)

         Signal = signal;

     }

   if(UseInd == MathAbs(Signal))

     {

      if(Signal != 0)

        {

         LastBuySignal = Signal;

         LastSellSignal = Signal;

        }

     }

  }



//+-------------------------------------------------------------------------------------+

//|  0AH8D@>2:0 A>>1I5=8O >1 >H81:5                                                     |

//+-------------------------------------------------------------------------------------+

string ErrorToString(int Error)

  {

   switch(Error)

     {

      case 2:

         return (string)("70D8:A8@>20=0 >1I0O >H81:0, >1@0B8B5AL 2 B5E?>445@6:C.");

      case 5:

         return (string)("C 20A AB0@0O 25@A8O B5@<8=0;0, >1=>28B5 55.");

      case 6:

         return (string)("=5B A2O78 A A5@25@><, ?>?@>1C9B5 ?5@5703@C78BL B5@<8=0;.");

      case 64:

         return (string)("AG5B 701;>:8@>20=, >1@0B8B5AL 2 B5E?>445@6:C.");

      case 132:

         return (string)("@K=>: 70:@KB.");

      case 133:

         return (string)("B>@3>2;O 70?@5I5=0.");

      case 149:

         return (string)("70?@5I5=> ;>:8@>20=85.");

     }

   return (string)(":@5B8G5A:>9 >H81:8 =5 =0945=>.");

  }



//+-------------------------------------------------------------------------------------+

//| "@028;L=>5" >B:@KB85 ?>78F88                                                       |

//|  >B;8G85 >B OpenOrder ?@>25@O5B A>>B=>H5=85 B5:CI8E C@>2=59 8 CAB0=02;8205<KE      |

//| >72@0I05B:                                                                         |

//|   0 - =5B >H81>:                                                                    |

//|   1 - H81:0 >B:@KB8O                                                               |

//|   2 - H81:0 7=0G5=8O Price                                                         |

//|   3 - H81:0 7=0G5=8O SL                                                            |

//|   4 - H81:0 7=0G5=8O TP                                                            |

//|   5 - H81:0 7=0G5=8O Lots                                                          |

//+-------------------------------------------------------------------------------------+

int OpenOrderCorrect(int Type, double lots, double Price, double sl, double tp, int magic, bool Redefinition = True)

// Redefinition - ?@8 True 4>>?@545;OBL ?0@0<5B@K 4> <8=8<0;L=> 4>?CAB8<KE,  ?@8 False - 2>72@0I0BL >H81:C

  {

   int ticket=0;

// - 1 - == @>25@:0 4>AB0B>G=>AB8 A2>1>4=KE A@54AB2 ====================================

   if(AccountFreeMarginCheck(Symbol(), OP_BUY, Lots) <= 0 || GetLastError() == 134)

     {

      if(!FreeMarginAlert)

        {

         Print("54>AB0B>G=> A@54AB2 4;O >B:@KB8O ?>78F88. Free Margin = ",

               AccountFreeMargin());

         FreeMarginAlert = True;

        }

      return(5);

     }

   FreeMarginAlert = False;

// - 1 - == :>=G0=85 1;>:0 =============================================================



// - 2 - == >@@5:B8@>2:0 7=0G5=89 Price, SL 8 TP 8;8 2>72@0B >H81:8 ====================



   RefreshRates();

   string S;

   switch(Type)

     {

      case OP_BUY:

         S = "BUY";

         if(MathAbs(Price-Ask)/Point > 3)

            if(Redefinition)

               Price = NP(Ask);

            else

               return(2);

         if(ND(tp-Bid) <= StopLevel && TP != 0)

            if(Redefinition)

               tp = NP(Bid+StopLevel+Tick);

            else

               return(4);

         if(ND(Bid-sl) <= StopLevel)

            if(Redefinition)

               sl = NP(Bid-StopLevel-Tick);

            else

               return(3);

         break;

      case OP_SELL:

         S = "SELL";

         if(MathAbs(Price-Bid)/Point > 3)

            if(Redefinition)

               Price = NP(Bid);

            else

               return(2);

         if(NP(Ask-tp) <= StopLevel)

            if(Redefinition)

               tp = NP(Ask-StopLevel-Tick);

            else

               return(4);

         if(ND(sl-Ask) <= StopLevel && SL != 0)

            if(Redefinition)

               sl = NP(Ask+StopLevel+Tick);

            else

               return(3);

         break;

      case OP_BUYSTOP:

         S = "BUYSTOP";

         if(ND(Price-Ask) <= StopLevel)

            if(Redefinition)

               Price = NP(Ask+StopLevel+Tick);

            else

               return(2);

         if(ND(tp-Price) <= StopLevel && TP != 0)

            if(Redefinition)

               tp = NP(Price+StopLevel+Tick);

            else

               return(4);

         if(ND(Price-sl) <= StopLevel)

            if(Redefinition)

               sl = NP(Price-StopLevel-Tick);

            else

               return(3);

         break;

      case OP_SELLSTOP:

         S = "SELLSTOP";

         if(ND(Bid-Price) <= StopLevel)

            if(Redefinition)

               Price = NP(Bid-StopLevel-Tick);

            else

               return(2);

         if(ND(Price-tp) <= StopLevel)

            if(Redefinition)

               tp = NP(Price-StopLevel-Tick);

            else

               return(4);

         if(ND(sl-Price) <= StopLevel && SL != 0)

            if(Redefinition)

               sl = NP(Price+StopLevel+Tick);

            else

               return(3);

         break;

      case OP_BUYLIMIT:

         S = "BUYLIMIT";

         if(ND(Ask-Price) <= StopLevel)

            if(Redefinition)

               Price = NP(Ask-StopLevel-Tick);

            else

               return(2);

         if(ND(tp-Price) <= StopLevel && TP != 0)

            if(Redefinition)

               tp = NP(Price+StopLevel+Tick);

            else

               return(4);

         if(ND(Price-sl) <= StopLevel)

            if(Redefinition)

               sl = NP(Price-StopLevel-Tick);

            else

               return(3);

         break;

      case OP_SELLLIMIT:

         S = "SELLLIMIT";

         if(ND(Price - Bid) <= StopLevel)

            if(Redefinition)

               Price = NP(Bid+StopLevel+Tick);

            else

               return(2);

         if(ND(Price-tp) <= StopLevel)

            if(Redefinition)

               tp = NP(Price-StopLevel-Tick);

            else

               return(4);

         if(ND(sl-Price) <= StopLevel && SL != 0)

            if(Redefinition)

               sl = NP(Price+StopLevel+Tick);

            else

               return(3);

         break;

     }



// - 3 - == B:@KB85 >@45@0 A >6840=85 B>@3>2>3> ?>B>:0 =================================

   if(WaitForTradeContext())  // >6840=85 >A2>1>645=8O B>@3>2>3> ?>B>:0

     {

      Comment("B?@02;5= 70?@>A =0 >B:@KB85 ?>78F88 ", S, " ...");

      ticket=OrderSend(Symbol(), Type, lots, Price, 3, 0, 0, NULL, magic, 0);// >B:@KB85 ?>78F88

      if(ticket<=0)  // >?KB:0 >B:@KB8O ?>78F88 7025@H8;0AL =5C40G59

        {

         int Error = GetLastError();

         if(Error == 2 || Error == 5 || Error == 6 || Error == 64

            || Error == 132 || Error == 133 || Error == 149)     // A?8A>: D0B0;L=KE >H81>:

           {

            Comment("$0B0;L=0O >H81:0 ?@8 >B:@KB88 ?>78F88 B. :. "+

                    ErrorToString(Error)+" !>25B=8: >B:;NG5=!");

            FatalError = True;

           }

         else

            Comment("H81:0 >B:@KB8O ?>78F88 ", S, ": ", Error);       // =5D0B0;L=0O >H81:0



         return(1);

        }

     }

// ---------------------------------------------



// #40G=>5 >B:@KB85 ?>78F88

   if(ticket > 0)

     {

      if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))

        {

         bool res= OrderModify(ticket, OrderOpenPrice(), sl, tp, 0);

         if(!res)

            Print("H81:0 <>48D8:0F88 >@45@0. >4 >H81:8=",GetLastError());

        }

      Comment(">78F8O ", S, " >B:@KB0 CA?5H=>!");

      PlaySound(OpenOrderSound);

      return(ticket);

      // ------------------------

     }

   else

     {

      Comment("@5<O >6840=8O >A2>1>645=8O B>@3>2>3> ?>B>:0 8AB5:;>!");

      return(1);

     }

// - 3 - == :>=G0=85 1;>:0 =============================================================

  }



//+-------------------------------------------------------------------------------------+

//| @82545=85 7=0G5=89 : B>G=>AB8 >4=>3> ?C=:B0                                        |

//+-------------------------------------------------------------------------------------+

double ND(double A)

  {

   return(NormalizeDouble(A, Digits));

  }



//+-------------------------------------------------------------------------------------+

//| @82545=85 7=0G5=89 : B>G=>AB8 >4=>3> B8:0                                          |

//+-------------------------------------------------------------------------------------+

double NP(double A)

  {

   return(MathFloor(A/Tick)*Tick);

  }



//+-------------------------------------------------------------------------------------+

//| 6840=85 B>@3>2>3> ?>B>:0. A;8 ?>B>: A2>1>45=, B> @57C;LB0B True, 8=0G5 - False    |

//+-------------------------------------------------------------------------------------+

bool WaitForTradeContext()

  {

   int P = 0;

// F8:; "?>:0"

   while(IsTradeContextBusy() && P < 5)

     {

      P++;

      Sleep(1000);

     }

// -------------

   if(P == 5)

      return(false);

   return(true);

  }



//+-------------------------------------------------------------------------------------+

//| 0<5=0 AB0=40@B=>3> 1;>:0 "if-else"                                                 |

//+-------------------------------------------------------------------------------------+

double IF(bool Condition, double IfTrue, double IfFalse)

  {

   if(Condition)

      return(IfTrue);    // A;8 CA;>285 8AB8==>, B> 2>72@0I05< 7=0G5=85 IfTrue

   else

      return(IfFalse);    // A;8 CA;>285 ;>6=>, B> 2>72@0I05< 7=0G5=85 IfFalse

  }



//+-------------------------------------------------------------------------------------+

//| 0:@K205B 2A5 ?>78F88 B8?0 Type. A;8 70:@KBL =5 C40;>AL 70:@KBL, B> 1.             |

//| A;8 ?@8ACBAB2C5B ?@>B82>?>;>6=0O, B> 2>72@0I05B 2.  A;CG05 CA?5E0 0.              |

//+-------------------------------------------------------------------------------------+

int CheckOrdersReal(int Type, int magic)

  {

   double Pr;

   for(int i = OrdersTotal()-1; i >= 0; i--)

      if(OrderSelect(i, SELECT_BY_POS))

         if(OrderSymbol() == Symbol() && OrderMagicNumber() == magic

            && OrderType() < 2)                                       // ?>8A: "A2>59" A45;:8

            if(OrderType() == Type)                                  // A;8 ?>78F8O B8?0 Type

              {

               if(WaitForTradeContext())

                 {

                  if(Type == OP_BUY)

                     Pr = ND(MarketInfo(Symbol(), MODE_BID));

                  else

                     Pr = ND(MarketInfo(Symbol(), MODE_ASK));

                  if(!OrderClose(OrderTicket(), OrderLots(), Pr, 3)) // B> ?KB05<AO 70:@KBL 55       OrderLots()

                     return(1);                                 // =5 C40;>AL 70:@KBL - 25@=5< 1

                 }

               else

                  return(1);//25@=5< 1, 5A;8 =5 C40;>AL 4>640BLAO >A2>1>645=8O B>@3>2>3> ?>B>:0

              }

            else

               return(2);          // 25@=5< 2, 5A;8 >B:@KB0 ?>78F8O, ?@>B82>?>;>6=0O C:070==>9

   return(0);                                                   // 5A;8 2A5 , B> 25@=5< 0

  }



//+-------------------------------------------------------------------------------------+



//+------------------------------------------------------------------+

// DC=:F8O C25;8G5=8O ;>B0 ?>A;5 C1KB>G=>9 A45;:8                    +

//+------------------------------------------------------------------+

int RaiseMultiPlier(int magig)

  {

   double oldopenlots = 0;

   double openlots = 0;

   int oldlots = 0 ;

   int ticket = 0;

   int  oldticket = 0;

   int dig = 0;

   double Nlots = 0;

   double op_profit = 0;

   int magig_test = 0;



   RefreshRates();



   for(int i=OrdersHistoryTotal(); i>=0; i--)

     {

      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))

        {

         if(OrderSymbol() == Symbol() && OrderType() <= 1 && OrderMagicNumber()== magig)

           {

            oldticket = OrderTicket();

            if(oldticket > ticket)

              {

               op_profit = OrderProfit();

               oldopenlots = OrderLots();

               ticket = oldticket;



               //             magig_test = OrderMagicNumber();

              }

           }

        }

     }



//+---------------------------------------+



   if(oldopenlots > 0)

      openlots = oldopenlots;

   if(openlots >= 1)

      dig = 0;

   if(1 > openlots && openlots >= 0.1)

      dig = 1;

   if(0.1 > openlots && openlots >= 0.01)

      dig = 2;



   if(op_profit < 0)

     {

      oldlots = (int)NormalizeDouble(MathRound((openlots*MultiPlier)*MathPow(10,dig)),0); //MathPow(10,dig);

      if(oldlots <= maxlots*MathPow(10,dig))

         Nlots = (double)NormalizeDouble(oldlots/MathPow(10,dig),dig);

      if(oldlots > maxlots*MathPow(10,dig))

         Nlots = maxlots;

      if(oldlots ==0)

         Nlots = Lots;

     }

   if(op_profit >= 0)

      Nlots = Lots;

//      Print("===="!"==== 0@0<5B@ 4;O ?@>25@:8. AB@ 955 magig_test=", magig_test);

//      Print("===="!"==== 0@0<5B@ 4;O ?@>25@:8. AB@ 956 oldlots=", oldlots);

//      Print("===="!"==== 0@0<5B@ 4;O ?@>25@:8. AB@ 957 Nlots=", Nlots);

//     Print("===="!"==== 0@0<5B@ 4;O ?@>25@:8. AB@ 932 tiket=", tiket);



   return (int)(Nlots*100);

  }

//+------------------------------------------------------------------+



//+------------------------------------------------------------------+

//| D-F8O ?@>25@O5B =0;8G8O >@45@>2                                            |

//+------------------------------------------------------------------+

int CountOfOrders(int magic)//?>4AG5B >@45@>2 sell 8;8 buy

  {

   int count = 0;

   int ticket = 0;

   for(int i = 0; i < OrdersTotal(); i++)

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

         if(OrderSymbol()==Symbol() && OrderMagicNumber()== magic)

            count++;

   if(count > 0)

      ticket = OrderTicket();



   return(ticket);

  }

//+------------------------------------------------------------------+

Comments