/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property link      "www.TurboForexProfit.com"

extern string gp = "-- General Parameters --";
extern double Lots = 1.0;
extern double AutoLots = 1.0;
extern int MaxOrders = 1;
extern int MaxLots = 100;
extern int StopTime = 5;
extern int SlipPage = 3;
extern int OrderMagic = 8987609;
extern string eep = "-- Entry-Exit Parameters --";
extern int TakeProfit = 30;
extern int StopLoss = 0;
extern int TrailingStop = 50;
extern string hp = "-- Hedgeing Parameters --";
extern bool hedge_enable = FALSE;
extern int hedge_magic = 818181;
extern int hedge_at = 60;
extern int hedge_end = 10;
extern string tp = "-- Timimg Parameters --";
extern int StartHour = 0;
extern int StartMin = 0;
extern int StopHour = 0;
extern int StopMin = 0;
extern string ip = "-- Internal Parameters (Default) --";
extern int MAC = 1;
extern int PeMMA = 100;
extern int PrMMA = 0;
extern int PRTP = 0;
extern int TF1_MA = 40;
extern int TF2_MA = 80;
extern int TF3_MA = 160;
extern double L1 = 0.5;
extern double L2 = 0.2;
extern double L3 = 0.0;
extern int idm_var = 13;
extern int i2wp_var = 14;
int gi_256 = 1;
int g_period_260 = 100;
int g_applied_price_264 = PRICE_CLOSE;
int g_ma_method_268 = MODE_SMA;
double gd_272 = 0.5;
double gd_unused_280 = 0.2;
double gd_unused_288 = 0.0;
int g_period_296 = 13;
int g_period_300 = 14;
double g_icustom_308;
double g_icustom_316;
double g_icustom_324;
double g_digits_332;
double g_str2dbl_340;
double g_ifractals_348;
double g_ifractals_356;
double g_ima_364;
double g_ima_372;
string g_dbl2str_380;
double gda_388[];
double gda_392[];
int g_datetime_396;
int g_ticket_400 = 0;

int init() {
   HideTestIndicators(TRUE);
   if (StartHour < 0 || StartHour > 23) StartHour = 0;
   if (StartMin < 0 || StartMin > 59) StartMin = 0;
   if (StopHour < 0 || StopHour > 23) StopHour = 0;
   if (StopMin < 0 || StopMin > 59) StopMin = 0;
   return (0);
}

int deinit() {
   return (0);
}

int start() {
   int li_0;
   gi_256 = MAC;
   g_period_260 = PeMMA;
   g_applied_price_264 = PrMMA;
   g_ma_method_268 = PRTP;
   gd_272 = L1;
   gd_unused_280 = L2;
   gd_unused_288 = L3;
   g_period_296 = idm_var;
   g_period_300 = i2wp_var;
   setHedge();
   g_digits_332 = MarketInfo(Symbol(), MODE_DIGITS);
   g_dbl2str_380 = DoubleToStr(g_str2dbl_340, g_digits_332);
   g_str2dbl_340 = StrToDouble(g_dbl2str_380);
   if (!IsTradeAllowed()) return (0);
   if (!IsConnected()) return (0);
   if (IsStopped()) return (0);
   if (IsTradeContextBusy()) {
      Print("Trade context is busy!");
      return (0);
   }
   if (!IsDateTimeEnabled(TimeCurrent())) return (0);
   if (!CheckParams()) return (0);
   if (!PrepareIndicators()) return (0);
   int li_4 = CalculateCurrentOrders();
   if (li_4 > 0) {
      for (int li_8 = li_4 - 1; li_8 >= 0; li_8--) {
         if (GetOrderByPos(li_8)) {
            if (OrderMagicNumber() == OrderMagic) {
               if (TradeSignalCloseOrder() || TradeSignalCloseOrderOnTime(li_8)) CloseOrder();
               else {
                  if (OrderTakeProfit() == 0.0) {
                     if (OrderType() == OP_BUY) li_0 = (Bid - OrderOpenPrice()) / Point;
                     else li_0 = (OrderOpenPrice() - Ask) / Point;
                     if (li_0 >= TakeProfit && TakeProfit != 0.0) {
                        CloseOrder();
                        return (0);
                     }
                     if (TakeProfit > MarketInfo(Symbol(), MODE_STOPLEVEL)) ModifyOrder();
                  }
                  if (TrailingStop > 0.0) TrailOrderStop();
               }
            }
         }
      }
      if (li_4 < MaxOrders)
         if ((TimeCurrent() - g_datetime_396) / 60 >= StopTime) TrueOpen();
      return (0);
   }
   TrueOpen();
   return (0);
}

int CalculateCurrentOrders() {
   int li_ret_0;
   for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) {
      if (OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES))
         if (OrderMagicNumber() == OrderMagic && OrderSymbol() == Symbol()) li_ret_0++;
   }
   return (li_ret_0);
}

int TrueOpen() {
   if (!IsTradeTime(TimeCurrent())) return (0);
   int li_0 = TradeSignalOpenOrder();
   if (li_0 == 0) return (0);
   double ld_4 = CalcLotsVolume();
   if (ld_4 == 0.0 || !CheckAccount(DirectionOrderType(li_0), ld_4)) return (0);
   int li_12 = OpenOrder(li_0, ld_4);
   return (0);
}

bool CheckParams() {
   if (Bars < 100) {
      Print("Bars less than 100");
      return (FALSE);
   }
   if (Lots == 0.0 && AutoLots < 1.0) {
      Print("AutoLots is less than 1");
      return (FALSE);
   }
   return (TRUE);
}

bool CheckAccount(int a_cmd_0, double ad_4) {
   bool li_ret_12 = TRUE;
   double ld_16 = AccountFreeMarginCheck(Symbol(), a_cmd_0, ad_4);
   if (GetLastError() == 134/* NOT_ENOUGH_MONEY */) li_ret_12 = FALSE;
   if (!li_ret_12) Print("No money to open more orders.", " Lot=", ad_4, " Free Margin = ", AccountFreeMargin(), " Balance = ", AccountBalance());
   return (li_ret_12);
}

double CalcLotsVolume() {
   double ld_ret_0;
   double l_maxlot_8;
   double l_minlot_16;
   double l_lotstep_24;
   if (Lots > 0.0) ld_ret_0 = NormalizeLot(Lots, 0, "");
   else {
      l_maxlot_8 = MarketInfo(Symbol(), MODE_MAXLOT);
      l_minlot_16 = MarketInfo(Symbol(), MODE_MINLOT);
      l_lotstep_24 = MarketInfo(Symbol(), MODE_LOTSTEP);
      ld_ret_0 = NormalizeDouble(MathFloor(AccountFreeMargin() * AutoLots / 100.0 / (MarketInfo(Symbol(), MODE_MARGINREQUIRED) * l_lotstep_24)) * l_lotstep_24, 2);
      if (ld_ret_0 < l_minlot_16) ld_ret_0 = l_minlot_16;
      if (ld_ret_0 > l_maxlot_8) ld_ret_0 = l_maxlot_8;
   }
   if (ld_ret_0 > MaxLots) ld_ret_0 = MaxLots;
   return (ld_ret_0);
}

bool PrepareIndicators() {
   double l_ifractals_0;
   g_icustom_308 = iCustom(NULL, 0, "Alligator", 13, 8, 8, 5, 5, 3, 0, 0);
   if (IsError("Indicator")) return (FALSE);
   g_icustom_316 = iCustom(NULL, 0, "Alligator", 13, 8, 8, 5, 5, 3, 1, 0);
   if (IsError("Indicator")) return (FALSE);
   g_icustom_324 = iCustom(NULL, 0, "Alligator", 13, 8, 8, 5, 5, 3, 2, 0);
   if (IsError("Indicator")) return (FALSE);
   int li_8 = 3;
   g_ifractals_348 = 0;
   g_ifractals_356 = 0;
   for (int li_12 = 0; li_12 <= li_8; li_12++) {
      l_ifractals_0 = iFractals(NULL, 0, MODE_LOWER, li_12);
      if (IsError("Lower Fractals")) return (FALSE);
      if (l_ifractals_0 != 0.0) g_ifractals_348 = l_ifractals_0;
      l_ifractals_0 = iFractals(NULL, 0, MODE_UPPER, li_12);
      if (IsError("Upper Fractals")) return (FALSE);
      if (l_ifractals_0 != 0.0) g_ifractals_356 = l_ifractals_0;
   }
   li_8 = 0;
   ArrayResize(gda_388, li_8 + 1);
   ArrayResize(gda_392, li_8 + 1);
   for (li_12 = 0; li_12 <= li_8; li_12++) {
      gda_388[li_12] = iDeMarker(NULL, 0, g_period_296, li_12);
      if (IsError("DeMarker")) return (FALSE);
      gda_392[li_12] = (-iWPR(NULL, 0, g_period_300, li_12 + 1)) / 100.0;
      if (IsError("WPR")) return (FALSE);
   }
   if (gi_256 != 0) {
      g_ima_364 = iMA(NULL, PERIOD_D1, g_period_260, 0, g_ma_method_268, g_applied_price_264, 1);
      g_ima_372 = iMA(NULL, PERIOD_D1, g_period_260, 0, g_ma_method_268, g_applied_price_264, 0);
   }
   return (TRUE);
}

int TradeSignalOpenOrder() {
   if (gi_256 == 0)
      if (!IsGatorActiveUp()) return (0);
   if (g_ima_364 < g_ima_372)
      if (!IsGatorActiveUp()) return (0);
   if (g_ima_364 > g_ima_372)
      if (!IsGatorActiveDown()) return (0);
   if (!IsGatorActiveUp() && !IsGatorActiveDown()) return (0);
   if (WasWPROverBuy() || WasWPROverSell()) return (0);
   if (IsFractalLower() && WasDemarkerHigh()) return (1);
   if (IsFractalUpper() && WasDemarkerLow()) return (-1);
   return (0);
}

int TradeSignalCloseOrder() {
   return (!IsOrderProfitable());
}

double NormalizeLot(double ad_0, bool ai_8 = FALSE, string a_symbol_12 = "") {
   double ld_ret_20;
   double ld_28;
   if (a_symbol_12 == "" || a_symbol_12 == "0") a_symbol_12 = Symbol();
   double l_lotstep_36 = MarketInfo(a_symbol_12, MODE_LOTSTEP);
   double l_minlot_44 = MarketInfo(a_symbol_12, MODE_MINLOT);
   double l_maxlot_52 = MarketInfo(a_symbol_12, MODE_MAXLOT);
   if (l_minlot_44 == 0.0) l_minlot_44 = 0.1;
   if (l_maxlot_52 == 0.0) l_maxlot_52 = 100;
   if (l_lotstep_36 > 0.0) ld_28 = 1 / l_lotstep_36;
   else ld_28 = 1 / l_minlot_44;
   if (ai_8) ld_ret_20 = MathCeil(ad_0 * ld_28) / ld_28;
   else ld_ret_20 = MathFloor(ad_0 * ld_28) / ld_28;
   if (ld_ret_20 < l_minlot_44) ld_ret_20 = l_minlot_44;
   if (ld_ret_20 > l_maxlot_52) ld_ret_20 = l_maxlot_52;
   return (ld_ret_20);
}

int ModifyOrder() {
   int li_0 = OrderTypeDirection();
   Print("TakeProfit value is not set due to server connection error. Please modify the order and set it manually");
   double l_price_4 = g_str2dbl_340 + TakeProfit * Point * li_0;
   OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), l_price_4, 0, Red);
   return (0);
}

int TradeSignalCloseOrderOnTime(int ai_unused_0) {
   if (TimeMonth(OrderOpenTime()) != TimeMonth(TimeCurrent())) return (1);
   return (0);
}

bool IsDateTimeEnabled(int ai_0) {
   int l_day_of_year_4 = TimeDayOfYear(ai_0);
   int l_day_of_week_8 = TimeDayOfWeek(ai_0);
   return (l_day_of_week_8 > 0 && l_day_of_week_8 < 6 && l_day_of_year_4 > 7 && l_day_of_year_4 < 360);
}

bool IsGatorActiveUp() {
   return (g_icustom_324 - g_icustom_316 >= gd_272 * Point && g_icustom_316 - g_icustom_308 >= gd_272 * Point && g_icustom_324 - g_icustom_308 >= gd_272 * Point);
}

bool IsGatorActiveDown() {
   return (g_icustom_316 - g_icustom_324 >= gd_272 * Point && g_icustom_308 - g_icustom_316 >= gd_272 * Point && g_icustom_308 - g_icustom_324 >= gd_272 * Point);
}

int IsFractalLower() {
   return (g_ifractals_348 != 0.0);
}

int IsFractalUpper() {
   return (g_ifractals_356 != 0.0);
}

bool IsOrderProfitable() {
   return (TRUE);
}

int WasDemarkerLow() {
   return (ArrayMinValue(gda_388) < 0.5);
}

int WasDemarkerHigh() {
   return (ArrayMaxValue(gda_388) > 0.5);
}

int WasWPROverBuy() {
   return (ArrayMinValue(gda_392) <= 0.25);
}

int WasWPROverSell() {
   return (ArrayMaxValue(gda_392) >= 0.75);
}

int OpenOrder(int ai_0, double a_lots_4, string as_unused_12 = "") {
   int li_20;
   double l_price_24 = 0;
   double l_price_32 = 0;
   double l_price_40 = PriceOpen(ai_0);
   l_price_32 = l_price_40 + TakeProfit * Point * ai_0;
   if (StopLoss > 0.0) l_price_24 = PriceClose(ai_0) - StopLoss * Point * ai_0;
   int l_ticket_48 = OrderSend(Symbol(), DirectionOrderType(ai_0), a_lots_4, l_price_40, SlipPage, 0, 0, "TFP", OrderMagic, 0, ColorOpen(ai_0));
   Sleep(1000);
   if (l_ticket_48 > 0) {
      li_20 = MarketInfo(Symbol(), MODE_STOPLEVEL);
      if (TakeProfit > li_20 || TakeProfit == 0.0)
         if (StopLoss > li_20 || StopLoss == 0.0) OrderModify(l_ticket_48, l_price_40, l_price_24, l_price_32, 0, Red);
   }
   g_datetime_396 = iTime(Symbol(), 0, 0);
   return (0);
}

void CloseOrder() {
   Print("Closing!");
   OrderClose(OrderTicket(), OrderLots(), Bid, SlipPage, Red);
}

bool GetOrderByPos(int a_pos_0) {
   return (OrderSelect(a_pos_0, SELECT_BY_POS, MODE_TRADES) && OrderType() <= OP_SELL && OrderSymbol() == Symbol());
}

void TrailOrderStop() {
   int li_0;
   double ld_4;
   double ld_12;
   double l_price_20;
   double ld_28;
   double ld_36;
   int li_44;
   if (OrderTicket() > 0) {
      li_0 = OrderTypeDirection();
      ld_4 = NormalizeDouble(TrailingStop * Point * li_0, Digits);
      ld_12 = NormalizeDouble(iif(li_0 > 0 || OrderStopLoss() != 0.0, OrderStopLoss(), 999999), Digits);
      l_price_20 = NormalizeDouble(PriceClose(li_0) - ld_4, Digits);
      ld_28 = NormalizeDouble(l_price_20 - OrderOpenPrice(), Digits);
      ld_36 = NormalizeDouble(l_price_20 - ld_12, Digits);
      if (ld_28 * li_0 > 0.0 && ld_36 * li_0 >= Point) {
         if (OrderType() == OP_BUY) li_44 = (Bid - l_price_20) / Point;
         else li_44 = (l_price_20 - Ask) / Point;
         if (li_44 > MarketInfo(Symbol(), MODE_STOPLEVEL)) {
            OrderModify(OrderTicket(), OrderOpenPrice(), l_price_20, OrderTakeProfit(), 0, ColorOpen(li_0));
            if (GetLastError() != 0/* NO_ERROR */) {
            }
         }
      }
   }
}

int OrderTypeDirection() {
   int li_ret_0 = 0;
   if (OrderType() == OP_BUY) li_ret_0 = 1;
   if (OrderType() == OP_SELL) li_ret_0 = -1;
   return (li_ret_0);
}

int DirectionOrderType(int ai_0) {
   return (iif(ai_0 > 0, 0, 1));
}

int ColorOpen(int ai_0) {
   return (iif(ai_0 > 0, 32768, 255));
}

double PriceOpen(int ai_0) {
   return (iif(ai_0 > 0, Ask, Bid));
}

double PriceClose(int ai_0) {
   return (iif(ai_0 > 0, Bid, Ask));
}

double iif(bool ai_0, double ad_4, double ad_12) {
   if (ai_0) return (ad_4);
   return (ad_12);
}

bool IsError(string as_0 = "Raptor V1") {
   int l_error_8 = GetLastError();
   bool li_ret_12 = l_error_8 > 1/* NO_RESULT */;
   if (li_ret_12) Print(as_0, " error = ", l_error_8, "; desc = ", ErrorDescription(l_error_8));
   return (li_ret_12);
}

string ErrorDescription(int ai_0) {
   string ls_ret_4;
   switch (ai_0) {
   case 0:
   case 1:
      ls_ret_4 = "no error";
      break;
   case 2:
      ls_ret_4 = "common error";
      break;
   case 3:
      ls_ret_4 = "invalid trade parameters";
      break;
   case 4:
      ls_ret_4 = "trade server is busy";
      break;
   case 5:
      ls_ret_4 = "old version of the client terminal";
      break;
   case 6:
      ls_ret_4 = "no connection with trade server";
      break;
   case 7:
      ls_ret_4 = "not enough rights";
      break;
   case 8:
      ls_ret_4 = "too frequent requests";
      break;
   case 9:
      ls_ret_4 = "malfunctional trade operation (never returned error)";
      break;
   case 64:
      ls_ret_4 = "account disabled";
      break;
   case 65:
      ls_ret_4 = "invalid account";
      break;
   case 128:
      ls_ret_4 = "trade timeout";
      break;
   case 129:
      ls_ret_4 = "invalid price";
      break;
   case 130:
      ls_ret_4 = "invalid stops";
      break;
   case 131:
      ls_ret_4 = "invalid trade volume";
      break;
   case 132:
      ls_ret_4 = "market is closed";
      break;
   case 133:
      ls_ret_4 = "trade is disabled";
      break;
   case 134:
      ls_ret_4 = "not enough money";
      break;
   case 135:
      ls_ret_4 = "price changed";
      break;
   case 136:
      ls_ret_4 = "off quotes";
      break;
   case 137:
      ls_ret_4 = "broker is busy (never returned error)";
      break;
   case 138:
      ls_ret_4 = "requote";
      break;
   case 139:
      ls_ret_4 = "order is locked";
      break;
   case 140:
      ls_ret_4 = "long positions only allowed";
      break;
   case 141:
      ls_ret_4 = "too many requests";
      break;
   case 145:
      ls_ret_4 = "modification denied because order too close to market";
      break;
   case 146:
      ls_ret_4 = "trade context is busy";
      break;
   case 147:
      ls_ret_4 = "expirations are denied by broker";
      break;
   case 148:
      ls_ret_4 = "amount of open and pending orders has reached the limit";
      break;
   case 4000:
      ls_ret_4 = "no error (never generated code)";
      break;
   case 4001:
      ls_ret_4 = "wrong function pointer";
      break;
   case 4002:
      ls_ret_4 = "array index is out of range";
      break;
   case 4003:
      ls_ret_4 = "no memory for function call stack";
      break;
   case 4004:
      ls_ret_4 = "recursive stack overflow";
      break;
   case 4005:
      ls_ret_4 = "not enough stack for parameter";
      break;
   case 4006:
      ls_ret_4 = "no memory for parameter string";
      break;
   case 4007:
      ls_ret_4 = "no memory for temp string";
      break;
   case 4008:
      ls_ret_4 = "not initialized string";
      break;
   case 4009:
      ls_ret_4 = "not initialized string in array";
      break;
   case 4010:
      ls_ret_4 = "no memory for array\' string";
      break;
   case 4011:
      ls_ret_4 = "too long string";
      break;
   case 4012:
      ls_ret_4 = "remainder from zero divide";
      break;
   case 4013:
      ls_ret_4 = "zero divide";
      break;
   case 4014:
      ls_ret_4 = "unknown command";
      break;
   case 4015:
      ls_ret_4 = "wrong jump (never generated error)";
      break;
   case 4016:
      ls_ret_4 = "not initialized array";
      break;
   case 4017:
      ls_ret_4 = "dll calls are not allowed";
      break;
   case 4018:
      ls_ret_4 = "cannot load library";
      break;
   case 4019:
      ls_ret_4 = "cannot call function";
      break;
   case 4020:
      ls_ret_4 = "expert function calls are not allowed";
      break;
   case 4021:
      ls_ret_4 = "not enough memory for temp string returned from function";
      break;
   case 4022:
      ls_ret_4 = "system is busy (never generated error)";
      break;
   case 4050:
      ls_ret_4 = "invalid function parameters count";
      break;
   case 4051:
      ls_ret_4 = "invalid function parameter value";
      break;
   case 4052:
      ls_ret_4 = "string function internal error";
      break;
   case 4053:
      ls_ret_4 = "some array error";
      break;
   case 4054:
      ls_ret_4 = "incorrect series array using";
      break;
   case 4055:
      ls_ret_4 = "custom indicator error";
      break;
   case 4056:
      ls_ret_4 = "arrays are incompatible";
      break;
   case 4057:
      ls_ret_4 = "global variables processing error";
      break;
   case 4058:
      ls_ret_4 = "global variable not found";
      break;
   case 4059:
      ls_ret_4 = "function is not allowed in testing mode";
      break;
   case 4060:
      ls_ret_4 = "function is not confirmed";
      break;
   case 4061:
      ls_ret_4 = "send mail error";
      break;
   case 4062:
      ls_ret_4 = "string parameter expected";
      break;
   case 4063:
      ls_ret_4 = "integer parameter expected";
      break;
   case 4064:
      ls_ret_4 = "double parameter expected";
      break;
   case 4065:
      ls_ret_4 = "array as parameter expected";
      break;
   case 4066:
      ls_ret_4 = "requested history data in update state";
      break;
   case 4099:
      ls_ret_4 = "end of file";
      break;
   case 4100:
      ls_ret_4 = "some file error";
      break;
   case 4101:
      ls_ret_4 = "wrong file name";
      break;
   case 4102:
      ls_ret_4 = "too many opened files";
      break;
   case 4103:
      ls_ret_4 = "cannot open file";
      break;
   case 4104:
      ls_ret_4 = "incompatible access to a file";
      break;
   case 4105:
      ls_ret_4 = "no order selected";
      break;
   case 4106:
      ls_ret_4 = "unknown symbol";
      break;
   case 4107:
      ls_ret_4 = "invalid price parameter for trade function";
      break;
   case 4108:
      ls_ret_4 = "invalid ticket";
      break;
   case 4109:
      ls_ret_4 = "trade is not allowed in the expert properties";
      break;
   case 4110:
      ls_ret_4 = "longs are not allowed in the expert properties";
      break;
   case 4111:
      ls_ret_4 = "shorts are not allowed in the expert properties";
      break;
   case 4200:
      ls_ret_4 = "object is already exist";
      break;
   case 4201:
      ls_ret_4 = "unknown object property";
      break;
   case 4202:
      ls_ret_4 = "object is not exist";
      break;
   case 4203:
      ls_ret_4 = "unknown object type";
      break;
   case 4204:
      ls_ret_4 = "no object name";
      break;
   case 4205:
      ls_ret_4 = "object coordinates error";
      break;
   case 4206:
      ls_ret_4 = "no specified subwindow";
      break;
   default:
      ls_ret_4 = "unknown error";
   }
   return (ls_ret_4);
}

double ArrayMinValue(double ada_0[]) {
   return (ada_0[ArrayMinimum(ada_0)]);
}

double ArrayMaxValue(double ada_0[]) {
   return (ada_0[ArrayMaximum(ada_0)]);
}

bool IsTradeTime(int ai_0) {
   int li_4;
   int li_8;
   int l_hour_12 = TimeHour(ai_0);
   int l_minute_16 = TimeMinute(ai_0);
   if (StartHour == StopHour && StartMin == StopMin) return (TRUE);
   if (StopMin == 0) {
      if (StopHour == 0) li_4 = 23;
      else li_4 = StopHour - 1;
      li_8 = 59;
   } else {
      li_4 = StopHour;
      li_8 = StopMin - 1;
   }
   if (StartHour == li_4) {
      if (StartMin == li_8) return (TRUE);
      if (StartMin < li_8) {
         if (l_hour_12 != StartHour) return (FALSE);
         if (l_minute_16 < StartMin || l_minute_16 > li_8) return (FALSE);
         return (TRUE);
      }
      if (StartMin > li_8) {
         if (l_hour_12 == StartHour)
            if (l_minute_16 < StartMin && l_minute_16 > li_8) return (FALSE);
         return (TRUE);
      }
   }
   if (StartHour < li_4) {
      if (l_hour_12 < StartHour || l_hour_12 > li_4) return (FALSE);
      if (l_hour_12 == StartHour && l_minute_16 < StartMin) return (FALSE);
      if (l_hour_12 == li_4 && l_minute_16 > li_8) return (FALSE);
      return (TRUE);
   }
   if (StartHour > li_4) {
      if (l_hour_12 < StartHour && l_hour_12 > li_4) return (FALSE);
      if (l_hour_12 == StartHour && l_minute_16 < StartMin) return (FALSE);
      if (l_hour_12 == li_4 && l_minute_16 > li_8) return (FALSE);
      return (TRUE);
   }
   return (TRUE);
}

int setHedge() {
   double l_price_8;
   double l_price_16;
   if (!hedge_enable) return (0);
   int l_ord_lots_0 = 0;
   int l_pos_4 = 0;
   for (l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) {
      if (OrderSelect(l_pos_4, SELECT_BY_POS)) {
         if (OrderCloseTime() == 0 && OrderMagicNumber() == OrderMagic && OrderType() == OP_SELL) {
            if (Ask > OrderOpenPrice() + hedge_at * Point) {
               l_ord_lots_0 = OrderLots();
               if ((OrderSelect(g_ticket_400, SELECT_BY_TICKET) && OrderCloseTime() != 0) || OrderSelect(g_ticket_400, SELECT_BY_TICKET) == FALSE) {
                  l_price_8 = Bid - hedge_end * Point;
                  g_ticket_400 = OrderSend(Symbol(), OP_BUY, l_ord_lots_0, Ask, SlipPage, l_price_8, 0, "Hedge Trade", hedge_magic, 0, Black);
               }
            }
         }
      }
      if (OrderSelect(l_pos_4, SELECT_BY_POS)) {
         if (OrderCloseTime() == 0 && OrderMagicNumber() == OrderMagic && OrderType() == OP_BUY) {
            if (Bid < OrderOpenPrice() - hedge_at * Point) {
               l_ord_lots_0 = OrderLots();
               if ((OrderSelect(g_ticket_400, SELECT_BY_TICKET) && OrderCloseTime() != 0) || OrderSelect(g_ticket_400, SELECT_BY_TICKET) == FALSE) {
                  l_price_16 = Ask + hedge_end * Point;
                  g_ticket_400 = OrderSend(Symbol(), OP_SELL, l_ord_lots_0, Bid, SlipPage, l_price_16, 0, "Hedge Trade", hedge_magic, 0, Black);
               }
            }
         }
      }
   }
   if (OrderSelect(g_ticket_400, SELECT_BY_TICKET)) {
      if (OrderCloseTime() == 0) {
         if (OrderType() == OP_BUY) {
            if (Bid - OrderOpenPrice() > Point * hedge_end)
               if (OrderStopLoss() < Bid - Point * hedge_end) OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * hedge_end, OrderTakeProfit(), 0, Black);
         }
         if (OrderType() == OP_SELL) {
            if (OrderOpenPrice() - Ask > Point * hedge_end)
               if (OrderStopLoss() > Ask + Point * hedge_end || OrderStopLoss() == 0.0) OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * hedge_end, OrderTakeProfit(), 0, Black);
         }
      }
   }
   return (0);
}
