/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2006"
#property link      "http://expertmillionaire.ru"

extern string SYMBOL = "EURUSD";
extern int PERIOD = 60;
extern int DefineLotMethod = 2;
extern double MM_Lots = 0.1;
extern int MM_Delta = 400;
extern int MM_Risk = 10;
extern int ProcOfClose = 85;
extern int Level = 80;
extern double Koeff = 0.7;
extern int CCIPeriod = 14;
extern int MAPeriod = 4;
extern int Slippage = 3;
extern int StopLoss = 0;
extern int TakeProfit = 0;
extern int Trailing = 0;
extern int TrailStep = 10;
extern bool NeedSignal = TRUE;
extern bool NeedTorg = TRUE;
double g_lots_160;
double g_point_168;
int g_cmd_176;
int g_stoplevel_180;
int g_timeframe_184;
int g_magic_188;
string g_symbol_192;
string gs_200;
double gd_208 = 2.5;
double gd_216 = 3.5;
double gd_224 = 3.5;
double gd_232 = 4.5;
double gd_240 = 3.5;
double gd_248 = 3.5;
double gd_256 = 4.5;
int gi_unused_264 = 120;

int start() {
   double ld_0;
   double l_icci_8;
   double l_price_16;
   double l_price_24;
   double ld_40;
   double l_price_48;
   double ld_56;
   double l_ima_64;
   double l_ima_72;
   bool li_92;
   if (!IsTradeAllowed()) return (0);
   int l_datetime_88 = 0;
   g_lots_160 = 0;
   g_cmd_176 = -1;
   for (int l_pos_80 = 0; l_pos_80 < OrdersHistoryTotal(); l_pos_80++) {
      OrderSelect(l_pos_80, SELECT_BY_POS, MODE_HISTORY);
      if (OrderMagicNumber() != g_magic_188) continue;
      if (l_datetime_88 < OrderCloseTime()) {
         l_datetime_88 = OrderCloseTime();
         g_lots_160 = OrderLots();
         g_cmd_176 = OrderType();
         if (OrderProfit() >= 0.0) li_92 = TRUE;
         else li_92 = FALSE;
      }
   }
   if (DefineLotMethod > 3 || DefineLotMethod < 0) DefineLotMethod = 0;
   switch (DefineLotMethod) {
   case 0:
      g_lots_160 = MM_Lots;
      break;
   case 1:
      g_lots_160 = NormalizeDouble(AccountFreeMargin() / 1000.0 * (MM_Risk / 100.0), 1);
      break;
   case 2:
      if (g_lots_160 < 0.1) g_lots_160 = 0.1;
      if (g_lots_160 > 200.0) g_lots_160 = 200;
      l_price_48 = AccountFreeMargin();
      for (l_pos_80 = 1; l_pos_80 < 20000; l_pos_80++)
         if (l_price_48 <= MM_Delta * l_pos_80) break;
      ld_40 = NormalizeDouble(l_pos_80 / 10.0, 1);
      l_pos_80 = MathCeil(ld_40);
      if (g_lots_160 <= ld_40 || g_lots_160 >= ld_40 + 0.2 * l_pos_80) g_lots_160 = ld_40;
      break;
   case 3:
      if (li_92) g_lots_160 = MM_Lots;
      else g_lots_160 = 2.0 * g_lots_160;
      if (g_lots_160 < 0.1) g_lots_160 = 0.1;
      if (g_lots_160 > 200.0) g_lots_160 = 200;
   }
   int l_cmd_84 = -1;
   bool li_100 = FALSE;
   bool li_96 = FALSE;
   if (g_timeframe_184 == PERIOD_M1) {
      ld_56 = (iBullsPower(g_symbol_192, g_timeframe_184, 1, PRICE_CLOSE, 0) + iBearsPower(g_symbol_192, g_timeframe_184, 1, PRICE_CLOSE, 0)) / g_point_168;
      if ((iClose(g_symbol_192, g_timeframe_184, 0) - iOpen(g_symbol_192, g_timeframe_184, 1)) / g_point_168 > gd_232) l_cmd_84 = 1;
      if ((iClose(g_symbol_192, g_timeframe_184, 0) - iOpen(g_symbol_192, g_timeframe_184, 1)) / g_point_168 < -gd_232) l_cmd_84 = 0;
      if ((iClose(g_symbol_192, g_timeframe_184, 0) - iOpen(g_symbol_192, g_timeframe_184, 0)) / g_point_168 > gd_224) l_cmd_84 = 1;
      if ((iClose(g_symbol_192, g_timeframe_184, 0) - iOpen(g_symbol_192, g_timeframe_184, 0)) / g_point_168 < -gd_224) l_cmd_84 = 0;
      if ((iClose(g_symbol_192, g_timeframe_184, 0) - iClose(g_symbol_192, g_timeframe_184, 1)) / g_point_168 > gd_216) l_cmd_84 = 1;
      if ((iClose(g_symbol_192, g_timeframe_184, 0) - iClose(g_symbol_192, g_timeframe_184, 1)) / g_point_168 < -gd_216) l_cmd_84 = 0;
      if (ld_56 > gd_208) l_cmd_84 = 0;
      if (ld_56 < -gd_208) l_cmd_84 = 1;
      if (ld_56 < (-gd_240) || (iClose(g_symbol_192, g_timeframe_184, 0) - iOpen(g_symbol_192, g_timeframe_184, 0)) / g_point_168 > gd_256 || (iClose(g_symbol_192, g_timeframe_184, 0) - iClose(g_symbol_192, g_timeframe_184, 1)) / g_point_168 > gd_248 ||
         AccountEquity() - AccountBalance() > 0.0) li_100 = TRUE;
      if (ld_56 > gd_240 || (iClose(g_symbol_192, g_timeframe_184, 0) - iOpen(g_symbol_192, g_timeframe_184, 0)) / g_point_168 < (-gd_256) || (iClose(g_symbol_192, g_timeframe_184, 0) - iClose(g_symbol_192, g_timeframe_184, 1)) / g_point_168 < (-gd_248) ||
         AccountEquity() - AccountBalance() > 0.0) li_96 = TRUE;
   } else {
      ld_0 = SHI_Laquerre(0);
      l_icci_8 = iCCI(g_symbol_192, g_timeframe_184, CCIPeriod, PRICE_CLOSE, 0);
      l_ima_64 = iMA(g_symbol_192, g_timeframe_184, MAPeriod, 0, MODE_EMA, PRICE_MEDIAN, 0);
      l_ima_72 = iMA(g_symbol_192, g_timeframe_184, MAPeriod, 0, MODE_EMA, PRICE_MEDIAN, 1);
      if (ld_0 < ProcOfClose / 100.0) li_96 = TRUE;
      if (ld_0 > (100.0 - ProcOfClose) / 100.0) li_100 = TRUE;
      if (ld_0 == 0.0 && l_ima_64 > l_ima_72 && l_icci_8 < (-Level)) l_cmd_84 = 0;
      if (ld_0 == 1.0 && l_ima_64 < l_ima_72 && l_icci_8 > Level) l_cmd_84 = 1;
   }
   if (NeedSignal) {
      gs_200 = "Советник Lucky v2.2 работает";
      if (li_96) {
         gs_200 = gs_200 
         + "\nЕсли есть ордера в продажу, ЗАКРЫТЬ.";
      }
      if (li_100) {
         gs_200 = gs_200 
         + "\nЕсли есть ордера в покупку, ЗАКРЫТЬ.";
      }
      if (l_cmd_84 == OP_BUY) {
         gs_200 = gs_200 
         + "\nПокупать.";
      }
      if (l_cmd_84 == OP_SELL) {
         gs_200 = gs_200 
         + "\nПродавать.";
      }
      Comment(gs_200);
   }
   if (!NeedTorg) return (0);
   if (g_cmd_176 == l_cmd_84) l_cmd_84 = -1;
   for (l_pos_80 = 0; l_pos_80 < OrdersTotal(); l_pos_80++) {
      OrderSelect(l_pos_80, SELECT_BY_POS);
      if (OrderMagicNumber() != g_magic_188) continue;
      if (OrderType() == OP_SELL) {
         l_price_48 = MarketInfo(g_symbol_192, MODE_ASK);
         if (li_96) OrderClose(OrderTicket(), OrderLots(), l_price_48, Slippage, CLR_NONE);
         else {
            if (Trailing > g_stoplevel_180)
               if (OrderStopLoss() == 0.0 || OrderStopLoss() - l_price_48 > (Trailing + TrailStep) * g_point_168) OrderModify(OrderTicket(), OrderOpenPrice(), l_price_48 + Trailing * g_point_168, OrderTakeProfit(), 0, CLR_NONE);
         }
      }
      if (OrderType() == OP_BUY) {
         l_price_48 = MarketInfo(g_symbol_192, MODE_BID);
         if (li_100) OrderClose(OrderTicket(), OrderLots(), l_price_48, Slippage, CLR_NONE);
         else {
            if (Trailing > g_stoplevel_180)
               if (l_price_48 - OrderStopLoss() > (Trailing + TrailStep) * g_point_168) OrderModify(OrderTicket(), OrderOpenPrice(), l_price_48 - Trailing * g_point_168, OrderTakeProfit(), 0, CLR_NONE);
         }
      }
      return (0);
   }
   if (g_lots_160 < 0.1) {
      Alert("We have no money. Free Margin = ", AccountFreeMargin());
      return (0);
   }
   if (l_cmd_84 == OP_BUY) {
      RefreshRates();
      l_price_48 = MarketInfo(g_symbol_192, MODE_ASK);
      if (StopLoss < g_stoplevel_180) l_price_16 = 0;
      else l_price_16 = l_price_48 - StopLoss * g_point_168;
      if (TakeProfit < g_stoplevel_180) l_price_24 = 0;
      else l_price_24 = l_price_48 + TakeProfit * g_point_168;
      OrderSend(g_symbol_192, OP_BUY, g_lots_160, l_price_48, Slippage, l_price_16, l_price_24, 0, g_magic_188, 0, Red);
   }
   if (l_cmd_84 == OP_SELL) {
      RefreshRates();
      l_price_48 = MarketInfo(g_symbol_192, MODE_BID);
      if (StopLoss < g_stoplevel_180) l_price_16 = 0;
      else l_price_16 = l_price_48 + StopLoss * g_point_168;
      if (TakeProfit < g_stoplevel_180) l_price_24 = 0;
      else l_price_24 = l_price_48 - TakeProfit * g_point_168;
      OrderSend(g_symbol_192, OP_SELL, g_lots_160, l_price_48, Slippage, l_price_16, l_price_24, 0, g_magic_188, 0, Blue);
   }
   return (0);
}

int init() {
   if (MM_Risk > 100) MM_Risk = 100;
   if (MM_Risk < 1) MM_Risk = 1;
   if (SYMBOL == "*") g_symbol_192 = Symbol();
   else g_symbol_192 = SYMBOL;
   if (PERIOD == 0) g_timeframe_184 = Period();
   else g_timeframe_184 = PERIOD;
   g_magic_188 = StringGetChar(g_symbol_192, 1) + StringGetChar(g_symbol_192, 4) + g_timeframe_184;
   g_point_168 = MarketInfo(g_symbol_192, MODE_POINT);
   g_stoplevel_180 = MarketInfo(g_symbol_192, MODE_STOPLEVEL);
   return (0);
}

double SHI_Laquerre(int ai_0) {
   double ld_ret_4;
   double lda_12[100];
   double lda_16[100];
   double lda_20[100];
   double lda_24[100];
   double ld_28;
   double ld_36;
   for (int li_44 = ai_0 + 99; li_44 >= ai_0; li_44--) {
      lda_12[li_44] = 0.3 * iClose(g_symbol_192, g_timeframe_184, li_44) + Koeff * (lda_12[li_44 + 1]);
      lda_16[li_44] = (-Koeff) * lda_12[li_44] + (lda_12[li_44 + 1]) + Koeff * (lda_16[li_44 + 1]);
      lda_20[li_44] = (-Koeff) * lda_16[li_44] + (lda_16[li_44 + 1]) + Koeff * (lda_20[li_44 + 1]);
      lda_24[li_44] = (-Koeff) * lda_20[li_44] + (lda_20[li_44 + 1]) + Koeff * (lda_24[li_44 + 1]);
      ld_28 = 0;
      ld_36 = 0;
      if (lda_12[li_44] >= lda_16[li_44]) ld_28 = lda_12[li_44] - lda_16[li_44];
      else ld_36 = lda_16[li_44] - lda_12[li_44];
      if (lda_16[li_44] >= lda_20[li_44]) ld_28 += lda_16[li_44] - lda_20[li_44];
      else ld_36 += lda_20[li_44] - lda_16[li_44];
      if (lda_20[li_44] >= lda_24[li_44]) ld_28 += lda_20[li_44] - lda_24[li_44];
      else ld_36 += lda_24[li_44] - lda_20[li_44];
      if (ld_28 + ld_36 != 0.0) ld_ret_4 = ld_28 / (ld_28 + ld_36);
   }
   return (ld_ret_4);
}
