//+------------------------------------------------------------------+
//|                                                                  |
//|  In no event will author be liable for any damages whatsoever.   |
//|                      Use at your own risk.                       |
//|                                                                  |
//+------------------- DO NOT REMOVE THIS HEADER --------------------+

#define SIGNAL_NONE 0
#define SIGNAL_BUY   1
#define SIGNAL_SELL  2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4

#property link      "e"

extern int MagicNumber = 0;
extern bool SignalMail = False;
extern bool EachTickMode = False;
extern double Lots = 1.0;
extern int Slippage = 2;
extern bool UseStopLoss = True;
extern int StopLoss = 30;
extern bool UseTakeProfit = True;
extern int TakeProfit = 60;
extern bool UseTrailingStop = False;
extern int TrailingStop = 30;


int BarCount;
int Current;
bool TickCheck = False;
int LastPosition=0;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init() {
   BarCount = Bars;

   if (EachTickMode) Current = 0; else Current = 1;

   return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit() {
   return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start() {
   int Order = SIGNAL_NONE;
   int Total, Ticket;
   int allOrders=OrdersTotal();
   double StopLossLevel, TakeProfitLevel;



   if (EachTickMode && Bars != BarCount) TickCheck = False;
   Total = OrdersTotal();
   Order = SIGNAL_NONE;

   //+------------------------------------------------------------------+
   //| Variable Begin                                                   |
   //+------------------------------------------------------------------+

double Var1 = iRSI(NULL, 0, 13, PRICE_WEIGHTED, Current + 0);
double Var2 = iRSI(NULL, 0, 13, PRICE_WEIGHTED, Current + 1);
double Var3 = iRSI(NULL, 0, 13, PRICE_WEIGHTED, Current + 2);
double Var4 = iRSI(NULL, 0, 13, PRICE_WEIGHTED, Current + 3);
double Var5 = iRSI(NULL, 0, 13, PRICE_WEIGHTED, Current + 4);
double Var6 = iRSI(NULL, 0, 13, PRICE_WEIGHTED, Current + 5);
double Var7 = iRSI(NULL, 0, 13, PRICE_WEIGHTED, Current + 6);
double Var8 = iRSI(NULL, 0, 13, PRICE_WEIGHTED, Current + 7);
double Var9 = iRSI(NULL, 0, 13, PRICE_WEIGHTED, Current + 8);
double Var10 = iRSI(NULL, 0, 13, PRICE_WEIGHTED, Current + 9);
double Var11 = iRSI(NULL, 0, 13, PRICE_WEIGHTED, Current + 10);
double Var12 = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_CLOSE, Current + 0);
double Var13 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_OPEN, Current + 0);
double Var14 = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_CLOSE, Current + 1);
double Var15 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_OPEN, Current + 1);
double Var16 = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_CLOSE, Current + 2);
double Var17 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_OPEN, Current + 2);
double Var18 = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_CLOSE, Current + 3);
double Var19 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_OPEN, Current + 3);
double Var20 = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_CLOSE, Current + 4);
double Var21 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_OPEN, Current + 4);
double Var22 = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_CLOSE, Current + 5);
double Var23 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_OPEN, Current + 5);
double Var24 = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_CLOSE, Current + 6);
double Var25 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_OPEN, Current + 6);
double Var26 = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_CLOSE, Current + 7);
double Var27 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_OPEN, Current + 7);
double Var28 = iMA(NULL, 0, 5, 0, MODE_EMA, PRICE_CLOSE, Current + 8);
double Var29 = iMA(NULL, 0, 8, 0, MODE_EMA, PRICE_OPEN, Current + 8);
double Var30 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_MAIN, Current + 0);
double Var31 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_SIGNAL, Current + 0);
double Var32 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_MAIN, Current + 1);
double Var33 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_SIGNAL, Current + 1);
double Var34 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_MAIN, Current + 2);
double Var35 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_SIGNAL, Current + 2);
double Var36 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_MAIN, Current + 3);
double Var37 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_SIGNAL, Current + 3);
double Var38 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_MAIN, Current + 4);
double Var39 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_SIGNAL, Current + 4);
double Var40 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_MAIN, Current + 5);
double Var41 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_SIGNAL, Current + 5);
double Var42 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_MAIN, Current + 6);
double Var43 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_SIGNAL, Current + 6);
double Var44 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_MAIN, Current + 7);
double Var45 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_SIGNAL, Current + 7);
double Var46 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_MAIN, Current + 8);
double Var47 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_SIGNAL, Current + 8);
double Var48 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_MAIN, Current + 9);
double Var49 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_SIGNAL, Current + 9);
double Var50 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_MAIN, Current + 10);
double Var51 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_SIGNAL, Current + 10);
double Var52 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_MAIN, Current + 11);
double Var53 = iStochastic(NULL, 0, 8, 3, 3, MODE_LWMA, 0, MODE_SIGNAL, Current + 11);

double Buy1_1 =  Var1 ;
double Buy1_2 = 50;
double Buy2_1 =  Var2 ;
double Buy2_2 = 50;
double Buy3_1 =  Var3 ;
double Buy3_2 = 50;
double Buy4_1 =  Var4 ;
double Buy4_2 = 50;
double Buy5_1 =  Var5 ;
double Buy5_2 = 50;
double Buy6_1 =  Var6 ;
double Buy6_2 = 50;
double Buy7_1 =  Var7 ;
double Buy7_2 = 50;
double Buy8_1 =  Var8 ;
double Buy8_2 = 50;
double Buy9_1 =  Var9 ;
double Buy9_2 = 50;
double Buy10_1 = Var10 ;
double Buy10_2 = 50;
double Buy11_1 = Var11 ;
double Buy11_2 = 50;
double Buy12_1 = Var12 ;
double Buy12_2 = Var13 ;
double Buy13_1 = Var14 ;
double Buy13_2 = Var15 ;
double Buy14_1 = Var16 ;
double Buy14_2 = Var17 ;
double Buy15_1 = Var18 ;
double Buy15_2 = Var19 ;
double Buy16_1 = Var20 ;
double Buy16_2 = Var21 ;
double Buy17_1 = Var22 ;
double Buy17_2 = Var23 ;
double Buy18_1 = Var24 ;
double Buy18_2 = Var25 ;
double Buy19_1 = Var26 ;
double Buy19_2 = Var27 ;
double Buy20_1 = Var28 ;
double Buy20_2 = Var29 ;
double Buy21_1 = Var30 ;
double Buy21_2 = Var31 ;
double Buy22_1 = Var32 ;
double Buy22_2 = Var33 ;
double Buy23_1 = Var34 ;
double Buy23_2 = Var35 ;
double Buy24_1 = Var36 ;
double Buy24_2 = Var37 ;
double Buy25_1 = Var38 ;
double Buy25_2 = Var39 ;
double Buy26_1 = Var40 ;
double Buy26_2 = Var41 ;
double Buy27_1 = Var42 ;
double Buy27_2 = Var43 ;
double Buy28_1 = Var44 ;
double Buy28_2 = Var45 ;
double Buy29_1 = Var46 ;
double Buy29_2 = Var47 ;
double Buy30_1 = Var48 ;
double Buy30_2 = Var49 ;
double Buy31_1 = Var50 ;
double Buy31_2 = Var51 ;
double Buy32_1 = Var52 ;
double Buy32_2 = Var53 ;


double Sell1_1 =  Var1 ;
double Sell1_2 = 50;
double Sell2_1 =  Var2 ;
double Sell2_2 = 50;
double Sell3_1 =  Var3 ;
double Sell3_2 = 50;
double Sell4_1 =  Var4 ;
double Sell4_2 = 50;
double Sell5_1 =  Var5 ;
double Sell5_2 = 50;
double Sell6_1 =  Var6 ;
double Sell6_2 = 50;
double Sell7_1 =  Var7 ;
double Sell7_2 = 50;
double Sell8_1 =  Var8 ;
double Sell8_2 = 50;
double Sell9_1 =  Var9 ;
double Sell9_2 = 50;
double Sell10_1 = Var10 ;
double Sell10_2 = 50;
double Sell11_1 = Var11 ;
double Sell11_2 = 50;
double Sell12_1 = Var12 ;
double Sell12_2 = Var13 ;
double Sell13_1 = Var14 ;
double Sell13_2 = Var15 ;
double Sell14_1 = Var16 ;
double Sell14_2 = Var17 ;
double Sell15_1 = Var18 ;
double Sell15_2 = Var19 ;
double Sell16_1 = Var20 ;
double Sell16_2 = Var21 ;
double Sell17_1 = Var22 ;
double Sell17_2 = Var23 ;
double Sell18_1 = Var24 ;
double Sell18_2 = Var25 ;
double Sell19_1 = Var26 ;
double Sell19_2 = Var27 ;
double Sell20_1 = Var28 ;
double Sell20_2 = Var29 ;
double Sell21_1 = Var30 ;
double Sell21_2 = Var31 ;
double Sell22_1 = Var32 ;
double Sell22_2 = Var33 ;
double Sell23_1 = Var34 ;
double Sell23_2 = Var35 ;
double Sell24_1 = Var36 ;
double Sell24_2 = Var37 ;
double Sell25_1 = Var38 ;
double Sell25_2 = Var39 ;
double Sell26_1 = Var40 ;
double Sell26_2 = Var41 ;
double Sell27_1 = Var42 ;
double Sell27_2 = Var43 ;
double Sell28_1 = Var44 ;
double Sell28_2 = Var45 ;
double Sell29_1 = Var46 ;
double Sell29_2 = Var47 ;
double Sell30_1 = Var48 ;
double Sell30_2 = Var49 ;
double Sell31_1 = Var50 ;
double Sell31_2 = Var51 ;
double Sell32_1 = Var52 ;
double Sell32_2 = Var53 ;




double CloseBuy1_1 =  Var1 ;
double CloseBuy1_2 = 50;
double CloseBuy2_1 =  Var2 ;
double CloseBuy2_2 = 50;
double CloseBuy3_1 =  Var3 ;
double CloseBuy3_2 = 50;
double CloseBuy4_1 =  Var4 ;
double CloseBuy4_2 = 50;
double CloseBuy5_1 =  Var5 ;
double CloseBuy5_2 = 50;
double CloseBuy6_1 =  Var6 ;
double CloseBuy6_2 = 50;
double CloseBuy7_1 =  Var7 ;
double CloseBuy7_2 = 50;
double CloseBuy8_1 =  Var8 ;
double CloseBuy8_2 = 50;
double CloseBuy9_1 =  Var9 ;
double CloseBuy9_2 = 50;
double CloseBuy10_1 = Var10 ;
double CloseBuy10_2 = 50;
double CloseBuy11_1 = Var11 ;
double CloseBuy11_2 = 50;
double CloseBuy12_1 = Var12 ;
double CloseBuy12_2 = Var13 ;
double CloseBuy13_1 = Var14 ;
double CloseBuy13_2 = Var15 ;
double CloseBuy14_1 = Var16 ;
double CloseBuy14_2 = Var17 ;
double CloseBuy15_1 = Var18 ;
double CloseBuy15_2 = Var19 ;
double CloseBuy16_1 = Var20 ;
double CloseBuy16_2 = Var21 ;
double CloseBuy17_1 = Var22 ;
double CloseBuy17_2 = Var23 ;
double CloseBuy18_1 = Var24 ;
double CloseBuy18_2 = Var25 ;
double CloseBuy19_1 = Var26 ;
double CloseBuy19_2 = Var27 ;
double CloseBuy20_1 = Var28 ;
double CloseBuy20_2 = Var29 ;
double CloseBuy21_1 = Var30 ;
double CloseBuy21_2 = Var31 ;
double CloseBuy22_1 = Var32 ;
double CloseBuy22_2 = Var33 ;
double CloseBuy23_1 = Var34 ;
double CloseBuy23_2 = Var35 ;
double CloseBuy24_1 = Var36 ;
double CloseBuy24_2 = Var37 ;
double CloseBuy25_1 = Var38 ;
double CloseBuy25_2 = Var39 ;
double CloseBuy26_1 = Var40 ;
double CloseBuy26_2 = Var41 ;
double CloseBuy27_1 = Var42 ;
double CloseBuy27_2 = Var43 ;
double CloseBuy28_1 = Var44 ;
double CloseBuy28_2 = Var45 ;
double CloseBuy29_1 = Var46 ;
double CloseBuy29_2 = Var47 ;
double CloseBuy30_1 = Var48 ;
double CloseBuy30_2 = Var49 ;
double CloseBuy31_1 = Var50 ;
double CloseBuy31_2 = Var51 ;
double CloseBuy32_1 = Var52 ;
double CloseBuy32_2 = Var53 ;



double CloseSell1_1 =  Var1 ;
double CloseSell1_2 = 50;
double CloseSell2_1 =  Var2 ;
double CloseSell2_2 = 50;
double CloseSell3_1 =  Var3 ;
double CloseSell3_2 = 50;
double CloseSell4_1 =  Var4 ;
double CloseSell4_2 = 50;
double CloseSell5_1 =  Var5 ;
double CloseSell5_2 = 50;
double CloseSell6_1 =  Var6 ;
double CloseSell6_2 = 50;
double CloseSell7_1 =  Var7 ;
double CloseSell7_2 = 50;
double CloseSell8_1 =  Var8 ;
double CloseSell8_2 = 50;
double CloseSell9_1 =  Var9 ;
double CloseSell9_2 = 50;
double CloseSell10_1 = Var10 ;
double CloseSell10_2 = 50;
double CloseSell11_1 = Var11 ;
double CloseSell11_2 = 50;
double CloseSell12_1 = Var12 ;
double CloseSell12_2 = Var13 ;
double CloseSell13_1 = Var14 ;
double CloseSell13_2 = Var15 ;
double CloseSell14_1 = Var16 ;
double CloseSell14_2 = Var17 ;
double CloseSell15_1 = Var18 ;
double CloseSell15_2 = Var19 ;
double CloseSell16_1 = Var20 ;
double CloseSell16_2 = Var21 ;
double CloseSell17_1 = Var22 ;
double CloseSell17_2 = Var23 ;
double CloseSell18_1 = Var24 ;
double CloseSell18_2 = Var25 ;
double CloseSell19_1 = Var26 ;
double CloseSell19_2 = Var27 ;
double CloseSell20_1 = Var28 ;
double CloseSell20_2 = Var29 ;
double CloseSell21_1 = Var30 ;
double CloseSell21_2 = Var31 ;
double CloseSell22_1 = Var32 ;
double CloseSell22_2 = Var33 ;
double CloseSell23_1 = Var34 ;
double CloseSell23_2 = Var35 ;
double CloseSell24_1 = Var36 ;
double CloseSell24_2 = Var37 ;
double CloseSell25_1 = Var38 ;
double CloseSell25_2 = Var39 ;
double CloseSell26_1 = Var40 ;
double CloseSell26_2 = Var41 ;
double CloseSell27_1 = Var42 ;
double CloseSell27_2 = Var43 ;
double CloseSell28_1 = Var44 ;
double CloseSell28_2 = Var45 ;
double CloseSell29_1 = Var46 ;
double CloseSell29_2 = Var47 ;
double CloseSell30_1 = Var48 ;
double CloseSell30_2 = Var49 ; 
double CloseSell31_1 = Var50 ;
double CloseSell31_2 = Var51 ; 
double CloseSell32_1 = Var52 ;
double CloseSell32_2 = Var53 ;
   
   //+------------------------------------------------------------------+
   //| Variable End                                                     |
   //+------------------------------------------------------------------+

   //Check position
   bool IsTrade = False;

   for (int i = 0; i < Total; i ++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderType() <= OP_SELL &&  OrderSymbol() == Symbol()) {
         IsTrade = True;
         if(OrderType() == OP_BUY) {
            //Close

            //+------------------------------------------------------------------+
            //| Signal Begin(Exit Buy)                                           |
            //+------------------------------------------------------------------+

             if (((CloseSell1_1<CloseSell1_2 && CloseSell2_1>CloseSell2_2)||(CloseSell2_1<CloseSell2_2 && CloseSell3_1>CloseSell3_2)||(CloseSell3_1<CloseSell3_2 && CloseSell4_1>CloseSell4_2)||(CloseSell4_1<CloseSell4_2 && CloseSell5_1>CloseSell5_2)||(CloseSell5_1<CloseSell5_2 && CloseSell6_1>CloseSell6_2)||(CloseSell6_1<CloseSell6_2 && CloseSell7_1>CloseSell7_2)||(CloseSell7_1<CloseSell7_2 && CloseSell8_1>CloseSell8_2)||(CloseSell8_1<CloseSell8_2 && CloseSell9_1>CloseSell9_2)||(CloseSell9_1<CloseSell9_2 && CloseSell10_1>CloseSell10_2)||(CloseSell10_1<CloseSell10_2 && CloseSell11_1>CloseSell11_2))&&((CloseSell12_1<CloseSell12_2 && CloseSell13_1>CloseSell13_2)||(CloseSell13_1<CloseSell13_2 && CloseSell14_1>CloseSell14_2)||(CloseSell14_1<CloseSell14_2 && CloseSell15_1>CloseSell15_2)||(CloseSell5_1<CloseSell15_2 && CloseSell16_1>CloseSell16_2)||(CloseSell16_1<CloseSell16_2 && CloseSell17_1>CloseSell17_2)||(CloseSell17_1<CloseSell17_2 && CloseSell18_1>CloseSell18_2)||(CloseSell18_1<CloseSell18_2 && CloseSell19_1>CloseSell19_2)||(CloseSell19_1<CloseSell19_2 && CloseSell20_1>CloseSell20_2))&&((CloseSell21_1<CloseSell21_2 && CloseSell22_1>CloseSell22_2)||(CloseSell22_1<CloseSell22_2 && CloseSell23_1>CloseSell23_2)||(CloseSell23_1<CloseSell23_2 && CloseSell24_1>CloseSell24_2)||(CloseSell24_1<CloseSell24_2 && CloseSell25_1>CloseSell25_2)||(CloseSell25_1<CloseSell25_2 && CloseSell26_1>CloseSell26_2)||(CloseSell26_1<CloseSell26_2 && CloseSell27_1>CloseSell27_2)||(CloseSell27_1<CloseSell27_2 && CloseSell28_1>CloseSell28_2)||(CloseSell28_1<CloseSell28_2 && CloseSell29_1>CloseSell29_2)||(CloseSell29_1<CloseSell29_2 && CloseSell30_1>CloseSell30_2)||(CloseSell30_1<CloseSell30_2 && CloseSell31_1>CloseSell31_2)||(CloseSell31_1<CloseSell31_2 && CloseSell32_1>CloseSell32_2)))Order = SIGNAL_CLOSESELL;


            //+------------------------------------------------------------------+
            //| Signal End(Exit Buy)                                             |
            //+------------------------------------------------------------------+

            if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
               OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
               if (!EachTickMode) BarCount = Bars;
               IsTrade = False;
               continue;
            }
            //Trailing stop
            if(UseTrailingStop && TrailingStop > 0) {                 
               if(Bid - OrderOpenPrice() > Point * TrailingStop) {
                  if(OrderStopLoss() < Bid - Point * TrailingStop) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
                     if (!EachTickMode) BarCount = Bars;
                     continue;
                  }
               }
            }
         } else {
            //Close

            //+------------------------------------------------------------------+
            //| Signal Begin(Exit Sell)      &&      ||                          |
            //+------------------------------------------------------------------+

             if (((CloseBuy1_1>CloseBuy1_2 && CloseBuy2_1<CloseBuy2_2)||(CloseBuy2_1>CloseBuy2_2 && CloseBuy3_1<CloseBuy3_2)||(CloseBuy3_1>CloseBuy3_2 && CloseBuy4_1<CloseBuy4_2)||(CloseBuy4_1>CloseBuy4_2 && CloseBuy5_1<CloseBuy5_2)||(CloseBuy5_1>CloseBuy5_2 && CloseBuy6_1<CloseBuy6_2)||(CloseBuy6_1>CloseBuy6_2 && CloseBuy7_1<CloseBuy7_2)||(CloseBuy7_1>CloseBuy7_2 && CloseBuy8_1<CloseBuy8_2)||(CloseBuy8_1>CloseBuy8_2 && CloseBuy9_1<CloseBuy9_2)||(CloseBuy9_1>CloseBuy9_2 && CloseBuy10_1<CloseBuy10_2)||(CloseBuy10_1>CloseBuy10_2 && CloseBuy11_1<CloseBuy11_2))&&((CloseBuy12_1>CloseBuy12_2 && CloseBuy13_1<CloseBuy13_2)||(CloseBuy13_1>CloseBuy13_2 && CloseBuy14_1<CloseBuy14_2)||(CloseBuy14_1>CloseBuy14_2 && CloseBuy15_1<CloseBuy15_2)||(CloseBuy15_1>CloseBuy15_2 && CloseBuy16_1<CloseBuy16_2)||(CloseBuy16_1>CloseBuy16_2 && CloseBuy17_1<CloseBuy17_2)||(CloseBuy17_1>CloseBuy17_2 && CloseBuy18_1<CloseBuy18_2)||(CloseBuy18_1>CloseBuy18_2 && CloseBuy19_1<CloseBuy19_2)||(CloseBuy19_1>CloseBuy19_2 && CloseBuy20_1<CloseBuy20_2))&&((CloseBuy21_1>CloseBuy21_2 && CloseBuy22_1<CloseBuy22_2)||(CloseBuy22_1>CloseBuy22_2 && CloseBuy23_1<CloseBuy23_2)||(CloseBuy23_1>CloseBuy23_2 && CloseBuy24_1<CloseBuy24_2)||(CloseBuy24_1>CloseBuy24_2 && CloseBuy25_1<CloseBuy25_2)||(CloseBuy25_1>CloseBuy25_2 && CloseBuy26_1<CloseBuy26_2)||(CloseBuy26_1>CloseBuy26_2 && CloseBuy27_1<CloseBuy27_2)||(CloseBuy27_1>CloseBuy27_2 && CloseBuy28_1<CloseBuy28_2)||(CloseBuy28_1>CloseBuy28_2 && CloseBuy29_1<CloseBuy29_2)||(CloseBuy29_1>CloseBuy29_2 && CloseBuy30_1<CloseBuy30_2)||(CloseBuy30_1>CloseBuy30_2 && CloseBuy31_1<CloseBuy31_2)||(CloseBuy31_1>CloseBuy31_2 && CloseBuy32_1<CloseBuy32_2)))Order = SIGNAL_CLOSESELL;


            //+------------------------------------------------------------------+
            //| Signal End(Exit Sell)                                            |
            //+------------------------------------------------------------------+

            if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
               OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
               if (!EachTickMode) BarCount = Bars;
               IsTrade = False;
               continue;
            }
            //Trailing stop
            if(UseTrailingStop && TrailingStop > 0) {                 
               if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {
                  if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
                     if (!EachTickMode) BarCount = Bars;
                     continue;
                  }
               }
            }
         }
      }
   }

   //+------------------------------------------------------------------+
   //| Signal Begin(Entry)         &&      ||                                     |
   //+------------------------------------------------------------------+
   
   
   //pozisyon yoksa
   if(OrdersTotal()<1){
   
  
  
   //rsi 50 yi yukarı kesmişsse
   if ((Buy1_1>Buy1_2 && Buy2_1<Buy2_2)||(Buy2_1>Buy2_2 && Buy3_1<Buy3_2)||(Buy3_1>Buy3_2 && Buy4_1<Buy4_2)||(Buy4_1>Buy4_2 && Buy5_1<Buy5_2)||(Buy5_1>Buy5_2 && Buy6_1<Buy6_2)||(Buy6_1>Buy6_2 && Buy7_1<Buy7_2)||(Buy7_1>Buy7_2 && Buy8_1<Buy8_2)||(Buy8_1>Buy8_2 && Buy9_1<Buy9_2)||(Buy9_1>Buy9_2 && Buy10_1<Buy10_2)||(Buy10_1>Buy10_2 && Buy11_1<Buy11_2)){
   
   //şart doğru
   bool pozyoksart1=true;
   }

   //emalar kesişmişse
   if((Buy12_1>Buy12_2 && Buy13_1<Buy13_2)||(Buy13_1>Buy13_2 && Buy14_1<Buy14_2)||(Buy14_1>Buy14_2 && Buy15_1<Buy15_2)||(Buy15_1>Buy15_2 && Buy16_1<Buy16_2)||(Buy16_1>Buy16_2 && Buy17_1<Buy17_2)||(Buy17_1>Buy17_2 && Buy18_1<Buy18_2)||(Buy18_1>Buy18_2 && Buy19_1<Buy19_2)||(Buy19_1>Buy19_2 && Buy20_1<Buy20_2)){
   
   //şart doğru
   bool pozyoksart2=true;
   }
   
   //stokastik kesişmişse
   if((Buy21_1>Buy21_2 && Buy22_1<Buy22_2)||(Buy22_1>Buy22_2 && Buy23_1<Buy23_2)||(Buy23_1>Buy23_2 && Buy24_1<Buy24_2)||(Buy24_1>Buy24_2 && Buy25_1<Buy25_2)||(Buy25_1>Buy25_2 && Buy26_1<Buy26_2)||(Buy26_1>Buy26_2 && Buy27_1<Buy27_2)||(Buy27_1>Buy27_2 && Buy28_1<Buy28_2)||(Buy28_1>Buy28_2 && Buy29_1<Buy29_2)||(Buy29_1>Buy29_2 && Buy30_1<Buy30_2)||(Buy30_1>Buy30_2 && Buy31_1<Buy31_2)||(Buy31_1>Buy31_2 && Buy32_1<Buy32_2)){
   
   //şart doğru
   bool pozyoksart3=true;
   }
   
   
   //eğer bütün şartlar doğruysa işleme gir
   if(pozyoksart1==true && pozyoksart2==true&& pozyoksart3==true){
   
   Order = SIGNAL_BUY;
   }
   pozyoksart1= false;
   pozyoksart2= false;
   pozyoksart3= false;
   

   //eğer bir şart doğru değilse tüm şartlar doğru değil
   if(pozyoksart1==false ||pozyoksart2==false||pozyoksart3==false){
   
   pozyoksart1= false;
   pozyoksart2= false;
   pozyoksart3= false;
   }

   
   //eğer rsi terse dönerse şartı yanlışla 
   if(pozyoksart1==true){
   
   if ((Sell1_1<Sell1_2 && Sell2_1>Sell2_2)||(Sell2_1<Sell2_2 && Sell3_1>Sell3_2)||(Sell3_1<Sell3_2 && Sell4_1>Sell4_2)||(Sell4_1<Sell4_2 && Sell5_1>Sell5_2)||(Sell5_1<Sell5_2 && Sell6_1>Sell6_2)||(Sell6_1<Sell6_2 && Sell7_1>Sell7_2)||(Sell7_1<Sell7_2 && Sell8_1>Sell8_2)||(Sell8_1<Sell8_2 && Sell9_1>Sell9_2)||(Sell9_1<Sell9_2 && Sell10_1>Sell10_2)||(Sell10_1<Sell10_2 && Sell11_1>Sell11_2)){
   pozyoksart1=false;
   }
   }
   
   //eğer emalar terse dönerse şartı yanlışla
   if(pozyoksart2==true){
  
   if ((Sell12_1<Sell12_2 && Sell13_1>Sell13_2)||(Sell13_1<Sell13_2 && Sell14_1>Sell14_2)||(Sell14_1<Sell14_2 && Sell15_1>Sell15_2)||(Sell5_1<Sell15_2 && Sell16_1>Sell16_2)||(Sell16_1<Sell16_2 && Sell17_1>Sell17_2)||(Sell17_1<Sell17_2 && Sell18_1>Sell18_2)||(Sell18_1<Sell18_2 && Sell19_1>Sell19_2)||(Sell19_1<Sell19_2 && Sell20_1>Sell20_2)){
   pozyoksart2=false;
   }
   }
   
   //eğer stokastik terse kesişirse şartı yanlışla
   if(pozyoksart3==true){
  
   if ((Sell21_1<Sell21_2 && Sell22_1>Sell22_2)||(Sell22_1<Sell22_2 && Sell23_1>Sell23_2)||(Sell23_1<Sell23_2 && Sell24_1>Sell24_2)||(Sell24_1<Sell24_2 && Sell25_1>Sell25_2)||(Sell25_1<Sell25_2 && Sell26_1>Sell26_2)||(Sell26_1<Sell26_2 && Sell27_1>Sell27_2)||(Sell27_1<Sell27_2 && Sell28_1>Sell28_2)||(Sell28_1<Sell28_2 && Sell29_1>Sell29_2)||(Sell29_1<Sell29_2 && Sell30_1>Sell30_2)||(Sell30_1<Sell30_2 && Sell31_1>Sell31_2)||(Sell31_1<Sell31_2 && Sell32_1>Sell32_2)){
   pozyoksart3=false;
   }  
   }

   
  
   
   
   
   
   
   
   
   //rsi 50 yi aşağı kesmişsse
   if ((Sell1_1<Sell1_2 && Sell2_1>Sell2_2)||(Sell2_1<Sell2_2 && Sell3_1>Sell3_2)||(Sell3_1<Sell3_2 && Sell4_1>Sell4_2)||(Sell4_1<Sell4_2 && Sell5_1>Sell5_2)||(Sell5_1<Sell5_2 && Sell6_1>Sell6_2)||(Sell6_1<Sell6_2 && Sell7_1>Sell7_2)||(Sell7_1<Sell7_2 && Sell8_1>Sell8_2)||(Sell8_1<Sell8_2 && Sell9_1>Sell9_2)||(Sell9_1<Sell9_2 && Sell10_1>Sell10_2)||(Sell10_1<Sell10_2 && Sell11_1>Sell11_2)){
   
   //şart doğru
   bool pozyoksart4 = true;
   }
   
   //emalar kesişmişse
   if ((Sell12_1<Sell12_2 && Sell13_1>Sell13_2)||(Sell13_1<Sell13_2 && Sell14_1>Sell14_2)||(Sell14_1<Sell14_2 && Sell15_1>Sell15_2)||(Sell5_1<Sell15_2 && Sell16_1>Sell16_2)||(Sell16_1<Sell16_2 && Sell17_1>Sell17_2)||(Sell17_1<Sell17_2 && Sell18_1>Sell18_2)||(Sell18_1<Sell18_2 && Sell19_1>Sell19_2)||(Sell19_1<Sell19_2 && Sell20_1>Sell20_2)){
   
   //şart doğru
   bool pozyoksart5 = true;
   }
   
   //stokastik kesişmişse
   if ((Sell21_1<Sell21_2 && Sell22_1>Sell22_2)||(Sell22_1<Sell22_2 && Sell23_1>Sell23_2)||(Sell23_1<Sell23_2 && Sell24_1>Sell24_2)||(Sell24_1<Sell24_2 && Sell25_1>Sell25_2)||(Sell25_1<Sell25_2 && Sell26_1>Sell26_2)||(Sell26_1<Sell26_2 && Sell27_1>Sell27_2)||(Sell27_1<Sell27_2 && Sell28_1>Sell28_2)||(Sell28_1<Sell28_2 && Sell29_1>Sell29_2)||(Sell29_1<Sell29_2 && Sell30_1>Sell30_2)||(Sell30_1<Sell30_2 && Sell31_1>Sell31_2)||(Sell31_1<Sell31_2 && Sell32_1>Sell32_2)){
   
   //şart doğru
   bool pozyoksart6 = true;
   }

   
   //eğer bütün şartlar doğruysa işleme gir
   if(pozyoksart4==true&& pozyoksart5==true&& pozyoksart6==true){
   
   Order = SIGNAL_SELL;
   }
   pozyoksart4 =false;
   pozyoksart5 =false;
   pozyoksart6 =false;
 
   
   //eğer bir şart doğru değilse tüm şartlar doğru değil
   if(pozyoksart4==false||pozyoksart5==false||pozyoksart6==false){
   
   pozyoksart4 =false;
   pozyoksart5 =false;
   pozyoksart6 =false;
   }
   
   //eğer rsi terse dönerse şartı yanlışla
   if(pozyoksart4==true){
   
   if ((Buy1_1>Buy1_2 && Buy2_1<Buy2_2)||(Buy2_1>Buy2_2 && Buy3_1<Buy3_2)||(Buy3_1>Buy3_2 && Buy4_1<Buy4_2)||(Buy4_1>Buy4_2 && Buy5_1<Buy5_2)||(Buy5_1>Buy5_2 && Buy6_1<Buy6_2)||(Buy6_1>Buy6_2 && Buy7_1<Buy7_2)||(Buy7_1>Buy7_2 && Buy8_1<Buy8_2)||(Buy8_1>Buy8_2 && Buy9_1<Buy9_2)||(Buy9_1>Buy9_2 && Buy10_1<Buy10_2)||(Buy10_1>Buy10_2 && Buy11_1<Buy11_2)){
   pozyoksart4=false;
   }
   }
   
   //eğer emalar terse dönerse şartı yanlışla
   if(pozyoksart5==true){
   
   if((Buy12_1>Buy12_2 && Buy13_1<Buy13_2)||(Buy13_1>Buy13_2 && Buy14_1<Buy14_2)||(Buy14_1>Buy14_2 && Buy15_1<Buy15_2)||(Buy15_1>Buy15_2 && Buy16_1<Buy16_2)||(Buy16_1>Buy16_2 && Buy17_1<Buy17_2)||(Buy17_1>Buy17_2 && Buy18_1<Buy18_2)||(Buy18_1>Buy18_2 && Buy19_1<Buy19_2)||(Buy19_1>Buy19_2 && Buy20_1<Buy20_2)){
   pozyoksart5=false;
   }
   }
   
   //eğer stokastik terse kesişirse şartı yanlışla
   if(pozyoksart6==true){
   
   if((Buy21_1>Buy21_2 && Buy22_1<Buy22_2)||(Buy22_1>Buy22_2 && Buy23_1<Buy23_2)||(Buy23_1>Buy23_2 && Buy24_1<Buy24_2)||(Buy24_1>Buy24_2 && Buy25_1<Buy25_2)||(Buy25_1>Buy25_2 && Buy26_1<Buy26_2)||(Buy26_1>Buy26_2 && Buy27_1<Buy27_2)||(Buy27_1>Buy27_2 && Buy28_1<Buy28_2)||(Buy28_1>Buy28_2 && Buy29_1<Buy29_2)||(Buy29_1>Buy29_2 && Buy30_1<Buy30_2)||(Buy30_1>Buy30_2 && Buy31_1<Buy31_2)||(Buy31_1>Buy31_2 && Buy32_1<Buy32_2)){
   pozyoksart6=false;
   }
   }

   
   }

   
   
   
   
   
  
   
   
   
   
   
   //pozisyon varsa
   if(OrdersTotal()==1){
   
   
   
   //eğer pozisyon alışsa
   if(Order== SIGNAL_BUY){
   
   //eğer rsi aşağı kesişmişse
   if ((Sell1_1<Sell1_2 && Sell2_1>Sell2_2)||(Sell2_1<Sell2_2 && Sell3_1>Sell3_2)||(Sell3_1<Sell3_2 && Sell4_1>Sell4_2)||(Sell4_1<Sell4_2 && Sell5_1>Sell5_2)||(Sell5_1<Sell5_2 && Sell6_1>Sell6_2)||(Sell6_1<Sell6_2 && Sell7_1>Sell7_2)||(Sell7_1<Sell7_2 && Sell8_1>Sell8_2)||(Sell8_1<Sell8_2 && Sell9_1>Sell9_2)||(Sell9_1<Sell9_2 && Sell10_1>Sell10_2)||(Sell10_1<Sell10_2 && Sell11_1>Sell11_2)){
   
   //rsi satış şartını doğrula
   bool pozvarsart4 = true;
   }
   
   //eğer emalar aşağı kesişmişse
   if ((Sell12_1<Sell12_2 && Sell13_1>Sell13_2)||(Sell13_1<Sell13_2 && Sell14_1>Sell14_2)||(Sell14_1<Sell14_2 && Sell15_1>Sell15_2)||(Sell5_1<Sell15_2 && Sell16_1>Sell16_2)||(Sell16_1<Sell16_2 && Sell17_1>Sell17_2)||(Sell17_1<Sell17_2 && Sell18_1>Sell18_2)||(Sell18_1<Sell18_2 && Sell19_1>Sell19_2)||(Sell19_1<Sell19_2 && Sell20_1>Sell20_2)){
   
   //ema satış şartını doğrula
   bool pozvarsart5 = true;
   }
   
   //stokastik aşağı kesişmişse
   if ((Sell21_1<Sell21_2 && Sell22_1>Sell22_2)||(Sell22_1<Sell22_2 && Sell23_1>Sell23_2)||(Sell23_1<Sell23_2 && Sell24_1>Sell24_2)||(Sell24_1<Sell24_2 && Sell25_1>Sell25_2)||(Sell25_1<Sell25_2 && Sell26_1>Sell26_2)||(Sell26_1<Sell26_2 && Sell27_1>Sell27_2)||(Sell27_1<Sell27_2 && Sell28_1>Sell28_2)||(Sell28_1<Sell28_2 && Sell29_1>Sell29_2)||(Sell29_1<Sell29_2 && Sell30_1>Sell30_2)||(Sell30_1<Sell30_2 && Sell31_1>Sell31_2)||(Sell31_1<Sell31_2 && Sell32_1>Sell32_2)){
   
   //stokastik satış şartını doğrula
   bool pozvarsart6 = true;
   }
   

   //eğer bütün satış şartları doğruysa
   if(pozvarsart4==true&& pozvarsart5==true&& pozvarsart6==true){
   
   //alış pozisyonunu kapa satış pozisyonu aç şartları yanlışla
   Order = SIGNAL_CLOSEBUY;
   Order = SIGNAL_SELL;
      
   pozvarsart4 =false;
   pozvarsart5 =false;
   pozvarsart6 =false;

   }
   }
   
   
   
   //eğer pozisyon satışsa
   if(Order== SIGNAL_SELL){

   //eğer rsi yukarı kesişmişse
   if ((Buy1_1>Buy1_2 && Buy2_1<Buy2_2)||(Buy2_1>Buy2_2 && Buy3_1<Buy3_2)||(Buy3_1>Buy3_2 && Buy4_1<Buy4_2)||(Buy4_1>Buy4_2 && Buy5_1<Buy5_2)||(Buy5_1>Buy5_2 && Buy6_1<Buy6_2)||(Buy6_1>Buy6_2 && Buy7_1<Buy7_2)||(Buy7_1>Buy7_2 && Buy8_1<Buy8_2)||(Buy8_1>Buy8_2 && Buy9_1<Buy9_2)||(Buy9_1>Buy9_2 && Buy10_1<Buy10_2)||(Buy10_1>Buy10_2 && Buy11_1<Buy11_2)){
   
   //rsi şartını doğrula
   bool pozvarsart1=true;
   }
   
   //eğer ema yukarı kesişmişse
   if((Buy12_1>Buy12_2 && Buy13_1<Buy13_2)||(Buy13_1>Buy13_2 && Buy14_1<Buy14_2)||(Buy14_1>Buy14_2 && Buy15_1<Buy15_2)||(Buy15_1>Buy15_2 && Buy16_1<Buy16_2)||(Buy16_1>Buy16_2 && Buy17_1<Buy17_2)||(Buy17_1>Buy17_2 && Buy18_1<Buy18_2)||(Buy18_1>Buy18_2 && Buy19_1<Buy19_2)||(Buy19_1>Buy19_2 && Buy20_1<Buy20_2)){
   
   //ema şartını doğrula
   bool pozvarsart2=true;
   }
   
   //stokastik yukarı kesişmişse
   if((Buy21_1>Buy21_2 && Buy22_1<Buy22_2)||(Buy22_1>Buy22_2 && Buy23_1<Buy23_2)||(Buy23_1>Buy23_2 && Buy24_1<Buy24_2)||(Buy24_1>Buy24_2 && Buy25_1<Buy25_2)||(Buy25_1>Buy25_2 && Buy26_1<Buy26_2)||(Buy26_1>Buy26_2 && Buy27_1<Buy27_2)||(Buy27_1>Buy27_2 && Buy28_1<Buy28_2)||(Buy28_1>Buy28_2 && Buy29_1<Buy29_2)||(Buy29_1>Buy29_2 && Buy30_1<Buy30_2)||(Buy30_1>Buy30_2 && Buy31_1<Buy31_2)||(Buy31_1>Buy31_2 && Buy32_1<Buy32_2)){
   
   //stokastik şartını doğrula
   bool pozvarsart3=true;
   }

   //eğer bütün alış şartları doğruysa
   if(pozvarsart1==true&& pozvarsart2==true&& pozvarsart3==true){
   
   //satış pozisyonunu kapa alış pozisyonu aç şartları yanlışla
   Order = SIGNAL_CLOSESELL;
   Order = SIGNAL_BUY;
   
   pozvarsart1 =false;
   pozvarsart2 =false;
   pozvarsart3 =false;

   
   }
   }


   }
































//+------------------------------------------------------------------+
   //| Signal End                                                       |
   //+------------------------------------------------------------------+

   //Buy
   if (Order == SIGNAL_BUY && Order != LastPosition && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
      if(!IsTrade) {
         //Check free margin
         if (AccountFreeMargin() < (1000 * Lots)) {
            Print("We have no money. Free Margin = ", AccountFreeMargin());
            return(0);
         }

         if (UseStopLoss) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;
         if (UseTakeProfit) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;

         Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);
         if(Ticket > 0) {
            if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
				Print("BUY order opened : ", OrderOpenPrice());
				LastPosition = SIGNAL_BUY;
                if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");
			} else {
				Print("Error opening BUY order : ", GetLastError());
			}
         }
         if (EachTickMode) TickCheck = True;
         if (!EachTickMode) BarCount = Bars;
         return(0);
      }
   }

   //Sell
   if (Order == SIGNAL_SELL && LastPosition!= Order && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
      if(!IsTrade) {
         //Check free margin
         if (AccountFreeMargin() < (1000 * Lots)) {
            Print("We have no money. Free Margin = ", AccountFreeMargin());
            return(0);
         }

         if (UseStopLoss) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;
         if (UseTakeProfit) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;

         Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);
         if(Ticket > 0) {
            if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
            LastPosition = Order;
				Print("SELL order opened : ", OrderOpenPrice());
                if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");
			} else {
				Print("Error opening SELL order : ", GetLastError());
			}
         }
         if (EachTickMode) TickCheck = True;
         if (!EachTickMode) BarCount = Bars;
         return(0);
      }
   }

   if (!EachTickMode) BarCount = Bars;

   return(0);

}


//+------------------------------------------------------------------+