//+------------------------------------------------------------------+
//| 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 = 65 ;
extern double Percentage_ATR_TL = 20 ;
extern double Percentage_ATR_SL = 25 ;
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 double breakevengain=25;

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 double Lots = 2.0;         //Lots to trade with
extern int Take_Partial_Profit = 40; //Set to 0 to turn Partial Profit off.
extern int TakeProfit = 130;      //Where to take profit out at


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 tenkansen=iIchimoku(NULL,0,9,26,52,MODE_TENKANSEN,0);
   double kijunsen=iIchimoku(NULL,0,9,26,52,MODE_KIJUNSEN,0);
   double ctenkansen=iIchimoku(NULL,0,9,26,52,MODE_TENKANSEN,0);
   double ckijunsen=iIchimoku(NULL,0,9,26,52,MODE_KIJUNSEN,0);
   double cls=iClose(NULL,0,0);

    double aw1 =iAO(NULL,0,0);
   double aw2 =iAO(NULL,0, +1);
   double dm = iDeMarker(NULL,PERIOD_M30,25,0);
   double dmu= 0.85;
   double dmd= 0.2;
   double ma = iMA(NULL,PERIOD_M15,9,0,MODE_SMA,PRICE_CLOSE,0);
   double stoch = iStochastic(NULL,PERIOD_M30,35,9,15,MODE_SMA,PRICE_CLOSE,MODE_MAIN,0);
   double stoch2 = iStochastic(NULL,PERIOD_M30,35,9,15,MODE_SMA,PRICE_CLOSE,MODE_SIGNAL,0);
   double adx= iADX(NULL,0,15,PRICE_CLOSE,MODE_MAIN,0);
   double adx1= 22;
   
   double senkouspana=iIchimoku(NULL,0,9,26,52,MODE_SENKOUSPANA,0);
   double senkouspanb=iIchimoku(NULL,0,9,26,52,MODE_SENKOUSPANB,0);
   double h=iIchimoku(NULL,PERIOD_M15,7,20,45,MODE_SENKOUSPANA,0);
   double h2=iIchimoku(NULL,PERIOD_M15,7,20,45,MODE_SENKOUSPANB,0);
   double cls2=iClose(NULL,PERIOD_H1,0);
   double ch1 = iCustom(NULL, PERIOD_H1, "ChandelierExitM",0,7,0,12,2.5, Current);
   double ch2 = iCustom(NULL, PERIOD_H1, "ChandelierExitM",0,7,0,12,2.5, Current);
   double chikouspan=iIchimoku(NULL,PERIOD_M15,9,26,52,MODE_CHINKOUSPAN,+26);
   double cspana=iIchimoku(NULL,PERIOD_M15,9,26,52,MODE_SENKOUSPANA,+26);
   double cspanb=iIchimoku(NULL,PERIOD_M15,9,26,52,MODE_SENKOUSPANB,+26);
   //double cchikouspan=iIchimoku(NULL,0,ptenkan,pkijun,psenkou,MODE_CHINKOUSPAN,shift);
   

  


double CloseBuy2_1 = iCustom(NULL, PERIOD_M30, "ChandelierExitM",0,7,12,2.5, Current);
double CloseBuy2_2 = iClose(NULL, 0, Current);
double CloseBuy3_1 =  iStochastic(NULL,0,35,9,15,MODE_SMA,PRICE_CLOSE,MODE_MAIN,0);
double CloseBuy3_2 = iStochastic(NULL,0,35,9,15,MODE_SMA,PRICE_CLOSE,MODE_SIGNAL,0);
double CloseBuy4_1 = iADX(NULL,0,18,PRICE_CLOSE,MODE_MAIN, Current);
double CloseBuy4_2 = 24;

double CloseSell2_1 = iCustom(NULL, PERIOD_M30,  "ChandelierExitM", 0,7,12,2.5, Current);
double CloseSell2_2 = iClose(NULL, 0, Current); 
double CloseSell3_1 = iStochastic(NULL,0,35,9,15,MODE_SMA,PRICE_CLOSE,MODE_MAIN,0);
double CloseSell3_2 = iStochastic(NULL,0,35,9,15,MODE_SMA,PRICE_CLOSE,MODE_SIGNAL,0);
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)) 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(), 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)) 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(), 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((tenkansen > ckijunsen)
   
   && (ma > h)&& (ma > h2)
   && (cls2 > ctenkansen)
   && (aw1 > aw2)
   && (chikouspan > cspanb)&& (chikouspan > cspana)
  && (stoch > stoch2 )
   && (dm < dmu )
   && (cls > ma)&& (cls2 > ch1)
   && (adx > adx1)) Order = SIGNAL_BUY;
   
   if
   ((ma < h)&& (ma < h2)
   && (cls2 < ctenkansen )
   && (aw1 < aw2)
   && (chikouspan < cspanb)&& (chikouspan < cspana)
   && (stoch < stoch2 )
   && (dm > dmd)
   && (cls < ma)&& (cls2 < ch2)
   && (adx > adx1)) 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);
  
}



//++------------------------------------------------------------------+