//+------------------------------------------------------------------+
//| This MQL is generated by Expert Advisor Builder                  |
//|                http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/ |
//|                                                                  |
//|  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_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4

#property copyright "Expert Advisor Builder"
#property link      "http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/"

extern int MagicNumber = 0;
extern bool SignalMail = False;
extern bool EachTickMode = True;
extern double Lots = 1.0;
extern int Slippage = 2;
extern bool UseStopLoss = True;
extern int StopLoss = 100;
extern bool UseTakeProfit = True;
extern int TakeProfit = 100;

int BarCount;
int Current;
bool TickCheck = False;
//+------------------------------------------------------------------+
//| 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;
   double StopLossLevel, TakeProfitLevel;

   if (EachTickMode && Bars != BarCount)
     TickCheck = False;
   Total = OrdersTotal();
   Order = SIGNAL_NONE;

   //+------------------------------------------------------------------+
   //| Variable Begin                                                   |
   //+------------------------------------------------------------------+

double CloseBuy1_1 = iRSI(NULL, 0, 3, PRICE_CLOSE, Current + 0);
double CloseBuy1_2 = 50;

double CloseSell1_1 = iRSI(NULL, 0, 3, PRICE_CLOSE, Current + 0);
double CloseSell1_2 = 50;
   
   //+------------------------------------------------------------------+
   //| 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)
          {
            //| Signal Begin(Exit Buy)                                           |

                     if (CloseBuy1_1 == CloseBuy1_2) Order = SIGNAL_CLOSEBUY;
            //| 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;
             }
           }  // if opbuy
         }   // if ordertype
         
         else
          {
            //| Signal Begin(Exit Sell)                                          |
           if (CloseSell1_1 == CloseSell1_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;
              }
            
            } // else
         } // for i
  }
 


//+------------------------------------------------------------------+