/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#include <stdlib.mqh>

double gd_unused_76;
double gd_84;
extern double sbros = 0.0;
int g_corner_100 = 1;
int gi_unused_104 = 65535;
int gi_unused_108 = 15;
string gs_calibri_112 = "Calibri";
int gi_unused_120 = 15;
int gi_unused_124 = 2;
double g_pips_128 = 20.0;
double gd_136 = 10.0;
double gd_144 = 10.0;
extern double LotExponent = 1.7;
extern bool DynamicPips = TRUE;
extern int DefaultPips = 22;
extern double slip = 10.0;
extern double Lots = 0.1;
extern int lotdecimal = 1;
extern double TakeProfit = 20.0;
extern double RsiMinimum = 30.0;
extern double RsiMaximum = 70.0;
extern int MagicNumber = 2222;
int gi_216 = 0;
extern int MaxTrades = 40;
extern bool UseEquityStop = FALSE;
extern double TotalEquityRisk = 20.0;
extern bool UseTrailingStop = FALSE;
extern bool UseTimeOut = FALSE;
extern double MaxTradeOpenHours = 48.0;
double g_price_252;
double gd_260;
double gd_unused_268;
double gd_unused_276;
double g_price_284;
double g_bid_292;
double g_ask_300;
double gd_308;
double gd_316;
double gd_324;
bool gi_332;
string gs_336 = "3.6";
int g_time_344 = 0;
int gi_348;
int gi_352 = 0;
double gd_356;
int g_pos_364 = 0;
int gi_368;
double gd_372 = 0.0;
bool gi_380 = FALSE;
bool gi_384 = FALSE;
bool gi_388 = FALSE;
int gi_392;
int g_ticket_396;
bool gi_404 = FALSE;
double gd_408;
double gd_416;
double g_lots_424;
double g_lots_432;
double gd_440;
int g_magic_448;
double gda_452[2];
int g_slippage_456;
bool gi_460 = TRUE;

int init() {
   gd_324 = MarketInfo(Symbol(), MODE_SPREAD) * Point;
   return (0);
}

int deinit() {
   return (0);
}

int start() {
   double l_high_40;
   double l_low_48;
   double l_iclose_56;
   double l_iclose_64;
   if (CountTrades() > 11) {
   }
   string l_dbl2str_0 = DoubleToStr(AccountBalance(), 2);
   ObjectCreate("zagolovok_Label", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("zagolovok_Label", "ILAN_TURBO_BETA_v4.0 by Geret", 15, gs_calibri_112, LawnGreen);
   ObjectSet("zagolovok_Label", OBJPROP_CORNER, g_corner_100);
   ObjectSet("zagolovok_Label", OBJPROP_XDISTANCE, 1);
   ObjectSet("zagolovok_Label", OBJPROP_YDISTANCE, 10);
   string l_dbl2str_8 = DoubleToStr(AccountBalance(), 2);
   ObjectCreate("Balans_Label", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("Balans_Label", "������ �����    " + l_dbl2str_8, 15, gs_calibri_112, Yellow);
   ObjectSet("Balans_Label", OBJPROP_CORNER, g_corner_100);
   ObjectSet("balans_Label", OBJPROP_XDISTANCE, 20);
   ObjectSet("Balans_Label", OBJPROP_YDISTANCE, 40);
   string l_dbl2str_16 = DoubleToStr(AccountEquity(), 2);
   ObjectCreate("sr_Label", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("sr_Label", "��������� ��������    " + l_dbl2str_16, 15, gs_calibri_112, Yellow);
   ObjectSet("sr_Label", OBJPROP_CORNER, g_corner_100);
   ObjectSet("sr_Label", OBJPROP_XDISTANCE, 20);
   ObjectSet("sr_Label", OBJPROP_YDISTANCE, 70);
   string ls_24 = CountTrades();
   ObjectCreate("orders_Label", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("orders_Label", "���������� �����            " + ls_24, 15, gs_calibri_112, Yellow);
   ObjectSet("orders_Label", OBJPROP_CORNER, g_corner_100);
   ObjectSet("orders_Label", OBJPROP_XDISTANCE, 20);
   ObjectSet("orders_Label", OBJPROP_YDISTANCE, 100);
   string l_ord_total_32 = OrdersTotal();
   ObjectCreate("orders1_Label", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("orders1_Label", "����� �������            " + l_ord_total_32, 15, gs_calibri_112, Yellow);
   ObjectSet("orders1_Label", OBJPROP_CORNER, g_corner_100);
   ObjectSet("orders1_Label", OBJPROP_XDISTANCE, 20);
   ObjectSet("orders1_Label", OBJPROP_YDISTANCE, 130);
   if (CountTrades() == 7) {
   }
   if (CountTrades() == 0) gd_84 = AccountBalance();
   if (AccountEquity() - gd_84 > 600.0) delete();
   if (DynamicPips) {
      l_high_40 = High[iHighest(NULL, 0, MODE_HIGH, 36, 1)];
      l_low_48 = Low[iLowest(NULL, 0, MODE_LOW, 36, 1)];
      gi_216 = NormalizeDouble((l_high_40 - l_low_48) / 3.0 / Point, 2);
      if (gi_216 < DefaultPips / 2) gi_216 = DefaultPips / 2;
      if (gi_216 > 2 * DefaultPips) gi_216 = DefaultPips * 2;
   } else gi_216 = DefaultPips;
   if (UseTrailingStop) TrailingAlls(gd_136, gd_144, g_price_284);
   if (UseTimeOut) {
      if (TimeCurrent() >= gi_348) {
         CloseThisSymbolAll();
         Print("Closed All due to TimeOut");
      }
   }
   if (g_time_344 == Time[0]) return (0);
   g_time_344 = Time[0];
   double ld_72 = CalculateProfit();
   if (UseEquityStop) {
      if (ld_72 < 0.0 && MathAbs(ld_72) > TotalEquityRisk / 100.0 * AccountEquityHigh()) {
         CloseThisSymbolAll();
         Print("Closed All due to Stop Out");
         gi_404 = FALSE;
      }
   }
   gi_368 = CountTrades();
   if (gi_368 == 0) gi_332 = FALSE;
   for (g_pos_364 = OrdersTotal() - 1; g_pos_364 >= 0; g_pos_364--) {
      OrderSelect(g_pos_364, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         if (OrderType() == OP_BUY) {
            gi_384 = TRUE;
            gi_388 = FALSE;
            break;
         }
      }
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         if (OrderType() == OP_SELL) {
            gi_384 = FALSE;
            gi_388 = TRUE;
            break;
         }
      }
   }
   if (gi_368 > 0 && gi_368 <= MaxTrades) {
      RefreshRates();
      gd_308 = FindLastBuyPrice();
      gd_316 = FindLastSellPrice();
      if (gi_384 && gd_308 - Ask >= gi_216 * Point) gi_380 = TRUE;
      if (gi_388 && Bid - gd_316 >= gi_216 * Point) gi_380 = TRUE;
   }
   if (gi_368 < 1) {
      gi_388 = FALSE;
      gi_384 = FALSE;
      gi_380 = TRUE;
      gd_260 = AccountEquity();
   }
   if (gi_380) {
      gd_308 = FindLastBuyPrice();
      gd_316 = FindLastSellPrice();
      if (gi_388) {
         gi_352 = gi_368;
         gd_356 = NormalizeDouble(Lots * MathPow(LotExponent, gi_352), lotdecimal);
         RefreshRates();
         gi_392 = OpenPendingOrder(1, gd_356, Bid, slip, Ask, 0, 0, gs_336 + "-" + gi_352, MagicNumber, 0, HotPink);
         if (gi_392 < 0) {
            Print("Error: ", GetLastError());
            return (0);
         }
         gd_316 = FindLastSellPrice();
         gi_380 = FALSE;
         gi_404 = TRUE;
      } else {
         if (gi_384) {
            gi_352 = gi_368;
            gd_356 = NormalizeDouble(Lots * MathPow(LotExponent, gi_352), lotdecimal);
            gi_392 = OpenPendingOrder(0, gd_356, Ask, slip, Bid, 0, 0, gs_336 + "-" + gi_352, MagicNumber, 0, Lime);
            if (gi_392 < 0) {
               Print("Error: ", GetLastError());
               return (0);
            }
            gd_308 = FindLastBuyPrice();
            gi_380 = FALSE;
            gi_404 = TRUE;
         }
      }
   }
   if (gi_380 && gi_368 < 1) {
      l_iclose_56 = iClose(Symbol(), 0, 2);
      l_iclose_64 = iClose(Symbol(), 0, 1);
      g_bid_292 = Bid;
      g_ask_300 = Ask;
      if (!gi_388 && !gi_384) {
         gi_352 = gi_368;
         gd_356 = NormalizeDouble(Lots * MathPow(LotExponent, gi_352), lotdecimal);
         if (l_iclose_56 > l_iclose_64) {
            if (iRSI(NULL, PERIOD_H1, 14, PRICE_CLOSE, 1) > RsiMinimum) {
               gi_392 = OpenPendingOrder(1, gd_356, g_bid_292, slip, g_bid_292, 0, 0, gs_336 + "-" + gi_352, MagicNumber, 0, HotPink);
               if (gi_392 < 0) {
                  Print("Error: ", GetLastError());
                  return (0);
               }
               gd_308 = FindLastBuyPrice();
               gi_404 = TRUE;
            }
         } else {
            if (iRSI(NULL, PERIOD_H1, 14, PRICE_CLOSE, 1) < RsiMaximum) {
               gi_392 = OpenPendingOrder(0, gd_356, g_ask_300, slip, g_ask_300, 0, 0, gs_336 + "-" + gi_352, MagicNumber, 0, Lime);
               if (gi_392 < 0) {
                  Print("Error: ", GetLastError());
                  return (0);
               }
               gd_316 = FindLastSellPrice();
               gi_404 = TRUE;
            }
         }
         if (gi_392 > 0) gi_348 = TimeCurrent() + 60.0 * (60.0 * MaxTradeOpenHours);
         gi_380 = FALSE;
      }
   }
   gi_368 = CountTrades();
   g_price_284 = 0;
   double ld_80 = 0;
   for (g_pos_364 = OrdersTotal() - 1; g_pos_364 >= 0; g_pos_364--) {
      OrderSelect(g_pos_364, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         if (OrderType() == OP_BUY || OrderType() == OP_SELL) {
            g_price_284 += OrderOpenPrice() * OrderLots();
            ld_80 += OrderLots();
         }
      }
   }
   if (gi_368 > 0) g_price_284 = NormalizeDouble(g_price_284 / ld_80, Digits);
   if (gi_404) {
      for (g_pos_364 = OrdersTotal() - 1; g_pos_364 >= 0; g_pos_364--) {
         OrderSelect(g_pos_364, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
            if (OrderType() == OP_BUY) {
               g_price_252 = g_price_284 + TakeProfit * Point;
               gd_unused_268 = g_price_252;
               gd_372 = g_price_284 - g_pips_128 * Point;
               gi_332 = TRUE;
            }
         }
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
            if (OrderType() == OP_SELL) {
               g_price_252 = g_price_284 - TakeProfit * Point;
               gd_unused_276 = g_price_252;
               gd_372 = g_price_284 + g_pips_128 * Point;
               gi_332 = TRUE;
            }
         }
      }
   }
   if (gi_404) {
      if (gi_332 == TRUE) {
         for (g_pos_364 = OrdersTotal() - 1; g_pos_364 >= 0; g_pos_364--) {
            OrderSelect(g_pos_364, SELECT_BY_POS, MODE_TRADES);
            if (CountTrades() < 6)
               if (OrderSymbol() == Symbol()) OrderModify(OrderTicket(), g_price_284, OrderStopLoss(), g_price_252, 0, Yellow);
            if (CountTrades() > 3) {
               if (OrderSymbol() == Symbol()) OrderModify(OrderTicket(), g_price_284, OrderStopLoss(), NormalizeDouble(Ask - 20.0 * Point, Digits), 0, Yellow);
               if (OrderSymbol() == Symbol()) OrderModify(OrderTicket(), g_price_284, OrderStopLoss(), NormalizeDouble(Ask + 20.0 * Point, Digits), 0, Yellow);
            }
            gi_404 = FALSE;
         }
      }
   }
   return (0);
}

int CountTrades() {
   int l_count_0 = 0;
   for (int l_pos_4 = OrdersTotal() - 1; l_pos_4 >= 0; l_pos_4--) {
      OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         if (OrderType() == OP_SELL || OrderType() == OP_BUY) l_count_0++;
   }
   return (l_count_0);
}

int delete() {
   int li_0 = OrdersTotal() - 1;
   if (gi_460) {
      for (int l_pos_4 = li_0; l_pos_4 >= 0; l_pos_4--)
         if (OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES) && OrderType() <= OP_SELL && GetMarketInfo() && !OrderClose(OrderTicket(), OrderLots(), gda_452[1 - OrderType()], g_slippage_456)) Print(OrderError());
   }
   return (0);
}

string OrderError() {
   int l_error_0 = GetLastError();
   return (StringConcatenate("Order:", OrderTicket(), " GetLastError()=", l_error_0, " ", ErrorDescription(l_error_0)));
}

int GetMarketInfo() {
   RefreshRates();
   gda_452[0] = MarketInfo(OrderSymbol(), MODE_ASK);
   gda_452[1] = MarketInfo(OrderSymbol(), MODE_BID);
   double l_point_0 = MarketInfo(OrderSymbol(), MODE_POINT);
   if (l_point_0 == 0.0) return (0);
   g_slippage_456 = (gda_452[0] - gda_452[1]) / l_point_0;
   return (gda_452[0] > 0.0 && gda_452[1] > 0.0);
}

void CloseThisSymbolAll() {
   for (int l_pos_0 = OrdersTotal() - 1; l_pos_0 >= 0; l_pos_0--) {
      OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()) {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
            if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), Bid, slip, Blue);
            if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), Ask, slip, Red);
         }
         Sleep(1000);
      }
   }
}

int OpenPendingOrder(int ai_0, double a_lots_4, double a_price_12, int a_slippage_20, double ad_24, int ai_32, int ai_36, string a_comment_40, int a_magic_48, int a_datetime_52, color a_color_56) {
   int l_ticket_60 = 0;
   int l_error_64 = 0;
   int l_count_68 = 0;
   int li_72 = 100;
   switch (ai_0) {
   case 2:
      for (l_count_68 = 0; l_count_68 < li_72; l_count_68++) {
         l_ticket_60 = OrderSend(Symbol(), OP_BUYLIMIT, a_lots_4, a_price_12, a_slippage_20, StopLong(ad_24, ai_32), TakeLong(a_price_12, ai_36), a_comment_40, a_magic_48, a_datetime_52, a_color_56);
         l_error_64 = GetLastError();
         if (l_error_64 == 0/* NO_ERROR */) break;
         if (!((l_error_64 == 4/* SERVER_BUSY */ || l_error_64 == 137/* BROKER_BUSY */ || l_error_64 == 146/* TRADE_CONTEXT_BUSY */ || l_error_64 == 136/* OFF_QUOTES */))) break;
         Sleep(1000);
      }
      break;
   case 4:
      for (l_count_68 = 0; l_count_68 < li_72; l_count_68++) {
         l_ticket_60 = OrderSend(Symbol(), OP_BUYSTOP, a_lots_4, a_price_12, a_slippage_20, StopLong(ad_24, ai_32), TakeLong(a_price_12, ai_36), a_comment_40, a_magic_48, a_datetime_52, a_color_56);
         l_error_64 = GetLastError();
         if (l_error_64 == 0/* NO_ERROR */) break;
         if (!((l_error_64 == 4/* SERVER_BUSY */ || l_error_64 == 137/* BROKER_BUSY */ || l_error_64 == 146/* TRADE_CONTEXT_BUSY */ || l_error_64 == 136/* OFF_QUOTES */))) break;
         Sleep(5000);
      }
      break;
   case 0:
      RefreshRates();
      if (CountTrades() > 3) {
         g_lots_424 = Lots;
         g_lots_432 = 3;
         gd_440 = NormalizeDouble(Bid - 20.0 * Point, Digits);
         g_magic_448 = 111;
         gd_unused_76 = Ask;
      }
      if (CountTrades() < 4) {
         g_lots_424 = a_lots_4;
         g_lots_432 = a_lots_4 - Lots;
         gd_440 = StopLong(Bid, ai_32);
         g_magic_448 = 111;
      }
      for (l_count_68 = 0; l_count_68 < li_72; l_count_68++) {
         RefreshRates();
         l_ticket_60 = OrderSend(Symbol(), OP_BUY, g_lots_424, Ask, a_slippage_20, 0, NormalizeDouble(Ask + 40.0 * Point, Digits), a_comment_40, a_magic_48, a_datetime_52, a_color_56);
         l_ticket_60 = OrderSend(Symbol(), OP_SELL, g_lots_432, Ask, a_slippage_20, StopLong(Bid, ai_32), NormalizeDouble(Ask - 20.0 * Point, Digits), a_comment_40, g_magic_448, a_datetime_52, a_color_56);
         l_error_64 = GetLastError();
         if (l_error_64 == 0/* NO_ERROR */) break;
         if (!((l_error_64 == 4/* SERVER_BUSY */ || l_error_64 == 137/* BROKER_BUSY */ || l_error_64 == 146/* TRADE_CONTEXT_BUSY */ || l_error_64 == 136/* OFF_QUOTES */))) break;
         Sleep(5000);
      }
      for (l_count_68 = 0; l_count_68 < li_72; l_count_68++) {
         RefreshRates();
         g_ticket_396 = OrderSend(Symbol(), OP_SELL, g_lots_432, Ask, a_slippage_20, StopLong(Bid, ai_32), NormalizeDouble(Ask - 20.0 * Point, Digits), a_comment_40, g_magic_448, a_datetime_52, a_color_56);
         l_error_64 = GetLastError();
         if (l_error_64 == 0/* NO_ERROR */) break;
         if (!((l_error_64 == 4/* SERVER_BUSY */ || l_error_64 == 137/* BROKER_BUSY */ || l_error_64 == 146/* TRADE_CONTEXT_BUSY */ || l_error_64 == 136/* OFF_QUOTES */))) break;
         Sleep(5000);
      }
      break;
   case 3:
      for (l_count_68 = 0; l_count_68 < li_72; l_count_68++) {
         l_ticket_60 = OrderSend(Symbol(), OP_SELLLIMIT, a_lots_4 / 2.0, a_price_12, a_slippage_20, StopShort(ad_24, ai_32), TakeShort(a_price_12, ai_36), a_comment_40, a_magic_48, a_datetime_52, a_color_56);
         l_error_64 = GetLastError();
         if (l_error_64 == 0/* NO_ERROR */) break;
         if (!((l_error_64 == 4/* SERVER_BUSY */ || l_error_64 == 137/* BROKER_BUSY */ || l_error_64 == 146/* TRADE_CONTEXT_BUSY */ || l_error_64 == 136/* OFF_QUOTES */))) break;
         Sleep(5000);
      }
      break;
   case 5:
      for (l_count_68 = 0; l_count_68 < li_72; l_count_68++) {
         l_ticket_60 = OrderSend(Symbol(), OP_SELLSTOP, a_lots_4, a_price_12, a_slippage_20, StopShort(ad_24, ai_32), TakeShort(a_price_12, ai_36), a_comment_40, a_magic_48, a_datetime_52, a_color_56);
         l_error_64 = GetLastError();
         if (l_error_64 == 0/* NO_ERROR */) break;
         if (!((l_error_64 == 4/* SERVER_BUSY */ || l_error_64 == 137/* BROKER_BUSY */ || l_error_64 == 146/* TRADE_CONTEXT_BUSY */ || l_error_64 == 136/* OFF_QUOTES */))) break;
         Sleep(5000);
      }
      break;
   case 1:
      RefreshRates();
      if (CountTrades() > 3) {
         g_lots_424 = Lots;
         g_lots_432 = 3;
         g_magic_448 = 111;
         gd_440 = NormalizeDouble(Ask + 20.0 * Point, Digits);
         gd_unused_76 = Bid;
      }
      if (CountTrades() < 4) {
         g_lots_424 = a_lots_4;
         g_lots_432 = a_lots_4 - Lots;
         g_magic_448 = 111;
         gd_440 = StopShort(Ask, ai_32);
      }
      for (l_count_68 = 0; l_count_68 < li_72; l_count_68++) {
         l_ticket_60 = OrderSend(Symbol(), OP_SELL, g_lots_424, NormalizeDouble(Bid, Digits), a_slippage_20, 0, NormalizeDouble(Ask - 40.0 * Point, Digits), a_comment_40, a_magic_48, a_datetime_52, a_color_56);
         l_ticket_60 = OrderSend(Symbol(), OP_BUY, g_lots_432, NormalizeDouble(Bid, Digits), a_slippage_20, StopShort(Ask, ai_32), NormalizeDouble(Bid + 20.0 * Point, Digits), a_comment_40, g_magic_448, a_datetime_52, a_color_56);
         l_error_64 = GetLastError();
         if (l_error_64 == 0/* NO_ERROR */) break;
         if (!((l_error_64 == 4/* SERVER_BUSY */ || l_error_64 == 137/* BROKER_BUSY */ || l_error_64 == 146/* TRADE_CONTEXT_BUSY */ || l_error_64 == 136/* OFF_QUOTES */))) break;
         Sleep(5000);
      }
      for (l_count_68 = 0; l_count_68 < li_72; l_count_68++) {
         g_ticket_396 = OrderSend(Symbol(), OP_BUY, g_lots_432, NormalizeDouble(Bid, Digits), a_slippage_20, StopShort(Ask, ai_32), NormalizeDouble(Bid + 20.0 * Point, Digits), a_comment_40, g_magic_448, a_datetime_52, a_color_56);
         l_error_64 = GetLastError();
         if (l_error_64 == 0/* NO_ERROR */) break;
         if (!((l_error_64 == 4/* SERVER_BUSY */ || l_error_64 == 137/* BROKER_BUSY */ || l_error_64 == 146/* TRADE_CONTEXT_BUSY */ || l_error_64 == 136/* OFF_QUOTES */))) break;
         Sleep(5000);
      }
   }
   return (l_ticket_60);
}

double StopLong(double ad_0, int ai_8) {
   if (ai_8 == 0) return (0);
   return (ad_0 - ai_8 * Point);
}

double StopShort(double ad_0, int ai_8) {
   if (ai_8 == 0) return (0);
   return (ad_0 + ai_8 * Point);
}

double TakeLong(double ad_0, int ai_8) {
   if (ai_8 == 0) return (0);
   return (ad_0 + ai_8 * Point);
}

double TakeShort(double ad_0, int ai_8) {
   if (ai_8 == 0) return (0);
   return (ad_0 - ai_8 * Point);
}

double CalculateProfit() {
   double ld_ret_0 = 0;
   for (g_pos_364 = OrdersTotal() - 1; g_pos_364 >= 0; g_pos_364--) {
      OrderSelect(g_pos_364, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         if (OrderType() == OP_BUY || OrderType() == OP_SELL) ld_ret_0 += OrderProfit();
   }
   return (ld_ret_0);
}

void TrailingAlls(int ai_0, int ai_4, double a_price_8) {
   int li_16;
   double l_ord_stoploss_20;
   double l_price_28;
   if (ai_4 != 0) {
      for (int l_pos_36 = OrdersTotal() - 1; l_pos_36 >= 0; l_pos_36--) {
         if (OrderSelect(l_pos_36, SELECT_BY_POS, MODE_TRADES)) {
            if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
            if (OrderSymbol() == Symbol() || OrderMagicNumber() == MagicNumber) {
               if (OrderType() == OP_BUY) {
                  li_16 = NormalizeDouble((Bid - a_price_8) / Point, 0);
                  if (li_16 < ai_0) continue;
                  l_ord_stoploss_20 = OrderStopLoss();
                  l_price_28 = Bid - ai_4 * Point;
                  if (l_ord_stoploss_20 == 0.0 || (l_ord_stoploss_20 != 0.0 && l_price_28 > l_ord_stoploss_20)) OrderModify(OrderTicket(), a_price_8, l_price_28, OrderTakeProfit(), 0, Aqua);
               }
               if (OrderType() == OP_SELL) {
                  li_16 = NormalizeDouble((a_price_8 - Ask) / Point, 0);
                  if (li_16 < ai_0) continue;
                  l_ord_stoploss_20 = OrderStopLoss();
                  l_price_28 = Ask + ai_4 * Point;
                  if (l_ord_stoploss_20 == 0.0 || (l_ord_stoploss_20 != 0.0 && l_price_28 < l_ord_stoploss_20)) OrderModify(OrderTicket(), a_price_8, l_price_28, OrderTakeProfit(), 0, Red);
               }
            }
            Sleep(1000);
         }
      }
   }
}

double AccountEquityHigh() {
   if (CountTrades() == 0) gd_408 = AccountEquity();
   if (gd_408 < gd_416) gd_408 = gd_416;
   else gd_408 = AccountEquity();
   gd_416 = AccountEquity();
   return (gd_408);
}

double FindLastBuyPrice() {
   double l_ord_open_price_0;
   int l_ticket_8;
   double ld_unused_12 = 0;
   int l_ticket_20 = 0;
   for (int l_pos_24 = OrdersTotal() - 1; l_pos_24 >= 0; l_pos_24--) {
      OrderSelect(l_pos_24, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY) {
         l_ticket_8 = OrderTicket();
         if (l_ticket_8 > l_ticket_20) {
            l_ord_open_price_0 = OrderOpenPrice();
            ld_unused_12 = l_ord_open_price_0;
            l_ticket_20 = l_ticket_8;
         }
      }
   }
   return (l_ord_open_price_0);
}

double FindLastSellPrice() {
   double l_ord_open_price_0;
   int l_ticket_8;
   double ld_unused_12 = 0;
   int l_ticket_20 = 0;
   for (int l_pos_24 = OrdersTotal() - 1; l_pos_24 >= 0; l_pos_24--) {
      OrderSelect(l_pos_24, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_SELL) {
         l_ticket_8 = OrderTicket();
         if (l_ticket_8 > l_ticket_20) {
            l_ord_open_price_0 = OrderOpenPrice();
            ld_unused_12 = l_ord_open_price_0;
            l_ticket_20 = l_ticket_8;
         }
      }
   }
   return (l_ord_open_price_0);
}