//+------------------------------------------------------------------+
//| 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_BUY   1
#define SIGNAL_SELL  2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4

#property copyright "Expert Advisor Builder"
#property link      "http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/"

extern double Percentage_ATR_TP = 58 ;
extern double Percentage_ATR_TL = 38 ;
extern double Percentage_ATR_SL = 38 ;
extern bool Orders_ATR = True ;
extern bool Lots_ATR = True ;
extern bool backtesting = True ;
extern bool five_digit_broker = True ;
extern bool mini_account = True ;
extern double Risk = 0.5 ;
extern int MagicNumber = 0;
extern bool SignalMail = False;
extern bool EachTickMode = False;
extern int Slippage = 3;
extern bool UseStopLoss = True;
extern int StopLoss = 67;
extern bool UseTakeProfit = True;
extern bool UseTrailingStop = True;
extern int TrailingStop = 66;
extern int Start_BE = 40;         // When to move to B/E
extern double Lots = 2.0;         //Lots to trade with
extern double Take_Partial_Profit_Lots = 1.0;
extern int Take_Partial_Profit = 40; //Set to 0 to turn Partial Profit off.
extern int TakeProfit = 130;      //Where to take profit out at
extern bool Use_ATR_Stop = true;   //Set to true if set initial stop based on ATR(10) versus high/low prev bar.
extern double Use_ATR_Pct = 0.7;
extern int Init_Stop_Cushion = 0; //Stops set at previous high/low, plus a little cushion above or below
  //Set to true if you want to use the trailing stop
extern int TrailingAct = 65;      //When to start trailing (BE + Trailing Step)
extern int TrailingStep = 25;
int magic = 7984;

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 Buy1_1 = iSAR(NULL, PERIOD_H1, 0.01, 0.2, Current); 
double Buy2_1 = iCustom(NULL, 0, "ATRStops_v1", 14,9,4, 0, Current);
double Buy2_2 = iClose(NULL, 0, Current);
double Buy5_3 = iMA(NULL, PERIOD_H1, 55, 0, MODE_SMA, PRICE_CLOSE, Current);
double Buy4_1 = iADX(NULL,0,18,PRICE_CLOSE,MODE_MAIN,Current );
double Buy4_2 = 16;
double Buy5_1 = iMA(NULL, PERIOD_H1, 7, 0, MODE_SMA, PRICE_CLOSE, Current);
double Buy5_2 = iMA(NULL, PERIOD_H1, 18, 0, MODE_SMA, PRICE_CLOSE, Current);
double Buy6_1 = iDeMarker(NULL, 0, 13, Current);
double Buy6_2 = 0.75;
double Buy7_1 = iAO(NULL, PERIOD_H1, Current);
double Buy7_2 = iAO(NULL, PERIOD_H1, Current + 1);
double Buy8_1 = iAC(NULL, PERIOD_H1, Current);
double Buy8_2 = iAC(NULL, PERIOD_H1, Current + 1);
double Buy9_1 = iRSI(NULL, PERIOD_H1, 12, PRICE_CLOSE, Current);
double Buy9_2 = 70;
double Buy10_1 =  iBands( NULL, PERIOD_M30,25,2,0,PRICE_CLOSE, MODE_UPPER, Current);
double Buy10_2 =  iBands( NULL, PERIOD_M30,25,2,0,PRICE_CLOSE,MODE_UPPER, Current +1);

double Sell1_1 = iSAR(NULL, PERIOD_H1, 0.01, 0.2, Current);
double Sell2_1 = iCustom(NULL, 0, "ATRStops_v1", 14,9,4, 0, Current);
double Sell2_2 = iClose(NULL, 0, Current);
double Sell5_3 = iMA(NULL, PERIOD_H1, 55, 0, MODE_SMA, PRICE_CLOSE, Current);
double Sell4_1 = iADX(NULL,0,18,PRICE_CLOSE,MODE_MAIN,Current);
double Sell4_2 = 16;
double Sell5_1 = iMA(NULL,PERIOD_H1, 7, 0, MODE_SMA, PRICE_CLOSE, Current );
double Sell5_2 = iMA(NULL, PERIOD_H1, 18, 0, MODE_SMA, PRICE_CLOSE, Current );
double Sell6_1 = iDeMarker(NULL, 0, 13, Current);
double Sell6_2 = 0.2;
double Sell7_1 = iAO(NULL, PERIOD_H1, Current);
double Sell7_2 = iAO(NULL, PERIOD_H1, Current + 1);
double Sell8_1 = iAC(NULL, PERIOD_H1, Current);
double Sell8_2 = iAC(NULL, PERIOD_H1, Current + 1);
double Sell9_1 = iRSI(NULL, PERIOD_H1, 12, PRICE_CLOSE, Current);
double Sell9_2 = 18;
double Sell10_1 =  iBands( NULL, PERIOD_M30,25,2, 0,PRICE_CLOSE,MODE_LOWER, Current);
double Sell10_2 =  iBands( NULL, PERIOD_M30,25,2, 0, PRICE_CLOSE,MODE_LOWER, Current +1);

double CloseBuy2_1 = iCustom(NULL, 0, "ATRStops_v1", 14,9,4, 0, Current);
double CloseBuy2_2 = iClose(NULL, 0, Current);
double CloseBuy3_1 = iDeMarker(NULL, 0, 13, Current);
double CloseBuy3_2 = 0.27;
double CloseBuy4_1 = iADX(NULL,0,18,PRICE_CLOSE,MODE_MAIN, Current);
double CloseBuy4_2 = 24;

double CloseSell2_1 = iCustom(NULL, 0, "ATRStops_v1", 14,9,4, 0, Current);
double CloseSell2_2 = iClose(NULL, 0, Current);
double CloseSell3_1 = iDeMarker(NULL, 0, 13, Current);
double CloseSell3_2 = 0.67;
double CloseSell4_1 = iADX(NULL,0,18,PRICE_CLOSE,MODE_MAIN,Current);
double CloseSell4_2 = 24;



   //+------------------------------------------------------------------+
   //| Variable End                                                     |
   //+------------------------------------------------------------------+
   
    //+------------------------------------------------------------------+
   //| ATR Dynamic Logic Begin                                           |
   //+------------------------------------------------------------------+
   
   
   double ATR = iATR(NULL,0,14,0);
   double ContractSize = 100000 ;
   
   if(ATR > 0.1)
   ATR = iATR(NULL,0,14,0)/100 ;
   
   if(mini_account == true)
   ContractSize=10000;
   
   if(Orders_ATR ==  true)
   {
   StopLoss=Percentage_ATR_SL*ATR; TakeProfit=Percentage_ATR_TP*ATR; TrailingStop=Percentage_ATR_TL*ATR;
   }
   
   if(Lots_ATR ==  true)
   {
   Lots=Risk*1000/(ContractSize*Percentage_ATR_SL*ATR); Lots=NormalizeDouble(Lots, 2);
   }

   if(five_digit_broker == true && Orders_ATR == true)
   {
   StopLoss=100*Percentage_ATR_SL*ATR*10; TakeProfit=100*Percentage_ATR_TP*ATR*10; TrailingStop=100*Percentage_ATR_TL*ATR*10;
   }
   
   if(five_digit_broker == true && Orders_ATR == false)
   {
   StopLoss=StopLoss*10 ;  TakeProfit=TakeProfit*10 ;  TrailingStop=TrailingStop*10 ;
   }
   
   if(backtesting == true)
   Lots=NormalizeDouble(Lots, 1);

   //+------------------------------------------------------------------+
   //| ATR Dynamic Logic 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 ((CloseBuy2_1 > CloseBuy2_2) || (CloseBuy3_1 < CloseBuy3_2 && CloseBuy4_1 > CloseBuy4_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(OrderType() == OP_BUY)
     {
              
        OrderClose(OrderTicket(),Take_Partial_Profit_Lots, Bid, 0, LimeGreen);        
       }
            //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 ((CloseSell2_1 < CloseSell2_2) || (CloseSell3_1 > CloseSell3_2 && CloseSell4_1 > CloseSell4_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;
            }
            if(OrderType() == OP_SELL)
     
        OrderClose(OrderTicket(),Take_Partial_Profit_Lots, Ask, 0, LimeGreen);
            //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)                                              |
   //+------------------------------------------------------------------+

   if (Buy1_1 < Buy2_2 && Buy2_1 < Buy2_2 && Buy2_2 > Buy5_3 && Buy4_1 > Buy4_2 && Buy2_2 > Buy5_1 && Buy5_1 > Buy5_2 && Buy5_2 > Buy5_3 && Buy6_1 < Buy6_2 && Buy7_1 > Buy7_2 && Buy8_1 > Buy8_2 && Buy9_1 < Buy9_2 && Buy10_1 > Buy10_2) Order = SIGNAL_BUY;
   if (Sell1_1 > Sell2_2 && Sell2_1 > Sell2_2 && Sell2_2 < Sell5_3 && Sell4_1 > Sell4_2 && Sell2_2 < Sell5_1&& Sell5_1 < Sell5_2 && Sell5_2 < Sell5_3 && Sell6_1 > Sell6_2 && Sell7_1 < Sell7_2 && Sell8_1 < Sell8_2 && Sell9_1 > Sell9_2 && Sell10_1 < Sell10_2) Order = SIGNAL_SELL;

     

   //+------------------------------------------------------------------+
   //| Signal End                                                       |
   //+------------------------------------------------------------------+

   //Buy
   if (Order == SIGNAL_BUY && ((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());
                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 && ((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)) {
				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);
  
}



//++------------------------------------------------------------------+