#property copyright "Copyright © 2009"
#property link      ""

extern int FromHourTrade = 0;
extern int ToHourTrade = 10;
extern bool UseHourTrade = TRUE;
extern int SL = 150;
extern int TakeProfit = 200;
extern double Lots = 5.0;
extern int Risk = 40;
extern bool MM = TRUE;
extern int MaxTrades = 2;
extern bool ShowStatus = TRUE;
extern color StatusColor = White;
extern color StatusBackColor = Blue;
int gi_128 = 15;
bool gi_132 = TRUE;
int gi_136 = 0;
string gs_140;
int gia_148[10];
int gi_152 = 0;
double gd_156 = 0.0;
int gi_164 = 0;
int gi_168 = 0;
int g_count_172 = 0;
int g_datetime_176;
int g_datetime_180;
double g_ima_200;
double g_ima_208;
double g_irsi_224;
double g_irsi_232;
int g_period_244 = 15;
int g_period_248 = 50;
int gi_252 = 18;
int gi_256 = 5;
int gi_260;
double g_irsi_268 = 0.0;
int g_period_276 = 50;
bool gi_280;
bool gi_284;
int g_ord_total_300;
int g_pos_304;
int g_bool_316;
double g_point_336;

int init() {
   if (ShowStatus) CreateStatusLabels();
   else DestroyStatusLabels();
   return (0);
}

int deinit() {
   DestroyStatusLabels();
   return (0);
}

int start() {
   double l_irsi_4;
   double l_irsi_12;
   double l_irsi_20;
   double l_irsi_28;
   bool l_bool_36;
   bool l_bool_40;
   if (Point == 0.00001) g_point_336 = 0.0001;
   else {
      if (Point == 0.001) g_point_336 = 0.01;
      else g_point_336 = Point;
   }
   for (int l_index_0 = 0; l_index_0 < MaxTrades; l_index_0++) {
      if (gia_148[l_index_0] > 0) {
         OrderSelect(gia_148[l_index_0], SELECT_BY_TICKET, MODE_TRADES);
         g_datetime_176 = OrderCloseTime();
         if (g_datetime_176 != 0) {
            g_datetime_180 = OrderOpenTime();
            Print(" *** Order #", gia_148[l_index_0], " stopped out at ", OrderClosePrice(), ", P/L:", OrderProfit());
            gd_156 += OrderProfit();
            if (OrderProfit() < 0.0) {
               g_count_172++;
               gi_168++;
            } else {
               g_count_172 = 0;
               gi_164++;
            }
            Print(" *** Total P/L:", gd_156);
            gia_148[l_index_0] = 0;
         }
      }
   }
   if (gi_136 == -1 || gi_136 == 0) {
      if (UseHourTrade) {
         if ((FromHourTrade <= ToHourTrade && Hour() < FromHourTrade || Hour() > ToHourTrade) || (FromHourTrade > ToHourTrade && (Hour() < FromHourTrade && Hour() > ToHourTrade))) {
            gi_136 = -1;
            gs_140 = "Wrong Hour";
         } else gi_136 = 0;
      } else gi_136 = 0;
   }
   if (ShowStatus) {
      if (gi_132 == FALSE) CreateStatusLabels();
      if (gi_132 == TRUE) UpdateAccountInfo();
   } else
      if (gi_132 == TRUE) DestroyStatusLabels();
   gi_260 = getPeriod();
   if (gi_136 >= 0) {
      if (checkParams() != 0) {
         CheckEquity();
         HideTestIndicators(TRUE);
         g_ima_200 = iMA(NULL, 0, g_period_244, 0, MODE_EMA, PRICE_WEIGHTED, 0);
         g_ima_208 = iMA(NULL, 0, g_period_248, 0, MODE_EMA, PRICE_WEIGHTED, 0);
         HideTestIndicators(FALSE);
         HideTestIndicators(TRUE);
         g_irsi_224 = iRSI(NULL, PERIOD_M5, 14, PRICE_WEIGHTED, 0);
         g_irsi_232 = iRSI(NULL, PERIOD_M5, 14, PRICE_WEIGHTED, 7);
         l_irsi_4 = iRSI(NULL, PERIOD_M15, 14, PRICE_WEIGHTED, 0);
         l_irsi_12 = iRSI(NULL, PERIOD_M15, 14, PRICE_WEIGHTED, 7);
         l_irsi_20 = iRSI(NULL, PERIOD_M30, 14, PRICE_WEIGHTED, 0);
         l_irsi_28 = iRSI(NULL, PERIOD_M30, 14, PRICE_WEIGHTED, 7);
         g_irsi_268 = iRSI(NULL, PERIOD_H4, g_period_276, PRICE_WEIGHTED, 0);
         HideTestIndicators(FALSE);
         if (MM == TRUE) Lots = LotSize();
         l_bool_36 = g_irsi_224 > 55.0 && l_irsi_4 > 55.0 && l_irsi_20 > 55.0 && (l_irsi_28 < 50.0 && l_irsi_12 < 50.0 && g_irsi_232 < 50.0) && g_ima_208 > g_ima_200;
         l_bool_40 = g_irsi_224 < 55.0 && l_irsi_4 < 55.0 && l_irsi_20 < 55.0 && (l_irsi_28 > 50.0 && l_irsi_12 > 50.0 && g_irsi_232 > 50.0) && g_ima_208 < g_ima_200;
         g_bool_316 = l_bool_36;
         if (g_bool_316 && !gi_280) {
            if (OrdersTotal() < MaxTrades) {
               for (l_index_0 = 0; l_index_0 < MaxTrades; l_index_0++)
                  if (gia_148[l_index_0] <= 0) gi_152 = l_index_0;
               gia_148[gi_152] = OrderSend(Symbol(), OP_BUY, Lots, Ask, 2, Ask - SL * g_point_336, Ask + TakeProfit * g_point_336, "Cross Buy", 16384, Minute() - 100, Lime);
               gi_280 = TRUE;
               gi_284 = FALSE;
            }
         }
         if (!gi_284) {
            if (OrdersTotal() < MaxTrades) {
               for (l_index_0 = 0; l_index_0 < MaxTrades; l_index_0++)
                  if (gia_148[l_index_0] <= 0) gi_152 = l_index_0;
               gia_148[gi_152] = OrderSend(Symbol(), OP_SELL, Lots, Bid, 2, Bid + SL * g_point_336, Bid - TakeProfit * g_point_336, "Cross Sell", 16384, Minute() - 100, Orange);
               gi_284 = TRUE;
               gi_280 = FALSE;
            }
         }
      }
   }
   g_ord_total_300 = OrdersTotal();
   for (g_pos_304 = 0; g_pos_304 < g_ord_total_300; g_pos_304++) {
      OrderSelect(g_pos_304, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() == 16384) {
         if (OrderType() <= OP_SELL && OrderSymbol() == Symbol()) {
            if (OrderType() == OP_BUY) {
               if (gi_252 <= 0) continue;
               if (Bid - OrderOpenPrice() <= g_point_336 * gi_252) continue;
               if (OrderStopLoss() >= Bid - g_point_336 * gi_252) continue;
               OrderModify(OrderTicket(), OrderOpenPrice(), Bid - g_point_336 * gi_252, OrderTakeProfit(), 0, Green);
               if (OrderTakeProfit() - Ask < gi_256 * g_point_336) OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderTakeProfit() + gi_256 * g_point_336, 0, Pink);
               if (OrderOpenPrice() < Ask && OrderStopLoss() < Ask) OrderModify(OrderTicket(), OrderOpenPrice(), Ask, OrderTakeProfit(), 0, Pink);
               return (0);
            }
            if (gi_252 > 0) {
               if (OrderOpenPrice() - Ask > g_point_336 * gi_252) {
                  if (OrderStopLoss() > Ask + g_point_336 * gi_252 || OrderStopLoss() == 0.0) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Ask + g_point_336 * gi_252, OrderTakeProfit() - gi_256 * g_point_336, 0, Red);
                     if (Bid - OrderTakeProfit() < gi_256 * g_point_336) OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderTakeProfit() - gi_256 * g_point_336, 0, Pink);
                     if (OrderOpenPrice() > Ask && OrderStopLoss() > Ask) OrderModify(OrderTicket(), OrderOpenPrice(), Ask, OrderTakeProfit(), 0, Pink);
                     return (0);
                  }
               }
            }
         }
      }
   }
   return (0);
}

double LotSize() {
   double ld_ret_0 = MathCeil(AccountFreeMargin() * Risk / 10000.0) / 10.0;
   if (ld_ret_0 < 0.1) ld_ret_0 = Lots;
   if (ld_ret_0 > 1.0) ld_ret_0 = MathCeil(ld_ret_0);
   if (ld_ret_0 > 100.0) ld_ret_0 = 100;
   return (ld_ret_0);
}

int checkParams() {
   if (DayOfWeek() >= 5 && Hour() >= 21) return (0);
   if (Bars < 100) {
      Print("bars less than 100");
      return (0);
   }
   if (SL < 4) Print("StopLoss less than 10");
   if (TakeProfit < 4) {
      Print("TakeProfit less than 10");
      return (0);
   }
   if (gi_252 < 10) {
      Print("StopLoss less than 10");
      return (0);
   }
   return (1);
}

int CheckEquity() {
   double ld_0 = 100.0 * ((AccountEquity() - AccountBalance()) / AccountBalance()) + 100.0;
   return (0);
}

int getPeriod() {
   switch (Period()) {
   case PERIOD_M5:
      return (8);
   case PERIOD_M15:
      return (10);
   case PERIOD_M30:
      return (14);
   case PERIOD_H1:
      return (20);
   }
   Print("NOT An Acceptable Timeframe");
   return (0);
}

void CreateStatusLabels() {
   ObjectCreate("lbl_bg", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("lbl_bg", "gggg", gi_128 + 38, "Webdings", StatusBackColor);
   ObjectSet("lbl_bg", OBJPROP_CORNER, 2);
   ObjectSet("lbl_bg", OBJPROP_XDISTANCE, 3);
   ObjectSet("lbl_bg", OBJPROP_YDISTANCE, 2);
   ObjectCreate("lbl_status", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("lbl_status", "Status:      Initializing", 10, "Courier New", StatusColor);
   ObjectSet("lbl_status", OBJPROP_CORNER, 2);
   ObjectSet("lbl_status", OBJPROP_XDISTANCE, 8);
   ObjectSet("lbl_status", OBJPROP_YDISTANCE, 55);
   ObjectCreate("lbl_t1", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("lbl_t1", "Bid/Ask:     0.00000/0.00000", 10, "Courier New", StatusColor);
   ObjectSet("lbl_t1", OBJPROP_CORNER, 2);
   ObjectSet("lbl_t1", OBJPROP_XDISTANCE, 8);
   ObjectSet("lbl_t1", OBJPROP_YDISTANCE, 41);
   ObjectCreate("lbl_t2", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("lbl_t2", "win/loss:    0/0", 10, "Courier New", StatusColor);
   ObjectSet("lbl_t2", OBJPROP_CORNER, 2);
   ObjectSet("lbl_t2", OBJPROP_XDISTANCE, 8);
   ObjectSet("lbl_t2", OBJPROP_YDISTANCE, 28);
   ObjectCreate("lbl_t3", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("lbl_t3", "Profit/Loss: 0.00", 10, "Courier New", StatusColor);
   ObjectSet("lbl_t3", OBJPROP_CORNER, 2);
   ObjectSet("lbl_t3", OBJPROP_XDISTANCE, 8);
   ObjectSet("lbl_t3", OBJPROP_YDISTANCE, 15);
   ObjectCreate("lbl_t4", OBJ_LABEL, 0, 0, 0);
   string l_text_0 = "Margin/Bal:  " + DoubleToStr(AccountFreeMargin(), 2);
   ObjectSetText("lbl_t4", l_text_0, 10, "Courier New", StatusColor);
   ObjectSet("lbl_t4", OBJPROP_CORNER, 2);
   ObjectSet("lbl_t4", OBJPROP_XDISTANCE, 8);
   ObjectSet("lbl_t4", OBJPROP_YDISTANCE, 2);
   gi_132 = TRUE;
}

void DestroyStatusLabels() {
   ObjectDelete("lbl_status");
   ObjectDelete("lbl_t1");
   ObjectDelete("lbl_t2");
   ObjectDelete("lbl_t3");
   ObjectDelete("lbl_t4");
   ObjectDelete("lbl_bg");
   gi_132 = FALSE;
}

void UpdateAccountInfo() {
   string l_text_8;
   ObjectSet("lbl_bg", OBJPROP_XDISTANCE, 3);
   ObjectSet("lbl_bg", OBJPROP_YDISTANCE, 2);
   ObjectSet("lbl_status", OBJPROP_XDISTANCE, 8);
   ObjectSet("lbl_status", OBJPROP_YDISTANCE, 55);
   if (gi_136 < 0) l_text_8 = "Status:      Stopped - " + gs_140;
   else l_text_8 = "Status:      Running";
   ObjectSetText("lbl_status", l_text_8, 10, "Courier New", StatusColor);
   ObjectSet("lbl_status", OBJPROP_CORNER, 2);
   ObjectSet("lbl_t1", OBJPROP_XDISTANCE, 8);
   ObjectSet("lbl_t1", OBJPROP_YDISTANCE, 41);
   string l_text_0 = "Bid/Ask:     " + DoubleToStr(Bid, 5) + "/" + DoubleToStr(Ask, 5);
   ObjectSetText("lbl_t1", l_text_0, 10, "Courier New", StatusColor);
   ObjectSet("lbl_t1", OBJPROP_CORNER, 2);
   l_text_0 = "win/loss:    " + gi_164 + "/" + gi_168;
   ObjectSet("lbl_t2", OBJPROP_XDISTANCE, 8);
   ObjectSet("lbl_t2", OBJPROP_YDISTANCE, 28);
   ObjectSetText("lbl_t2", l_text_0, 10, "Courier New", StatusColor);
   ObjectSet("lbl_t2", OBJPROP_CORNER, 2);
   l_text_0 = "Profit/Loss: " + DoubleToStr(gd_156, 2);
   ObjectSetText("lbl_t3", l_text_0, 10, "Courier New", StatusColor);
   ObjectSet("lbl_t3", OBJPROP_XDISTANCE, 8);
   ObjectSet("lbl_t3", OBJPROP_YDISTANCE, 15);
   ObjectSet("lbl_t3", OBJPROP_CORNER, 2);
   l_text_0 = "Margin/Bal:  " + DoubleToStr(AccountFreeMargin(), 2) + "/" + DoubleToStr(AccountBalance(), 2);
   ObjectSet("lbl_t4", OBJPROP_XDISTANCE, 8);
   ObjectSet("lbl_t4", OBJPROP_YDISTANCE, 2);
   ObjectSet("lbl_t4", OBJPROP_CORNER, 2);
   ObjectSetText("lbl_t4", l_text_0, 10, "Courier New", StatusColor);
}