#property copyright "Copyright � 2010, expforex"
#property link      "http://www.expforex.at.ua"
#property show_inputs

#include <WinUser32.mqh>
#include <stdlib.mqh>

#import "kernel32.dll"
   int _lopen(string a0, int a1);
   int _lcreat(string a0, int a1);
   int _llseek(int a0, int a1, int a2);
   int _lread(int a0, string a1, int a2);
   int _lwrite(int a0, string a1, int a2);
   int _lclose(int a0);
#import "shell32.dll"
   int ShellExecuteA(int a0, string a1, string a2, string a3, string a4, int a5);
#import

extern string pathREAD = "C:\\wtest2.txt";
extern int Magic = 123;
extern int Lot = 0;
extern bool TakeProfitCopy = TRUE;
extern bool StopLossCopy = TRUE;
extern bool ReverseCopy = FALSE;
extern bool ProfitCopyOnly = FALSE;
extern int SLEEPING = 1000;
extern bool ComentShow = FALSE;
string gsa_116[25];
int gia_120[25];
double gda_124[25];
double gda_128[25];
double gda_132[25];
string gsa_136[25];
int gia_140[25];
double gda_144[25];
int gi_148 = 25;
int g_datetime_156;

void start() {
   string ls_0;
   string ls_8;
   string ls_20;
   double ld_28;
   double ld_36;
   double ld_44;
   bool li_52;
   Stamp();
   while (true) {
      if (!(CheckDonate())) continue;
      ls_0 = ReadFile(pathREAD);
      startWriteInfo(AccountNumber(), ls_0);
      startReadOpen(AccountNumber());
      for (int l_index_16 = 0; l_index_16 <= gi_148; l_index_16++) {
         if (gsa_116[l_index_16] != "0") {
            ls_8 = ls_8 
               + "\n" 
            + gsa_116[l_index_16] + " " + gia_120[l_index_16] + " " + NormalizeDouble(gda_124[l_index_16], 2) + " " + NormalizeDouble(gda_128[l_index_16], Digits) + " " + NormalizeDouble(gda_132[l_index_16], Digits) + " " + gsa_136[l_index_16] + " " + gia_140[l_index_16] + " " + NormalizeDouble(gda_144[l_index_16], 2);
         }
      }
      ls_20 = "������:  " + "���:  " + "����:  " + "��������:  " + "����������:  " + "�����������:  " + "�����:  " + "������:  ";
      if (ComentShow) {
         Comment(ls_20 + ls_8 
            + "\n" 
         + "������ ��������, ��������� ����� ����������� ��������:" + TimeToStr(g_datetime_156 + SLEEPING / 1000, TIME_DATE|TIME_MINUTES|TIME_SECONDS));
      }
      for (l_index_16 = 0; l_index_16 < gi_148; l_index_16++) {
         if (!EPS(gia_140[l_index_16]) && gsa_116[l_index_16] != "0" && !ProfitCopyOnly || (ProfitCopyOnly && gda_144[l_index_16] > 0.0)) {
            if (!ReverseCopy) {
               if (Lot == 0) ld_28 = gda_124[l_index_16];
               else ld_28 = Lot;
               if (StopLossCopy) ld_44 = gda_128[l_index_16];
               else ld_44 = 0;
               if (TakeProfitCopy) ld_36 = gda_132[l_index_16];
               else ld_36 = 0;
               OPs(gsa_116[l_index_16], gia_120[l_index_16], ld_28, ld_44, ld_36, Magic, gia_140[l_index_16]);
            } else {
               if (Lot == 0) ld_28 = gda_124[l_index_16];
               else ld_28 = Lot;
               if (StopLossCopy) ld_44 = gda_128[l_index_16];
               else ld_44 = 0;
               if (TakeProfitCopy) ld_36 = gda_132[l_index_16];
               else ld_36 = 0;
               if (gia_120[l_index_16] == 0) li_52 = TRUE;
               if (gia_120[l_index_16] == 1) li_52 = FALSE;
               OPs(gsa_116[l_index_16], li_52, ld_28, ld_36, ld_44, Magic, gia_140[l_index_16]);
            }
         }
         if (EPS(gia_140[l_index_16]) && gsa_116[l_index_16] != "0" && OrderStopLoss() != gda_128[l_index_16] && StopLossCopy) ModifyOrder(-1, gda_128[l_index_16], -1);
         if (EPS(gia_140[l_index_16]) && gsa_116[l_index_16] != "0" && OrderTakeProfit() != gda_132[l_index_16] && TakeProfitCopy) ModifyOrder(-1, -1, gda_132[l_index_16]);
      }
      CPD("", -1, Magic);
      if (!ComentShow) Comment("������ ��������, ��������� ����� ����������� ��������:" + TimeToStr(g_datetime_156 + SLEEPING / 1000, TIME_DATE|TIME_MINUTES|TIME_SECONDS));
      g_datetime_156 = TimeCurrent();
      Sleep(SLEEPING);
   }
}

void deinit() {
   Comment("������ ������ � �������");
}

int Coment(string as_0) {
   bool li_ret_8;
   for (int l_index_12 = 0; l_index_12 < gi_148; l_index_12++) {
      if (StrToInteger(as_0) == gia_140[l_index_12]) {
         li_ret_8 = TRUE;
         break;
      }
   }
   return (li_ret_8);
}

void CPD(string as_0 = "", int a_cmd_8 = -1, int a_magic_12 = -1) {
   bool li_24;
   int l_ord_total_20 = OrdersTotal();
   if (as_0 == "0") as_0 = Symbol();
   for (int l_pos_16 = l_ord_total_20 - 1; l_pos_16 >= 0; l_pos_16--) {
      if (OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == as_0 || as_0 == "" && a_cmd_8 < OP_BUY || OrderType() == a_cmd_8) {
            if (OrderType() == OP_BUY || OrderType() == OP_SELL) {
               if (a_magic_12 < 0 || OrderMagicNumber() == a_magic_12) li_24 = Coment(OrderComment());
               if (li_24 == 0) CPBS();
            }
         }
      }
   }
}

void Stamp() {
   ObjectCreate("Original", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("Original", " www.expforex.at.ua", 12, "Arial Bold", Chartreuse);
   ObjectSet("Original", OBJPROP_CORNER, 3);
   ObjectSet("Original", OBJPROP_XDISTANCE, 0);
   ObjectSet("Original", OBJPROP_YDISTANCE, 10);
}

bool EPS(int ai_0 = 0) {
   int l_ord_total_8 = OrdersTotal();
   for (int l_pos_4 = 0; l_pos_4 < l_ord_total_8; l_pos_4++) {
      if (OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES))
         if (ai_0 == StrToInteger(OrderComment()) && OrderCloseTime() == 0) return (TRUE);
   }
   return (FALSE);
}

void ModifyOrder(double a_ord_open_price_0 = -1.0, double a_ord_stoploss_8 = 0.0, double a_ord_takeprofit_16 = 0.0, int a_datetime_24 = 0) {
   bool l_bool_28;
   double l_ask_40;
   double l_bid_48;
   int l_error_76;
   int l_digits_72 = MarketInfo(OrderSymbol(), MODE_DIGITS);
   if (a_ord_open_price_0 <= 0.0) a_ord_open_price_0 = OrderOpenPrice();
   if (a_ord_stoploss_8 < 0.0) a_ord_stoploss_8 = OrderStopLoss();
   if (a_ord_takeprofit_16 < 0.0) a_ord_takeprofit_16 = OrderTakeProfit();
   a_ord_open_price_0 = NormalizeDouble(a_ord_open_price_0, l_digits_72);
   a_ord_stoploss_8 = NormalizeDouble(a_ord_stoploss_8, l_digits_72);
   a_ord_takeprofit_16 = NormalizeDouble(a_ord_takeprofit_16, l_digits_72);
   double ld_32 = NormalizeDouble(OrderOpenPrice(), l_digits_72);
   double ld_56 = NormalizeDouble(OrderStopLoss(), l_digits_72);
   double ld_64 = NormalizeDouble(OrderTakeProfit(), l_digits_72);
   if (a_ord_open_price_0 != ld_32 || a_ord_stoploss_8 != ld_56 || a_ord_takeprofit_16 != ld_64) {
      for (int li_80 = 1; li_80 <= 3; li_80++) {
         if (!IsTesting() && !IsExpertEnabled() || IsStopped()) break;
         while (!IsTradeAllowed()) Sleep(5000);
         RefreshRates();
         l_bool_28 = OrderModify(OrderTicket(), a_ord_open_price_0, a_ord_stoploss_8, a_ord_takeprofit_16, a_datetime_24);
         if (l_bool_28) break;
         l_error_76 = GetLastError();
         l_ask_40 = MarketInfo(OrderSymbol(), MODE_ASK);
         l_bid_48 = MarketInfo(OrderSymbol(), MODE_BID);
         Alert("Error(", l_error_76, ") modifying order: ", ErrorDescription(l_error_76), ", try ", li_80);
         Alert("Ask=", l_ask_40, "  Bid=", l_bid_48, "  sy=", OrderSymbol(), "  op=" + OrderType(), "  pp=", a_ord_open_price_0, "  sl=", a_ord_stoploss_8, "  tp=", a_ord_takeprofit_16);
         Sleep(10000);
      }
   }
}

int OPs(string a_symbol_0, int a_cmd_8, double a_lots_12, double a_price_20 = 0.0, double a_price_28 = 0.0, int a_magic_36 = 0, string a_comment_40 = "") {
   color l_color_48;
   int l_datetime_52;
   double l_price_56;
   double l_ask_64;
   double l_bid_72;
   int l_digits_80;
   int l_error_84;
   int l_ticket_92 = 0;
   string ls_unused_96 = "";
   if (a_symbol_0 == "" || a_symbol_0 == "0") a_symbol_0 = Symbol();
   if (a_cmd_8 == OP_BUY) l_color_48 = Red;
   else l_color_48 = Green;
   for (int li_88 = 1; li_88 <= 3; li_88++) {
      if (!IsTesting() && !IsExpertEnabled() || IsStopped()) break;
      while (!IsTradeAllowed()) Sleep(5000);
      RefreshRates();
      l_digits_80 = MarketInfo(a_symbol_0, MODE_DIGITS);
      l_ask_64 = MarketInfo(a_symbol_0, MODE_ASK);
      l_bid_72 = MarketInfo(a_symbol_0, MODE_BID);
      if (a_cmd_8 == OP_BUY) l_price_56 = l_ask_64;
      else l_price_56 = l_bid_72;
      l_price_56 = NormalizeDouble(l_price_56, l_digits_80);
      l_datetime_52 = TimeCurrent();
      l_ticket_92 = OrderSend(a_symbol_0, a_cmd_8, a_lots_12, l_price_56, 1, a_price_20, a_price_28, a_comment_40, a_magic_36, 0, l_color_48);
      if (l_ticket_92 > 0) break;
      l_error_84 = GetLastError();
      if (l_error_84 == 128/* TRADE_TIMEOUT */ || l_error_84 == 142 || l_error_84 == 143) Sleep(66666.0);
      Print("Error(", l_error_84, ") opening position: ", ErrorDescription(l_error_84), ", try ", li_88);
      Print("Ask=", l_ask_64, " Bid=", l_bid_72, " sy=", a_symbol_0, " ll=", a_lots_12, " op=", a_cmd_8, " pp=", l_price_56, " sl=", a_price_20, " tp=", a_price_28, " mn=", a_magic_36);
      if (l_ask_64 == 0.0 && l_bid_72 == 0.0) Print("��������� � ������ ����� ������� ������� " + a_symbol_0);
      if (l_error_84 == 2/* COMMON_ERROR */ || l_error_84 == 64/* ACCOUNT_DISABLED */ || l_error_84 == 65/* INVALID_ACCOUNT */ || l_error_84 == 133/* TRADE_DISABLED */) break;
      if (l_error_84 == 4/* SERVER_BUSY */ || l_error_84 == 131/* INVALID_TRADE_VOLUME */ || l_error_84 == 132/* MARKET_CLOSED */) {
         Sleep(300000);
         break;
      }
      if (l_error_84 == 140/* LONG_POSITIONS_ONLY_ALLOWED */ || l_error_84 == 148/* ERR_TRADE_TOO_MANY_ORDERS */ || l_error_84 == 4110/* LONGS__NOT_ALLOWED */ || l_error_84 == 4111/* SHORTS_NOT_ALLOWED */) break;
      if (l_error_84 == 141/* TOO_MANY_REQUESTS */) Sleep(100000);
      if (l_error_84 == 145/* TRADE_MODIFY_DENIED */) Sleep(17000);
      if (l_error_84 == 146/* TRADE_CONTEXT_BUSY */) while (IsTradeContextBusy()) Sleep(11000);
      if (l_error_84 != 135/* PRICE_CHANGED */) Sleep(7700.0);
   }
   return (l_ticket_92);
}

void CPBS() {
   bool l_ord_close_0;
   color l_color_4;
   double l_ord_lots_8;
   double l_ask_16;
   double l_bid_24;
   double l_price_32;
   int l_error_40;
   if (OrderType() == OP_BUY || OrderType() == OP_SELL) {
      for (int li_44 = 1; li_44 <= 1; li_44++) {
         if (!IsTesting() && !IsExpertEnabled() || IsStopped()) break;
         while (!IsTradeAllowed()) Sleep(5000);
         RefreshRates();
         l_ask_16 = MarketInfo(OrderSymbol(), MODE_ASK);
         l_bid_24 = MarketInfo(OrderSymbol(), MODE_BID);
         if (OrderType() == OP_BUY) l_price_32 = l_bid_24;
         else l_price_32 = l_ask_16;
         l_ord_lots_8 = OrderLots();
         l_ord_close_0 = OrderClose(OrderTicket(), l_ord_lots_8, l_price_32, 1, l_color_4);
         if (l_ord_close_0) break;
         l_error_40 = GetLastError();
         if (l_error_40 == 146/* TRADE_CONTEXT_BUSY */) while (IsTradeContextBusy()) Sleep(11000);
         Sleep(5000);
      }
   }
}

string startReadOpen(string as_0 = "") {
   string ls_ret_24;
   string l_name_16 = as_0;
   int l_file_8 = FileOpen(l_name_16, FILE_CSV|FILE_WRITE|FILE_READ, ";");
   if (l_file_8 < 0) {
      if (GetLastError() == 4103/* CANNOT_OPEN_FILE */) {
         Comment("��� ����� � ������ ", l_name_16);
         return;
      }
      Comment("������ ��� �������� ����� ", l_name_16);
      return;
   }
   for (int l_index_32 = 0; l_index_32 <= gi_148; l_index_32++) {
      gsa_116[l_index_32] = FileReadString(l_file_8);
      gia_120[l_index_32] = StrToInteger(FileReadString(l_file_8));
      gda_124[l_index_32] = StrToDouble(FileReadString(l_file_8));
      gda_128[l_index_32] = StrToDouble(FileReadString(l_file_8));
      gda_132[l_index_32] = StrToDouble(FileReadString(l_file_8));
      gsa_136[l_index_32] = FileReadString(l_file_8);
      gia_140[l_index_32] = StrToInteger(FileReadString(l_file_8));
      gda_144[l_index_32] = StrToDouble(FileReadString(l_file_8));
   }
   FileClose(l_file_8);
   return (ls_ret_24);
}

string startWriteInfo(string as_0 = "file.csv", string as_8 = "") {
   string ls_ret_32;
   string l_name_24 = as_0;
   int l_file_16 = FileOpen(l_name_24, FILE_CSV|FILE_WRITE, ";");
   if (l_file_16 < 0) {
      if (GetLastError() == 4103/* CANNOT_OPEN_FILE */) {
         Comment("��� ����� � ������ ", l_name_24);
         return;
      }
      Comment("������ ��� �������� ����� ", l_name_24);
      return;
   }
   FileWrite(l_file_16, as_8);
   FileClose(l_file_16);
   return (ls_ret_32);
}

string ReadFile(string as_0) {
   int li_8 = _lopen(as_0, 0);
   if (li_8 < 0) {
      Print("������ �������� ����� ", as_0);
      return ("");
   }
   int li_12 = _llseek(li_8, 0, 0);
   if (li_12 < 0) {
      Print("������ ��������� ���������");
      return ("");
   }
   string ls_ret_16 = "";
   string ls_24 = "x";
   int l_count_32 = 0;
   for (li_12 = _lread(li_8, ls_24, 1); li_12 > 0; li_12 = _lread(li_8, ls_24, 1)) {
      ls_ret_16 = ls_ret_16 + ls_24;
      ls_24 = "x";
      l_count_32++;
   }
   li_12 = _lclose(li_8);
   if (li_12 < 0) Print("������ �������� ����� ", as_0);
   return (ls_ret_16);
}

bool CheckDonate() {
   int li_24;
   int l_file_0 = 0;
   string ls_4 = "0";
   int li_12 = 0;
   string l_name_16 = "CL.txt";
   l_file_0 = FileOpen(l_name_16, FILE_CSV|FILE_READ, ";");
   if (l_file_0 >= 1) {
      ls_4 = FileReadString(l_file_0);
      if (ls_4 != "0" && ls_4 != "1") ls_4 = "0";
      li_12 = StrToTime(FileReadString(l_file_0));
   } else {
      li_12 = TimeCurrent();
      l_file_0 = FileOpen(l_name_16, FILE_CSV|FILE_WRITE, ";");
      FileWrite(l_file_0, "0", TimeToStr(li_12, TIME_DATE));
   }
   FileClose(l_file_0);
   if (ls_4 == "0" && TimeCurrent() - li_12 > 604800) {
      li_24 = MessageBoxA(0, "������ ������� �������� � ������������������� ������,\n ��� ����������� ������� �������� ������ ����� ������� \n \n ���������� �������?", "������", 4);
      if (li_24 == 6) {
         ShellExecuteA(0, "Open", "iexplore.exe", "http://www.plati.ru/asp/pay.asp?idd=984951", "", 7);
         ls_4 = "0";
         return (FALSE);
      }
      if (li_24 != 6) {
         ShellExecuteA(0, "Open", "iexplore.exe", "http://expforex.at.ua", "", 7);
         ls_4 = "0";
         return (FALSE);
      }
      li_12 = TimeCurrent();
      l_file_0 = FileOpen(l_name_16, FILE_CSV|FILE_WRITE, ";");
      FileWrite(l_file_0, ls_4, TimeToStr(li_12, TIME_DATE));
      FileClose(l_file_0);
   }
   FileClose(l_file_0);
   return (TRUE);
}