
#property copyright "Alfred Ebner"
#property link      "http://www.cad-trader.com"

#import "CadTrader_v1.dll"
   int Verify(int a0, int a1, int a2, int a3, int a4);
   int BuyExit(int a0, int a1, int a2, int a3, int a4, double a5, double a6, double a7, double a8, double a9);
   int SellExit(int a0, int a1, int a2, int a3, int a4, double a5, double a6, double a7, double a8, double a9);
   double GetSellLimitPrice(int a0, int a1, int a2, int a3, int a4, double a5, double a6, double a7, int a8);
   double GetBuyLimitPrice(int a0, int a1, int a2, int a3, int a4, double a5, double a6, double a7, int a8);
#import

extern int pips = 11;
extern bool Moneymanagement = TRUE;
extern double lotsize = 0.1;
extern double maxSpread = 8.0;
extern double win_per_trade = 12.0;
extern double Risk = 4.5;
extern double SecuritySL = 50.0;
extern bool IncreaseLots = FALSE;
extern int NumberofTrades = 5;
extern double IncreaseFactor = 1.2;
double Numget= 0.326960;

int init() {
   return (0);
}

int deinit() {
   return (0);
}

int start() {
   bool lba_0[2];
   bool lba_4[2];
   string lsa_8[20];
   double ld_12;
   int l_ord_close_20;
   int l_ticket_24;
   int l_ticket_28;
   int l_error_32;
   int l_ord_delete_36;
   double l_lots_40;
   double l_lots_48;
   int li_56;
   int l_cmd_60;
   string ls_64;
   int li_72;
   int li_76;
   double l_price_80;
   double l_price_88;
   lba_0[0] = 0;
   lba_4[0] = 0;
   double l_point_96 = MarketInfo(Symbol(), MODE_POINT);
   int l_count_104 = 0;
   int l_count_108 = 0;
   double ld_112 = 0;
   double ld_120 = 0;
   double ld_128 = 0;
   double ld_136 = 0;
   double ld_144 = 0;
   double ld_152 = 0;
   int li_unused_160 = 1014195;
   double l_minlot_164 = MarketInfo(Symbol(), MODE_MINLOT);
   double l_maxlot_172 = MarketInfo(Symbol(), MODE_MAXLOT);
   double l_lotstep_180 = MarketInfo(Symbol(), MODE_LOTSTEP);
   double l_tickvalue_188 = MarketInfo("EURUSD", MODE_TICKVALUE);
   double l_lotsize_196 = MarketInfo(Symbol(), MODE_LOTSIZE);
   int li_299 = ((MathArcsin(Numget)*198)+(MathArccos(Numget)*548))*10000+1968386;
   int li_208 = (li_299 * 2 + 15687) << 2;
   int li_212 = li_208 | li_299;
   int li_216 = 0;
   int li_220 = li_216 | li_208;
   int li_224 = Verify(li_299, li_208, li_212, li_216, li_220);
   if (li_224 == 0) {
      Alert("This version of the EA only runs on demo accounts!!!");
      f0_1();
      lsa_8[1] = "This version of the EA only runs on demo accounts!!!";
      f0_2(1, lsa_8[1]);
      return (0);
   }
   double ld_228 = 100000 / l_lotsize_196;
   if (Risk > 10.0) {
      Alert("The maximum value of the Risk variable is 10!!!");
      return (0);
   }
   if (Risk < 2.5) {
      Alert("The minimum value of the Risk varible is 2.5!!!");
      return (0);
   }
   if (lotsize < l_minlot_164 && Moneymanagement == FALSE) {
      Alert("The minimum value of the Lotsize variable is ", l_minlot_164, "!!!");
      return (0);
   }
   int l_ord_total_236 = OrdersTotal();
   for (int l_pos_240 = l_ord_total_236 - 1; l_pos_240 >= 0; l_pos_240--) {
      OrderSelect(l_pos_240, SELECT_BY_POS);
      l_cmd_60 = OrderType();
      ls_64 = OrderComment();
      if (ls_64 == "Cadtrader") {
         if (OrderType() == OP_BUY) {
            l_count_104++;
            ld_120 = ld_120 + OrderProfit() + OrderCommission() + OrderSwap();
            ld_136 += OrderLots();
         }
         if (OrderType() == OP_SELL) {
            l_count_108++;
            ld_112 = ld_112 + OrderProfit() + OrderCommission() + OrderSwap();
            ld_128 += OrderLots();
         }
         if (OrderType() == OP_SELLLIMIT) lba_0[0] = 1;
         if (OrderType() == OP_BUYLIMIT) lba_4[0] = 1;
      }
   }
   bool li_244 = TRUE;
   int li_248 = 1;
   if (100000.0 * Point == 1.0) {
      ld_12 = (Ask - Bid) / 0.0001;
      li_248 = 10;
   } else {
      if (1000.0 * Point == 1.0) {
         ld_12 = (Ask - Bid) / 0.01;
         li_248 = 10;
      } else ld_12 = (Ask - Bid) / Point;
   }
   string l_comment_252 = "Cadtrader";
   string l_dbl2str_260 = DoubleToStr(ld_112, 2);
   string l_dbl2str_268 = DoubleToStr(ld_120, 2);
   ld_144 = 10.0 * win_per_trade * ld_136 / ld_228;
   ld_152 = 10.0 * win_per_trade * ld_128 / ld_228;
   string l_dbl2str_276 = DoubleToStr(ld_144, 2);
   string l_dbl2str_284 = DoubleToStr(ld_152, 2);
   lsa_8[1] = "No of buy trades: " + l_count_104;
   f0_2(1, lsa_8[1]);
   lsa_8[2] = "No of sell trades: " + l_count_108;
   f0_2(2, lsa_8[2]);
   lsa_8[3] = "Profit open buy orders: " + l_dbl2str_268;
   f0_2(3, lsa_8[3]);
   lsa_8[4] = "Profit open sell orders: " + l_dbl2str_260;
   f0_2(4, lsa_8[4]);
   lsa_8[5] = "Necessary Profit open buy orders: " + l_dbl2str_276;
   f0_2(5, lsa_8[5]);
   lsa_8[6] = "Necessary Profit open sell orders: " + l_dbl2str_284;
   f0_2(6, lsa_8[6]);
   if (f0_1()) {
   }
   double ld_292 = AccountBalance() - AccountBalance() * (100 - SecuritySL) / 100.0;
   int li_300 = BuyExit(li_299, li_208, li_212, li_216, li_220, ld_120, ld_144, 0.1, MathAbs(ld_120), ld_292);
   if (li_300 == 1) {
      l_ord_total_236 = OrdersTotal();
      for (l_pos_240 = l_ord_total_236 - 1; l_pos_240 >= 0; l_pos_240--) {
         OrderSelect(l_pos_240, SELECT_BY_POS);
         l_cmd_60 = OrderType();
         ls_64 = OrderComment();
         if (ls_64 == "Cadtrader") {
            Print("i: ", l_pos_240, " Ticket: ", OrderTicket(), " Ordertype: ", OrderType());
            if (OrderType() == OP_BUY) {
               l_ord_close_20 = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 50, Red);
               if (l_ord_close_20 <= 0) {
                  l_error_32 = GetLastError();
                  Print("Error: ", l_error_32);
                  Sleep(10);
                  RefreshRates();
               } else {
                  Print("Ticket: ", l_ord_close_20);
                  OrderSelect(l_ord_close_20, SELECT_BY_TICKET);
                  OrderPrint();
               }
            }
            if (OrderType() == OP_BUYLIMIT) {
               Print("OrderClose 1., Ordertype: ", OrderType());
               l_ord_delete_36 = OrderDelete(OrderTicket());
               if (l_ord_delete_36 <= 0) {
                  l_error_32 = GetLastError();
                  Print("Error: ", l_error_32);
                  Sleep(10);
                  RefreshRates();
               } else {
                  OrderSelect(l_ord_delete_36, SELECT_BY_TICKET);
                  OrderPrint();
               }
            }
            if (OrderType() == OP_SELLLIMIT && l_count_108 == 0) {
               Print("OrderClose 1., Ordertype: ", OrderType());
               l_ord_delete_36 = OrderDelete(OrderTicket());
               if (l_ord_delete_36 <= 0) {
                  l_error_32 = GetLastError();
                  Print("Error: ", l_error_32);
                  Sleep(10);
                  RefreshRates();
               } else {
                  OrderSelect(l_ord_delete_36, SELECT_BY_TICKET);
                  OrderPrint();
               }
            }
         }
      }
   }
   int li_304 = SellExit(li_299, li_208, li_212, li_216, li_220, ld_112, ld_152, 0.1, MathAbs(ld_112), ld_292);
   if (li_304 == 1) {
      l_ord_total_236 = OrdersTotal();
      for (l_pos_240 = l_ord_total_236 - 1; l_pos_240 >= 0; l_pos_240--) {
         OrderSelect(l_pos_240, SELECT_BY_POS);
         l_cmd_60 = OrderType();
         ls_64 = OrderComment();
         if (ls_64 == "Cadtrader") {
            Print("i: ", l_pos_240, " Ticket: ", OrderTicket(), " Ordertype: ", OrderType());
            if (OrderType() == OP_SELL) {
               l_ord_close_20 = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 50, Red);
               if (l_ord_close_20 <= 0) {
                  l_error_32 = GetLastError();
                  Print("Error: ", l_error_32);
                  Sleep(10);
                  RefreshRates();
               } else {
                  OrderSelect(l_ord_close_20, SELECT_BY_TICKET);
                  OrderPrint();
               }
            }
            if (OrderType() == OP_SELLLIMIT) {
               Print("OrderClose 1., Ordertype: ", OrderType());
               l_ord_delete_36 = OrderDelete(OrderTicket());
               if (l_ord_delete_36 <= 0) {
                  l_error_32 = GetLastError();
                  Print("Error: ", l_error_32);
                  Sleep(10);
                  RefreshRates();
               } else {
                  OrderSelect(l_ord_delete_36, SELECT_BY_TICKET);
                  OrderPrint();
               }
            }
            if (OrderType() == OP_BUYLIMIT && l_count_104 == 0) {
               Print("OrderClose 1., Ordertype: ", OrderType());
               l_ord_delete_36 = OrderDelete(OrderTicket());
               if (l_ord_delete_36 <= 0) {
                  l_error_32 = GetLastError();
                  Print("Error: ", l_error_32);
                  Sleep(10);
                  RefreshRates();
               } else {
                  OrderSelect(l_ord_delete_36, SELECT_BY_TICKET);
                  OrderPrint();
               }
            }
         }
      }
   }
   bool li_308 = FALSE;
   if (l_lotstep_180 == 0.1) li_56 = 1;
   if (l_lotstep_180 == 0.01) li_56 = 2;
   if (li_244 == TRUE) li_308 = TRUE;
   if (li_308 == TRUE) {
      if (IncreaseLots == TRUE) {
         if (l_count_104 < NumberofTrades) l_lots_40 = NormalizeDouble((AccountEquity() - ld_112 - ld_120) * ld_228 / (1000.0 * Risk) / 10.0, li_56);
         else {
            li_72 = MathFloor(l_count_104 / NumberofTrades);
            l_lots_40 = NormalizeDouble(MathPow(IncreaseFactor, li_72) * ((AccountEquity() - ld_112 - ld_120) * ld_228 / (1000.0 * Risk) / 10.0), li_56);
         }
         if (l_count_108 < NumberofTrades) l_lots_48 = NormalizeDouble((AccountEquity() - ld_112 - ld_120) * ld_228 / (1000.0 * Risk) / 10.0, 2);
         else {
            li_76 = MathFloor(l_count_108 / NumberofTrades);
            l_lots_48 = NormalizeDouble(MathPow(IncreaseFactor, li_76) * ((AccountEquity() - ld_112 - ld_120) * ld_228 / (1000.0 * Risk) / 10.0), li_56);
         }
      } else {
         if (Moneymanagement == TRUE) {
            l_lots_40 = NormalizeDouble((AccountEquity() - ld_112 - ld_120) * ld_228 / (1000.0 * Risk) / 10.0, li_56);
            if (l_lots_40 < l_minlot_164) l_lots_40 = l_minlot_164;
            l_lots_48 = l_lots_40;
         } else {
            l_lots_40 = lotsize;
            l_lots_48 = l_lots_40;
         }
      }
      if (lba_0[0] != 1) {
         l_price_80 = GetSellLimitPrice(li_299, li_208, li_212, li_216, li_220, Bid, pips, l_point_96, li_248);
         l_ticket_24 = OrderSend(Symbol(), OP_SELLLIMIT, l_lots_48, l_price_80, maxSpread * li_248, 0, 0, l_comment_252, 1111, 0, Green);
         if (l_ticket_24 <= 0) {
            l_error_32 = GetLastError();
            Print("Error: ", l_error_32);
            Sleep(10);
            RefreshRates();
         } else {
            OrderSelect(l_ticket_24, SELECT_BY_TICKET);
            OrderPrint();
         }
      }
      if (lba_4[0] != 1) {
         l_price_88 = GetBuyLimitPrice(li_299, li_208, li_212, li_216, li_220, Bid, pips, l_point_96, li_248);
         l_ticket_28 = OrderSend(Symbol(), OP_BUYLIMIT, l_lots_40, l_price_88, maxSpread * li_248, 0, 0, l_comment_252, 2222, 0, Green);
         if (l_ticket_28 <= 0) {
            l_error_32 = GetLastError();
            Print("Error: ", l_error_32);
            Sleep(10);
            RefreshRates();
         } else {
            OrderSelect(l_ticket_28, SELECT_BY_TICKET);
            OrderPrint();
         }
      }
   }
   if (li_308 == FALSE) {
      if (lba_0[0] == 1 || lba_4[0] == 1) {
         l_ord_total_236 = OrdersTotal();
         for (l_pos_240 = l_ord_total_236 - 1; l_pos_240 >= 0; l_pos_240--) {
            OrderSelect(l_pos_240, SELECT_BY_POS);
            l_cmd_60 = OrderType();
            l_ord_delete_36 = 0;
            ls_64 = OrderComment();
            if (ls_64 == "Cadtrader") {
               if (OrderType() == OP_SELLLIMIT || OrderType() == OP_BUYLIMIT) {
                  Print("OrderClose 1., Ordertype: ", OrderType());
                  l_ord_delete_36 = OrderDelete(OrderTicket());
                  if (l_ord_delete_36 <= 0) {
                     l_error_32 = GetLastError();
                     Print("Error: ", l_error_32);
                     Sleep(10);
                     RefreshRates();
                  } else {
                     OrderSelect(l_ord_delete_36, SELECT_BY_TICKET);
                     OrderPrint();
                  }
               }
            }
         }
      }
   }
   return (0);
}

bool f0_0(int ai_0, string a_text_4) {
   string l_name_12 = "label_object" + ai_0;
   ObjectDelete(l_name_12);
   if (!ObjectCreate(l_name_12, OBJ_LABEL, 0, 0, 0)) {
      Print("error: canīt create ", l_name_12, " #", GetLastError());
      return (FALSE);
   }
   ObjectSet(l_name_12, OBJPROP_XDISTANCE, 10);
   ObjectSet(l_name_12, OBJPROP_YDISTANCE, 15 * ai_0 + 5);
   ObjectSetText(l_name_12, a_text_4, 8, "Arial", White);
   return (FALSE);
}

bool f0_1() {
   string l_name_0;
   for (int li_8 = 1; li_8 == 10; li_8++) {
      l_name_0 = "label_object" + li_8;
      ObjectDelete(l_name_0);
   }
   return (FALSE);
}

int f0_2(int ai_0, string as_4) {
   if (f0_0(ai_0, as_4)) {
   }
   return (0);
}