//+------------------------------------------------------------------+
//| 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 bool close_with_ma_d = False;
extern bool close_with_ma_h = False;
extern bool close_with_sto_d = False;
extern bool close_with_sto_h = False;
extern bool close_with_rsi_d = False;
extern bool close_with_rsi_h = False;
extern string ind1 = "stochastics h1";
extern int k_h = 8;
extern int sl_h = 3;
extern int d_h = 3;
extern string ind2 = "stochastics day";
extern int k_d = 8;
extern int sl_d = 3;
extern int d_d = 3;
extern string ind3 = "rsi";
extern int rsih = 13;
extern int rsid = 13;
extern string ind4 = "ma";
extern int ma1h = 5;
extern int ma2h = 8;
extern int ma1d = 5;
extern int ma2d = 8;
 int bar = 0;
 int bard = 0;

extern bool EachTickMode = False;
extern double Lots = 0.01;
extern int Slippage = 10;
 bool StopLossMode = true;
extern int StopLoss = 2000;
 bool TakeProfitMode = true;
extern int TakeProfit = 2000;
bool TrailingStopMode = true;
extern int TrailingStop = 2000;
extern int MagicNumber = 0;
extern bool SignalMail = False;
string              daily_direction = "no trade";
string              hour_direction = "no trade";
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() {
double bma1d = iMA(NULL, 1440, ma1d, 0, 1, 0, Current + 1);
double bma2d = iMA(NULL, 1440, ma2d, 0, 1, 1, Current + 1);
double sma1d = iMA(NULL, 1440, ma1d, 0, 1, 0, Current + 1);
double sma2d = iMA(NULL, 1440, ma2d, 0, 1, 1, Current + 1);
double bma1h = iMA(NULL, 60, ma1h, 0, 1, 0, Current + 0);
double bma2h = iMA(NULL, 60, ma2h, 0, 1, 1, Current + 0);
double sma1h = iMA(NULL, 60, ma1h, 0, 1, 0, Current + 0);
double sma2h = iMA(NULL, 60, ma2h, 0, 1, 1, Current + 0);


double bst1h = iStochastic(NULL, 60, k_h, sl_h, d_h, MODE_SMA, 0, 0, Current + 0);
double bst2h = iStochastic(NULL, 60, k_h, sl_h, d_h, MODE_SMA, 0, 1, Current + 0);
double bst1d = iStochastic(NULL, 1440, k_d, sl_d, d_d, MODE_SMA, 0, 0, Current + 1);
double bst2d = iStochastic(NULL, 1440, k_d, sl_d, d_d, MODE_SMA, 0, 1, Current + 1);

double sst1h = iStochastic(NULL, 60, k_h, sl_h, d_h, MODE_SMA, 0, 0, Current + 0);
double sst2h = iStochastic(NULL, 60, k_h, sl_h, d_h, MODE_SMA, 0, 1, Current + 0);
double sst1d = iStochastic(NULL, 1440, k_d, sl_d, d_d, MODE_SMA, 0, 0, Current + 1);
double sst2d = iStochastic(NULL, 1440, k_d, sl_d, d_d, MODE_SMA, 0, 1, Current + 1);

double bst1h2 = iStochastic(NULL, 60, 8, 3, 3, MODE_SMA, 0, 0, Current + 3);
double bst2h2 = iStochastic(NULL, 60, 8, 3, 3, MODE_SMA, 0, 1, Current + 3);
double bst1d2 = iStochastic(NULL, 1440, 8, 3, 3, MODE_SMA, 0, 0, Current + 3);
double bst2d2 = iStochastic(NULL, 1440, 8, 3, 3, MODE_SMA, 0, 1, Current + 3);

double sst1h2 = iStochastic(NULL, 60, 8, 3, 3, MODE_SMA, 0, 0, Current + 3);
double sst2h2 = iStochastic(NULL, 60, 8, 3, 3, MODE_SMA, 0, 1, Current + 3);
double sst1d2 = iStochastic(NULL, 1440, 8, 3, 3, MODE_SMA, 0, 0, Current + 3);
double sst2d2 = iStochastic(NULL, 1440, 8, 3, 3, MODE_SMA, 0, 1, Current + 3);

double bst1h21 = iStochastic(NULL, 60, 8, 3, 3, MODE_SMA, 0, 0, Current + 2);
double bst2h21 = iStochastic(NULL, 60, 8, 3, 3, MODE_SMA, 0, 1, Current + 2);
double bst1d21 = iStochastic(NULL, 1440, 8, 3, 3, MODE_SMA, 0, 0, Current + 2);
double bst2d21 = iStochastic(NULL, 1440, 8, 3, 3, MODE_SMA, 0, 1, Current + 2);

double sst1h21 = iStochastic(NULL, 60, 8, 3, 3, MODE_SMA, 0, 0, Current + 2);
double sst2h21 = iStochastic(NULL, 60, 8, 3, 3, MODE_SMA, 0, 1, Current + 2);
double sst1d21 = iStochastic(NULL, 1440, 8, 3, 3, MODE_SMA, 0, 0, Current + 2);
double sst2d21 = iStochastic(NULL, 1440, 8, 3, 3, MODE_SMA, 0, 1, Current + 2);

double brsid = iRSI(NULL, 1440, rsid, 0, Current + 1);
double brsild = 50;
double brsih = iRSI(NULL, 60, rsih, 0, Current + 0);
double brsilh = 50;
double srsid = iRSI(NULL, 1440, rsid, 0, Current + 1);
double srsild = 50;
double srsih = iRSI(NULL, 60, rsih, 0, Current + 0);
double srsilh = 50;

double brsid2 = iRSI(NULL, 1440, rsid, 0, Current + 1);
double brsild2 = 50;
double brsih2 = iRSI(NULL, 60, rsih, 0, Current + 0);
double brsilh2 = 50;
double srsid2 = iRSI(NULL, 1440, rsid, 0, Current + 1);
double srsild2 = 50;
double srsih2 = iRSI(NULL, 60, rsih, 0, Current + 0);
double srsilh2 = 50;

double brsid21 = iRSI(NULL, 1440, rsid, 0, Current + 1);
double brsild21 = 50;
double brsih21 = iRSI(NULL, 60, rsih, 0, Current + 0);
double brsilh21 = 50;
double srsid21 = iRSI(NULL, 1440, rsid, 0, Current + 1);
double srsild21 = 50;
double srsih21 = iRSI(NULL, 60, rsih, 0, Current + 0);
double srsilh21 = 50;
   
if (bma1d > bma2d&&brsid > brsild&&bst1d > bst2d){
   daily_direction ="long";
   
     {

     }
   }
   if (bma1d < bma2d&&brsid > brsild&&bst1d > bst2d){
   daily_direction ="no trade";
   
     {

     }
   }
      if (bma1d > bma2d&&brsid < brsild&&bst1d > bst2d){
   daily_direction ="no trade";
   
     {

     }
   }
         if (bma1d > bma2d&&brsid > brsild&&bst1d < bst2d){
   daily_direction ="no trade";
   
     {

     }
   }
         if (bma1d < bma2d&&brsid < brsild&&bst1d > bst2d){
   daily_direction ="no trade";
   
     {

     }
   }
         if (bma1d < bma2d&&brsid > brsild&&bst1d < bst2d){
   daily_direction ="no trade";
   
     {

     }
   }
         if (bma1d > bma2d&&brsid < brsild&&bst1d < bst2d){
   daily_direction ="no trade";
   
     {

     }
   }
   if (sma1d < sma2d&&srsid < srsild&&sst1d < sst2d){
   daily_direction ="short";
   
     {

     }
   }
      if (sma1d > sma2d&&srsid < srsild&&sst1d < sst2d){
   daily_direction ="no trade";
   
     {

     }
   }
         if (sma1d < sma2d&&srsid > srsild&&sst1d < sst2d){
   daily_direction ="no trade";
   
     {

     }
   }
         if (sma1d < sma2d&&srsid < srsild&&sst1d > sst2d){
   daily_direction ="no trade";
   
     {

     }
   }
         if (sma1d > sma2d&&srsid > srsild&&sst1d < sst2d){
   daily_direction ="no trade";
   
     {

     }
   }
         if (sma1d < sma2d&&srsid > srsild&&sst1d > sst2d){
   daily_direction ="no trade";
   
     {

     }
   }
         if (sma1d > sma2d&&srsid < srsild&&sst1d > sst2d){
   daily_direction ="no trade";
   
     {

     }
   }
   
   if (bma1h > bma2h&&brsih > brsilh&&bst1h > bst2h){
   hour_direction ="long";
  
     {

     }
   }
      if (bma1h < bma2h&&brsih > brsilh&&bst1h > bst2h){
   hour_direction ="no trade";
  
     {

     }
   }
         if (bma1h > bma2h&&brsih < brsilh&&bst1h > bst2h){
   hour_direction ="no trade";
  
     {

     }
   }
         if (bma1h > bma2h&&brsih > brsilh&&bst1h < bst2h){
   hour_direction ="no trade";
  
     {

     }
   }
         if (bma1h < bma2h&&brsih < brsilh&&bst1h > bst2h){
   hour_direction ="no trade";
  
     {

     }
   }
         if (bma1h > bma2h&&brsih < brsilh&&bst1h < bst2h){
   hour_direction ="no trade";
  
     {

     }
   }
         if (bma1h < bma2h&&brsih > brsilh&&bst1h < bst2h){
   hour_direction ="no trade";
  
     {

     }
   }
   if (sma1h < sma2h&&srsih < srsilh&&sst1h < sst2h){
   hour_direction ="short";
   
     {

     }
   }
      if (sma1h > sma2h&&srsih < srsilh&&sst1h < sst2h){
   hour_direction ="no trade";
   
     {

     }
   }
         if (sma1h < sma2h&&srsih > srsilh&&sst1h < sst2h){
   hour_direction ="no trade";
   
     {

     }
   }
         if (sma1h < sma2h&&srsih < srsilh&&sst1h > sst2h){
   hour_direction ="no trade";
   
     {

     }
   }
         if (sma1h > sma2h&&srsih > srsilh&&sst1h < sst2h){
   hour_direction ="no trade";
   
     {

     }
   }
         if (sma1h < sma2h&&srsih > srsilh&&sst1h > sst2h){
   hour_direction ="no trade";
   
     {

     }
   }
         if (sma1h > sma2h&&srsih < srsilh&&sst1h > sst2h){
   hour_direction ="no trade";
   
     {

     }
   }
  


Comment("\n"," --***--TREND CHASER - KEY TO THE MINT--***--",(""),

"\n","     daily trend direction= ",(daily_direction),
"\n","     hourly trend direction= ",(hour_direction),
"\n","     Broker time= ",TimeToStr(TimeCurrent()),
"\n","     MagicNumber= ",DoubleToStr(MagicNumber,0));

   int Order = SIGNAL_NONE;
   int Total, Ticket;
   double StopLossLevel, TakeProfitLevel;



   if (EachTickMode && Bars != BarCount) TickCheck = False;
   Total = OrdersTotal();
   Order = SIGNAL_NONE;

   //+------------------------------------------------------------------+
   //| Variable Begin                                                   |
   //+------------------------------------------------------------------+


////////////////////////////////////
double cbst1h = iStochastic(NULL, 60, k_h, sl_h, d_h, MODE_SMA, 0, 0, Current + 0);
double cbst2h = iStochastic(NULL, 60, k_h, sl_h, d_h, MODE_SMA, 0, 1, Current + 0);
double cbst1d = iStochastic(NULL, 1440, k_d, sl_d, d_d, MODE_SMA, 0, 0, Current + 0);
double cbst2d = iStochastic(NULL, 1440, k_d, sl_d, d_d, MODE_SMA, 0, 1, Current + 0);

double csst1h = iStochastic(NULL, 60, k_h, sl_h, d_h, MODE_SMA, 0, 0, Current + 0);
double csst2h = iStochastic(NULL, 60, k_h, sl_h, d_h, MODE_SMA, 0, 1, Current + 0);
double csst1d = iStochastic(NULL, 1440, k_d, sl_d, d_d, MODE_SMA, 0, 0, Current + 0);
double csst2d = iStochastic(NULL, 1440, k_d, sl_d, d_d, MODE_SMA, 0, 1, Current + 0);

double cbrsid = iRSI(NULL, 1440, rsid, 0, Current + 0);
double cbrsild = 50;
double cbrsih = iRSI(NULL, 60, rsih, 0, Current + 0);
double cbrsilh = 50;
double csrsid = iRSI(NULL, 1440, rsid, 0, Current + 0);
double csrsild = 50;
double csrsih = iRSI(NULL, 60, rsih, 0, Current + 0);
double csrsilh = 50;

double cbma1h = iMA(NULL, 60, ma1h, 0, 1, 0, Current + 0);
double cbma2h = iMA(NULL, 60, ma2h, 0, 1, 1, Current + 0);
double csma1h = iMA(NULL, 60, ma1h, 0, 1, 0, Current + 0);
double csma2h = iMA(NULL, 60, ma2h, 0, 1, 1, Current + 0);
double cbma1d = iMA(NULL, 1440, ma1d, 0, 1, 0, Current + 0);
double cbma2d = iMA(NULL, 1440, ma2d, 0, 1, 1, Current + 0);
double csma1d = iMA(NULL, 1440, ma1d, 0, 1, 0, Current + 0);
double csma2d = iMA(NULL, 1440, ma2d, 0, 1, 1, Current + 0);
   
   //+------------------------------------------------------------------+
   //| 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 (close_with_ma_d){
         
  if (cbma1d < cbma2d) Order = SIGNAL_CLOSEBUY;
     else
     {

     }
   }
        if (close_with_ma_h){
         
  if (cbma1h < cbma2h) Order = SIGNAL_CLOSEBUY;
     else
     {

     }
   }
   if (close_with_rsi_d){
         
  if (cbrsid < cbrsild) Order = SIGNAL_CLOSEBUY;
     else
     {

     }
   }
      if (close_with_rsi_h){
         
  if (cbrsih < cbrsilh) Order = SIGNAL_CLOSEBUY;
     else
     {

     }
   }
      if (close_with_sto_d){
         
  if (cbst1d < cbst2d) Order = SIGNAL_CLOSEBUY;
     else
     {

     }
   }
         if (close_with_sto_h){
         
  if (cbst1h < cbst2h) Order = SIGNAL_CLOSEBUY;
     else
     {

     }
   }
   //if (cbma1h < cbma2h||cbrsih < cbrsilh||cbst1h < cbst2h) 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;
            }
            //Trailing stop
            if(TrailingStopMode && 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 (close_with_ma_d){
         
  if (csma1d > csma2d) Order = SIGNAL_CLOSESELL;
     else
     {

     }
   }
        if (close_with_ma_h){
         
  if (csma1h > csma2h) Order = SIGNAL_CLOSESELL;
     else
     {

     }
   }
   if (close_with_rsi_d){
         
  if (csrsid > csrsild) Order = SIGNAL_CLOSESELL;
     else
     {

     }
   }
      if (close_with_rsi_h){
         
  if (csrsih > csrsilh) Order = SIGNAL_CLOSESELL;
     else
     {

     }
   }
      if (close_with_sto_d){
         
  if (csst1d > csst2d) Order = SIGNAL_CLOSESELL;
     else
     {

     }
   }
         if (close_with_sto_h){
         
  if (csst1h > csst2h) Order = SIGNAL_CLOSESELL;
     else
     {

     }
   }
                  //   if (csma1h > csma2h||csrsih > csrsilh||csst1h > csst2h) 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(TrailingStopMode && 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 (bma1d > bma2d&&bma1h > bma2h&&brsih > brsilh&&brsid > brsild&&bst1h > bst2h&&bst1d > bst2d) Order = SIGNAL_BUY;

   if (sma1h < sma2h&&sma1d < sma2d&&srsih < srsilh&&srsid < srsild&&sst1h < sst2h&&sst1d < sst2d) 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 (StopLossMode) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;
         if (TakeProfitMode) 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 (StopLossMode) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;
         if (TakeProfitMode) 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);
}
//+------------------------------------------------------------------+