#property copyright "�й��Զ�����������-��֮����"
#property link      "QQ:712933993"

extern string Name = "++++++++ Calypso ver 1.04 ++++++++";
extern string Cop = "+++ Copyright ?2010, winzfx.com +++";
extern string no = "";
extern string n1 = "����ѡ��";
double gd_108 = 0.01;
extern string n8 = "�������";
extern double Dinamic_lot = 10.0;
extern string n9 = "������λ";
extern bool lv_mode = FALSE;
extern int Level_stop = 0;
extern string n3 = "����ֹͣ";
extern bool sm_mode = FALSE;
extern int stop = 25;
extern string n4 = "��󶩵�";
extern int sn_mode = 50;
extern string n5 = "֮�䶩��";
extern int sh_mode = 50;
extern string n6 = "���ڵij˷�";
extern bool st_mode = TRUE;
extern double sk_mode = 2.0;
extern string n7 = "��������";
extern bool sr_mode = TRUE;
int gi_unused_220 = 0;
int g_magic_224 = 59999;
int gi_228 = 24;
int g_pos_232;
datetime g_time_236;
string g_symbol_240;
double g_ima_248;
double g_ima_256;
double gd_264;

void ads() {
   SetLab("ads01", 20, 25, 3, "�й��Զ�����������-��֮����", 11, "����", Lime);
   SetLab("ads02", 1, 10, 3, "QQ:712933993               ", 11, "Arial", Aqua);
}

void SetLab(string a_name_0, int a_x_8, int a_y_12, int a_corner_16, string a_text_20, int a_fontsize_28, string a_fontname_32, color a_color_40 = -1) {
   ObjectCreate(a_name_0, OBJ_LABEL, 0, 0, 0);
   ObjectSet(a_name_0, OBJPROP_XDISTANCE, a_x_8);
   ObjectSet(a_name_0, OBJPROP_YDISTANCE, a_y_12);
   ObjectSet(a_name_0, OBJPROP_CORNER, a_corner_16);
   ObjectSetText(a_name_0, a_text_20, a_fontsize_28, a_fontname_32, a_color_40);
}

double setpoint() {
   double ld_ret_0;
   if (Digits <= 3) ld_ret_0 = 0.01;
   else ld_ret_0 = 0.0001;
   return (ld_ret_0);
}

int start() {
   double ld_0;
   double ld_8;
   double l_ord_open_price_16;
   double l_ord_open_price_24;
   double l_minlot_32;
   double l_marginrequired_40;
   double ld_48;
   int li_56;
   double ld_60;
   int l_count_68;
   bool li_72;
   bool li_76;
   int l_count_80;
   int l_count_84;
   int l_count_88;
   double ld_92;
   double l_minlot_100;
   double ld_108;
   double ld_116;
   double ld_124;
   double ld_132;
   int l_error_140;
   int li_144;
   string ls_148;
   string ls_156;
   string ls_164;
   string ls_172;
   ads();
   HideTestIndicators(TRUE);
   g_ima_256 = iMA(Symbol(), 0, 3, 0, MODE_LWMA, PRICE_MEDIAN, 0);
   g_ima_248 = iMA(Symbol(), 0, 3, 0, MODE_LWMA, PRICE_MEDIAN, 1);
   HideTestIndicators(FALSE);
   if (IsTradeAllowed()) {
      if (Time[0] == g_time_236) return (0);
      g_time_236 = Time[0];
      g_symbol_240 = Symbol();
      ld_0 = NormalizeDouble(Ask, Digits);
      ld_8 = NormalizeDouble(Bid, Digits);
      l_ord_open_price_16 = 0;
      l_ord_open_price_24 = 0;
      l_minlot_32 = MarketInfo(g_symbol_240, MODE_MINLOT);
      l_marginrequired_40 = MarketInfo(Symbol(), MODE_MARGINREQUIRED);
      ld_48 = l_minlot_32;
      for (double ld_180 = 0; ld_48 < 1.0; ld_180++) ld_48 = 10.0 * ld_48;
      li_56 = OrdersTotal() - 1;
      if (lv_mode == TRUE) {
         if (AccountBalance() - AccountBalance() * Level_stop / 100.0 > AccountEquity()) {
            for (int l_pos_188 = li_56; l_pos_188 >= 0; l_pos_188--) {
               OrderSelect(l_pos_188, SELECT_BY_POS, MODE_TRADES);
               if (OrderMagicNumber() == g_magic_224 && OrderSymbol() == g_symbol_240) {
                  if (OrderType() == OP_BUY) CloseBuy();
                  if (OrderType() == OP_SELL) CloseSell();
               }
            }
         }
      }
      ld_60 = 0;
      l_count_68 = 0;
      li_72 = TRUE;
      li_76 = TRUE;
      for (l_pos_188 = li_56; l_pos_188 >= 0; l_pos_188--) {
         OrderSelect(l_pos_188, SELECT_BY_POS, MODE_TRADES);
         if (OrderMagicNumber() == g_magic_224 && OrderSymbol() == g_symbol_240) l_count_68++;
      }
      if (l_count_68 >= sn_mode) li_72 = FALSE;
      l_count_80 = 0;
      l_count_84 = 0;
      l_count_88 = 0;
      ld_92 = gd_108;
      l_minlot_100 = gd_108;
      ld_108 = 0;
      ld_116 = 0;
      ld_124 = 0;
      for (l_pos_188 = li_56; l_pos_188 >= 0; l_pos_188--) {
         OrderSelect(l_pos_188, SELECT_BY_POS, MODE_TRADES);
         if (OrderType() == OP_BUY && OrderMagicNumber() == g_magic_224 && OrderSymbol() == g_symbol_240) {
            ld_60 = OrderProfit() + OrderSwap() + ld_60;
            ld_108 += OrderLots();
            l_ord_open_price_16 = OrderOpenPrice();
            l_count_88++;
            if (MathAbs(l_ord_open_price_16 - ld_0) < sh_mode * setpoint()) l_count_80++;
            if (l_ord_open_price_16 - ld_0 > sh_mode * setpoint()) l_count_84++;
            if (ld_0 < l_ord_open_price_16) ld_116++;
            if (OrderLots() > ld_92) ld_92 = OrderLots();
         }
      }
      ld_108 /= 2.0;
      if (l_count_80 != 0) li_76 = FALSE;
      ld_92 = l_minlot_32;
      if (l_count_88 == 0) {
         l_minlot_100 = NormalizeDouble(AccountFreeMargin() * Dinamic_lot / 1000.0 / MarketInfo(Symbol(), MODE_MARGINREQUIRED), ld_180);
         if (l_minlot_100 < l_minlot_32) l_minlot_100 = l_minlot_32;
      }
      if (l_count_84 == l_count_88 && l_count_88 != 0 && ld_116 == l_count_88) {
         if (st_mode == TRUE) {
            for (l_pos_188 = li_56; l_pos_188 >= 0; l_pos_188--) {
               OrderSelect(l_pos_188, SELECT_BY_POS, MODE_TRADES);
               if (OrderType() == OP_BUY && OrderMagicNumber() == g_magic_224 && OrderSymbol() == g_symbol_240)
                  if (OrderLots() > ld_92) ld_92 = OrderLots();
            }
            l_minlot_100 = NormalizeDouble(ld_92 * sk_mode, ld_180);
         }
      }
      if (ld_60 >= l_marginrequired_40 * ld_108) {
         li_56 = OrdersTotal() - 1;
         for (l_pos_188 = li_56; l_pos_188 >= 0; l_pos_188--) {
            OrderSelect(l_pos_188, SELECT_BY_POS, MODE_TRADES);
            RefreshRates();
            if (OrderType() == OP_BUY && OrderMagicNumber() == g_magic_224 && OrderSymbol() == g_symbol_240) {
               if (IsTesting() || IsOptimization()) OrderClose(OrderTicket(), OrderLots(), MarketInfo(g_symbol_240, MODE_BID), 3, Blue);
               else CloseBuy();
            }
         }
      }
      if (sr_mode == FALSE) gd_264 = iHighest(g_symbol_240, 0, MODE_HIGH, gi_228, 0);
      else gd_264 = iLowest(g_symbol_240, 0, MODE_LOW, gi_228, 0);
      if (gd_264 == 1.0 || gd_264 == 2.0 || gd_264 == 3.0 && g_ima_256 > g_ima_248 && li_72 == TRUE && li_76 == TRUE) {
         RefreshRates();
         if (IsTesting() || IsOptimization()) OrderSend(g_symbol_240, OP_BUY, l_minlot_100, NormalizeDouble(MarketInfo(g_symbol_240, MODE_ASK), Digits), 3, 0, 0, "Calypso ver 1.04", g_magic_224, 0, Blue);
         else OpenBuy(g_symbol_240, l_minlot_100);
      }
      ld_132 = 0;
      li_76 = TRUE;
      l_count_80 = 0;
      l_count_84 = 0;
      l_count_88 = 0;
      ld_92 = gd_108;
      l_minlot_100 = gd_108;
      ld_60 = 0;
      li_56 = OrdersTotal() - 1;
      for (l_pos_188 = li_56; l_pos_188 >= 0; l_pos_188--) {
         OrderSelect(l_pos_188, SELECT_BY_POS, MODE_TRADES);
         if (OrderType() == OP_SELL && OrderMagicNumber() == g_magic_224 && OrderSymbol() == g_symbol_240) {
            ld_60 = OrderProfit() + OrderSwap() + ld_60;
            ld_132 += OrderLots();
            l_ord_open_price_24 = OrderOpenPrice();
            l_count_88++;
            if (MathAbs(l_ord_open_price_24 - ld_8) < sh_mode * setpoint()) l_count_80++;
            if (ld_8 - l_ord_open_price_24 > sh_mode * setpoint()) l_count_84++;
            if (ld_8 > l_ord_open_price_24) ld_124++;
            if (OrderLots() > ld_92) ld_92 = OrderLots();
         }
      }
      ld_132 /= 2.0;
      if (l_count_80 != 0) li_76 = FALSE;
      ld_92 = l_minlot_32;
      if (l_count_88 == 0) {
         l_minlot_100 = NormalizeDouble(AccountFreeMargin() * Dinamic_lot / 1000.0 / MarketInfo(Symbol(), MODE_MARGINREQUIRED), ld_180);
         if (l_minlot_100 < l_minlot_32) l_minlot_100 = l_minlot_32;
      }
      if (l_count_84 == l_count_88 && l_count_88 != 0 && ld_124 == l_count_88) {
         if (st_mode == TRUE) {
            for (l_pos_188 = li_56; l_pos_188 >= 0; l_pos_188--) {
               OrderSelect(l_pos_188, SELECT_BY_POS, MODE_TRADES);
               if (OrderType() == OP_SELL && OrderMagicNumber() == g_magic_224 && OrderSymbol() == g_symbol_240)
                  if (OrderLots() > ld_92) ld_92 = OrderLots();
            }
            l_minlot_100 = NormalizeDouble(ld_92 * sk_mode, ld_180);
         }
      }
      if (ld_60 >= l_marginrequired_40 * ld_132) {
         li_56 = OrdersTotal() - 1;
         for (l_pos_188 = li_56; l_pos_188 >= 0; l_pos_188--) {
            OrderSelect(l_pos_188, SELECT_BY_POS, MODE_TRADES);
            RefreshRates();
            if (OrderType() == OP_SELL && OrderMagicNumber() == g_magic_224 && OrderSymbol() == g_symbol_240) {
               if (IsTesting() || IsOptimization()) OrderClose(OrderTicket(), OrderLots(), MarketInfo(g_symbol_240, MODE_ASK), 3, Fuchsia);
               else CloseSell();
            }
         }
      }
      if (sr_mode == FALSE) gd_264 = iLowest(g_symbol_240, 0, MODE_LOW, gi_228, 0);
      else gd_264 = iHighest(g_symbol_240, 0, MODE_HIGH, gi_228, 0);
      if (gd_264 == 1.0 || gd_264 == 2.0 || gd_264 == 3.0 && g_ima_256 < g_ima_248 && li_72 == TRUE && li_76 == TRUE) {
         RefreshRates();
         if (IsTesting() || IsOptimization()) OrderSend(g_symbol_240, OP_SELL, l_minlot_100, NormalizeDouble(MarketInfo(g_symbol_240, MODE_BID), Digits), 3, 0, 0, "Calypso ver 1.04", g_magic_224, 0, Fuchsia);
         else OpenSell(g_symbol_240, l_minlot_100);
      }
      g_symbol_240 = Symbol();
      l_error_140 = 0;
      if (sm_mode == TRUE) {
         if (stop < MarketInfo(g_symbol_240, MODE_STOPLEVEL)) stop = MarketInfo(g_symbol_240, MODE_STOPLEVEL);
         for (g_pos_232 = 0; g_pos_232 <= OrdersTotal() - 1; g_pos_232++) {
            OrderSelect(g_pos_232, SELECT_BY_POS, MODE_TRADES);
            if (OrderMagicNumber() == g_magic_224 && OrderStopLoss() == 0.0 && OrderSymbol() == Symbol()) {
               while (li_144 <= 10) {
                  OrderModify(OrderTicket(), 0, MarketInfo(g_symbol_240, MODE_ASK) - stop * setpoint(), 0, 0, C'0x02,0x00,0x00');
                  l_error_140 = GetLastError();
                  if (l_error_140 == 0/* NO_ERROR */) {
                     Print("��ɹ�");
                     break;
                  }
                  Print("�Ҳ����޸ģ���ʧ�� " + GetLastError());
                  RefreshRates();
                  Sleep(5000);
                  li_144++;
               }
               Print("-----ERROR-----  �Ҳ����޸��򶩵�");
               return (l_error_140);
            }
         }
      }
   } else Comment("���׽�ֹ");
   if (sm_mode == TRUE) ls_148 = "ֹͣ���룿�񻶣�" + stop + " ��Ŀ";
   else ls_148 = "վ�³�";
   if (st_mode == TRUE) ls_156 = "��Ԫ�����ض��ڣ��أ�" + DoubleToStr(sk_mode, ld_180);
   else ls_156 = "��Ԫ�����ضιر�";
   if (sr_mode == TRUE) ls_164 = "��������";
   else ls_164 = "�����";
   if (lv_mode == TRUE) ls_172 = "����֧�Ÿ߶ȣ��أ��趨��" + DoubleToStr(Level_stop, 0);
   else ls_172 = "����֧�Ÿ߶ȣ��ر�";
   Comment("" 
      + "\n" 
      + " Calypso ver. 1.04" 
      + "\n" 
      + " forex-shop.net" 
      + "\n" 
      + "_________________________________________" 
      + "\n" 
      + "\n" 
      + "�ܸ�                               1:" + DoubleToStr(AccountLeverage(), 0) 
      + "\n" 
      + "�ʻ�                               " + DoubleToStr(AccountNumber(), 0) 
      + "\n" 
      + "ʱ�������                      " + TimeToStr(TimeCurrent(), TIME_SECONDS) 
      + "\n" 
      + "ƽ��                               " + DoubleToStr(AccountBalance(), 0) 
      + "\n" 
      + "��Ч                               " + DoubleToStr(AccountEquity(), 0) 
      + "\n" 
      + "_________________________________________" 
      + "\n" 
      + "���ǵ�Ŀ�ꣿ��Ҵ����" + DoubleToStr(ld_108 * l_marginrequired_40, ld_180) + "$" 
      + "\n" 
      + "���ǵ�Ŀ�ꣿ���Ҵ����" + DoubleToStr(ld_132 * l_marginrequired_40, ld_180) + "$" 
      + "\n" 
      + "���ض�̬������Ϊ  " + DoubleToStr(Dinamic_lot, ld_180) + "%" 
      + "\n" 
      + ls_172 
      + "\n" 
      + ls_148 
      + "\n" 
      + "���������������         " + sn_mode 
      + "\n" 
      + "ɳ������������                     " + sh_mode + " ��" 
      + "\n" 
      + ls_156 
      + "\n" 
      + ls_164 
      + "\n" 
   + "_________________________________________");
   return (0);
}

int OpenBuy(string a_symbol_0, double a_lots_8) {
   int l_ticket_16;
   int l_slippage_20;
   int li_24 = 1;
   while (true) {
      l_slippage_20 = 1;
      l_ticket_16 = OrderSend(a_symbol_0, OP_BUY, a_lots_8, NormalizeDouble(MarketInfo(a_symbol_0, MODE_ASK), Digits), l_slippage_20, 0, 0, "Calypso ver 1.04", g_magic_224, 0, Blue);
      if (l_ticket_16 <= 0) {
         Print("���޷��򿪣����䣿���򡣴���", GetLastError());
         while (RefreshRates() == FALSE) Sleep(5000);
         li_24++;
         if (li_24 % 5 == 0) l_slippage_20++;
      } else {
         Print("���䣿����ɹ��򿪣�");
         break;
      }
      if (li_24 != 11) continue;
      Comment("���޷��򿪣����䣿��һ�����ţ�", a_symbol_0, " ����", GetLastError());
      break;
   }
   return (0);
}

int OpenSell(string a_symbol_0, double a_lots_8) {
   int l_ticket_16;
   int l_slippage_20;
   int li_24 = 1;
   while (true) {
      l_slippage_20 = 1;
      l_ticket_16 = OrderSend(a_symbol_0, OP_SELL, a_lots_8, NormalizeDouble(MarketInfo(a_symbol_0, MODE_BID), Digits), l_slippage_20, 0, 0, "Calypso ver 1.04", g_magic_224, 0, Fuchsia);
      if (l_ticket_16 <= 0) {
         Print("���޷��򿪣����䣿��������", GetLastError());
         while (RefreshRates() == FALSE) Sleep(5000);
         li_24++;
         if (li_24 % 5 == 0) l_slippage_20++;
      } else {
         Print("���䣿���ɹ��򿪣�");
         break;
      }
      if (li_24 != 11) continue;
      Comment("���޷��򿪣����䣿�����Ը���", a_symbol_0, " ����", GetLastError());
      break;
   }
   return (0);
}

void CloseBuy() {
   int l_error_0;
   int l_count_4 = 0;
   while (true) {
      OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(MarketInfo(OrderSymbol(), MODE_BID), Digits), 2, White);
      l_error_0 = GetLastError();
      if (l_error_0 == 0/* NO_ERROR */) {
         Print("�رչ��򡣲��䣿���ͣ��ɹ��ر�");
         return;
      }
      Print("���޷��򿪣����䣿��һ�����ţ�", Symbol(), " ����", GetLastError());
      Sleep(500);
      RefreshRates();
      l_count_4++;
      if (l_count_4 <= 25) continue;
      break;
   }
   Alert("Order failed to CLOSE - See Journal for errors");
}

void CloseSell() {
   int l_error_0;
   int l_count_4 = 0;
   while (true) {
      OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(MarketInfo(OrderSymbol(), MODE_ASK), Digits), 2, Red);
      l_error_0 = GetLastError();
      if (l_error_0 == 0/* NO_ERROR */) {
         Print("�ر��������䣿���ɹ��ر�");
         return;
      }
      Print("���޷��򿪣����䣿�����Ը���", Symbol(), " ����", GetLastError());
      Sleep(500);
      RefreshRates();
      l_count_4++;
      if (l_count_4 <= 25) continue;
      break;
   }
   Alert("�����޷��ر� - �뿴��־�Ĵ���");
}