/*
   Scalping EA 
   
   
*/
extern bool Identify = FALSE;
extern int MODE = 1;
extern bool MM = TRUE;
extern bool micro = FALSE;
extern bool CountBalance = FALSE;
extern bool FastClose = TRUE;
extern bool Booster = FALSE;
extern double Risk = 20.0;
double gd_unused_112 = 1.1;
extern double ManLot = 0.1;
extern double inLot = 0.1;
extern double LevelLot = 12.0;
int gi_144 = 3;
extern int SL1 = 25;
extern int SL2 = 25;
int gi_156;
int gi_160 = 3;
extern int iMinute = 60;
extern int Slippage = 3;
extern int Magic = 2008;
bool gi_176 = TRUE;
extern double iMaxLot = 10.0;
extern int MaxTrades = 15;
extern int MaxTradePerBar = 5;
extern int StartTrade = 17;
extern int EndTrade = 9;
double gd_unused_204 = 0.1;
int gi_212 = 1;
string g_comment_216;
int g_timeframe_224;
int g_pos_228;
int g_count_232;
int g_count_236;
int g_count_240;
double gd_244;
double g_lots_252;
double gd_260;
int gi_268;
int gi_272;
double gd_276;
double g_ask_284;
double g_bid_292;
double g_ord_open_price_300;
double g_ord_open_price_308;
int gi_unused_316;
double g_isar_320;
double g_ask_328;
double g_bid_336;

int start() {
   g_timeframe_224 = Period();
   /*if (Identify) g_comment_216 = g_timeframe_224 + "DpkScalper";
   else g_comment_216 = "";
   string ls_0 = "ACCOUNT IS NOT ALLOWED";
   if (Year() >= 2009.11) {
      Comment("Expire.. to Renwa Please Contact at centraviz@yahoo.co.uk");
      return (0);
   }
   if (!(AccountNumber() == 24404 || AccountNumber() == 120076 || AccountNumber() == 122378 || AccountNumber() == 123938 || AccountNumber() == D'14.09.2001 07:00:40' ||
      AccountNumber() == 124223 || AccountNumber() == 124208 || AccountNumber() == 124203 || AccountNumber() == 124201 || AccountNumber() == 26865 || AccountNumber() == 124120 ||
      AccountNumber() == 124105 || AccountNumber() == 124063 || AccountNumber() == 124004 || AccountNumber() == 124002 || AccountNumber() == 123964 || AccountNumber() == 26724 ||
      AccountNumber() == 123942) && !(AccountNumber() == 123938))*/ {
      //Comment(ls_0);
      return (0);
   }
   if (IsTesting()) return (0);
   if (CountBalance) gd_260 = AccountBalance();
   else gd_260 = AccountEquity();
   if (micro) {
      if (MM) gd_244 = NormalizeDouble(MathCeil(gd_260 * Risk / 10000.0) / 100.0, 1);
      else gd_244 = ManLot;
   } else {
      if (MM) gd_244 = NormalizeDouble(MathCeil(gd_260 * Risk / 10000.0) / 10.0, 1);
      else gd_244 = ManLot;
   }
   gd_276 = gd_244 + inLot * LevelLot;
   HideTestIndicators(TRUE);
   if (g_ask_328 == 10000000.0) GetLots();
   if (gi_176) {
      g_ask_328 = Ask;
      g_bid_336 = Bid;
      gi_176 = FALSE;
      return (0);
   }
   g_count_240 = 0;
   g_count_232 = 0;
   g_count_236 = 0;
   g_ord_open_price_308 = 0;
   g_ord_open_price_300 = 0;
   for (g_pos_228 = 0; g_pos_228 < OrdersTotal(); g_pos_228++) {
      OrderSelect(g_pos_228, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) {
         g_count_240++;
         if (OrderType() == OP_BUY) {
            g_count_232++;
            g_ord_open_price_300 = OrderOpenPrice();
         }
         if (OrderType() == OP_SELL) {
            g_count_236++;
            g_ord_open_price_308 = OrderOpenPrice();
         }
      }
   }
   g_lots_252 = gd_244;
   for (g_pos_228 = 1; g_pos_228 <= g_count_240; g_pos_228++) g_lots_252 += inLot;
   if (g_lots_252 > gd_276) g_lots_252 = gd_276;
   if (g_lots_252 > iMaxLot) g_lots_252 = iMaxLot;
   double l_irsi_8 = iRSI(Symbol(), PERIOD_M15, 14, PRICE_CLOSE, 0);
   double l_irsi_16 = iRSI(Symbol(), PERIOD_M15, 14, PRICE_CLOSE, 1);
   double l_irsi_24 = iRSI(Symbol(), 0, 14, PRICE_CLOSE, 0);
   double l_ima_32 = iMA(Symbol(), 0, 5, 0, MODE_EMA, PRICE_OPEN, 0);
   double l_ima_40 = iMA(Symbol(), 0, 5, 0, MODE_EMA, PRICE_OPEN, 1);
   g_isar_320 = iSAR(Symbol(), 0, 0.02, 0.2, 0);
   if (l_irsi_24 >= 40.0 && l_irsi_24 <= 60.0) gi_156 = SL1;
   else gi_156 = SL2;
   if (Hour() > EndTrade && Hour() < StartTrade) {
      if (Ask - g_ord_open_price_308 > 12000.0 * Point) CloseSell();
      if (g_ord_open_price_300 - Ask > 12000.0 * Point) CloseBuy();
   }
   if (Volume[0] < 2.0) {
      gi_268 = MaxTradePerBar;
      gi_272 = MaxTradePerBar;
   }
   if (Booster) {
      g_ask_284 = 1;
      g_bid_292 = 1;
   } else {
      g_ask_284 = Ask;
      g_bid_292 = Bid;
   }
   if (Hour() < EndTrade || Hour() > StartTrade) {
      if (g_count_240 < MaxTrades) {
         if (MODE == 1 && Minute() <= iMinute) {
            if (Low[0] >= Low[1] || l_irsi_8 == l_irsi_16 && Ask - g_ask_328 >= gi_144 * Point && Close[0] > Open[0] && gi_268 > 0) {
               if (g_count_236 < 1 || g_bid_292 != g_ord_open_price_308) {
                  OrderSend(Symbol(), OP_SELL, g_lots_252, Bid, Slippage, 0, 0, g_comment_216, Magic, 0, Red);
                  gi_268--;
               }
            }
            if (High[0] <= High[1] || l_irsi_8 == l_irsi_16 && g_bid_336 - Bid >= gi_144 * Point && Close[0] < Open[0] && gi_268 > 0) {
               if (g_count_232 < 1 || g_ask_284 != g_ord_open_price_300) {
                  OrderSend(Symbol(), OP_BUY, g_lots_252, Ask, Slippage, 0, 0, g_comment_216, Magic, 0, Blue);
                  gi_272--;
               }
            }
         }
         if (MODE == 2 && Minute() <= iMinute) {
            if (Low[0] >= Low[1] || l_irsi_8 == l_irsi_16 && Ask - g_ask_328 >= gi_144 * Point && Close[0] > Open[0] && gi_268 > 0 && Open[0] > l_ima_32) {
               OrderSend(Symbol(), OP_SELL, g_lots_252, Bid, Slippage, 0, 0, g_comment_216, Magic, 0, Red);
               gi_268--;
            }
            if (High[0] <= High[1] || l_irsi_8 == l_irsi_16 && g_bid_336 - Bid >= gi_144 * Point && Close[0] < Open[0] && gi_268 > 0 && Open[0] < l_ima_32) {
               OrderSend(Symbol(), OP_BUY, g_lots_252, Ask, Slippage, 0, 0, g_comment_216, Magic, 0, Blue);
               gi_272--;
            }
         }
         if (MODE == 3 && Minute() <= iMinute) {
            if (Low[0] >= Low[1] || l_irsi_8 == l_irsi_16 && High[0] - Low[0] < 11.0 * Point && High[0] - Low[0] > 2.0 * Point && Ask - g_ask_328 >= gi_144 * Point && Close[0] > Open[0] &&
               gi_268 > 0 && Open[0] > l_ima_32 && l_ima_32 > l_ima_40) {
               OrderSend(Symbol(), OP_SELL, g_lots_252, Bid, Slippage, 0, 0, g_timeframe_224 + g_comment_216, Magic, 0, Red);
               gi_268--;
            }
            if (High[0] <= High[1] || l_irsi_8 == l_irsi_16 && High[0] - Low[0] < 11.0 * Point && High[0] - Low[0] > 2.0 * Point && g_bid_336 - Bid >= gi_144 * Point && Close[0] < Open[0] &&
               gi_268 > 0 && Open[0] < l_ima_32 && l_ima_32 < l_ima_40) {
               OrderSend(Symbol(), OP_BUY, g_lots_252, Ask, Slippage, 0, 0, g_timeframe_224 + g_comment_216, Magic, 0, Blue);
               gi_272--;
            }
         }
      }
   }
   if (g_count_236 > 2 && Ask - g_ord_open_price_308 > 13.0 * Point && g_bid_336 - Bid >= gi_144 * Point && g_count_232 < g_count_236 && Close[0] < Open[0] && Ask != g_ord_open_price_300) OrderSend(Symbol(), OP_BUY, g_lots_252, Ask, Slippage, 0, 0, g_comment_216, Magic, 0, Blue);
   if (g_count_232 > 2 && g_ord_open_price_300 - Ask > 13.0 * Point && Ask - g_ask_328 >= gi_144 * Point && g_count_236 < g_count_232 && Close[0] > Open[0] && Bid != g_ord_open_price_308) OrderSend(Symbol(), OP_SELL, g_lots_252, Bid, Slippage, 0, 0, g_comment_216, Magic, 0, Red);
   g_ask_328 = Ask;
   g_bid_336 = Bid;
   int li_48 = -10000;
   if (AccountProfit() >= 0.3 * AccountBalance() * li_48) CloseLoss();
   if (FastClose && g_count_240 < 5) {
      if (g_count_236 > 0 && g_count_236 <= 2) CloseSell();
      if (g_count_232 > 0 && g_count_232 <= 2) CloseBuy();
   }
   CloseAll();
   Comment("\n   EA Name ", g_comment_216, 
      "\n   Profit = +/-$ ", AccountProfit(), 
      "\n   Equity = $ ", AccountEquity(), 
      "\n   Balance= $ ", AccountBalance(), 
      "\n   Free Margin= $ ", AccountFreeMargin(), 
      "\n   Leverage=  ", AccountLeverage(), 
      "\n   OrderSell= ", g_count_236, 
      "\n   OrderBuy= ", g_count_232, 
      "\n   LASTBuy= ", g_ord_open_price_300, 
      "\n   Lots= ", gd_244, 
      "\n   MaxiLots= ", gd_276, 
      "\n   Server= ", AccountServer(), 
   "\n   GOOD LUCK n HAPPY TRADING");
   return (0);
}

void CloseAll() {
   for (int l_pos_0 = OrdersTotal() - 1; l_pos_0 >= 0; l_pos_0--) {
      OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) {
         if (SecurProfit() == 1) {
            if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Blue);
            if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Red);
         } else {
            if (OrderType() == OP_BUY && (OrderOpenPrice() - Ask) / Point > gi_156) OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Blue);
            if (OrderType() == OP_SELL && (Bid - OrderOpenPrice()) / Point > gi_156) OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Red);
         }
      }
   }
}

void CloseSell() {
   if (g_isar_320 > Open[0]) gi_unused_316 = 0;
   else gi_unused_316 = 1;
   for (int l_pos_0 = OrdersTotal() - 1; l_pos_0 >= 0; l_pos_0--) {
      OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
         if (OrderType() == OP_SELL && OrderOpenPrice() - Ask >= gi_160 * Point) OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Red);
   }
}

void CloseBuy() {
   if (g_isar_320 > Open[0]) gi_unused_316 = 1;
   else gi_unused_316 = 0;
   for (int l_pos_0 = OrdersTotal() - 1; l_pos_0 >= 0; l_pos_0--) {
      OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
         if (OrderType() == OP_BUY && Bid - OrderOpenPrice() >= gi_160 * Point) OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Blue);
   }
}

void CloseLoss() {
   for (int l_pos_0 = OrdersTotal() - 1; l_pos_0 >= 0; l_pos_0--) {
      OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) {
         if (OrderType() == OP_BUY && g_isar_320 > Bid && OrderOpenPrice() - Bid > 15.0 * Point) {
         }
         if (OrderType() == OP_SELL && g_isar_320 < Ask && Ask - OrderOpenPrice() > 15.0 * Point) OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Yellow);
      }
   }
}

double GetLots() {
   double ld_ret_0;
   double ld_8;
   double ld_16;
   double ld_24;
   double ld_unused_32;
   double ld_40;
   int li_48;
   if (micro == FALSE) {
      ld_8 = NormalizeDouble(MarketInfo(Symbol(), MODE_LOTSTEP), 2);
      ld_16 = NormalizeDouble(MarketInfo(Symbol(), MODE_MARGINREQUIRED), 4);
      ld_24 = 100.0 * (ld_16 + 5.0);
      if (ld_8 == 0.01) li_48 = 2;
      else li_48 = 1;
      gi_212 = li_48;
      if (MM == TRUE) ld_ret_0 = NormalizeDouble(AccountFreeMargin() / (ld_24 / Risk) - 0.05, gi_212);
      else ld_ret_0 = ManLot;
      ld_40 = NormalizeDouble(MarketInfo(Symbol(), MODE_MAXLOT), 2);
      if (gi_212 == 2) ld_unused_32 = 0.01;
   } else {
      if (MM == TRUE) ld_ret_0 = NormalizeDouble(AccountFreeMargin() * Risk / 10000.0, 2);
      else ld_ret_0 = ManLot;
   }
   if (ld_ret_0 > iMaxLot) ld_ret_0 = iMaxLot;
   return (ld_ret_0);
}

double TickValue() {
   double ld_0 = NormalizeDouble(MarketInfo(Symbol(), MODE_TICKVALUE), 4);
   return (ld_0);
}

int SecurProfit() {
   bool li_ret_0 = FALSE;
   if (OrderProfit() > TickValue() * (GetLots() / 1.1) * gi_160) li_ret_0 = TRUE;
   return (li_ret_0);
}