//+------------------------------------------------------------------+
//|                                                                  |
//|  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 OPEN_BUY   1
#define OPEN_SELL  2
#define CLOSE_BUY 3
#define CLOSE_SELL 4
#define UPTREND 5
#define DTREND 6

#property link      "http://eatbuilder.com/"

extern int A = 5;
extern int B = 13;
extern int D = 220;
extern int MagicNumber = 0;
extern bool SignalMail = False;
extern bool EachTickMode = True;
extern double Lots = 0.5;
extern int Slippage = 3;
extern bool UseStopLoss = False;
extern int StopLoss = 0;
extern bool UseTakeProfit = False;
extern int TakeProfit = 50;



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                                                   |
   //+------------------------------------------------------------------+

// RenkoScalp_Signal   indicator 
double envup = iCustom(NULL,0,"RenkoScalp_Signal",3,2,1,1,1,250,250,0,0);
double envdown = iCustom(NULL,0,"RenkoScalp_Signal",3,2,1,1,1,250,250,1,0);
double upsignal = iCustom(NULL,0,"RenkoScalp_Signal",3,2,1,1,1,250,250,2,0);
double downsignal = iCustom(NULL,0,"RenkoScalp_Signal",3,2,1,1,1,250,250,3,0);


// RenkoScalp_SMF   indicator 

double smfup = 0, smfdown = 0;
  for (int z=0; z<Bars; z++)  {
    double x_up = iCustom(NULL,0,"RenkoScalp_SMF",0.0,0.0,24.0,"0,0","0,0","3,8",0,0,177,5,z);
    double x_dn = iCustom(NULL,0,"RenkoScalp_SMF",0.0,0.0,24.0,"0,0","0,0","3,8",0,0,177,6,z);
    if (x_up == EMPTY || x_up == EMPTY_VALUE)    x_up = 0;
    if (x_dn == EMPTY || x_dn == EMPTY_VALUE)    x_dn = 0;
    if (x_up > 0 && smfup == 0)     smfup   = x_up;
    if (x_dn > 0 && smfdown == 0)   smfdown = x_dn;
    if (smfup > 0 || smfdown > 0)   break;
  }
double psmfup = iCustom(NULL,0,"RenkoScalp_SMF",0.0,0.0,24.0,"0,0","0,0","3,8",0,0,177,4,0);
double psmfdown = iCustom(NULL,0,"RenkoScalp_SMF",0.0,0.0,24.0,"0,0","0,0","3,8",0,0,177,5,0);

// RenkoScalp_DotNRP   indicator 

double dot = 0;
  for (int k=0; k<Bars; k++)  {
    double k_dot = iCustom(NULL,0,"RenkoScalp_DotNRP",1300,24,0,k);
    
    if (k_dot == EMPTY || k_dot == EMPTY_VALUE)    k_dot = 0;
   
    if (k_dot > 0 && dot == 0)     dot   = k_dot;

    if (dot > 0 )   break;
  }



  //+------------------------------------------------------------------+
   //| Signal Begin(Entry)                                              |
   //+------------------------------------------------------------------+

   if (upsignal > 0 && envup > 0 ) Order = OPEN_BUY;

   if (downsignal > 0 && envdown > 0) Order = OPEN_SELL;
   
   if ( envup == 0 || envup == EMPTY || envup == EMPTY_VALUE || psmfdown > 0  ) Order = CLOSE_BUY;

   if (envdown == 0 || envdown == EMPTY || envdown == EMPTY_VALUE || psmfdown > 0 ) Order = CLOSE_SELL;
   
   if ( psmfup > 0 || smfup > 0 ) Order = UPTREND;
   
   if ( psmfdown > 0 || smfdown > 0 || smfdown == dot ) Order = DTREND;
 
   
   //+------------------------------------------------------------------+
   //| Variable End                                                     |
   //+------------------------------------------------------------------+

   //Check position
   bool IsTrade = False;

   for (int i = 0; i < Total; i ++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == Symbol()) {
         IsTrade = True;
         if((OrderType() == OP_BUY) && (Order == CLOSE_BUY )) {
           
               OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);           
         continue;
         } 
         
         if((OrderType() == OP_SELL)&& (Order == CLOSE_SELL )) {  

               OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);
             
               continue;
     
         }
     }
   }





   //+------------------------------------------------------------------+
   //| Entry begin                                                       |
   //+------------------------------------------------------------------+

   //Buy
   if ((Order == UPTREND)&&(upsignal > 0) &&  ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
      if(!IsTrade) {
         //Check free margin
         if (AccountFreeMargin() < (100 * Lots)) {    // manual decreased by sajib
            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, "Saj Buy", 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 == DTREND)&&(downsignal > 0) && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
      if(!IsTrade) {
         //Check free margin
         if (AccountFreeMargin() < (100 * 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, "Saj Sell", 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);
}
//+------------------------------------------------------------------+
