/*
   Generated by EX4-TO-MQ4 decompiler V4.0.220.1a
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "steinitz@cox.net"
#property link      "steinitz@cox.net"

#include <stdlib.mqh>

int gi_76 = 12345;
int YourAccountNumber = 12345;
bool gi_84 = FALSE;
int gi_88 = 9;
int gi_92 = 10;
int gi_96 = 2007;
extern string MTF_HAS_METHOD = "Inventor:DON STEINITZ";
extern string PROGRAMMERS = "LEE YAN, Robert Hill";
extern string ContackUS = "steinitz@cox.net ; lee_yan_cn@126.com";
extern int MagicBase = 20000;
extern string cm = "---User Comment---";
extern string UserComment = "HAS_Hedge";
extern string HedgeUserComment = "HAS_Hedge1";
extern string has = "---HAS Inputs---";
extern int MaMethod = 2;
extern int MaPeriod = 6;
extern int MaMethod2 = 3;
extern int MaPeriod2 = 2;
extern string tf0 = "---TimeFrames---";
extern string tf1 = "1. Month";
extern string tf2 = "2. Week";
extern string tf3 = "3. Day";
extern string tf4 = "4. 4 Hour";
extern string tf5 = "5. 1 Hour";
extern string tf6 = "6. 30 minute";
extern string tf7 = "7. 15 minute";
extern string tf8 = "8. 5 minute";
extern int HigherTF_Set1 = 1;
extern int LowerTF_Set1 = 8;
extern int UpperTF_Set1 = 1;
extern string ss = "---Set 2 inputs---";
extern string ss1 = " Switch inputs when";
extern string ss2 = " UpperTF to SwitchTF do not agree";
extern string ss3 = "Enter 0 for false, 1 for true";
extern int UseSwitchSet = 1;
extern int SwitchSetTF = 3;
extern int HigherTF_Set2 = 4;
extern int LowerTF_Set2 = 8;
extern string ct = "---Trade Confirmation filters---";
extern string ct1 = " Use timeframe above HigherTF to confirm trade";
extern string ct2 = "Enter 0 for false, 1 for true";
extern int ConfirmUpperTimeFrame_Set1 = 1;
extern int ConfirmUpperTimeFrame_Set2 = 1;
extern string ct3 = "Confirm Higher to Upper Timeframes for change of trend";
extern string ct4 = "Enter 0 for false, 1 for true";
extern int ConfirmAllTimeFrames_Set1 = 1;
extern int ConfirmAllTimeFrames_Set2 = 1;
extern string ct5 = "Confirm Alert Timeframe for change of trend";
extern string ct6 = "Enter 0 for false, 1 for true";
extern int ConfirmAlertTimeFrame = 1;
extern string en0 = "--- Timeframe Entry---";
extern string en1 = "1. Enter on Alerted time frame - 1 unclosed candle";
extern string en2 = "2. Enter on Alerted time frame - 1 closed candle";
extern int EnterOnAlerted = 1;
extern string hen = "--- HedgeTimeframe Entry---";
extern string hen0 = "0. Do Not Use Hedge";
extern string hen1 = "1. Enter on Alerted time frame - 1";
extern string hen2 = "2. Enter on Alerted time frame";
extern int HedgeEnterOnAlerted = 0;
extern string en3 = "Fresh Signal - Enter 0 for false, 1 for true";
extern int EnterOnFreshSignal = 0;
extern int Slippage = 3;
extern string ep = "--- Profit Timeframe Exit---";
extern string ep0 = "0. Do Not Use";
extern string ep1 = "1. Alert Timeframe - 1 unclosed candle";
extern string ep2 = "2. Alert Timeframe - 1 closed candle";
extern string ep3 = "3. Alert Timeframe unclosed candle";
extern string ep4 = "4. Alert Timeframe closed candle";
extern string ep5 = "5. Exit at Take Profit";
extern int ExitOnAlertedProfit_Set1 = 5;
extern int ExitOnAlertedProfit_Set2 = 5;
extern int HedgeExitOnAlertedProfit = 1;
extern string e5 = "--- Partial Close---";
extern string e6 = "When true will close half of position";
extern string e7 = "when internal TakeProfit or Trigger is reached";
extern string e8 = "Enter 0 for false, 1 for true";
extern int UsePartialClose_Set1 = 0;
extern int UsePartialClose_Set2 = 0;
bool gi_572 = FALSE;
bool gi_576 = FALSE;
bool gi_580 = FALSE;
extern string e9 = "Roundup - Enter 0 for false, 1 for true";
extern int RoundUp = 0;
extern string ep6 = " Enter trigger to replace TP";
extern string ep7 = " for alert exit, partial close";
extern string ep8 = " and Trailing Stop";
extern string ep9 = "UseTrigger - Enter 0 for false, 1 for true";
extern int UseTrigger_Set1 = 1;
extern int ExitTrigger_Set1 = 25;
extern int UseTrigger_Set2 = 1;
extern int ExitTrigger_Set2 = 20;
bool gi_644 = FALSE;
extern int HedgeUseTrigger = 1;
extern int HedgeExitTrigger = 10;
bool gi_656 = FALSE;
extern string set = "---Exit at Set Change Signal---";
extern int UseExitOnSetChangeSignal = 0;
extern string hd = " --Delay time after small loss--";
extern int useDelayAfterLoss = 1;
extern int LossSizeInPips = 10;
extern int MinutesToDelay = 30;
extern string ts0 = "---TrailingStopLoss---";
extern string ts1 = " 1. None";
extern string ts2 = " 2. BreakEven at TP or Trigger";
extern string ts3 = " 3. Standard at TP or Trigger";
extern string ts6 = " 4. Breakeven at input";
extern string ts5 = " 5. Standard at input";
extern string ts4 = " 6. Trail immediately";
extern string ts7 = " 7. Immediately til Breakeven + Lockin";
extern int TrailingStopMethod_Set1 = 2;
extern int TrailingStopMethod_Set2 = 2;
extern int HedgeTrailingStopMethod = 2;
extern string ts8 = "2. BreakEven at TP/Trigger setting";
extern int BreakEvenLock_Set1 = 20;
extern int BreakEvenLock_Set2 = 15;
extern int HedgeBreakEvenLock = 5;
extern string ts9 = "3. Standard at TP/Trigger setting";
extern int TrailingStop = 25;
extern int HedgeTrailingStop = 25;
extern string ts11 = "4. Breakeven at input settings";
extern double BreakEvenLevel = 30.0;
extern int LockInPips = 5;
extern double HedgeBreakEvenLevel = 10.0;
extern int HedgeLockInPips = 1;
extern string ts10 = "5. Standard at input setting";
extern double BeginTrailingStop = 35.0;
extern double HedgeBeginTrailingStop = 15.0;
extern string tp0 = "---TakeProfit Method---";
extern string tp1 = " 1. Use ATR";
extern string tp2 = " 2. Use Inputs";
extern int TPMethod = 2;
extern string tp3 = "ATR inputs";
extern int TPAtrPeriod = 14;
extern double TPAtrPercentage = 0.9;
extern string tp4 = "TakeProfit inputs";
extern int TakeProfitW1_Set1 = 200;
extern int TakeProfitD1_Set1 = 150;
extern int TakeProfitH4_Set1 = 100;
extern int TakeProfitH1_Set1 = 75;
extern int TakeProfitM30_Set1 = 50;
extern int TakeProfitM15_Set1 = 25;
extern int TakeProfitM5_Set1 = 15;
extern int TakeProfitW1_Set2 = 200;
extern int TakeProfitD1_Set2 = 150;
extern int TakeProfitH4_Set2 = 100;
extern int TakeProfitH1_Set2 = 75;
extern int TakeProfitM30_Set2 = 50;
extern int TakeProfitM15_Set2 = 25;
extern int TakeProfitM5_Set2 = 15;
extern int HedgeTakeProfit = 20;
extern double SafetyTakeProfitMultiplier = 1.5;
int g_global_var_984;
int gi_988;
extern string tms6 = "Step to move Take Profit";
extern int TP_Step = 20;
int g_global_var_1004;
int gi_1008;
extern string sl0 = "---StopLoss Method---";
extern string sl1 = " 1. Use ATR";
extern string sl2 = " 2. Use Input";
extern int SLMethod = 2;
extern string sl3 = "ATR inputs";
extern int SLAtrPeriod = 14;
extern double SLAtrPercentage = 0.9;
extern int mStopLoss_Set1 = 0;
extern int mStopLoss_Set2 = 0;
extern int mHedgeStopLoss = 0;
extern double SafetyStopLossMultiplier = 1.5;
int gi_1080;
int gi_1084;
int gi_1088;
int gi_1092;
extern string mm = "---Money Management---";
extern double Lots = 0.1;
extern int HedgeMultiplier = 1;
extern bool UseMoneyManagement = TRUE;
extern bool BrokerIsIBFX = FALSE;
extern bool BrokerIsCrownForex = FALSE;
extern string m1 = "Set mini and micro to false for standard account";
extern bool AccountIsMini = TRUE;
extern bool AccountIsMicro = FALSE;
extern double TradeSizePercent = 1.0;
extern bool BrokerPermitsFractionalLots = TRUE;
extern string sp0 = "---Large Spread Filter---";
extern bool UseSpreadDetection = TRUE;
extern int MaxSpread = 8;
extern string ex1 = " Close trades after X Days";
extern string ex2 = "Enter 0 for do not use";
extern int CloseTradeAfterX_Days = 0;
string gs_unused_1192 = " Close trades at End Of Month";
string gs_unused_1200 = "Enter 0 for false, 1 for true";
int gi_1208 = 0;
int gi_1212 = 0;
int gi_1216 = 5;
extern string ef1 = " Close trades on Friday";
extern string ef2 = "Enter 0 for false, 1 for true";
extern int UseFridayClose = 0;
extern int FridayCloseHour = 21;
extern string ef3 = "Friday No Trade - Enter 0 for false, 1 for true";
extern int UseFridayNoTradeTime = 0;
extern int FridayFinalHour = 20;
int gi_1260;
int gi_1264;
extern string sm0 = "--Trading Hours Filter--";
extern string sm2 = "UseTradingHours - Enter 0 for false, 1 for true";
extern int UseTradingHours = 0;
int gi_1288 = 0;
extern bool UseDST = TRUE;
extern string sm4 = "TradeAsian - Enter 0 for false, 1 for true";
extern int TradeAsianMarket = 1;
extern int myAsianStart = 100;
extern int myAsianStop = 400;
extern string sm5 = "Trade Europe - Enter 0 for false, 1 for true";
extern int TradeEuropeanMarket = 1;
extern int myEurStart = 1000;
extern int myEurStop = 1200;
extern string sm6 = "Trade NY - Enter 0 for false, 1 for true";
extern int TradeNewYorkMarket = 1;
extern int myNYStart = 1600;
extern int myNYStop = 1800;
extern string to = "---Text Object Settings---";
extern int StatusTxtSize = 10;
extern color StatusColor = White;
extern int CommentTxtSize = 10;
extern color CommentColor = White;
extern int TradeSetTxtSize = 10;
extern color TradeSetColor = White;
extern int TrendTxtSize = 10;
extern color TrendColor = White;
extern int LookingTxtSize = 10;
extern color LookingColor = White;
extern int FoundTxtSize = 10;
extern color FoundColor = White;
extern int WaitingTxtSize = 10;
extern color WaitingColor = White;
extern int ConditionTxtSize = 10;
extern color ConditionColor = White;
extern int TradeTxtSize = 10;
extern color TradeColor = White;
extern int DrawDownTxtSize = 10;
extern color DrawDownColor = White;
extern color MarkColor = Red;
int gi_1448;
int gi_1452;
int gi_1456;
int gi_1460;
int gi_1464;
int gi_1468;
int gi_1472;
int gi_1476;
int gi_1480;
int gi_1484;
int gi_1488;
int gi_1492;
int gi_1496;
int gi_1500;
int gi_1504;
string gs_1508;
string gs_1516;
string gs_1524;
string gs_1532;
string gs_1540;
string gs_1548;
string gs_1556;
string gs_1564;
string gs_1572;
string gs_1580;
string gs_1588;
string gs_1596;
string gs_1604;
string gs_1612;
string gs_1620;
string g_name_1628;
extern string db = "--- Debug file settings---";
extern bool Debug = FALSE;
extern bool Debug_Expert = FALSE;
extern bool Debug_LiveTrade = FALSE;
extern string Trade_Log = "_MTF_HAS_TradeLog";
extern bool Debug_BackTestTrade = FALSE;
extern string BT_Trade_Log = "_MTF_HAS_BTTradeLog";
bool gi_1676 = TRUE;
bool gi_1680 = TRUE;
int gi_1684;
int gi_1688;
int gi_1692;
bool gi_unused_1696 = FALSE;
int g_global_var_1700 = 0;
int g_global_var_1704 = 0;
int gi_1708 = 0;
int gi_1712;
bool g_global_var_1716;
int g_global_var_1720 = 0;
int g_global_var_1724 = 0;
bool g_global_var_1728;
int gi_1732;
int gi_1736;
int gi_1740;
string g_name_1744;
string gs_dummy_1752;
string gs_1760 = "MTF HAS Status";
double g_icustom_1768;
double g_icustom_1776;
double gd_1784;
double gd_1792;
double gd_1800;
double gd_1808;
int g_ticket_1816;
int g_ticket_1820;
int gi_1824;
int gi_1828;
int gi_1832;
int gi_1836;
int gi_1840;
int gi_1844;
int g_global_var_1848 = 1;
int gi_1852;
int gi_1856;
int gi_1860;
int gi_1864;
int gi_1868;
int gi_1872;
int gi_1876;
int gi_1880;
int gi_1884;
int gi_1888;
int gi_1892;
int gi_1896;
int gi_1900;
int gi_1904;
int gi_1908;
int gi_1912;
int gi_1916;
int gi_1920;
int gi_1924;
double gd_1928;
int g_magic_1936;
int g_magic_1940;
string g_var_name_1944;
string g_var_name_1952;
string g_var_name_1960;
string g_var_name_1968;
string g_var_name_1976;
string g_var_name_1984;
string g_var_name_1992;
string g_var_name_2000;
string g_var_name_2008;
string g_var_name_2016;
string g_var_name_2024;
string g_var_name_2032;
string g_var_name_2040;
string g_var_name_2048;
string g_var_name_2056;
string g_var_name_2064;
string g_var_name_2072;
string g_var_name_2080;
string g_var_name_2088;
string g_var_name_2096;
string g_var_name_2104;
string g_var_name_2112;
string g_var_name_2120;
string g_var_name_2128;
string g_var_name_2136;
string g_var_name_2144;
string g_var_name_2152;
string g_var_name_2160;
bool gi_2168 = FALSE;
double g_global_var_2172;
double gd_2180;
double g_global_var_2188;
double gd_2196;
int gi_2204 = 5;
int gi_2208 = 1000;
int gia_2212[9] = {43200, 10080, 1440, 240, 60, 30, 15, 5, 1};
string gs_2216 = "";

int init() {
   int li_0;
   gi_2168 = FALSE;
   if (IsTesting() == TRUE) {
      gi_2168 = TRUE;
      if (Debug_LiveTrade == TRUE) Debug_LiveTrade = FALSE;
   }
   if (IsTesting() == FALSE && Debug_BackTestTrade == TRUE) Debug_BackTestTrade = FALSE;
   //if (IsDemo() == TRUE) gi_2168 = TRUE;
   if (gi_2168 == FALSE) gi_2168 = CheckAccountNumber();
   if (gi_2168 == TRUE) {
      g_magic_1936 = MagicBase + 100 * func_Symbol2Val(Symbol()) + func_TimeFrame_Const2Val(Period());
      g_magic_1940 = g_magic_1936 + 1;
      DeleteExistingLabels();
      SetupLabels();
      ClearLabels();
      CorrectTimeFilterSettings();
      gd_1928 = SetPoint();
      if (CheckSet() == 1) {
         gi_1676 = TRUE;
         if (gi_84 == FALSE) OutputStatusToChart(gs_1760 + " INITIALIZED SUCCESSFULLY");
         else OutputStatusToChart(gs_1760 + "- Expires on " + gi_88 + "/" + gi_92 + "/" + gi_96);
         gi_1680 = TRUE;
         if (UseFridayClose == 1) UseFridayNoTradeTime = TRUE;
         gi_1260 = FridayCloseHour;
         if (FridayFinalHour < FridayCloseHour) gi_1264 = FridayFinalHour;
         else gi_1264 = FridayCloseHour - 1;
         if (UseDST) {
            gi_1260 = FridayCloseHour - 1;
            gi_1264 = FridayFinalHour - 1;
         }
         if (Debug == TRUE) SetupLogFiles();
         gi_1708 = 0;
         gi_1712 = 0;
         GetGlobalVars();
         li_0 = CalculateCurrentOrders(g_magic_1936);
         if (li_0 == 0) {
            g_global_var_1848 = GetCorrectInputSet(g_global_var_1848, 0);
            g_global_var_1700 = 0;
            g_global_var_1704 = FALSE;
            SaveAlertTimeFrame(g_global_var_1700);
            SaveEntryTimeFrame(g_global_var_1704);
         } else g_global_var_1848 = GetCorrectInputSet(g_global_var_1848, 1);
         SaveTradeSet(g_global_var_1848);
         gi_1732 = gia_2212[gi_1852 - 1];
         gi_1736 = gia_2212[gi_1856 - 1];
         gi_1740 = gia_2212[gi_1860 - 1];
         start();
      } else {
         gi_1676 = FALSE;
         OutputStatusToChart(gs_1760 + "INITIALIZED UNSUCCESSFULLY");
      }
   }
   return (0);
}

int deinit() {
   if (Debug == TRUE) CloseLogFiles();
   ClearLabels();
   DeleteExistingLabels();
   return (0);
}

int CheckSet() {
   if (HigherTF_Set1 < 1 || HigherTF_Set1 > 8) {
      Alert("HigherTF_Set1 is set to wrong value,please recheck it!");
      return (0);
   }
   if (LowerTF_Set1 < 1 || LowerTF_Set1 > 8) {
      Alert("LowerTF_Set1 is set to wrong value,please recheck it!");
      return (0);
   }
   if (LowerTF_Set1 <= HigherTF_Set1) {
      Alert("LowerTF_Set1 must be < HigherTF_Set1,please recheck it!");
      return (0);
   }
   if (ConfirmUpperTimeFrame_Set1 == 1) {
      if (UpperTF_Set1 > HigherTF_Set1) {
         Alert("UpperTF_Set1 must be >= HigherTF_Set1,please recheck it!");
         return (0);
      }
   }
   if (EnterOnAlerted < 1 || EnterOnAlerted > 2) {
      Alert("EnterOnAlerted must be 1 or 2,please recheck it!");
      return (0);
   }
   if (HigherTF_Set2 < 1 || HigherTF_Set2 > 8) {
      Alert("HigherTF_Set2 is set to wrong value,please recheck it!");
      return (0);
   }
   if (LowerTF_Set2 < 1 || LowerTF_Set2 > 8) {
      Alert("LowerTF_Set2 is set to wrong value,please recheck it!");
      return (0);
   }
   if (LowerTF_Set2 <= HigherTF_Set2) {
      Alert("LowerTF_Set2 must be < HigherTF_Set2,please recheck it!");
      return (0);
   }
   if (ConfirmUpperTimeFrame_Set2 == 1) {
      if (SwitchSetTF > HigherTF_Set2) {
         Alert("SwitchSetTF must be >= HigherTF_Set2,please recheck it!");
         return (0);
      }
   }
   string ls_4 = CheckForData();
   int li_0 = StringFind(ls_4, "NONE", 0);
   if (li_0 < 0) {
      OutputComment1ToChart(ls_4);
      return (0);
   }
   return (1);
}

string CheckForData() {
   double ld_4;
   bool li_0 = FALSE;
   string ls_ret_12 = "Missing data on ";
   for (int l_index_20 = 0; l_index_20 <= 8; l_index_20++) {
      ld_4 = getHAS(gia_2212[l_index_20], 1);
      if (ld_4 < 0.1) {
         li_0 = TRUE;
         ls_ret_12 = ls_ret_12 + " - " + tf2txt(gia_2212[l_index_20]);
      }
   }
   if (li_0 == FALSE) ls_ret_12 = ls_ret_12 + "NONE";
   return (ls_ret_12);
}

int start() {
   int li_0 = 0;
   int li_4 = 0;
   int li_8 = 0;
   if (gi_1452 < 10) {
      SetupLabels();
      ClearLabels();
      DeleteExistingLabels();
      SetupLabels();
   }
   if (IsTesting() == FALSE) {
      if (IsExpertEnabled() == FALSE) {
         OutputComment1ToChart("Expert is not enabled");
         return (0);
      }
   }
   if (gi_2168 == FALSE) return (0);
   if (gi_1676 == FALSE) return (0);
   if (IsTradeAllowed() == FALSE) return (0);
   li_4 = CalculateCurrentOrders(g_magic_1940);
   if (HedgeEnterOnAlerted > 0) {
      if (useDelayAfterLoss == 1) {
         gi_1692 = LastHedgeTradeStoppedOut();
         if (TimeCurrent() < gi_1692) {
            OutputComment3ToChart("No Hedge Trades Until : " + TimeToStr(gi_1692, TIME_DATE|TIME_MINUTES));
            return (0);
         }
         OutputComment3ToChart("Hedge Trading Resumed ");
      }
   }
   if (li_4 > 0) HedgeCheckForClose();
   li_0 = CalculateCurrentOrders(g_magic_1936);
   if (li_0 == 0) {
      if (useDelayAfterLoss == 1) {
         gi_1688 = LastTradeStoppedOut();
         if (TimeCurrent() < gi_1688) {
            OutputComment3ToChart("No Trades Until : " + TimeToStr(gi_1688, TIME_DATE|TIME_MINUTES));
            return (0);
         }
         OutputComment3ToChart("Trading Resumed ");
      }
      g_global_var_1848 = GetCorrectInputSet(g_global_var_1848, 0);
      SaveTradeSet(g_global_var_1848);
      gi_1684 = CheckTradeFilters();
      OutputTradeSetToChart("Using Trade Set " + g_global_var_1848);
      if (gi_1684 == 0) {
         ResetGlobals();
         li_8 = ScalplingMethod();
         if (li_8 != 0) {
            ResetGlobals();
            OpenTrade(li_8);
            li_4 = CalculateCurrentOrders(g_magic_1940);
            if (li_4 == 0) {
               ResetHedgeGlobals();
               if (HedgeEnterOnAlerted == 1) HedgeOpenTrade(-li_8);
            }
         }
      }
   } else {
      RefreshRates();
      TrackPrices();
      CheckForClose();
   }
   return (0);
}

void ResetGlobals() {
   if (gi_572 == TRUE) {
      gi_572 = FALSE;
      SaveFirst_TP_Level(gi_572);
   }
   if (gi_644 == TRUE) {
      gi_644 = FALSE;
      SaveFirst_Trigger_Level(gi_644);
   }
   if (gi_580 == TRUE) {
      gi_580 = FALSE;
      SaveClosedPartial(gi_580);
   }
}

void ResetHedgeGlobals() {
   if (gi_576 == TRUE) {
      gi_576 = FALSE;
      HedgeSaveFirst_TP_Level(gi_576);
   }
   if (gi_656 == TRUE) {
      gi_656 = FALSE;
      HedgeSaveFirst_Trigger_Level(gi_656);
   }
}

int ScalplingMethod() {
   int li_0;
   if (Debug_Expert == TRUE) WriteDash();
   int li_4 = getTFColor(gi_1732, 1);
   if (gi_1708 != li_4) gi_1680 = TRUE;
   if (gi_1680 == TRUE) {
      gi_1708 = li_4;
      if (gi_1864 == 1) {
         OutputTrendToChart(tf2txt(gi_1732) + "Trend is " + isUporDown(gi_1708));
         OutputLookingToChart("Checking Upper TimeFrames for " + isUporDown(gi_1708));
         if (CheckUpperTF(gi_1708) == 0) {
            OutputFoundToChart("UpperTimeFrame(s) do not match Trend on " + tf2txt(gi_1732));
            return (0);
         }
         OutputFoundToChart("UpperTimeFrame(s) match Trend on " + tf2txt(gi_1732));
      }
      g_global_var_1700 = 0;
      g_global_var_1704 = FALSE;
      SaveAlertTimeFrame(g_global_var_1700);
      SaveEntryTimeFrame(g_global_var_1704);
      g_global_var_1720 = FALSE;
      g_global_var_1724 = FALSE;
      HedgeSaveAlertTimeFrame(g_global_var_1720);
      HedgeSaveEntryTimeFrame(g_global_var_1724);
      gi_1680 = FALSE;
      OutputTrendToChart(tf2txt(gi_1732) + "Trend is " + isUporDown(gi_1708));
      OutputLookingToChart("Looking for " + isUporDown(-gi_1708) + " trend between " + tf2txt(gi_1732) + " and " + tf2txt(gi_1736));
   }
   int li_8 = GetAlertTimeFrame(gi_1708, 1);
   SetTimeFrames(li_8);
   if (g_global_var_1700 > 0) {
      OutputFoundToChart("Found " + isUporDown(-gi_1708) + " trend in " + tf2txt(g_global_var_1700));
      OutputWaitingToChart("Waiting for the " + tf2txt(g_global_var_1704) + " to turn " + isUporDown(gi_1708), 0);
      li_0 = CalculateCurrentOrders(g_magic_1940);
      if (li_0 == 0) {
         ResetHedgeGlobals();
         if (HedgeEnterOnAlerted == 2) HedgeOpenTrade(-gi_1708);
      }
      gi_1712 = getTFColor(g_global_var_1704, EnterOnAlerted - 1);
      if (gi_1712 == gi_1708) {
         gi_1680 = TRUE;
         if (ConfirmTrade() == 1) {
            OutputConditionToChart("All conditions met, entering trade");
            PlaySound("alert.wav");
            return (gi_1712);
         }
      }
   }
   return (0);
}

double getHAS(int a_timeframe_0, int ai_4) {
   g_icustom_1768 = iCustom(NULL, a_timeframe_0, "Heiken_Ashi_Smoothed", MaMethod, MaPeriod, MaMethod2, MaPeriod2, 1, ai_4);
   return (g_icustom_1768);
}

int getTFColor(int a_timeframe_0, int ai_4) {
   g_icustom_1768 = iCustom(NULL, a_timeframe_0, "Heiken_Ashi_Smoothed", MaMethod, MaPeriod, MaMethod2, MaPeriod2, 1, ai_4);
   g_icustom_1776 = iCustom(NULL, a_timeframe_0, "Heiken_Ashi_Smoothed", MaMethod, MaPeriod, MaMethod2, MaPeriod2, 0, ai_4);
   if (g_icustom_1768 < g_icustom_1776) return (-1);
   else return (1);
}

int GetAlertTimeFrame(int ai_0, int ai_4) {
   int li_ret_12;
   int li_16;
   bool li_8 = FALSE;
   for (int l_index_20 = 0; l_index_20 <= 9; l_index_20++) {
      if (gia_2212[l_index_20] < gi_1732) {
         if (gia_2212[l_index_20] >= gi_1736) {
            li_16 = getTFColor(gia_2212[l_index_20], ai_4);
            if (li_16 != ai_0) {
               li_ret_12 = l_index_20;
               li_8 = TRUE;
               break;
            }
         }
      }
      if (l_index_20 == 9) li_ret_12 = 10;
   }
   if (li_8 == FALSE) li_ret_12 = 10;
   return (li_ret_12);
}

void SetTimeFrames(int ai_0) {
   if (ai_0 < 9) {
      g_global_var_1700 = gia_2212[ai_0];
      SaveAlertTimeFrame(g_global_var_1700);
      g_global_var_1704 = gia_2212[ai_0 + 1];
      SaveEntryTimeFrame(g_global_var_1704);
      g_global_var_1720 = g_global_var_1700;
      g_global_var_1724 = g_global_var_1704;
      HedgeSaveAlertTimeFrame(g_global_var_1720);
      HedgeSaveEntryTimeFrame(g_global_var_1724);
      switch (gi_1872) {
      case 1:
         g_global_var_1716 = g_global_var_1704;
         break;
      case 2:
         g_global_var_1716 = g_global_var_1704;
         break;
      case 3:
         g_global_var_1716 = g_global_var_1700;
         break;
      case 4:
         g_global_var_1716 = g_global_var_1700;
         break;
      default:
         g_global_var_1716 = FALSE;
      }
      SaveExitTimeFrameProfit(g_global_var_1716);
      switch (HedgeExitOnAlertedProfit) {
      case 1:
         g_global_var_1728 = g_global_var_1704;
         break;
      case 2:
         g_global_var_1728 = g_global_var_1704;
         break;
      case 3:
         g_global_var_1728 = g_global_var_1700;
         break;
      case 4:
         g_global_var_1728 = g_global_var_1700;
         break;
      default:
         g_global_var_1728 = FALSE;
      }
      HedgeSaveExitTimeFrameProfit(g_global_var_1728);
      return;
   }
   g_global_var_1700 = 0;
   g_global_var_1704 = FALSE;
   g_global_var_1716 = FALSE;
   SaveAlertTimeFrame(g_global_var_1700);
   SaveEntryTimeFrame(g_global_var_1704);
   SaveExitTimeFrameProfit(g_global_var_1716);
   g_global_var_1720 = FALSE;
   g_global_var_1724 = FALSE;
   g_global_var_1728 = FALSE;
   HedgeSaveAlertTimeFrame(g_global_var_1720);
   HedgeSaveEntryTimeFrame(g_global_var_1724);
   HedgeSaveExitTimeFrameProfit(g_global_var_1728);
}

int ConfirmTrade() {
   int li_0;
   int li_4;
   bool li_16;
   if (gi_1868 == 1) {
      li_0 = CheckAllTF();
      if (li_0 < 10) {
         OutputConditionToChart(tf2txt(gia_2212[li_0]) + " TF changed, starting new cycle");
         return (0);
      }
   }
   int li_8 = getTFColor(gi_1732, 0);
   int li_12 = getTFColor(g_global_var_1700, 0);
   if (li_8 == gi_1708) {
      li_16 = FALSE;
      if (ConfirmAlertTimeFrame == 0) li_16 = TRUE;
      else
         if (li_12 != gi_1712) li_16 = TRUE;
      if (li_16 == TRUE) {
         if (EnterOnFreshSignal == 1) {
            li_4 = getTFColor(g_global_var_1704, 2);
            if (li_4 == gi_1712) {
               OutputConditionToChart("No Fresh Signal, starting new cycle");
               return (0);
            }
         }
         return (1);
      }
   } else {
      if (li_8 != gi_1708) OutputConditionToChart("Higher TF changed, starting new cycle");
      if (ConfirmAlertTimeFrame == 1 && li_12 == gi_1712) OutputConditionToChart("Alert TF changed, starting new cycle");
      return (0);
   }
   return (0);
}

int CheckAllTF() {
   int li_ret_4;
   int li_8;
   bool li_0 = FALSE;
   for (int l_index_12 = 0; l_index_12 <= 9; l_index_12++) {
      if (gia_2212[l_index_12] <= gi_1740) {
         if (gia_2212[l_index_12] > gi_1732) {
            li_8 = getTFColor(gia_2212[l_index_12], 0);
            if (li_8 != gi_1708) {
               li_ret_4 = l_index_12;
               li_0 = TRUE;
               break;
            }
         }
      }
   }
   if (li_0 == FALSE) li_ret_4 = 10;
   return (li_ret_4);
}

int CheckUpperTF(int ai_0) {
   int li_8;
   int li_12;
   bool li_ret_4 = TRUE;
   for (int li_16 = gi_1860; li_16 < gi_1852; li_16++) {
      li_12 = gia_2212[li_16 - 1];
      li_8 = getTFColor(li_12, 1);
      if (li_8 != ai_0) {
         li_ret_4 = FALSE;
         li_16 = gi_1852;
      }
   }
   return (li_ret_4);
}

void OpenTrade(int ai_0) {
   int l_ticket_4;
   int l_error_8;
   double ld_20;
   double ld_28;
   g_global_var_984 = CalTP(g_global_var_1700);
   SaveInternalTakeProfit(g_global_var_984);
   g_global_var_1004 = g_global_var_984;
   SaveTP_Level1(g_global_var_1004);
   int li_36 = MathFloor(g_global_var_984 * SafetyTakeProfitMultiplier);
   gi_1080 = CalSL(g_global_var_1700);
   SaveInternalStopLoss(gi_1080);
   gi_1084 = 0;
   if (gi_1080 > 0) gi_1084 = MathFloor(gi_1080 * SafetyStopLossMultiplier);
   SaveStopLoss(gi_1084);
   RefreshRates();
   gd_1784 = Bid;
   gd_1792 = Bid;
   gd_1800 = Ask;
   gd_1808 = Ask;
   SaveHighBid(gd_1784);
   SaveLowBid(gd_1792);
   SaveHighAsk(gd_1800);
   SaveLowAsk(gd_1808);
   double l_lots_12 = GetLots();
   if (ai_0 == 1) {
      l_ticket_4 = OrderSend(Symbol(), OP_BUY, l_lots_12, Ask, Slippage, 0, 0, UserComment + " Trade Set : " + g_global_var_1848 + " -" + tf2txt(g_global_var_1700), g_magic_1936, 0, Green);
      if (l_ticket_4 > 0) {
         g_ticket_1816 = l_ticket_4;
         OrderSelect(g_ticket_1816, SELECT_BY_TICKET, MODE_TRADES);
         if (gi_1084 != 0 || li_36 != 0) {
            ld_20 = TakeLong(OrderOpenPrice(), li_36);
            ld_28 = 0;
            if (gi_1084 > 0) {
               ld_28 = StopLong(OrderOpenPrice(), gi_1084);
               ld_28 = ValidStopLoss(0, Bid, ld_28);
            }
            g_global_var_2172 = TakeLong(OrderOpenPrice(), g_global_var_984);
            gd_2180 = 0;
            if (gi_1084 > 0) gd_2180 = StopLong(OrderOpenPrice(), gi_1080);
            if (Digits > 0) {
               ld_28 = NormalizeDouble(ld_28, Digits);
               ld_20 = NormalizeDouble(ld_20, Digits);
               gd_2180 = NormalizeDouble(gd_2180, Digits);
               g_global_var_2172 = NormalizeDouble(g_global_var_2172, Digits);
            }
            ModifyOrder(g_ticket_1816, OrderOpenPrice(), ld_28, ld_20, LightGreen);
            SaveIntSTprice(gd_2180);
            SaveIntTPprice(g_global_var_2172);
         }
         OutputTrade1ToChart("Ticket : " + g_ticket_1816 + " Trigger : " + tf2txt(g_global_var_1700));
         OutputTrade2ToChart(" BUY : " + DoubleToStr(l_lots_12, 2) + " Lots at " + DoubleToStr(OrderOpenPrice(), Digits));
         OutputTrade3ToChart(" SL  : " + DoubleToStr(ld_28, Digits) + "(" + DoubleToStr(gi_1084, 0) + ")" + "  TP : " + DoubleToStr(ld_20, Digits) + "(" + DoubleToStr(li_36, 0) +
            ")");
         OutputTrade4ToChart(" Internal SL : " + DoubleToStr(gd_2180, Digits) + "(" + DoubleToStr(gi_1080, 0) + ")" + "  Internal TP : " + DoubleToStr(g_global_var_2172, Digits) +
            "(" + DoubleToStr(g_global_var_984, 0) + ")");
         if (Debug == TRUE) {
            Write_OpenTrade(" " + Symbol() + " #" + g_ticket_1816 + " Trade Set : " + g_global_var_1848 + " Trigger : " + tf2txt(g_global_var_1700) + " OPEN BUY ORDER " + DoubleToStr(l_lots_12, 2) +
               " LOTS AT " + DoubleToStr(Ask, 4) + " sl: " + DoubleToStr(ld_28, Digits) + "  tp : " + DoubleToStr(ld_20, Digits));
         }
      }
   } else {
      if (ai_0 == -1) {
         l_ticket_4 = OrderSend(Symbol(), OP_SELL, l_lots_12, Bid, Slippage, 0, 0, UserComment + " Trade Set : " + g_global_var_1848 + " -" + tf2txt(g_global_var_1700), g_magic_1936, 0, Red);
         if (l_ticket_4 > 0) {
            g_ticket_1820 = l_ticket_4;
            OrderSelect(g_ticket_1820, SELECT_BY_TICKET, MODE_TRADES);
            if (gi_1084 != 0 || li_36 != 0) {
               ld_20 = TakeShort(OrderOpenPrice(), li_36);
               ld_28 = 0;
               if (gi_1084 > 0) {
                  ld_28 = StopShort(OrderOpenPrice(), gi_1084);
                  ld_28 = ValidStopLoss(1, Ask, ld_28);
               }
               g_global_var_2172 = TakeShort(OrderOpenPrice(), g_global_var_984);
               gd_2180 = 0;
               if (gi_1084 > 0) gd_2180 = StopShort(OrderOpenPrice(), gi_1080);
               if (Digits > 0) {
                  ld_28 = NormalizeDouble(ld_28, Digits);
                  ld_20 = NormalizeDouble(ld_20, Digits);
                  gd_2180 = NormalizeDouble(gd_2180, Digits);
                  g_global_var_2172 = NormalizeDouble(g_global_var_2172, Digits);
               }
               ModifyOrder(g_ticket_1820, OrderOpenPrice(), ld_28, ld_20, LightGreen);
               SaveIntSTprice(gd_2180);
               SaveIntTPprice(g_global_var_2172);
            }
            OutputTrade1ToChart("Ticket : " + g_ticket_1820 + " Trigger : " + tf2txt(g_global_var_1700));
            OutputTrade2ToChart(" SELL : " + DoubleToStr(l_lots_12, 2) + " Lots at " + DoubleToStr(OrderOpenPrice(), Digits));
            OutputTrade3ToChart(" SL   : " + DoubleToStr(ld_28, Digits) + "(" + DoubleToStr(gi_1084, 0) + ")" + "  TP : " + DoubleToStr(ld_20, Digits) + "(" + DoubleToStr(li_36, 0) +
               ")");
            OutputTrade4ToChart(" Internal SL : " + DoubleToStr(gd_2180, Digits) + "(" + DoubleToStr(gi_1080, 0) + ")" + "  Internal TP : " + DoubleToStr(g_global_var_2172, Digits) +
               "(" + DoubleToStr(g_global_var_984, 0) + ")");
            if (Debug == TRUE) {
               Write_OpenTrade(" " + Symbol() + " #" + g_ticket_1820 + " Trade Set : " + g_global_var_1848 + " Trigger : " + tf2txt(g_global_var_1700) + " OPEN SELL ORDER " + DoubleToStr(l_lots_12, 2) +
                  " LOTS AT " + DoubleToStr(Bid, Digits) + " sl: " + DoubleToStr(ld_28, Digits) + "  tp : " + DoubleToStr(ld_20, Digits));
            }
         }
      }
   }
   if (l_ticket_4 < 0) {
      l_error_8 = GetLastError();
      OutputTrade1ToChart("OrderSend failed");
      OutputTrade2ToChart("error(" + l_error_8 + ") " + ErrorDescription(l_error_8));
      OutputTrade3ToChart(" SL:" + "(" + DoubleToStr(gi_1084, 0) + ")" + " TP:" + "(" + DoubleToStr(li_36, 0) + ")" + " Lots:" + DoubleToStr(l_lots_12, 2));
      Print("OrderSend failed with error(" + l_error_8 + ") " + ErrorDescription(l_error_8) + " Lots:" + DoubleToStr(l_lots_12, 2));
      if (Debug == TRUE) Write_OpenError("Error opening order : (" + l_error_8 + ") " + ErrorDescription(l_error_8) + " Lots:" + DoubleToStr(l_lots_12, 2));
   }
}

void HedgeOpenTrade(int ai_0) {
   int l_ticket_4;
   int l_error_8;
   double ld_20;
   double ld_28;
   gi_988 = HedgeTakeProfit;
   HedgeSaveInternalTakeProfit(gi_988);
   gi_1008 = gi_988;
   HedgeSaveTP_Level1(gi_1008);
   int li_36 = MathFloor(gi_988 * SafetyTakeProfitMultiplier);
   gi_1088 = mHedgeStopLoss;
   HedgeSaveInternalStopLoss(gi_1088);
   gi_1092 = 0;
   if (gi_1088 > 0) gi_1092 = MathFloor(gi_1088 * SafetyStopLossMultiplier);
   HedgeSaveStopLoss(gi_1092);
   RefreshRates();
   double l_lots_12 = GetLots() * HedgeMultiplier;
   if (ai_0 == 1) {
      l_ticket_4 = OrderSend(Symbol(), OP_BUY, l_lots_12, Ask, Slippage, 0, 0, HedgeUserComment + " -" + tf2txt(g_global_var_1700), g_magic_1940, 0, Blue);
      if (l_ticket_4 > 0) {
         g_ticket_1816 = l_ticket_4;
         OrderSelect(g_ticket_1816, SELECT_BY_TICKET, MODE_TRADES);
         if (gi_1092 != 0 || li_36 != 0) {
            ld_20 = TakeLong(OrderOpenPrice(), li_36);
            ld_28 = 0;
            if (gi_1092 > 0) {
               ld_28 = StopLong(OrderOpenPrice(), gi_1092);
               ld_28 = ValidStopLoss(0, Bid, ld_28);
            }
            g_global_var_2188 = TakeLong(OrderOpenPrice(), gi_988);
            gd_2196 = 0;
            if (gi_1092 > 0) gd_2196 = StopLong(OrderOpenPrice(), gi_1088);
            if (Digits > 0) {
               ld_28 = NormalizeDouble(ld_28, Digits);
               ld_20 = NormalizeDouble(ld_20, Digits);
               gd_2196 = NormalizeDouble(gd_2196, Digits);
               g_global_var_2188 = NormalizeDouble(g_global_var_2188, Digits);
            }
            ModifyOrder(g_ticket_1816, OrderOpenPrice(), ld_28, ld_20, LightGreen);
            HedgeSaveIntSTprice(gd_2196);
            HedgeSaveIntTPprice(g_global_var_2188);
         }
         if (Debug == TRUE) {
            Write_OpenTrade(" " + Symbol() + " #" + g_ticket_1816 + " Trigger : " + tf2txt(g_global_var_1700) + " OPEN HEDGE BUY ORDER " + DoubleToStr(l_lots_12, 2) + " LOTS AT " +
               DoubleToStr(Ask, 4) + " sl: " + DoubleToStr(ld_28, Digits) + "  tp : " + DoubleToStr(ld_20, Digits));
         }
      }
   } else {
      if (ai_0 == -1) {
         l_ticket_4 = OrderSend(Symbol(), OP_SELL, l_lots_12, Bid, Slippage, 0, 0, HedgeUserComment + " -" + tf2txt(g_global_var_1700), g_magic_1940, 0, Yellow);
         if (l_ticket_4 > 0) {
            g_ticket_1820 = l_ticket_4;
            OrderSelect(g_ticket_1820, SELECT_BY_TICKET, MODE_TRADES);
            if (gi_1092 != 0 || li_36 != 0) {
               ld_20 = TakeShort(OrderOpenPrice(), li_36);
               ld_28 = 0;
               if (gi_1092 > 0) {
                  ld_28 = StopShort(OrderOpenPrice(), gi_1092);
                  ld_28 = ValidStopLoss(1, Ask, ld_28);
               }
               g_global_var_2188 = TakeShort(OrderOpenPrice(), gi_988);
               gd_2196 = 0;
               if (gi_1092 > 0) gd_2196 = StopShort(OrderOpenPrice(), gi_1088);
               if (Digits > 0) {
                  ld_28 = NormalizeDouble(ld_28, Digits);
                  ld_20 = NormalizeDouble(ld_20, Digits);
                  gd_2196 = NormalizeDouble(gd_2180, Digits);
                  g_global_var_2188 = NormalizeDouble(g_global_var_2172, Digits);
               }
               ModifyOrder(g_ticket_1820, OrderOpenPrice(), ld_28, ld_20, LightGreen);
               HedgeSaveIntSTprice(gd_2196);
               HedgeSaveIntTPprice(g_global_var_2188);
            }
            if (Debug == TRUE) {
               Write_OpenTrade(" " + Symbol() + " #" + g_ticket_1820 + " Trigger : " + tf2txt(g_global_var_1700) + " OPEN HEDGE SELL ORDER " + DoubleToStr(l_lots_12, 2) + " LOTS AT " +
                  DoubleToStr(Bid, Digits) + " sl: " + DoubleToStr(ld_28, Digits) + "  tp : " + DoubleToStr(ld_20, Digits));
            }
         }
      }
   }
   if (l_ticket_4 < 0) {
      l_error_8 = GetLastError();
      Print("OrderSend failed with error(" + l_error_8 + ") " + ErrorDescription(l_error_8) + " Lots:" + DoubleToStr(l_lots_12, 2));
      if (Debug == TRUE) Write_OpenError("Error opening order : (" + l_error_8 + ") " + ErrorDescription(l_error_8) + " Lots:" + DoubleToStr(l_lots_12, 2));
   }
}

void CheckForClose() {
   int li_0;
   int l_ticket_4;
   double l_ord_lots_8;
   double l_ord_open_price_16;
   double l_ord_takeprofit_24;
   double l_ord_stoploss_32;
   int l_datetime_40;
   for (int l_pos_44 = OrdersTotal() - 1; l_pos_44 >= 0; l_pos_44--) {
      OrderSelect(l_pos_44, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()) {
         if (OrderMagicNumber() == g_magic_1936) {
            l_ticket_4 = OrderTicket();
            l_ord_lots_8 = OrderLots();
            l_ord_open_price_16 = OrderOpenPrice();
            l_ord_takeprofit_24 = OrderTakeProfit();
            l_ord_stoploss_32 = OrderStopLoss();
            l_datetime_40 = OrderOpenTime();
            if (OrderType() == OP_BUY) {
               PlotDD(0, l_ord_open_price_16);
               li_0 = 0;
               li_0 = CheckSetChangeClose(0, l_ticket_4, l_ord_open_price_16, l_ord_lots_8);
               if (li_0 == 1) break;
               li_0 = 0;
               li_0 = CheckCloseAfterX_Days(0, l_ticket_4, l_ord_open_price_16, l_ord_lots_8, l_datetime_40);
               if (li_0 == 1) break;
               li_0 = 0;
               li_0 = CheckEndOfMonthClose(0, l_ticket_4, l_ord_open_price_16, l_ord_lots_8, l_datetime_40);
               if (li_0 == 1) break;
               li_0 = 0;
               li_0 = CheckFRIClose(0, l_ticket_4, l_ord_open_price_16, l_ord_lots_8);
               if (li_0 == 1) break;
               li_0 = 0;
               li_0 = CheckTPClose(0, l_ticket_4, l_ord_open_price_16, l_ord_lots_8, l_ord_takeprofit_24, gi_1872, g_global_var_2172);
               if (li_0 == 1) break;
               if (gi_644 == FALSE) {
                  gi_644 = Get_First_Trigger_Level(0, l_ord_open_price_16, gi_1876, gi_1880);
                  if (gi_644 == TRUE) SaveFirst_Trigger_Level(gi_644);
               }
               if (gi_572 == FALSE) {
                  gi_572 = Get_First_TP_Level(0, l_ord_open_price_16, g_global_var_1004);
                  if (gi_572 == TRUE) SaveFirst_TP_Level(gi_572);
               }
               li_0 = 0;
               if (gi_580 == FALSE) li_0 = ClosePartialLots(0, l_ticket_4, l_ord_lots_8);
               if (li_0 == 1) {
                  gi_580 = TRUE;
                  SaveClosedPartial(gi_580);
                  return;
               }
               li_0 = 0;
               li_0 = CheckForProfitExit(0, l_ticket_4, l_ord_open_price_16, l_ord_lots_8);
               if (li_0 == 1) break;
               li_0 = 0;
               li_0 = CheckSLClose(0, l_ticket_4, l_ord_open_price_16, l_ord_lots_8, l_ord_stoploss_32, gd_2180);
               if (li_0 == 1) break;
               HandleTrailingStop(0, l_ticket_4, l_ord_open_price_16, l_ord_stoploss_32, l_ord_takeprofit_24);
            }
            if (OrderType() == OP_SELL) {
               PlotDD(1, l_ord_open_price_16);
               li_0 = 0;
               li_0 = CheckSetChangeClose(1, l_ticket_4, l_ord_open_price_16, l_ord_lots_8);
               if (li_0 == 1) break;
               li_0 = 0;
               li_0 = CheckCloseAfterX_Days(1, l_ticket_4, l_ord_open_price_16, l_ord_lots_8, l_datetime_40);
               if (li_0 == 1) break;
               li_0 = 0;
               li_0 = CheckEndOfMonthClose(1, l_ticket_4, l_ord_open_price_16, l_ord_lots_8, l_datetime_40);
               if (li_0 == 1) break;
               li_0 = 0;
               li_0 = CheckFRIClose(1, l_ticket_4, l_ord_open_price_16, l_ord_lots_8);
               if (li_0 == 1) break;
               li_0 = 0;
               li_0 = CheckTPClose(1, l_ticket_4, l_ord_open_price_16, l_ord_lots_8, l_ord_takeprofit_24, gi_1872, g_global_var_2172);
               if (li_0 == 1) break;
               if (gi_644 == FALSE) {
                  gi_644 = Get_First_Trigger_Level(1, l_ord_open_price_16, gi_1876, gi_1880);
                  if (gi_644 == TRUE) SaveFirst_Trigger_Level(gi_644);
               }
               if (gi_572 == FALSE) {
                  gi_572 = Get_First_TP_Level(1, l_ord_open_price_16, g_global_var_1004);
                  if (gi_572 == TRUE) SaveFirst_TP_Level(gi_572);
               }
               li_0 = 0;
               if (gi_580 == FALSE) li_0 = ClosePartialLots(1, l_ticket_4, l_ord_lots_8);
               if (li_0 == 1) {
                  gi_580 = TRUE;
                  SaveClosedPartial(gi_580);
                  return;
               }
               li_0 = 0;
               li_0 = CheckForProfitExit(1, l_ticket_4, l_ord_open_price_16, l_ord_lots_8);
               if (li_0 == 1) break;
               li_0 = 0;
               li_0 = CheckSLClose(1, l_ticket_4, l_ord_open_price_16, l_ord_lots_8, l_ord_stoploss_32, gd_2180);
               if (li_0 == 1) break;
               HandleTrailingStop(1, l_ticket_4, l_ord_open_price_16, l_ord_stoploss_32, l_ord_takeprofit_24);
            }
         }
      }
   }
}

void HedgeCheckForClose() {
   int li_0;
   int l_ticket_4;
   double l_ord_lots_8;
   double l_ord_open_price_16;
   double l_ord_takeprofit_24;
   double l_ord_stoploss_32;
   int l_datetime_40;
   for (int l_pos_44 = OrdersTotal() - 1; l_pos_44 >= 0; l_pos_44--) {
      OrderSelect(l_pos_44, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()) {
         if (OrderMagicNumber() == g_magic_1940) {
            l_ticket_4 = OrderTicket();
            l_ord_lots_8 = OrderLots();
            l_ord_open_price_16 = OrderOpenPrice();
            l_ord_takeprofit_24 = OrderTakeProfit();
            l_ord_stoploss_32 = OrderStopLoss();
            l_datetime_40 = OrderOpenTime();
            if (OrderType() == OP_BUY) {
               li_0 = 0;
               li_0 = CheckCloseAfterX_Days(0, l_ticket_4, l_ord_open_price_16, l_ord_lots_8, l_datetime_40);
               if (li_0 == 1) break;
               li_0 = 0;
               li_0 = CheckEndOfMonthClose(0, l_ticket_4, l_ord_open_price_16, l_ord_lots_8, l_datetime_40);
               if (li_0 == 1) break;
               li_0 = 0;
               li_0 = CheckFRIClose(0, l_ticket_4, l_ord_open_price_16, l_ord_lots_8);
               if (li_0 == 1) break;
               li_0 = 0;
               li_0 = CheckTPClose(0, l_ticket_4, l_ord_open_price_16, l_ord_lots_8, l_ord_takeprofit_24, HedgeExitOnAlertedProfit, g_global_var_2188);
               if (li_0 == 1) break;
               if (gi_656 == FALSE) {
                  gi_656 = Get_First_Trigger_Level(0, l_ord_open_price_16, HedgeUseTrigger, HedgeExitTrigger);
                  if (gi_656 == TRUE) HedgeSaveFirst_Trigger_Level(gi_656);
               }
               if (gi_576 == FALSE) {
                  gi_576 = Get_First_TP_Level(0, l_ord_open_price_16, gi_1008);
                  if (gi_576 == TRUE) SaveFirst_TP_Level(gi_576);
               }
               li_0 = 0;
               li_0 = HedgeCheckForProfitExit(0, l_ticket_4, l_ord_open_price_16, l_ord_lots_8);
               if (li_0 == 1) break;
               li_0 = 0;
               li_0 = CheckSLClose(0, l_ticket_4, l_ord_open_price_16, l_ord_lots_8, l_ord_stoploss_32, gd_2196);
               if (li_0 == 1) break;
               HedgeHandleTrailingStop(0, l_ticket_4, l_ord_open_price_16, l_ord_stoploss_32, l_ord_takeprofit_24);
            }
            if (OrderType() == OP_SELL) {
               li_0 = 0;
               li_0 = CheckCloseAfterX_Days(1, l_ticket_4, l_ord_open_price_16, l_ord_lots_8, l_datetime_40);
               if (li_0 == 1) break;
               li_0 = 0;
               li_0 = CheckEndOfMonthClose(1, l_ticket_4, l_ord_open_price_16, l_ord_lots_8, l_datetime_40);
               if (li_0 == 1) break;
               li_0 = 0;
               li_0 = CheckFRIClose(1, l_ticket_4, l_ord_open_price_16, l_ord_lots_8);
               if (li_0 == 1) break;
               li_0 = 0;
               li_0 = CheckTPClose(1, l_ticket_4, l_ord_open_price_16, l_ord_lots_8, l_ord_takeprofit_24, HedgeExitOnAlertedProfit, g_global_var_2188);
               if (li_0 == 1) break;
               if (gi_656 == FALSE) {
                  gi_656 = Get_First_Trigger_Level(1, l_ord_open_price_16, HedgeUseTrigger, HedgeExitTrigger);
                  if (gi_656 == TRUE) HedgeSaveFirst_Trigger_Level(gi_656);
               }
               if (gi_576 == FALSE) {
                  gi_576 = Get_First_TP_Level(1, l_ord_open_price_16, gi_1008);
                  if (gi_576 == TRUE) HedgeSaveFirst_TP_Level(gi_576);
               }
               li_0 = 0;
               li_0 = HedgeCheckForProfitExit(1, l_ticket_4, l_ord_open_price_16, l_ord_lots_8);
               if (li_0 == 1) break;
               li_0 = 0;
               li_0 = CheckSLClose(1, l_ticket_4, l_ord_open_price_16, l_ord_lots_8, l_ord_stoploss_32, gd_2196);
               if (li_0 == 1) break;
               HedgeHandleTrailingStop(1, l_ticket_4, l_ord_open_price_16, l_ord_stoploss_32, l_ord_takeprofit_24);
            }
         }
      }
   }
}

int CheckForProfitExit(int ai_0, int ai_4, double ad_8, double ad_16) {
   int li_ret_24 = 0;
   bool li_28 = FALSE;
   if (gi_1876 == 1) li_28 = gi_644;
   else li_28 = gi_572;
   if (li_28 == TRUE) {
      switch (gi_1872) {
      case 1:
         li_ret_24 = CheckExit(ai_0, ai_4, ad_16, ad_8, g_global_var_1716, 0);
         break;
      case 2:
         li_ret_24 = CheckExit(ai_0, ai_4, ad_16, ad_8, g_global_var_1716, 1);
         break;
      case 3:
         li_ret_24 = CheckExit(ai_0, ai_4, ad_16, ad_8, g_global_var_1716, 0);
         break;
      case 4:
         li_ret_24 = CheckExit(ai_0, ai_4, ad_16, ad_8, g_global_var_1716, 1);
         break;
      default:
         li_ret_24 = 0;
      }
   }
   return (li_ret_24);
}

int HedgeCheckForProfitExit(int ai_0, int ai_4, double ad_8, double ad_16) {
   int li_ret_24 = 0;
   bool li_28 = FALSE;
   if (HedgeUseTrigger == 1) li_28 = gi_656;
   else li_28 = gi_576;
   if (li_28 == TRUE) {
      switch (HedgeExitOnAlertedProfit) {
      case 1:
         li_ret_24 = CheckExit(ai_0, ai_4, ad_16, ad_8, g_global_var_1728, 0);
         break;
      case 2:
         li_ret_24 = CheckExit(ai_0, ai_4, ad_16, ad_8, g_global_var_1728, 1);
         break;
      case 3:
         li_ret_24 = CheckExit(ai_0, ai_4, ad_16, ad_8, g_global_var_1728, 0);
         break;
      case 4:
         li_ret_24 = CheckExit(ai_0, ai_4, ad_16, ad_8, g_global_var_1728, 1);
         break;
      default:
         li_ret_24 = 0;
      }
   }
   return (li_ret_24);
}

int Get_First_TP_Level(int ai_0, double ad_4, int ai_12) {
   switch (ai_0) {
   case 0:
      if (Bid >= ad_4 + ai_12 * gd_1928) return (1);
      return (0);
      break;
   case 1:
      if (Ask <= ad_4 - ai_12 * gd_1928) return (1);
      return (0);
   }
   return (0);
}

int Get_First_Trigger_Level(int ai_0, double ad_4, int ai_12, int ai_16) {
   switch (ai_0) {
   case 0:
      if (ai_12 == 1)
         if (Bid >= ad_4 + ai_16 * gd_1928) return (1);
      return (0);
      break;
   case 1:
      if (ai_12 == 1)
         if (Ask <= ad_4 - ai_16 * gd_1928) return (1);
      return (0);
   }
   return (0);
}

int CloseOrder(int a_ticket_0, double a_lots_4, int ai_12) {
   int l_ord_close_20;
   int l_error_28;
   double l_price_32;
   bool li_16 = FALSE;
   if (ai_12 == 0) l_price_32 = Bid;
   if (ai_12 == 1) l_price_32 = Ask;
   if (Digits > 0) l_price_32 = NormalizeDouble(l_price_32, Digits);
   int l_count_24 = 0;
   while (!li_16) {
      if (IsTradeAllowed() == TRUE) {
         l_ord_close_20 = OrderClose(a_ticket_0, a_lots_4, l_price_32, Slippage, Violet);
         l_error_28 = GetLastError();
      } else l_count_24++;
      if (l_ord_close_20 == 1) li_16 = TRUE;
      l_error_28 = GetLastError();
      switch (l_error_28) {
      case 0/* NO_ERROR */:
         li_16 = TRUE;
         break;
      case 4/* SERVER_BUSY */:
      case 6/* NO_CONNECTION */:
      case 129/* INVALID_PRICE */:
      case 136/* OFF_QUOTES */:
      case 137/* BROKER_BUSY */:
      case 146/* TRADE_CONTEXT_BUSY */:
      case 128/* TRADE_TIMEOUT */:
         l_count_24++;
         break;
      case 135/* PRICE_CHANGED */:
      case 138/* REQUOTE */:
         RefreshRates();
         continue;
      default:
         li_16 = TRUE;
      }
      if (l_count_24 > gi_2204) li_16 = TRUE;
      if (!li_16) {
         Sleep(gi_2208);
         RefreshRates();
      }
   }
   if (l_ord_close_20 == 1 || l_error_28 == 0/* NO_ERROR */) return (1);
   Print(" Error closing order : (", l_error_28, ") " + ErrorDescription(l_error_28));
   return (0);
}

int CheckSetChangeClose(int ai_0, int ai_4, double ad_8, double ad_16) {
   int li_28;
   int li_32;
   int li_36;
   bool li_40;
   if (UseExitOnSetChangeSignal == 1) {
      if (g_global_var_1848 == 1) {
         li_40 = TRUE;
         li_28 = getTFColor(PERIOD_MN1, 1);
         for (int li_24 = 1; li_24 < SwitchSetTF; li_24++) {
            li_32 = getTFColor(gia_2212[li_24], 1);
            if (li_32 != li_28) {
               li_40 = FALSE;
               li_24 = SwitchSetTF;
            }
         }
         if (li_40 == TRUE) return (0);
         if (ai_0 == 0) {
            if (Debug == TRUE) WriteTradeExit("Set Change Exit", ai_4, 0, ad_8, ad_16);
            CloseOrder(ai_4, ad_16, 0);
            return (1);
         }
         if (ai_0 == 1) {
            if (Debug == TRUE) WriteTradeExit("Set Change Exit", ai_4, 1, ad_8, ad_16);
            CloseOrder(ai_4, ad_16, 1);
            return (1);
         }
      }
      if (g_global_var_1848 == 2) {
         li_32 = getTFColor(gia_2212[SwitchSetTF - 1], 1);
         li_36 = getTFColor(gia_2212[SwitchSetTF - 1], 2);
         if (li_32 == li_36) return (0);
         if (ai_0 == 0) {
            if (li_32 == -1) {
               if (Debug == TRUE) WriteTradeExit("Set Change Exit", ai_4, 0, ad_8, ad_16);
               CloseOrder(ai_4, ad_16, 0);
               return (1);
            }
         }
         if (ai_0 == 1) {
            if (li_32 == 1) {
               if (Debug == TRUE) WriteTradeExit("Set Change Exit", ai_4, 1, ad_8, ad_16);
               CloseOrder(ai_4, ad_16, 1);
               return (1);
            }
         }
      }
   }
   return (0);
}

int CheckCloseAfterX_Days(int ai_0, int ai_4, double ad_8, double ad_16, int ai_24) {
   int l_year_28;
   int li_40;
   int li_44;
   if (CloseTradeAfterX_Days <= 0) return (0);
   int l_day_of_year_32 = TimeDayOfYear(ai_24);
   int l_day_of_year_36 = TimeDayOfYear(TimeCurrent());
   if (l_day_of_year_36 < l_day_of_year_32) {
      l_year_28 = Year();
      li_40 = 365;
      if (l_year_28 == 2008 || l_year_28 == 2012 || l_year_28 == 2016 || l_year_28 == 2020) li_40 = 366;
      li_44 = li_40 - l_day_of_year_32 + l_day_of_year_36;
   } else li_44 = l_day_of_year_36 - l_day_of_year_32;
   if (li_44 >= CloseTradeAfterX_Days) {
      if (ai_0 == 0) {
         if (Debug == TRUE) WriteTradeExit("CloseAfterX_Days Exit", ai_4, 0, ad_8, ad_16);
         CloseOrder(ai_4, ad_16, 0);
         return (1);
      }
      if (ai_0 == 1) {
         if (Debug == TRUE) WriteTradeExit("CloseAfterX_Days Exit", ai_4, 1, ad_8, ad_16);
         CloseOrder(ai_4, ad_16, 1);
         return (1);
      }
   }
   return (0);
}

int CheckEndOfMonthClose(int ai_0, int ai_4, double ad_8, double ad_16, int ai_24) {
   int l_month_28;
   if (gi_1208 == 1) {
      l_month_28 = TimeMonth(ai_24);
      if (Month() != l_month_28) {
         if (ai_0 == 0) {
            if (Debug == TRUE) WriteTradeExit("EndOfMonth Exit", ai_4, 0, ad_8, ad_16);
            CloseOrder(ai_4, ad_16, 0);
            return (1);
         }
         if (ai_0 == 1) {
            if (Debug == TRUE) WriteTradeExit("EndOfMonth Exit", ai_4, 1, ad_8, ad_16);
            CloseOrder(ai_4, ad_16, 1);
            return (1);
         }
      }
   }
   return (0);
}

int CheckFRIClose(int ai_0, int ai_4, double ad_8, double ad_16) {
   if (UseFridayClose == 1) {
      if (DayOfWeek() == 5 && Hour() >= gi_1260) {
         if (ai_0 == 0) {
            if (Debug == TRUE) WriteTradeExit("Friday Exit", ai_4, 0, ad_8, ad_16);
            CloseOrder(ai_4, ad_16, 0);
            return (1);
         }
         if (ai_0 == 1) {
            if (Debug == TRUE) WriteTradeExit("Friday Exit", ai_4, 1, ad_8, ad_16);
            CloseOrder(ai_4, ad_16, 1);
            return (1);
         }
      }
   }
   return (0);
}

int CheckSLClose(int ai_0, int ai_4, double ad_8, double ad_16, double ad_unused_24, double ad_32) {
   if (ai_0 == 0) {
      if (ad_32 > 0.1) {
         if (Bid <= ad_32) {
            if (Debug == TRUE) WriteTradeExit("StopLoss Exit", ai_4, 0, ad_8, ad_16);
            CloseOrder(ai_4, ad_16, 0);
            return (1);
         }
      }
      return (0);
   }
   if (ai_0 == 1) {
      if (ad_32 > 0.1) {
         if (Ask >= ad_32) {
            if (Debug == TRUE) WriteTradeExit("StopLoss Exit", ai_4, 1, ad_8, ad_16);
            CloseOrder(ai_4, ad_16, 1);
            return (1);
         }
      }
   }
   return (0);
}

int CheckTPClose(int ai_0, int ai_4, double ad_8, double ad_16, double ad_unused_24, int ai_32, double ad_36) {
   if (ai_32 == 5) {
      if (ai_0 == 0) {
         if (Bid >= ad_36) {
            if (Debug == TRUE) WriteTradeExit("TakeProfit Exit", ai_4, 0, ad_8, ad_16);
            CloseOrder(ai_4, ad_16, 0);
            return (1);
         }
         return (0);
      }
      if (ai_0 == 1) {
         if (Ask <= ad_36) {
            if (Debug == TRUE) WriteTradeExit("TakeProfit Exit", ai_4, 1, ad_8, ad_16);
            CloseOrder(ai_4, ad_16, 1);
            return (1);
         }
      }
   }
   return (0);
}

int CheckExit(int ai_0, int ai_4, double ad_8, double ad_16, int ai_24, int ai_28) {
   int li_32 = 0;
   li_32 = getTFColor(ai_24, ai_28);
   if (ai_0 == 0) {
      if (li_32 == -1) {
         if (Debug == TRUE) WriteTradeExit("Buy Exit Signal", ai_4, 0, ad_16, ad_8);
         CloseOrder(ai_4, ad_8, 0);
         return (1);
      }
      return (0);
   }
   if (ai_0 == 1) {
      if (li_32 == 1) {
         if (Debug == TRUE) WriteTradeExit("Sell Exit Signal", ai_4, 1, ad_16, ad_8);
         CloseOrder(ai_4, ad_8, 1);
         return (1);
      }
      return (0);
   }
   return (0);
}

int CheckPartialClose(int ai_0, int ai_4, double ad_8) {
   string ls_unused_24;
   string ls_unused_32;
   string ls_unused_40;
   double ld_16 = GetTP_Lots(ad_8, 50);
   if (ld_16 > 0.0) {
      if (ai_0 == 0) {
         if (Debug == TRUE) WriteTradeExit("Partial Close Exit", ai_4, 0, 0, ld_16);
         CloseOrder(ai_4, ld_16, 0);
      }
      if (ai_0 == 1) {
         if (Debug == TRUE) WriteTradeExit("Partial Close Exit", ai_4, 1, 0, ld_16);
         CloseOrder(ai_4, ld_16, 1);
      }
      return (1);
   }
   return (0);
}

int ClosePartialLots(int ai_0, int ai_4, double ad_8) {
   bool li_16 = FALSE;
   if (gi_1876 == 1) li_16 = gi_644;
   else li_16 = gi_572;
   if (li_16 == TRUE) {
      switch (ai_0) {
      case 0:
         if (gi_1884 == 1) {
            if (CheckPartialClose(0, ai_4, ad_8) == 1) return (1);
            return (0);
         }
         return (1);
         break;
      case 1:
         if (gi_1884 == 1) {
            if (CheckPartialClose(1, ai_4, ad_8) == 1) return (1);
            return (0);
         }
         return (1);
      }
   }
   return (0);
}

int LastTradeStoppedOut() {
   bool li_12;
   double ld_16;
   double l_ord_open_price_24;
   int li_ret_8 = 0;
   int l_hist_total_4 = OrdersHistoryTotal();
   for (int l_pos_0 = l_hist_total_4 - 1; l_pos_0 >= 0; l_pos_0--) {
      OrderSelect(l_pos_0, SELECT_BY_POS, MODE_HISTORY);
      if (OrderSymbol() == Symbol()) {
         if (OrderMagicNumber() == g_magic_1936) {
            if (StringFind(OrderComment(), HedgeUserComment) < 0) {
               if (StringFind(OrderComment(), UserComment) >= 0) {
                  if (TimeCurrent() - OrderOpenTime() <= 60 * MinutesToDelay) {
                     li_12 = FALSE;
                     ld_16 = OrderClosePrice();
                     l_ord_open_price_24 = OrderOpenPrice();
                     if (OrderType() == OP_BUY) {
                        if (ld_16 - l_ord_open_price_24 < 0.0 && ld_16 - l_ord_open_price_24 > (-1.0 * LossSizeInPips) * gd_1928) {
                           if (OrderCloseTime() - OrderOpenTime() < 120) li_12 = TRUE;
                           l_pos_0 = 0;
                        }
                     }
                     if (OrderType() == OP_SELL) {
                        if (l_ord_open_price_24 - ld_16 < 0.0 && l_ord_open_price_24 - ld_16 > (-1.0 * LossSizeInPips) * gd_1928) {
                           if (OrderCloseTime() - OrderOpenTime() < 120) li_12 = TRUE;
                           l_pos_0 = 0;
                        }
                     }
                  }
               }
            }
         }
      }
   }
   if (li_12) li_ret_8 = OrderCloseTime() + 60 * MinutesToDelay;
   return (li_ret_8);
}

int LastHedgeTradeStoppedOut() {
   bool li_12;
   double ld_16;
   double l_ord_open_price_24;
   int li_ret_8 = 0;
   int l_hist_total_4 = OrdersHistoryTotal();
   for (int l_pos_0 = l_hist_total_4 - 1; l_pos_0 >= 0; l_pos_0--) {
      OrderSelect(l_pos_0, SELECT_BY_POS, MODE_HISTORY);
      if (OrderSymbol() == Symbol()) {
         if (OrderMagicNumber() == g_magic_1940) {
            if (StringFind(OrderComment(), HedgeUserComment) >= 0) {
               if (TimeCurrent() - OrderOpenTime() <= 60 * MinutesToDelay) {
                  li_12 = FALSE;
                  ld_16 = OrderClosePrice();
                  l_ord_open_price_24 = OrderOpenPrice();
                  if (OrderType() == OP_BUY) {
                     if (ld_16 - l_ord_open_price_24 < 0.0 && ld_16 - l_ord_open_price_24 > (-1.0 * LossSizeInPips) * gd_1928) {
                        if (OrderCloseTime() - OrderOpenTime() < 120) li_12 = TRUE;
                        l_pos_0 = 0;
                     }
                  }
                  if (OrderType() == OP_SELL) {
                     if (l_ord_open_price_24 - ld_16 < 0.0 && l_ord_open_price_24 - ld_16 > (-1.0 * LossSizeInPips) * gd_1928) {
                        if (OrderCloseTime() - OrderOpenTime() < 120) li_12 = TRUE;
                        l_pos_0 = 0;
                     }
                  }
               }
            }
         }
      }
   }
   if (li_12) li_ret_8 = OrderCloseTime() + 60 * MinutesToDelay;
   return (li_ret_8);
}

double RoundLots(double ad_0, int ai_8) {
   double ld_ret_12;
   if (RoundUp == 0) {
      switch (ai_8) {
      case 0:
         ld_ret_12 = MathFloor(ad_0);
         break;
      case 1:
         ld_ret_12 = MathFloor(10.0 * ad_0) / 10.0;
         break;
      case 2:
         ld_ret_12 = MathFloor(100.0 * ad_0) / 100.0;
      }
   } else {
      switch (ai_8) {
      case 0:
         ld_ret_12 = MathCeil(ad_0);
         break;
      case 1:
         ld_ret_12 = MathCeil(10.0 * ad_0) / 10.0;
         break;
      case 2:
         ld_ret_12 = MathCeil(100.0 * ad_0) / 100.0;
      }
   }
   return (ld_ret_12);
}

double GetTP_Lots(double ad_0, int ai_8) {
   double ld_ret_12 = 0;
   if (AccountIsMicro == TRUE) {
      if (BrokerIsIBFX == TRUE) {
         if (ad_0 > 0.1) {
            ld_ret_12 = RoundLots(ad_0 * ai_8 / 100.0, 2);
            if (ld_ret_12 < 0.1) ld_ret_12 = 0.1;
         }
         return (ld_ret_12);
      }
      if (ad_0 > 0.01) {
         ld_ret_12 = RoundLots(ad_0 * ai_8 / 100.0, 2);
         if (ld_ret_12 < 0.01) ld_ret_12 = 0.01;
      }
      return (ld_ret_12);
   }
   if (AccountIsMini == TRUE) {
      if (BrokerIsCrownForex == TRUE) {
         if (ad_0 > 10000.0) {
            ld_ret_12 = RoundLots(ad_0 * ai_8 / 100.0, 0);
            if (ld_ret_12 < 10000.0) ld_ret_12 = 10000;
         }
         return (ld_ret_12);
      }
      if (BrokerIsIBFX == TRUE) {
         if (ad_0 > 1.0) {
            ld_ret_12 = RoundLots(ad_0 * ai_8 / 100.0, 2);
            if (ld_ret_12 < 1.0) ld_ret_12 = 1;
         }
         return (ld_ret_12);
      }
      if (ad_0 > 0.1) {
         ld_ret_12 = RoundLots(ad_0 * ai_8 / 100.0, 1);
         if (ld_ret_12 < 0.1) ld_ret_12 = 0.1;
      }
      return (ld_ret_12);
   }
   if (BrokerIsCrownForex == TRUE) {
      if (ad_0 > 10000.0) {
         ld_ret_12 = RoundLots(ad_0 * ai_8 / 100.0, 0);
         if (ld_ret_12 < 10000.0) ld_ret_12 = 10000;
      }
      return (ld_ret_12);
   }
   if (ad_0 > 1.0) {
      ld_ret_12 = RoundLots(ad_0 * ai_8 / 100.0, 0);
      if (ld_ret_12 < 1.0) ld_ret_12 = 1;
   }
   return (ld_ret_12);
}

double GetLots() {
   double ld_ret_0;
   double l_maxlot_8 = MarketInfo(Symbol(), MODE_MAXLOT);
   if (UseMoneyManagement == FALSE) return (Lots);
   if (BrokerIsIBFX == TRUE) {
      ld_ret_0 = Calc_IBFX_Money_Management();
      return (ld_ret_0);
   }
   if (BrokerIsCrownForex == TRUE) {
      ld_ret_0 = Calc_CrownMoney_Management();
      return (ld_ret_0);
   }
   ld_ret_0 = NormalizeDouble(AccountEquity() * TradeSizePercent / 10000.0 / 10.0, 2);
   if (AccountIsMicro == TRUE) {
      ld_ret_0 = MathFloor(100.0 * ld_ret_0) / 100.0;
      if (ld_ret_0 < 0.01) ld_ret_0 = 0.01;
      if (ld_ret_0 > l_maxlot_8) ld_ret_0 = l_maxlot_8;
      return (ld_ret_0);
   }
   if (AccountIsMini == TRUE) {
      ld_ret_0 = MathFloor(10.0 * ld_ret_0) / 10.0;
      if (ld_ret_0 < 0.1) ld_ret_0 = 0.1;
      if (ld_ret_0 > l_maxlot_8) ld_ret_0 = l_maxlot_8;
      return (ld_ret_0);
   }
   if (BrokerPermitsFractionalLots == FALSE) {
      if (ld_ret_0 >= 1.0) ld_ret_0 = MathFloor(ld_ret_0);
      else ld_ret_0 = 1.0;
   }
   if (ld_ret_0 < 1.0) ld_ret_0 = 1.0;
   if (ld_ret_0 > l_maxlot_8) ld_ret_0 = l_maxlot_8;
   return (ld_ret_0);
}

double Calc_CrownMoney_Management() {
   double ld_ret_0 = NormalizeDouble(AccountEquity() * TradeSizePercent / 10000.0 / 10.0, 1);
   if (AccountIsMini == TRUE) {
      ld_ret_0 = 10000.0 * (MathFloor(10.0 * ld_ret_0) / 10.0);
      if (ld_ret_0 < 10000.0) ld_ret_0 = 10000;
      return (ld_ret_0);
   }
   ld_ret_0 = 100000.0 * ld_ret_0;
   if (ld_ret_0 < 100000.0) ld_ret_0 = 100000;
   return (ld_ret_0);
}

double Calc_IBFX_Money_Management() {
   double l_maxlot_8 = MarketInfo(Symbol(), MODE_MAXLOT);
   double ld_ret_0 = NormalizeDouble(AccountEquity() * TradeSizePercent / 10000.0 / 10.0, 2);
   if (AccountIsMicro == TRUE) {
      ld_ret_0 = 10.0 * ld_ret_0;
      ld_ret_0 = MathFloor(100.0 * ld_ret_0) / 100.0;
      if (ld_ret_0 < 0.1) ld_ret_0 = 0.1;
      if (ld_ret_0 > l_maxlot_8) ld_ret_0 = l_maxlot_8;
      return (ld_ret_0);
   }
   if (AccountIsMini == TRUE) {
      ld_ret_0 = 10.0 * ld_ret_0;
      ld_ret_0 = MathFloor(10.0 * ld_ret_0) / 10.0;
      if (ld_ret_0 < 1.0) ld_ret_0 = 1;
      if (ld_ret_0 > l_maxlot_8) ld_ret_0 = l_maxlot_8;
      return (ld_ret_0);
   }
   if (BrokerPermitsFractionalLots == TRUE) ld_ret_0 = StrToDouble(DoubleToStr(ld_ret_0, 2));
   else ld_ret_0 = MathFloor(ld_ret_0);
   if (ld_ret_0 > l_maxlot_8) ld_ret_0 = l_maxlot_8;
   return (ld_ret_0);
}

int SpreadOK() {
   if (Ask - Bid > MaxSpread * gd_1928) return (0);
   return (1);
}

int CheckTradeFilters() {
   bool li_4;
   int li_ret_0 = 0;
   if (gi_84 == TRUE) {
      li_4 = FALSE;
      if (Year() > gi_96) li_4 = TRUE;
      if (li_4 == FALSE) {
         if (Year() == gi_96 && Month() > gi_88) li_4 = TRUE;
         if (li_4 == FALSE)
            if (Year() == gi_96 && Month() == gi_88 && Day() > gi_92) li_4 = TRUE;
      }
      if (li_4 == TRUE) OutputComment1ToChart("EA has expired - renew license");
      li_ret_0 = li_4;
   }
   if (li_ret_0 == 0) {
      if (UseTradingHours == 1) {
         li_ret_0 = CheckTradingTimes();
         if (li_ret_0 == 1) OutputComment1ToChart("Trading has been stopped - wrong time of day");
         else OutputComment1ToChart("Trading has resumed - time is OK");
      } else OutputComment1ToChart("Trading Time filter not in use");
   }
   if (li_ret_0 == 0) {
      if (UseSpreadDetection == TRUE) {
         if (SpreadOK() == 0) {
            li_ret_0 = 1;
            OutputComment2ToChart("Trading has been stopped - Spread too large");
         } else OutputComment2ToChart("Trading has resumed - Spread is OK");
      } else OutputComment2ToChart("Spread Detection not in use");
   }
   if (li_ret_0 == 0) {
      if (UseFridayNoTradeTime == TRUE) {
         if (DayOfWeek() == 5 && Hour() >= gi_1264) {
            li_ret_0 = 1;
            OutputComment2ToChart("Trading has been stopped - Friday");
         }
      }
   }
   if (li_ret_0 == 0) {
      if (gi_1212 == 1) {
         if (CheckEndOfMonth() == 1) {
            li_ret_0 = 1;
            OutputComment2ToChart("Trading has been stopped - End of Month");
         }
      }
   }
   return (li_ret_0);
}

int CheckEndOfMonth() {
   int li_0;
   switch (Month()) {
   case 1:
      li_0 = 31 - gi_1216;
      break;
   case 2:
      li_0 = 28 - gi_1216;
      break;
   case 3:
      li_0 = 31 - gi_1216;
      break;
   case 4:
      li_0 = 30 - gi_1216;
      break;
   case 5:
      li_0 = 31 - gi_1216;
      break;
   case 6:
      li_0 = 30 - gi_1216;
      break;
   case 7:
      li_0 = 31 - gi_1216;
      break;
   case 8:
      li_0 = 31 - gi_1216;
      break;
   case 9:
      li_0 = 30 - gi_1216;
      break;
   case 10:
      li_0 = 31 - gi_1216;
      break;
   case 111:
      li_0 = 30 - gi_1216;
      break;
   case 12:
      li_0 = 31 - gi_1216;
   }
   if (Day() < li_0) return (0);
   return (1);
}

void CorrectTimeFilterSettings() {
   int li_0 = 100 * gi_1288;
   if (UseDST == TRUE) li_0 -= 100;
   gi_1824 = myAsianStart + li_0;
   gi_1828 = myAsianStop + li_0;
   gi_1832 = myEurStart + li_0;
   gi_1836 = myEurStop + li_0;
   gi_1840 = myNYStart + li_0;
   gi_1844 = myNYStop + li_0;
}

int IsNotValidTradingTime(int ai_0, int ai_4) {
   int li_8 = 100 * Hour() + Minute();
   if (ai_0 <= ai_4) {
      if (li_8 < ai_0 || li_8 > ai_4) return (1);
   } else
      if (li_8 > ai_4 && li_8 < ai_0) return (1);
   return (0);
}

int CheckTradingTimes() {
   int li_4 = 100 * Hour() + Minute();
   int li_ret_0 = 1;
   if (TradeAsianMarket == 1) li_ret_0 = IsNotValidTradingTime(gi_1824, gi_1828);
   if (li_ret_0 == 1)
      if (TradeEuropeanMarket == 1) li_ret_0 = IsNotValidTradingTime(gi_1832, gi_1836);
   if (li_ret_0 == 1)
      if (TradeNewYorkMarket == 1) li_ret_0 = IsNotValidTradingTime(gi_1840, gi_1844);
   return (li_ret_0);
}

double SetPoint() {
   double ld_ret_0;
   if (Digits < 4) ld_ret_0 = 0.01;
   else ld_ret_0 = 0.0001;
   return (ld_ret_0);
}

int GetCorrectInputSet(int ai_0, int ai_4) {
   int li_12;
   int li_16;
   int li_20;
   int li_24;
   bool li_8 = TRUE;
   int li_28 = 1;
   if (UseSwitchSet == 1) {
      li_20 = gia_2212[SwitchSetTF - 1];
      li_24 = getTFColor(li_20, 1);
      for (int li_32 = UpperTF_Set1; li_32 < SwitchSetTF; li_32++) {
         li_16 = gia_2212[li_32 - 1];
         li_12 = getTFColor(li_16, 1);
         if (li_12 != li_24) {
            li_8 = FALSE;
            li_32 = SwitchSetTF;
         }
      }
      if (li_8 == TRUE) li_28 = 1;
      else li_28 = 2;
   }
   if (ai_4 == 1) li_28 = ai_0;
   if (li_28 == 1) {
      gi_1852 = HigherTF_Set1;
      gi_1856 = LowerTF_Set1;
      gi_1860 = UpperTF_Set1;
      gi_1732 = gia_2212[gi_1852 - 1];
      gi_1736 = gia_2212[gi_1856 - 1];
      gi_1740 = gia_2212[gi_1860 - 1];
      gi_1864 = ConfirmUpperTimeFrame_Set1;
      gi_1868 = ConfirmAllTimeFrames_Set1;
      gi_1872 = ExitOnAlertedProfit_Set1;
      gi_1876 = UseTrigger_Set1;
      gi_1880 = ExitTrigger_Set1;
      gi_1884 = UsePartialClose_Set1;
      gi_1888 = mStopLoss_Set1;
      gi_1892 = TrailingStopMethod_Set1;
      gi_1896 = TakeProfitW1_Set1;
      gi_1900 = TakeProfitD1_Set1;
      gi_1904 = TakeProfitH4_Set1;
      gi_1908 = TakeProfitH1_Set1;
      gi_1912 = TakeProfitM30_Set1;
      gi_1916 = TakeProfitM15_Set1;
      gi_1920 = TakeProfitM5_Set1;
      gi_1924 = BreakEvenLock_Set1;
      return (1);
   }
   gi_1852 = HigherTF_Set2;
   gi_1856 = LowerTF_Set2;
   gi_1860 = SwitchSetTF;
   gi_1732 = gia_2212[gi_1852 - 1];
   gi_1736 = gia_2212[gi_1856 - 1];
   gi_1740 = gia_2212[gi_1860 - 1];
   gi_1864 = ConfirmUpperTimeFrame_Set2;
   gi_1868 = ConfirmAllTimeFrames_Set2;
   gi_1872 = ExitOnAlertedProfit_Set2;
   gi_1876 = UseTrigger_Set2;
   gi_1880 = ExitTrigger_Set2;
   gi_1884 = UsePartialClose_Set2;
   gi_1888 = mStopLoss_Set2;
   gi_1892 = TrailingStopMethod_Set2;
   gi_1896 = TakeProfitW1_Set2;
   gi_1900 = TakeProfitD1_Set2;
   gi_1904 = TakeProfitH4_Set2;
   gi_1908 = TakeProfitH1_Set2;
   gi_1912 = TakeProfitM30_Set2;
   gi_1916 = TakeProfitM15_Set2;
   gi_1920 = TakeProfitM5_Set2;
   gi_1924 = BreakEvenLock_Set2;
   return (2);
}

int CheckAccountNumber() {
   //if (YourAccountNumber == AccountNumber() && YourAccountNumber == gi_76) return (1);
   if (YourAccountNumber == gi_76) return (1);
   else {
      Alert("AccountNumber entered is incorrect.\n You entered ", YourAccountNumber);
      return (0);
   }
}

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

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

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

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

double ValidStopLoss(int ai_0, double ad_4, double ad_12) {
   if (ad_12 < 0.1) return (ad_12);
   double ld_20 = MarketInfo(Symbol(), MODE_STOPLEVEL);
   if (Digits == 3 || Digits == 5) ld_20 /= 10.0;
   if (ai_0 == 0)
      if (ad_4 - ad_12 < ld_20 * gd_1928) ad_12 = ad_4 - ld_20 * gd_1928;
   if (ai_0 == 1)
      if (ad_12 - ad_4 < ld_20 * gd_1928) ad_12 = ad_4 + ld_20 * gd_1928;
   return (ad_12);
}

int CalculateCurrentOrders(int a_magic_0) {
   int l_count_4 = 0;
   int l_count_8 = 0;
   int li_ret_12 = 0;
   for (int l_pos_16 = 0; l_pos_16 < OrdersTotal(); l_pos_16++) {
      OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()) {
         if (OrderMagicNumber() == a_magic_0) {
            if (OrderType() == OP_BUY) l_count_4++;
            if (OrderType() == OP_SELL) l_count_8++;
         }
      }
   }
   li_ret_12 = l_count_4 + l_count_8;
   return (li_ret_12);
}

int CalTP(int a_timeframe_0) {
   int li_ret_4 = 0;
   if (TPMethod == 1) li_ret_4 = MathRound(iATR(Symbol(), a_timeframe_0, TPAtrPeriod, 1) * TPAtrPercentage / gd_1928);
   else {
      switch (a_timeframe_0) {
      case PERIOD_W1:
         li_ret_4 = gi_1896;
         break;
      case PERIOD_D1:
         li_ret_4 = gi_1900;
         break;
      case PERIOD_H4:
         li_ret_4 = gi_1904;
         break;
      case PERIOD_H1:
         li_ret_4 = gi_1908;
         break;
      case PERIOD_M30:
         li_ret_4 = gi_1912;
         break;
      case PERIOD_M15:
         li_ret_4 = gi_1916;
         break;
      case PERIOD_M5:
         li_ret_4 = gi_1920;
         break;
      default:
         li_ret_4 = 5;
      }
   }
   return (li_ret_4);
}

int CalSL(int a_timeframe_0) {
   bool li_ret_4 = FALSE;
   if (SLMethod == 1) li_ret_4 = MathRound(iATR(Symbol(), a_timeframe_0, SLAtrPeriod, 1) * SLAtrPercentage / gd_1928);
   else li_ret_4 = gi_1888;
   return (li_ret_4);
}

string isUporDown(int ai_0) {
   if (ai_0 == 1) return ("LONG");
   else
      if (ai_0 == -1) return ("SHORT");
   return ("");
}

string tf2txt(int ai_0) {
   switch (ai_0) {
   case 1:
      return ("M1");
   case 5:
      return ("M5");
   case 15:
      return ("M15");
   case 30:
      return ("M30");
   case 60:
      return ("H1");
   case 240:
      return ("H4");
   case 1440:
      return ("D1");
   case 10080:
      return ("W1");
   case 43200:
      return ("MN");
   }
   return ("??");
}

void TrackPrices() {
   if (Bid < gd_1792) {
      gd_1792 = Bid;
      SaveLowBid(gd_1792);
   }
   if (Bid > gd_1784) {
      gd_1784 = Bid;
      SaveHighBid(gd_1784);
   }
   if (Ask < gd_1808) {
      gd_1808 = Ask;
      SaveLowAsk(gd_1808);
   }
   if (Ask > gd_1800) {
      gd_1800 = Ask;
      SaveHighAsk(gd_1800);
   }
}

void PlotDD(int ai_0, double ad_4) {
   double ld_12;
   double ld_20;
   double ld_28;
   if (ai_0 == 0) {
      ld_12 = (ad_4 - gd_1792) / gd_1928;
      ld_20 = (ad_4 - Bid) / gd_1928;
      ld_28 = (gd_1784 - ad_4) / gd_1928;
      if (ld_20 < 0.0) {
         if (gd_1792 < 0.1) OutputDrawDownToChart(" Current Profit : " + DoubleToStr(-ld_20, 0) + " Max Profit : " + "NA" + " Max Drawdown : " + "NA");
         else OutputDrawDownToChart(" Current Profit : " + DoubleToStr(-ld_20, 0) + " Max Profit : " + DoubleToStr(ld_28, 0) + " Max Drawdown : " + DoubleToStr(ld_12, 0));
      } else {
         if (gd_1792 < 0.1) OutputDrawDownToChart(" Current Drawdown : " + DoubleToStr(ld_20, 0) + " Max Profit : " + "NA" + " Max Drawdown : " + "NA");
         else OutputDrawDownToChart(" Current Drawdown : " + DoubleToStr(ld_20, 0) + " Max Profit : " + DoubleToStr(ld_28, 0) + " Max Drawdown : " + DoubleToStr(ld_12, 0));
      }
   }
   if (ai_0 == 1) {
      ld_12 = (gd_1800 - ad_4) / gd_1928;
      ld_20 = (Ask - ad_4) / gd_1928;
      ld_28 = (ad_4 - gd_1808) / gd_1928;
      if (ld_20 < 0.0) {
         if (gd_1800 < 0.1) {
            OutputDrawDownToChart(" Current Profit : " + DoubleToStr(-ld_20, 0) + " Max Profit : " + "NA" + " Max Drawdown : " + "NA");
            return;
         }
         OutputDrawDownToChart(" Current Profit : " + DoubleToStr(-ld_20, 0) + " Max Profit : " + DoubleToStr(ld_28, 0) + " Max Drawdown : " + DoubleToStr(ld_12, 0));
         return;
      }
      if (gd_1800 < 0.1) {
         OutputDrawDownToChart(" Current Drawdown : " + DoubleToStr(ld_20, 0) + " Max Profit : " + "NA" + " Max Drawdown : " + "NA");
         return;
      }
      OutputDrawDownToChart(" Current Drawdown : " + DoubleToStr(ld_20, 0) + " Max Profit : " + DoubleToStr(ld_28, 0) + " Max Drawdown : " + DoubleToStr(ld_12, 0));
   }
}

int func_Symbol2Val(string as_0) {
   string ls_8 = StringSubstr(as_0, 0, 6);
   if (ls_8 == "AUDCAD") return (1);
   if (ls_8 == "AUDJPY") return (2);
   if (ls_8 == "AUDNZD") return (3);
   if (ls_8 == "AUDUSD") return (4);
   if (ls_8 == "CHFJPY") return (5);
   if (ls_8 == "EURAUD") return (6);
   if (ls_8 == "EURCAD") return (7);
   if (ls_8 == "EURCHF") return (8);
   if (ls_8 == "EURGBP") return (9);
   if (ls_8 == "EURJPY") return (10);
   if (ls_8 == "EURUSD") return (11);
   if (ls_8 == "GBPCHF") return (12);
   if (ls_8 == "GBPJPY") return (13);
   if (ls_8 == "GBPUSD") return (14);
   if (ls_8 == "NZDJPY") return (15);
   if (ls_8 == "NZDUSD") return (16);
   if (ls_8 == "USDCAD") return (17);
   if (ls_8 == "USDCHF") return (18);
   if (ls_8 == "USDJPY") return (19);
   Comment("unexpected Symbol");
   return (999);
}

int func_TimeFrame_Const2Val(int ai_0) {
   switch (ai_0) {
   case 1:
      return (1);
   case 5:
      return (2);
   case 15:
      return (3);
   case 30:
      return (4);
   case 60:
      return (5);
   case 240:
      return (6);
   case 1440:
      return (7);
   case 10080:
      return (8);
   case 43200:
      return (9);
   }
   return (0);
}

void HandleTrailingStop(int ai_0, int ai_4, double ad_8, double ad_16, double ad_24) {
   switch (gi_1892) {
   case 1:
      MoveTP(ai_0, ai_4, ad_8, ad_16, ad_24);
      return;
   case 2:
      Trail_TP_BreakEven(ai_0, ai_4, ad_8, ad_16, ad_24);
      return;
   case 3:
      Trail_TP_Delayed(ai_0, ai_4, ad_8, ad_16, ad_24);
      return;
   case 4:
      BreakEven_TrailingStop(ai_0, ai_4, ad_8, ad_16, ad_24);
      return;
   case 5:
      Delayed_TrailingStop(ai_0, ai_4, ad_8, ad_16, ad_24);
      return;
   case 6:
      Immediate_TrailingStop(ai_0, ai_4, ad_8, ad_16, ad_24);
      return;
   case 7:
      Immediate_TrailingStopBE(ai_0, ai_4, ad_8, ad_16, ad_24);
      return;
   }
   MoveTP(ai_0, ai_4, ad_8, ad_16, ad_24);
}

void MoveTP(int ai_0, int ai_4, double ad_8, double ad_16, double ad_24) {
   double ld_32;
   if (gi_1872 != 5) {
      if (gi_572 == TRUE) {
         if (ai_0 == 0) {
            ld_32 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_32 = TakeLong(Bid, ld_32);
            ld_32 = NormalizeDouble(ld_32, Digits);
            if (ad_24 < ld_32) ModifyOrder(ai_4, ad_8, ad_16, ld_32, LightGreen);
         }
         if (ai_0 == 1) {
            ld_32 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_32 = TakeShort(Ask, ld_32);
            ld_32 = NormalizeDouble(ld_32, Digits);
            if (ad_24 > ld_32) ModifyOrder(ai_4, ad_8, ad_16, ld_32, DarkOrange);
         }
      }
   }
}

void Trail_TP_BreakEven(int ai_0, int ai_4, double ad_8, double ad_16, double ad_24) {
   int li_36;
   double ld_40;
   double ld_48;
   double ld_72;
   double ld_88;
   bool li_32 = FALSE;
   double ld_64 = ad_16;
   double ld_56 = ad_24;
   if (gi_1876 == 1) li_36 = gi_644;
   else li_36 = gi_572;
   if (ai_0 == 0) {
      if (li_36 == 1) {
         ld_72 = ad_8 + g_global_var_984 * gd_1928;
         if (gi_1876 == 1) ld_88 = ad_8 + gi_1880 * gd_1928;
         else ld_88 = ld_72;
         if (Bid >= ld_88) {
            if (ad_16 < ad_8) {
               li_32 = TRUE;
               ld_48 = ad_8 + gi_1924 * gd_1928;
               gd_2180 = ld_48;
               SaveIntSTprice(gd_2180);
               ld_48 = ValidStopLoss(0, Bid, ld_48);
               ld_48 = NormalizeDouble(ld_48, Digits);
               ld_64 = ld_48;
            }
         }
      }
      if (gi_1872 != 5) {
         if (gi_572 == TRUE) {
            ld_40 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_40 = TakeLong(Bid, ld_40);
            ld_40 = NormalizeDouble(ld_40, Digits);
            if (ad_24 < ld_40) {
               li_32 = TRUE;
               ld_56 = ld_40;
            }
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_64, ld_56, LightGreen);
   }
   if (ai_0 == 1) {
      if (li_36 == 1) {
         ld_72 = ad_8 - g_global_var_984 * gd_1928;
         if (gi_1876 == 1) ld_88 = ad_8 - gi_1880 * gd_1928;
         else ld_88 = ld_72;
         if (Ask <= ld_88) {
            if (ad_16 > ad_8 || ad_16 < 0.1) {
               li_32 = TRUE;
               ld_48 = ad_8 - gi_1924 * gd_1928;
               gd_2180 = ld_48;
               SaveIntSTprice(gd_2180);
               ld_48 = ValidStopLoss(1, Ask, ld_48);
               ld_48 = NormalizeDouble(ld_48, Digits);
               ld_64 = ld_48;
            }
         }
      }
      if (gi_1872 != 5) {
         if (gi_572 == TRUE) {
            ld_40 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_40 = TakeShort(Ask, ld_40);
            ld_40 = NormalizeDouble(ld_40, Digits);
            if (ad_24 > ld_40) {
               li_32 = TRUE;
               ld_56 = ld_40;
            }
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_64, ld_56, DarkOrange);
   }
}

void Trail_TP_Delayed(int ai_0, int ai_4, double ad_8, double ad_16, double ad_24) {
   int li_36;
   double ld_40;
   double ld_48;
   double ld_72;
   double ld_88;
   bool li_32 = FALSE;
   double ld_64 = ad_16;
   double ld_56 = ad_24;
   if (gi_1876 == 1) li_36 = gi_644;
   else li_36 = gi_572;
   if (ai_0 == 0) {
      if (li_36 == 1) {
         ld_72 = ad_8 + g_global_var_984 * gd_1928;
         if (gi_1876 == 1) ld_88 = ad_8 + gi_1880 * gd_1928;
         else ld_88 = ld_72;
         if (Bid >= ld_88) {
            ld_48 = Bid - TrailingStop * gd_1928;
            ld_48 = ValidStopLoss(0, Bid, ld_48);
            ld_48 = NormalizeDouble(ld_48, Digits);
            if (ad_16 < ld_48) {
               gi_1080 = ld_48;
               SaveInternalStopLoss(gi_1080);
               ld_64 = ld_48;
               li_32 = TRUE;
            }
         }
      }
      if (gi_1872 != 5) {
         if (gi_572 == TRUE) {
            ld_40 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_40 = TakeLong(Bid, ld_40);
            ld_40 = NormalizeDouble(ld_40, Digits);
            if (ad_24 < ld_40) {
               li_32 = TRUE;
               ld_56 = ld_40;
            }
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_64, ld_56, LightGreen);
   }
   if (ai_0 == 1) {
      if (li_36 == 1) {
         ld_72 = ad_8 - g_global_var_984 * gd_1928;
         if (gi_1876 == 1) ld_88 = ad_8 - gi_1880 * gd_1928;
         else ld_88 = ld_72;
         if (Ask <= ld_88) {
            ld_48 = Ask + TrailingStop * gd_1928;
            ld_48 = ValidStopLoss(1, Ask, ld_48);
            ld_48 = NormalizeDouble(ld_48, Digits);
            if (ad_16 > ld_48 || ad_16 < 0.1) {
               gd_2180 = ld_48;
               SaveIntSTprice(gd_2180);
               ld_64 = ld_48;
               li_32 = TRUE;
            }
         }
      }
      if (gi_1872 != 5) {
         if (gi_572 == TRUE) {
            ld_40 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_40 = TakeShort(Ask, ld_40);
            ld_40 = NormalizeDouble(ld_40, Digits);
            if (ad_24 > ld_40) {
               li_32 = TRUE;
               ld_56 = ld_40;
            }
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_64, ld_56, DarkOrange);
   }
}

void BreakEven_TrailingStop(int ai_0, int ai_4, double ad_8, double ad_16, double ad_24) {
   double ld_36;
   double ld_44;
   double ld_68;
   bool li_32 = FALSE;
   double ld_60 = ad_16;
   double ld_52 = ad_24;
   if (ai_0 == 0) {
      ld_68 = ad_8 + g_global_var_984 * gd_1928;
      if (Bid >= ad_8 + BreakEvenLevel * gd_1928) {
         if (ad_16 < ad_8) {
            li_32 = TRUE;
            ld_44 = ad_8 + LockInPips * gd_1928;
            gd_2180 = ld_44;
            SaveIntSTprice(gd_2180);
            ld_44 = ValidStopLoss(0, Bid, ld_44);
            ld_44 = NormalizeDouble(ld_44, Digits);
            ld_60 = ld_44;
         }
      }
      if (gi_1872 != 5) {
         if (gi_572 == TRUE) {
            ld_36 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_36 = TakeLong(Bid, ld_36);
            ld_36 = NormalizeDouble(ld_36, Digits);
            if (ad_24 < ld_36) {
               li_32 = TRUE;
               ld_52 = ld_36;
            }
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_60, ld_52, LightGreen);
   }
   if (ai_0 == 1) {
      ld_68 = ad_8 - g_global_var_984 * gd_1928;
      if (Ask <= ad_8 - BreakEvenLevel * gd_1928) {
         if (ad_16 > ad_8 || ad_16 < 0.1) {
            li_32 = TRUE;
            ld_44 = ad_8 - LockInPips * gd_1928;
            gd_2180 = ld_44;
            SaveIntSTprice(gd_2180);
            ld_44 = ValidStopLoss(1, Ask, ld_44);
            ld_44 = NormalizeDouble(ld_44, Digits);
            ld_60 = ld_44;
         }
      }
      if (gi_1872 != 5) {
         if (gi_572 == TRUE) {
            ld_36 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_36 = TakeShort(Ask, ld_36);
            ld_36 = NormalizeDouble(ld_36, Digits);
            if (ad_24 > ld_36) {
               li_32 = TRUE;
               ld_52 = ld_36;
            }
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_60, ld_52, DarkOrange);
   }
}

void Delayed_TrailingStop(int ai_0, int ai_4, double ad_8, double ad_16, double ad_24) {
   double ld_40;
   double ld_48;
   double ld_72;
   bool li_32 = FALSE;
   double ld_64 = ad_16;
   double ld_56 = ad_24;
   if (ai_0 == 0) {
      ld_72 = ad_8 + g_global_var_984 * gd_1928;
      if (Bid >= ad_8 + BeginTrailingStop * gd_1928) {
         ld_48 = Bid - BeginTrailingStop * gd_1928;
         ld_48 = ValidStopLoss(0, Bid, ld_48);
         ld_48 = NormalizeDouble(ld_48, Digits);
         if (ad_16 < ld_48) {
            gd_2180 = ld_48;
            SaveIntSTprice(gd_2180);
            ld_64 = ld_48;
            li_32 = TRUE;
         }
      }
      if (gi_1872 != 5) {
         if (gi_572 == TRUE) {
            ld_40 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_40 = TakeLong(Bid, ld_40);
            ld_40 = NormalizeDouble(ld_40, Digits);
            if (ad_24 < ld_40) {
               li_32 = TRUE;
               ld_56 = ld_40;
            }
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_64, ld_56, LightGreen);
   }
   if (ai_0 == 1) {
      ld_72 = ad_8 - g_global_var_984 * gd_1928;
      if (Ask <= ad_8 - BeginTrailingStop * gd_1928) {
         ld_48 = Ask + BeginTrailingStop * gd_1928;
         ld_48 = ValidStopLoss(1, Ask, ld_48);
         ld_48 = NormalizeDouble(ld_48, Digits);
         if (ad_16 > ld_48 || ad_16 < 0.1) {
            gd_2180 = ld_48;
            SaveIntSTprice(gd_2180);
            ld_64 = ld_48;
            li_32 = TRUE;
         }
      }
      if (gi_1872 != 5) {
         if (gi_572 == TRUE) {
            ld_40 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_40 = TakeShort(Ask, ld_40);
            ld_40 = NormalizeDouble(ld_40, Digits);
            if (ad_24 > ld_40) {
               li_32 = TRUE;
               ld_56 = ld_40;
            }
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_64, ld_56, DarkOrange);
   }
}

void Immediate_TrailingStop(int ai_0, int ai_4, double ad_8, double ad_16, double ad_24) {
   double ld_36;
   double ld_44;
   bool li_32 = FALSE;
   double ld_60 = ad_16;
   double ld_52 = ad_24;
   if (ai_0 == 0) {
      ld_44 = Bid - MathFloor(gi_1084 * SafetyStopLossMultiplier) * gd_1928;
      if (Digits > 0) ld_44 = NormalizeDouble(ld_44, Digits);
      ld_44 = ValidStopLoss(0, Bid, ld_44);
      ld_44 = NormalizeDouble(ld_44, Digits);
      if (ad_16 < ld_44) {
         li_32 = TRUE;
         ld_60 = ld_44;
         gd_2180 = Bid - gi_1084 * gd_1928;
         SaveIntSTprice(gd_2180);
      }
      if (gi_572 == TRUE) {
         ld_36 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
         ld_36 = TakeLong(Bid, ld_36);
         ld_36 = NormalizeDouble(ld_36, Digits);
         if (ad_24 < ld_36) {
            li_32 = TRUE;
            ld_52 = ld_36;
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_60, ld_52, LightGreen);
   }
   if (ai_0 == 1) {
      ld_44 = Ask + MathFloor(gi_1084 * SafetyStopLossMultiplier) * gd_1928;
      if (Digits > 0) ld_44 = NormalizeDouble(ld_44, Digits);
      ld_44 = ValidStopLoss(1, Ask, ld_44);
      ld_44 = NormalizeDouble(ld_44, Digits);
      if (ad_16 > ld_44) {
         li_32 = TRUE;
         ld_60 = ld_44;
         gd_2180 = Ask + gi_1084 * gd_1928;
         SaveIntSTprice(gd_2180);
      }
      if (gi_1872 != 5) {
         if (gi_572 == TRUE) {
            ld_36 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_36 = TakeShort(Ask, ld_36);
            ld_36 = NormalizeDouble(ld_36, Digits);
            if (ad_24 > ld_36) {
               li_32 = TRUE;
               ld_52 = ld_36;
            }
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_60, ld_52, DarkOrange);
   }
}

void Immediate_TrailingStopBE(int ai_0, int ai_4, double ad_8, double ad_16, double ad_24) {
   double ld_32;
   double ld_40;
   double ld_48;
   if (ai_0 == 0) {
      if (ad_16 > ad_8 + LockInPips * gd_1928) return;
      ld_32 = gi_1084 * gd_1928;
      if (Bid - ad_16 > ld_32) {
         ld_40 = Bid - ld_32;
         if (Digits > 0) ld_40 = NormalizeDouble(ld_40, Digits);
         ld_40 = ValidStopLoss(0, Bid, ld_40);
         if (Digits > 0) ld_40 = NormalizeDouble(ld_40, Digits);
         if (ad_16 >= ld_40) return;
         ModifyOrder(ai_4, ad_8, ld_40, ad_24, LightGreen);
         gd_2180 = Bid - gi_1080 * gd_1928;
         SaveIntSTprice(gd_2180);
         return;
      }
   }
   if (ai_0 == 1) {
      if (ad_16 >= ad_8 - LockInPips * gd_1928) {
         ld_32 = gi_1084 * gd_1928;
         if (ad_16 - Ask > ld_32) {
            ld_48 = Ask + ld_32;
            if (Digits > 0) ld_48 = NormalizeDouble(ld_48, Digits);
            ld_48 = ValidStopLoss(1, Ask, ld_48);
            if (Digits > 0) ld_48 = NormalizeDouble(ld_48, Digits);
            if (ad_16 > ld_48) {
               ModifyOrder(ai_4, ad_8, ld_48, ad_24, DarkOrange);
               gd_2180 = Ask + gi_1080 * gd_1928;
               SaveIntSTprice(gd_2180);
               return;
            }
         }
      }
   }
}

int ModifyOrder(int a_ticket_0, double a_price_4, double a_price_12, double a_price_20, color a_color_28) {
   int l_error_36;
   int l_count_32 = 0;
   while (l_count_32 < 3) {
      if (OrderModify(a_ticket_0, a_price_4, a_price_12, a_price_20, 0, a_color_28)) l_count_32 = 3;
      else {
         l_error_36 = GetLastError();
         Print(l_count_32, " Error modifying order : (", l_error_36, ") " + ErrorDescription(l_error_36));
         if (l_error_36 > 0/* NO_ERROR */) l_count_32++;
      }
   }
   return (0);
}

void HedgeHandleTrailingStop(int ai_0, int ai_4, double ad_8, double ad_16, double ad_24) {
   switch (HedgeTrailingStopMethod) {
   case 1:
      HedgeMoveTP(ai_0, ai_4, ad_8, ad_16, ad_24);
      return;
   case 2:
      HedgeTrail_TP_BreakEven(ai_0, ai_4, ad_8, ad_16, ad_24);
      return;
   case 3:
      HedgeTrail_TP_Delayed(ai_0, ai_4, ad_8, ad_16, ad_24);
      return;
   case 4:
      HedgeBreakEven_TrailingStop(ai_0, ai_4, ad_8, ad_16, ad_24);
      return;
   case 5:
      HedgeDelayed_TrailingStop(ai_0, ai_4, ad_8, ad_16, ad_24);
      return;
   case 6:
      HedgeImmediate_TrailingStop(ai_0, ai_4, ad_8, ad_16, ad_24);
      return;
   case 7:
      HedgeImmediate_TrailingStopBE(ai_0, ai_4, ad_8, ad_16, ad_24);
      return;
   }
   HedgeMoveTP(ai_0, ai_4, ad_8, ad_16, ad_24);
}

void HedgeMoveTP(int ai_0, int ai_4, double ad_8, double ad_16, double ad_24) {
   double ld_32;
   if (HedgeExitOnAlertedProfit != 5) {
      if (gi_576 == TRUE) {
         if (ai_0 == 0) {
            ld_32 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_32 = TakeLong(Bid, ld_32);
            ld_32 = NormalizeDouble(ld_32, Digits);
            if (ad_24 < ld_32) ModifyOrder(ai_4, ad_8, ad_16, ld_32, LightGreen);
         }
         if (ai_0 == 1) {
            ld_32 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_32 = TakeShort(Ask, ld_32);
            ld_32 = NormalizeDouble(ld_32, Digits);
            if (ad_24 > ld_32) ModifyOrder(ai_4, ad_8, ad_16, ld_32, DarkOrange);
         }
      }
   }
}

void HedgeTrail_TP_BreakEven(int ai_0, int ai_4, double ad_8, double ad_16, double ad_24) {
   int li_36;
   double ld_40;
   double ld_48;
   double ld_72;
   double ld_88;
   bool li_32 = FALSE;
   double ld_64 = ad_16;
   double ld_56 = ad_24;
   if (HedgeUseTrigger == 1) li_36 = gi_656;
   else li_36 = gi_576;
   if (ai_0 == 0) {
      if (li_36 == 1) {
         ld_72 = ad_8 + gi_988 * gd_1928;
         if (HedgeUseTrigger == 1) ld_88 = ad_8 + HedgeExitTrigger * gd_1928;
         else ld_88 = ld_72;
         if (Bid >= ld_88) {
            if (ad_16 < ad_8) {
               li_32 = TRUE;
               ld_48 = ad_8 + HedgeBreakEvenLock * gd_1928;
               gd_2196 = ld_48;
               HedgeSaveIntSTprice(gd_2196);
               ld_48 = ValidStopLoss(0, Bid, ld_48);
               ld_48 = NormalizeDouble(ld_48, Digits);
               ld_64 = ld_48;
            }
         }
      }
      if (HedgeExitOnAlertedProfit != 5) {
         if (gi_576 == TRUE) {
            ld_40 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_40 = TakeLong(Bid, ld_40);
            ld_40 = NormalizeDouble(ld_40, Digits);
            if (ad_24 < ld_40) {
               li_32 = TRUE;
               ld_56 = ld_40;
            }
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_64, ld_56, LightGreen);
   }
   if (ai_0 == 1) {
      if (li_36 == 1) {
         ld_72 = ad_8 - gi_988 * gd_1928;
         if (HedgeUseTrigger == 1) ld_88 = ad_8 - HedgeExitTrigger * gd_1928;
         else ld_88 = ld_72;
         if (Ask <= ld_88) {
            if (ad_16 > ad_8 || ad_16 < 0.1) {
               li_32 = TRUE;
               ld_48 = ad_8 - HedgeBreakEvenLock * gd_1928;
               gd_2196 = ld_48;
               HedgeSaveIntSTprice(gd_2196);
               ld_48 = ValidStopLoss(1, Ask, ld_48);
               ld_48 = NormalizeDouble(ld_48, Digits);
               ld_64 = ld_48;
            }
         }
      }
      if (HedgeExitOnAlertedProfit != 5) {
         if (gi_576 == TRUE) {
            ld_40 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_40 = TakeShort(Ask, ld_40);
            ld_40 = NormalizeDouble(ld_40, Digits);
            if (ad_24 > ld_40) {
               li_32 = TRUE;
               ld_56 = ld_40;
            }
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_64, ld_56, DarkOrange);
   }
}

void HedgeTrail_TP_Delayed(int ai_0, int ai_4, double ad_8, double ad_16, double ad_24) {
   int li_36;
   double ld_40;
   double ld_48;
   double ld_72;
   double ld_88;
   bool li_32 = FALSE;
   double ld_64 = ad_16;
   double ld_56 = ad_24;
   if (HedgeUseTrigger == 1) li_36 = gi_656;
   else li_36 = gi_576;
   if (ai_0 == 0) {
      if (li_36 == 1) {
         ld_72 = ad_8 + gi_988 * gd_1928;
         if (gi_1876 == 1) ld_88 = ad_8 + HedgeExitTrigger * gd_1928;
         else ld_88 = ld_72;
         if (Bid >= ld_88) {
            ld_48 = Bid - HedgeTrailingStop * gd_1928;
            ld_48 = ValidStopLoss(0, Bid, ld_48);
            ld_48 = NormalizeDouble(ld_48, Digits);
            if (ad_16 < ld_48) {
               gd_2196 = ld_48;
               HedgeSaveIntSTprice(gd_2196);
               ld_64 = ld_48;
               li_32 = TRUE;
            }
         }
      }
      if (HedgeExitOnAlertedProfit != 5) {
         if (gi_576 == TRUE) {
            ld_40 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_40 = TakeLong(Bid, ld_40);
            ld_40 = NormalizeDouble(ld_40, Digits);
            if (ad_24 < ld_40) {
               li_32 = TRUE;
               ld_56 = ld_40;
            }
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_64, ld_56, LightGreen);
   }
   if (ai_0 == 1) {
      if (li_36 == 1) {
         ld_72 = ad_8 - gi_988 * gd_1928;
         if (gi_1876 == 1) ld_88 = ad_8 - HedgeExitTrigger * gd_1928;
         else ld_88 = ld_72;
         if (Ask <= ld_88) {
            ld_48 = Ask + HedgeTrailingStop * gd_1928;
            ld_48 = ValidStopLoss(1, Ask, ld_48);
            ld_48 = NormalizeDouble(ld_48, Digits);
            if (ad_16 > ld_48 || ad_16 < 0.1) {
               gd_2196 = ld_48;
               HedgeSaveIntSTprice(gd_2196);
               ld_64 = ld_48;
               li_32 = TRUE;
            }
         }
      }
      if (HedgeExitOnAlertedProfit != 5) {
         if (gi_572 == TRUE) {
            ld_40 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_40 = TakeShort(Ask, ld_40);
            ld_40 = NormalizeDouble(ld_40, Digits);
            if (ad_24 > ld_40) {
               li_32 = TRUE;
               ld_56 = ld_40;
            }
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_64, ld_56, DarkOrange);
   }
}

void HedgeBreakEven_TrailingStop(int ai_0, int ai_4, double ad_8, double ad_16, double ad_24) {
   double ld_36;
   double ld_44;
   double ld_68;
   bool li_32 = FALSE;
   double ld_60 = ad_16;
   double ld_52 = ad_24;
   if (ai_0 == 0) {
      ld_68 = ad_8 + gi_988 * gd_1928;
      if (Bid >= ad_8 + HedgeBreakEvenLevel * gd_1928) {
         if (ad_16 < ad_8) {
            li_32 = TRUE;
            ld_44 = ad_8 + HedgeLockInPips * gd_1928;
            gd_2196 = ld_44;
            HedgeSaveIntSTprice(gd_2196);
            ld_44 = ValidStopLoss(0, Bid, ld_44);
            ld_44 = NormalizeDouble(ld_44, Digits);
            ld_60 = ld_44;
         }
      }
      if (HedgeExitOnAlertedProfit != 5) {
         if (gi_576 == TRUE) {
            ld_36 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_36 = TakeLong(Bid, ld_36);
            ld_36 = NormalizeDouble(ld_36, Digits);
            if (ad_24 < ld_36) {
               li_32 = TRUE;
               ld_52 = ld_36;
            }
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_60, ld_52, LightGreen);
   }
   if (ai_0 == 1) {
      ld_68 = ad_8 - gi_988 * gd_1928;
      if (Ask <= ad_8 - HedgeBreakEvenLevel * gd_1928) {
         if (ad_16 > ad_8 || ad_16 < 0.1) {
            li_32 = TRUE;
            ld_44 = ad_8 - HedgeLockInPips * gd_1928;
            gd_2196 = ld_44;
            HedgeSaveIntSTprice(gd_2196);
            ld_44 = ValidStopLoss(1, Ask, ld_44);
            ld_44 = NormalizeDouble(ld_44, Digits);
            ld_60 = ld_44;
         }
      }
      if (HedgeExitOnAlertedProfit != 5) {
         if (gi_576 == TRUE) {
            ld_36 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_36 = TakeShort(Ask, ld_36);
            ld_36 = NormalizeDouble(ld_36, Digits);
            if (ad_24 > ld_36) {
               li_32 = TRUE;
               ld_52 = ld_36;
            }
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_60, ld_52, DarkOrange);
   }
}

void HedgeDelayed_TrailingStop(int ai_0, int ai_4, double ad_8, double ad_16, double ad_24) {
   double ld_40;
   double ld_48;
   double ld_72;
   bool li_32 = FALSE;
   double ld_64 = ad_16;
   double ld_56 = ad_24;
   if (ai_0 == 0) {
      ld_72 = ad_8 + gi_988 * gd_1928;
      if (Bid >= ad_8 + HedgeBeginTrailingStop * gd_1928) {
         ld_48 = Bid - HedgeBeginTrailingStop * gd_1928;
         ld_48 = ValidStopLoss(0, Bid, ld_48);
         ld_48 = NormalizeDouble(ld_48, Digits);
         if (ad_16 < ld_48) {
            gd_2196 = ld_48;
            HedgeSaveIntSTprice(gd_2196);
            ld_64 = ld_48;
            li_32 = TRUE;
         }
      }
      if (HedgeExitOnAlertedProfit != 5) {
         if (gi_576 == TRUE) {
            ld_40 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_40 = TakeLong(Bid, ld_40);
            ld_40 = NormalizeDouble(ld_40, Digits);
            if (ad_24 < ld_40) {
               li_32 = TRUE;
               ld_56 = ld_40;
            }
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_64, ld_56, LightGreen);
   }
   if (ai_0 == 1) {
      ld_72 = ad_8 - gi_988 * gd_1928;
      if (Ask <= ad_8 - HedgeBeginTrailingStop * gd_1928) {
         ld_48 = Ask + HedgeBeginTrailingStop * gd_1928;
         ld_48 = ValidStopLoss(1, Ask, ld_48);
         ld_48 = NormalizeDouble(ld_48, Digits);
         if (ad_16 > ld_48 || ad_16 < 0.1) {
            gd_2196 = ld_48;
            HedgeSaveIntSTprice(gd_2196);
            ld_64 = ld_48;
            li_32 = TRUE;
         }
      }
      if (HedgeExitOnAlertedProfit != 5) {
         if (gi_576 == TRUE) {
            ld_40 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_40 = TakeShort(Ask, ld_40);
            ld_40 = NormalizeDouble(ld_40, Digits);
            if (ad_24 > ld_40) {
               li_32 = TRUE;
               ld_56 = ld_40;
            }
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_64, ld_56, DarkOrange);
   }
}

void HedgeImmediate_TrailingStop(int ai_0, int ai_4, double ad_8, double ad_16, double ad_24) {
   double ld_36;
   double ld_44;
   bool li_32 = FALSE;
   double ld_60 = ad_16;
   double ld_52 = ad_24;
   if (ai_0 == 0) {
      ld_44 = Bid - MathFloor(gi_1092 * SafetyStopLossMultiplier) * gd_1928;
      if (Digits > 0) ld_44 = NormalizeDouble(ld_44, Digits);
      ld_44 = ValidStopLoss(0, Bid, ld_44);
      ld_44 = NormalizeDouble(ld_44, Digits);
      if (ad_16 < ld_44) {
         li_32 = TRUE;
         ld_60 = ld_44;
         gd_2196 = Bid - gi_1092 * gd_1928;
         HedgeSaveIntSTprice(gd_2196);
      }
      if (HedgeExitOnAlertedProfit != 5) {
         if (gi_576 == TRUE) {
            ld_36 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_36 = TakeLong(Bid, ld_36);
            ld_36 = NormalizeDouble(ld_36, Digits);
            if (ad_24 < ld_36) {
               li_32 = TRUE;
               ld_52 = ld_36;
            }
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_60, ld_52, LightGreen);
   }
   if (ai_0 == 1) {
      ld_44 = Ask + MathFloor(gi_1092 * SafetyStopLossMultiplier) * gd_1928;
      if (Digits > 0) ld_44 = NormalizeDouble(ld_44, Digits);
      ld_44 = ValidStopLoss(1, Ask, ld_44);
      ld_44 = NormalizeDouble(ld_44, Digits);
      if (ad_16 > ld_44) {
         li_32 = TRUE;
         ld_60 = ld_44;
         gd_2196 = Ask + gi_1092 * gd_1928;
         HedgeSaveIntSTprice(gd_2196);
      }
      if (HedgeExitOnAlertedProfit != 5) {
         if (gi_576 == TRUE) {
            ld_36 = MathFloor(TP_Step * SafetyTakeProfitMultiplier);
            ld_36 = TakeShort(Ask, ld_36);
            ld_36 = NormalizeDouble(ld_36, Digits);
            if (ad_24 > ld_36) {
               li_32 = TRUE;
               ld_52 = ld_36;
            }
         }
      }
      if (li_32 == TRUE) ModifyOrder(ai_4, ad_8, ld_60, ld_52, DarkOrange);
   }
}

void HedgeImmediate_TrailingStopBE(int ai_0, int ai_4, double ad_8, double ad_16, double ad_24) {
   double ld_32;
   double ld_40;
   double ld_48;
   if (ai_0 == 0) {
      if (ad_16 > ad_8 + HedgeLockInPips * gd_1928) return;
      ld_32 = gi_1092 * gd_1928;
      if (Bid - ad_16 > ld_32) {
         ld_40 = Bid - ld_32;
         if (Digits > 0) ld_40 = NormalizeDouble(ld_40, Digits);
         ld_40 = ValidStopLoss(0, Bid, ld_40);
         if (Digits > 0) ld_40 = NormalizeDouble(ld_40, Digits);
         if (ad_16 >= ld_40) return;
         ModifyOrder(ai_4, ad_8, ld_40, ad_24, LightGreen);
         return;
      }
   }
   if (ai_0 == 1) {
      if (ad_16 >= ad_8 - HedgeLockInPips * gd_1928) {
         ld_32 = gi_1092 * gd_1928;
         if (ad_16 - Ask > ld_32) {
            ld_48 = Ask + ld_32;
            if (Digits > 0) ld_48 = NormalizeDouble(ld_48, Digits);
            ld_48 = ValidStopLoss(1, Ask, ld_48);
            if (Digits > 0) ld_48 = NormalizeDouble(ld_48, Digits);
            if (ad_16 > ld_48) {
               ModifyOrder(ai_4, ad_8, ld_48, ad_24, DarkOrange);
               return;
            }
         }
      }
   }
}

void OutputMarkToChart(int a_y_0, int ai_4, string a_text_8) {
   if (ObjectFind(g_name_1628) != 0) ObjectCreate(g_name_1628, OBJ_LABEL, 0, 0, 0);
   ObjectSet(g_name_1628, OBJPROP_CORNER, 0);
   ObjectSet(g_name_1628, OBJPROP_XDISTANCE, 10);
   ObjectSet(g_name_1628, OBJPROP_YDISTANCE, a_y_0);
   ObjectSetText(g_name_1628, a_text_8, ai_4 << 1, "Arial Bold", MarkColor);
}

void ClearLabels() {
   string ls_0 = " ";
   OutputLabelToChart(gs_1508, gi_1448, StatusTxtSize, StatusColor, ls_0, " ");
   OutputLabelToChart(gs_1516, gi_1452, CommentTxtSize, CommentColor, ls_0, " ");
   OutputLabelToChart(gs_1524, gi_1456, CommentTxtSize, CommentColor, ls_0, " ");
   OutputLabelToChart(gs_1532, gi_1460, CommentTxtSize, CommentColor, ls_0, " ");
   OutputLabelToChart(gs_1540, gi_1464, TradeSetTxtSize, TradeSetColor, ls_0, " ");
   OutputLabelToChart(gs_1548, gi_1468, TrendTxtSize, TrendColor, ls_0, " ");
   OutputLabelToChart(gs_1556, gi_1472, LookingTxtSize, LookingColor, ls_0, " ");
   OutputLabelToChart(gs_1564, gi_1476, FoundTxtSize, FoundColor, ls_0, " ");
   OutputLabelToChart(gs_1572, gi_1480, WaitingTxtSize, WaitingColor, ls_0, " ");
   OutputLabelToChart(gs_1580, gi_1484, ConditionTxtSize, ConditionColor, ls_0, " ");
   OutputLabelToChart(gs_1588, gi_1488, TradeTxtSize, TradeColor, ls_0, " ");
   OutputLabelToChart(gs_1596, gi_1492, TradeTxtSize, TradeColor, ls_0, " ");
   OutputLabelToChart(gs_1604, gi_1496, TradeTxtSize, TradeColor, ls_0, " ");
   OutputLabelToChart(gs_1612, gi_1500, TradeTxtSize, TradeColor, ls_0, " ");
   OutputLabelToChart(gs_1620, gi_1504, DrawDownTxtSize, DrawDownColor, ls_0, " ");
}

void DeleteExistingLabels() {
   string l_name_4;
   int l_objs_total_0 = ObjectsTotal(OBJ_LABEL);
   string ls_12 = "HH_" + Symbol() + tf2txt(Period());
   if (l_objs_total_0 > 0) {
      for (int l_objs_total_20 = l_objs_total_0; l_objs_total_20 >= 0; l_objs_total_20--) {
         l_name_4 = ObjectName(l_objs_total_20);
         if (StringFind(l_name_4, ls_12 + "Status", 0) >= 0) ObjectDelete(l_name_4);
         else {
            if (StringFind(l_name_4, ls_12 + "Comment1", 0) >= 0) ObjectDelete(l_name_4);
            else {
               if (StringFind(l_name_4, ls_12 + "Comment2", 0) >= 0) ObjectDelete(l_name_4);
               else {
                  if (StringFind(l_name_4, ls_12 + "Comment3", 0) >= 0) ObjectDelete(l_name_4);
                  else {
                     if (StringFind(l_name_4, ls_12 + "TradeSet", 0) >= 0) ObjectDelete(l_name_4);
                     else {
                        if (StringFind(l_name_4, ls_12 + "HASTrend", 0) >= 0) ObjectDelete(l_name_4);
                        else {
                           if (StringFind(l_name_4, ls_12 + "Looking", 0) >= 0) ObjectDelete(l_name_4);
                           else {
                              if (StringFind(l_name_4, ls_12 + "Found", 0) >= 0) ObjectDelete(l_name_4);
                              else {
                                 if (StringFind(l_name_4, ls_12 + "Waiting", 0) >= 0) ObjectDelete(l_name_4);
                                 else {
                                    if (StringFind(l_name_4, ls_12 + "Condition", 0) >= 0) ObjectDelete(l_name_4);
                                    else {
                                       if (StringFind(l_name_4, ls_12 + "Trade1", 0) >= 0) ObjectDelete(l_name_4);
                                       else {
                                          if (StringFind(l_name_4, ls_12 + "Trade2", 0) >= 0) ObjectDelete(l_name_4);
                                          else {
                                             if (StringFind(l_name_4, ls_12 + "Trade3", 0) >= 0) ObjectDelete(l_name_4);
                                             else {
                                                if (StringFind(l_name_4, ls_12 + "Trade4", 0) >= 0) ObjectDelete(l_name_4);
                                                else {
                                                   if (StringFind(l_name_4, ls_12 + "DrawDown", 0) >= 0) ObjectDelete(l_name_4);
                                                   else
                                                      if (StringFind(l_name_4, ls_12 + "Mark", 0) >= 0) ObjectDelete(l_name_4);
                                                }
                                             }
                                          }
                                       }
                                    }
                                 }
                              }
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }
}

void SetupLabels() {
   string ls_0 = "HH_" + Symbol() + tf2txt(Period());
   gi_1448 = 12;
   gi_1452 = gi_1448 + StatusTxtSize + 4;
   gi_1456 = gi_1452 + CommentTxtSize + 4;
   gi_1460 = gi_1456 + CommentTxtSize + 4;
   gi_1464 = gi_1460 + CommentTxtSize + 4;
   gi_1468 = gi_1464 + TradeSetTxtSize + 4;
   gi_1472 = gi_1468 + TrendTxtSize + 4;
   gi_1476 = gi_1472 + LookingTxtSize + 4;
   gi_1480 = gi_1476 + FoundTxtSize + 4;
   gi_1484 = gi_1480 + WaitingTxtSize + 4;
   gi_1488 = gi_1484 + ConditionTxtSize + 4;
   gi_1492 = gi_1488 + TradeTxtSize + 4;
   gi_1496 = gi_1492 + TradeTxtSize + 4;
   gi_1500 = gi_1496 + TradeTxtSize + 4;
   gi_1504 = gi_1500 + TradeTxtSize + 4;
   gs_1508 = ls_0 + "Status";
   gs_1516 = ls_0 + "Comment1";
   gs_1524 = ls_0 + "Comment2";
   gs_1532 = ls_0 + "Comment3";
   gs_1540 = ls_0 + "TradeSet";
   gs_1548 = ls_0 + "HASTrend";
   gs_1556 = ls_0 + "Looking";
   gs_1564 = ls_0 + "Found";
   gs_1572 = ls_0 + "Waiting";
   gs_1580 = ls_0 + "Condition";
   gs_1588 = ls_0 + "Trade1";
   gs_1596 = ls_0 + "Trade2";
   gs_1604 = ls_0 + "Trade3";
   gs_1612 = ls_0 + "Trade4";
   gs_1620 = ls_0 + "DrawDown";
   g_name_1628 = ls_0 + "Mark";
}

void OutputLabelToChart(string a_name_0, int a_y_8, int a_fontsize_12, color a_color_16, string a_text_20, string as_28) {
   if (ObjectFind(a_name_0) != 0) {
      ObjectCreate(a_name_0, OBJ_LABEL, 0, 0, 0);
      ObjectSet(a_name_0, OBJPROP_CORNER, 0);
      ObjectSet(a_name_0, OBJPROP_XDISTANCE, 20);
      ObjectSet(a_name_0, OBJPROP_YDISTANCE, a_y_8);
   }
   ObjectSetText(a_name_0, a_text_20, a_fontsize_12, "Arial Bold", a_color_16);
   if (Debug_Expert == TRUE) WriteComment(a_text_20);
   OutputMarkToChart(a_y_8, a_fontsize_12, as_28);
}

void OutputStatusToChart(string as_0) {
   OutputLabelToChart(gs_1508, gi_1448, StatusTxtSize, StatusColor, as_0, "*");
}

void OutputComment1ToChart(string as_0) {
   OutputLabelToChart(gs_1516, gi_1452, CommentTxtSize, CommentColor, as_0, "*");
}

void OutputComment2ToChart(string as_0) {
   OutputLabelToChart(gs_1524, gi_1456, CommentTxtSize, CommentColor, as_0, "*");
}

void OutputComment3ToChart(string as_0) {
   OutputLabelToChart(gs_1532, gi_1460, CommentTxtSize, CommentColor, as_0, "*");
}

void OutputTradeSetToChart(string as_0) {
   OutputLabelToChart(gs_1540, gi_1464, TradeSetTxtSize, TradeSetColor, as_0, "*");
}

void OutputTrendToChart(string as_0) {
   OutputLabelToChart(gs_1548, gi_1468, TrendTxtSize, TrendColor, as_0, "*");
}

void OutputLookingToChart(string as_0) {
   OutputLabelToChart(gs_1556, gi_1472, LookingTxtSize, LookingColor, as_0, "*");
}

void OutputFoundToChart(string as_0) {
   OutputLabelToChart(gs_1564, gi_1476, FoundTxtSize, FoundColor, as_0, "*");
}

void OutputWaitingToChart(string as_0, int ai_8) {
   gs_2216 = gs_2216 + ".";
   if (gs_2216 == ".........." || ai_8) gs_2216 = "";
   OutputLabelToChart(gs_1572, gi_1480, WaitingTxtSize, WaitingColor, as_0 + gs_2216, "*");
}

void OutputConditionToChart(string as_0) {
   OutputLabelToChart(gs_1580, gi_1484, ConditionTxtSize, ConditionColor, as_0, "*");
}

void OutputTrade1ToChart(string as_0) {
   OutputLabelToChart(gs_1588, gi_1488, TradeTxtSize, TradeColor, as_0, "*");
}

void OutputTrade2ToChart(string as_0) {
   OutputLabelToChart(gs_1596, gi_1492, TradeTxtSize, TradeColor, as_0, "*");
}

void OutputTrade3ToChart(string as_0) {
   OutputLabelToChart(gs_1604, gi_1496, TradeTxtSize, TradeColor, as_0, "*");
}

void OutputTrade4ToChart(string as_0) {
   OutputLabelToChart(gs_1612, gi_1500, TradeTxtSize, TradeColor, as_0, "*");
}

void OutputDrawDownToChart(string as_0) {
   OutputLabelToChart(gs_1620, gi_1504, DrawDownTxtSize, DrawDownColor, as_0, "*");
}

void CloseLogFiles() {
   int l_global_var_0;
   if (Debug_BackTestTrade == TRUE) {
      if (GlobalVariableCheck("BTMyHandleTrade") == TRUE) {
         l_global_var_0 = GlobalVariableGet("BTMyHandleTrade");
         FileFlush(l_global_var_0);
         FileClose(l_global_var_0);
         GlobalVariableDel("BTMyHandleTrade");
      }
   }
}

void SetupLogFiles() {
   int l_error_8;
   int l_file_12;
   string l_name_0 = Symbol() + " " + BT_Trade_Log + ".txt";
   if (Debug_BackTestTrade == TRUE) {
      GlobalVariableSet("BTMyHandleTrade", 0);
      if (GlobalVariableCheck("BTMyHandleTrade") == FALSE) {
         l_error_8 = GetLastError();
         Print("Error creating Global Variable BTMyHandleTrade: (" + l_error_8 + ") " + ErrorDescription(l_error_8));
         return;
      }
      l_file_12 = FileOpen(l_name_0, FILE_CSV|FILE_WRITE, "\t");
      GlobalVariableSet("BTMyHandleTrade", l_file_12);
   }
   g_name_1744 = AccountNumber() + " " + Symbol() + Trade_Log + "_" + Month() + "-" + Day() + ".txt";
}

void WriteDash() {
   if (Debug_BackTestTrade == TRUE) BTWrite_Trade("-----------------------------------------------------------------");
   if (Debug_LiveTrade == TRUE) Write_Trade("-----------------------------------------------------------------");
}

void WriteComment(string as_0) {
   if (Debug_BackTestTrade == TRUE) BTWrite_Trade(" " + as_0);
   if (Debug_LiveTrade == TRUE) Write_Trade(" " + as_0);
}

void Write_OpenError(string as_0) {
   if (Debug_BackTestTrade == TRUE) BTWrite_Trade(as_0);
   if (Debug_LiveTrade == TRUE) Write_Trade(as_0);
}

void Write_OpenTrade(string as_0) {
   if (Debug_BackTestTrade == TRUE) {
      BTWrite_Trade("****************************************************************");
      BTWrite_Trade(as_0);
   }
   if (Debug_LiveTrade == TRUE) {
      Write_Trade("****************************************************************");
      Write_Trade(as_0);
   }
}

void WriteExit(int ai_0, string as_4, int ai_12, double ad_16) {
   if (Debug_BackTestTrade == TRUE) {
      BTWrite_Trade("-----------------------------------------------------------------");
      BTWrite_Trade(" " + Symbol() + " #" + ai_0 + " " + as_4);
      if (ad_16 > 0.1) BTWrite_Prices(ai_12, ad_16);
   }
   if (Debug_LiveTrade == TRUE) {
      Write_Trade("-----------------------------------------------------------------");
      Write_Trade(" " + Symbol() + " #" + ai_0 + " " + as_4);
      if (ad_16 > 0.1) Write_Prices(ai_12, ad_16);
   }
}

void Write_Trade(string as_0) {
   int l_file_8;
   if (Debug_LiveTrade == TRUE) {
      l_file_8 = FileOpen(g_name_1744, FILE_CSV|FILE_WRITE|FILE_READ, "/t");
      FileSeek(l_file_8, 0, SEEK_END);
      FileWrite(l_file_8, TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS) + as_0);
      FileClose(l_file_8);
   }
}

void BTWrite_Trade(string as_0) {
   int l_global_var_8;
   if (Debug_BackTestTrade == TRUE) {
      if (GlobalVariableCheck("BTMyHandleTrade")) {
         l_global_var_8 = GlobalVariableGet("BTMyHandleTrade");
         FileWrite(l_global_var_8, TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS) + as_0);
      }
   }
}

void BTWrite_Prices(int ai_0, double ad_4) {
   double ld_12;
   double ld_20;
   if (Debug_BackTestTrade == TRUE) {
      BTWrite_Trade("  PRICES");
      BTWrite_Trade("  Low Ask : " + DoubleToStr(gd_1808, Digits) + "  High Ask : " + DoubleToStr(gd_1800, Digits));
      BTWrite_Trade("  Low Bid : " + DoubleToStr(gd_1792, Digits) + "  High Bid : " + DoubleToStr(gd_1784, Digits));
      if (ai_0 == 0) {
         ld_12 = (gd_1784 - ad_4) / gd_1928;
         ld_20 = (ad_4 - gd_1792) / gd_1928;
      }
      if (ai_0 == 1) {
         ld_12 = (ad_4 - gd_1808) / gd_1928;
         ld_20 = (gd_1800 - ad_4) / gd_1928;
      }
      BTWrite_Trade("  High Profit : " + DoubleToStr(ld_12, 0));
      BTWrite_Trade("  DrawDown : " + DoubleToStr(ld_20, 0));
   }
}

void Write_Prices(int ai_0, double ad_4) {
   double ld_12;
   double ld_20;
   if (Debug_LiveTrade == TRUE) {
      Write_Trade("  PRICES");
      Write_Trade("  Low Ask : " + DoubleToStr(gd_1808, Digits) + "  High Ask : " + DoubleToStr(gd_1800, Digits));
      Write_Trade("  Low Bid : " + DoubleToStr(gd_1792, Digits) + "  High Bid : " + DoubleToStr(gd_1784, Digits));
      if (ai_0 == 0) {
         ld_12 = (gd_1784 - ad_4) / gd_1928;
         ld_20 = (ad_4 - gd_1792) / gd_1928;
      }
      if (ai_0 == 1) {
         ld_12 = (ad_4 - gd_1808) / gd_1928;
         ld_20 = (gd_1800 - ad_4) / gd_1928;
      }
      Write_Trade("  High Profit : " + DoubleToStr(ld_12, 0));
      Write_Trade("  DrawDown    : " + DoubleToStr(ld_20, 0));
   }
}

void WriteTradeExit(string as_0, int ai_8, int ai_12, double ad_16, double ad_24) {
   string l_dbl2str_32 = DoubleToStr(ad_24, 2);
   string l_dbl2str_40 = DoubleToStr(Bid, Digits);
   string l_dbl2str_48 = DoubleToStr(Ask, Digits);
   if (ai_12 == 0) {
      WriteExit(ai_8, as_0, 0, ad_16);
      if (Debug_LiveTrade == TRUE) Write_Trade(" " + Symbol() + " #" + ai_8 + " CLOSE BUY ORDER " + l_dbl2str_32 + " LOTS AT " + l_dbl2str_40 + ".");
      if (Debug_BackTestTrade == TRUE) BTWrite_Trade(" " + Symbol() + " #" + ai_8 + " CLOSE BUY ORDER " + l_dbl2str_32 + " LOTS AT " + l_dbl2str_40 + ".");
   }
   if (ai_12 == 1) {
      WriteExit(ai_8, as_0, 1, ad_16);
      if (Debug_LiveTrade == TRUE) Write_Trade(" " + Symbol() + " #" + ai_8 + " CLOSE SELL ORDER " + l_dbl2str_32 + " LOTS AT " + l_dbl2str_48 + ".");
      if (Debug_BackTestTrade == TRUE) BTWrite_Trade(" " + Symbol() + " #" + ai_8 + " CLOSE SELL ORDER " + l_dbl2str_32 + " LOTS AT " + l_dbl2str_48 + ".");
   }
}

void GetGlobalVars() {
   NameGlobalVars();
   InitGlobalVars();
   GetGlobalVarValues();
}

void GetGlobalVarValues() {
   double l_global_var_0 = GlobalVariableGet(g_var_name_1944);
   if (l_global_var_0 > 0.0) gi_572 = TRUE;
   else gi_572 = FALSE;
   l_global_var_0 = GlobalVariableGet(g_var_name_1952);
   if (l_global_var_0 > 0.0) gi_580 = TRUE;
   else gi_580 = FALSE;
   l_global_var_0 = GlobalVariableGet(g_var_name_1960);
   if (l_global_var_0 > 0.0) gi_644 = TRUE;
   else gi_644 = FALSE;
   l_global_var_0 = GlobalVariableGet(g_var_name_2080);
   if (l_global_var_0 > 0.0) gi_576 = TRUE;
   else gi_576 = FALSE;
   l_global_var_0 = GlobalVariableGet(g_var_name_2088);
   if (l_global_var_0 > 0.0) gi_656 = TRUE;
   else gi_656 = FALSE;
   g_global_var_984 = GlobalVariableGet(g_var_name_1968);
   g_global_var_1004 = GlobalVariableGet(g_var_name_1976);
   gi_1080 = GlobalVariableGet(g_var_name_1984);
   gi_1084 = GlobalVariableGet(g_var_name_1992);
   g_global_var_1700 = GlobalVariableGet(g_var_name_2000);
   g_global_var_1704 = GlobalVariableGet(g_var_name_2008);
   g_global_var_1716 = GlobalVariableGet(g_var_name_2016);
   g_global_var_2172 = GlobalVariableGet(g_var_name_2064);
   gd_2180 = GlobalVariableGet(g_var_name_2072);
   gd_1784 = GlobalVariableGet(g_var_name_2024);
   gd_1792 = GlobalVariableGet(g_var_name_2032);
   gd_1800 = GlobalVariableGet(g_var_name_2040);
   gd_1808 = GlobalVariableGet(g_var_name_2048);
   g_global_var_1848 = GlobalVariableGet(g_var_name_2056);
   gi_988 = GlobalVariableGet(g_var_name_2096);
   gi_1008 = GlobalVariableGet(g_var_name_2104);
   gi_1088 = GlobalVariableGet(g_var_name_2112);
   gi_1092 = GlobalVariableGet(g_var_name_2120);
   g_global_var_1720 = GlobalVariableGet(g_var_name_2128);
   g_global_var_1724 = GlobalVariableGet(g_var_name_2136);
   g_global_var_1728 = GlobalVariableGet(g_var_name_2144);
   g_global_var_2188 = GlobalVariableGet(g_var_name_2152);
   gd_2196 = GlobalVariableGet(g_var_name_2160);
}

void InitGlobalVars() {
   if (!GlobalVariableCheck(g_var_name_1944)) GlobalVariableSet(g_var_name_1944, -10);
   if (!GlobalVariableCheck(g_var_name_1952)) GlobalVariableSet(g_var_name_1952, -10);
   if (!GlobalVariableCheck(g_var_name_1960)) GlobalVariableSet(g_var_name_1960, -10);
   if (!GlobalVariableCheck(g_var_name_1968)) GlobalVariableSet(g_var_name_1968, 0);
   if (!GlobalVariableCheck(g_var_name_1976)) GlobalVariableSet(g_var_name_1976, 0);
   if (!GlobalVariableCheck(g_var_name_1984)) GlobalVariableSet(g_var_name_1984, 0);
   if (!GlobalVariableCheck(g_var_name_1992)) GlobalVariableSet(g_var_name_1992, 0);
   if (!GlobalVariableCheck(g_var_name_2000)) GlobalVariableSet(g_var_name_2000, 0);
   if (!GlobalVariableCheck(g_var_name_2008)) GlobalVariableSet(g_var_name_2008, 0);
   if (!GlobalVariableCheck(g_var_name_2016)) GlobalVariableSet(g_var_name_2016, 0);
   if (!GlobalVariableCheck(g_var_name_2024)) GlobalVariableSet(g_var_name_2024, 0);
   if (!GlobalVariableCheck(g_var_name_2032)) GlobalVariableSet(g_var_name_2032, 0);
   if (!GlobalVariableCheck(g_var_name_2040)) GlobalVariableSet(g_var_name_2040, 0);
   if (!GlobalVariableCheck(g_var_name_2048)) GlobalVariableSet(g_var_name_2048, 0);
   if (!GlobalVariableCheck(g_var_name_2064)) GlobalVariableSet(g_var_name_2064, 0);
   if (!GlobalVariableCheck(g_var_name_2072)) GlobalVariableSet(g_var_name_2072, 0);
   if (!GlobalVariableCheck(g_var_name_2056)) GlobalVariableSet(g_var_name_2056, 0);
   if (!GlobalVariableCheck(g_var_name_2080)) GlobalVariableSet(g_var_name_2080, -10);
   if (!GlobalVariableCheck(g_var_name_2088)) GlobalVariableSet(g_var_name_2088, -10);
   if (!GlobalVariableCheck(g_var_name_2096)) GlobalVariableSet(g_var_name_2096, 0);
   if (!GlobalVariableCheck(g_var_name_2104)) GlobalVariableSet(g_var_name_2104, 0);
   if (!GlobalVariableCheck(g_var_name_2112)) GlobalVariableSet(g_var_name_2112, 0);
   if (!GlobalVariableCheck(g_var_name_2120)) GlobalVariableSet(g_var_name_2120, 0);
   if (!GlobalVariableCheck(g_var_name_2128)) GlobalVariableSet(g_var_name_2128, 0);
   if (!GlobalVariableCheck(g_var_name_2136)) GlobalVariableSet(g_var_name_2136, 0);
   if (!GlobalVariableCheck(g_var_name_2144)) GlobalVariableSet(g_var_name_2144, 0);
   if (!GlobalVariableCheck(g_var_name_2152)) GlobalVariableSet(g_var_name_2152, 0);
   if (!GlobalVariableCheck(g_var_name_2160)) GlobalVariableSet(g_var_name_2160, 0);
}

void NameGlobalVars() {
   string ls_0 = "HH_" + Symbol() + tf2txt(Period());
   g_var_name_1944 = ls_0 + "_First_TP_Level";
   g_var_name_1952 = ls_0 + "_ClosedPartial";
   g_var_name_1960 = ls_0 + "_First_Trigger_Level";
   g_var_name_1944 = ls_0 + "_First_TP_Level";
   g_var_name_1960 = ls_0 + "_First_Trigger_Level";
   g_var_name_1968 = ls_0 + "_InternalTakeProfit";
   g_var_name_1976 = ls_0 + "_TP_Level1";
   g_var_name_1984 = ls_0 + "_InternalStopLoss";
   g_var_name_1992 = ls_0 + "_StopLoss";
   g_var_name_2000 = ls_0 + "_AlertTimeFrame";
   g_var_name_2008 = ls_0 + "_EntryTimeFrame";
   g_var_name_2016 = ls_0 + "_ExitTimeFrameProfit";
   g_var_name_2024 = ls_0 + "_HighBid";
   g_var_name_2032 = ls_0 + "_LowBid";
   g_var_name_2040 = ls_0 + "_HighAsk";
   g_var_name_2048 = ls_0 + "_LowAsk";
   g_var_name_2064 = ls_0 + "_IntTPprice";
   g_var_name_2072 = ls_0 + "_IntSTprice";
   g_var_name_2056 = ls_0 + "_TradeSet";
   g_var_name_2080 = ls_0 + "_HedgeFirst_TP_Level";
   g_var_name_2088 = ls_0 + "_HedgeFirst_Trigger_Level";
   g_var_name_2096 = ls_0 + "_HedgeInternalTakeProfit";
   g_var_name_2104 = ls_0 + "_HedgeTP_Level1";
   g_var_name_2112 = ls_0 + "_HedgeInternalStopLoss";
   g_var_name_2120 = ls_0 + "_HedgeStopLoss";
   g_var_name_2128 = ls_0 + "_HedgeAlertTimeFrame";
   g_var_name_2136 = ls_0 + "_HedgeEntryTimeFrame";
   g_var_name_2144 = ls_0 + "_HedgeExitTimeFrameProfit";
   g_var_name_2152 = ls_0 + "_HedgeIntTPprice";
   g_var_name_2160 = ls_0 + "_HedgeIntSTprice";
}

void SaveFirst_TP_Level(int ai_0) {
   if (ai_0 == 1) {
      GlobalVariableSet(g_var_name_1944, 10.0);
      return;
   }
   GlobalVariableSet(g_var_name_1944, -10.0);
}

void SaveClosedPartial(int ai_0) {
   if (ai_0 == 1) {
      GlobalVariableSet(g_var_name_1952, 10.0);
      return;
   }
   GlobalVariableSet(g_var_name_1952, -10.0);
}

void SaveFirst_Trigger_Level(int ai_0) {
   if (ai_0 == 1) {
      GlobalVariableSet(g_var_name_1960, 10.0);
      return;
   }
   GlobalVariableSet(g_var_name_1960, -10.0);
}

void SaveInternalTakeProfit(int ai_0) {
   GlobalVariableSet(g_var_name_1968, ai_0);
}

void SaveTP_Level1(int ai_0) {
   GlobalVariableSet(g_var_name_1976, ai_0);
}

void SaveInternalStopLoss(int ai_0) {
   GlobalVariableSet(g_var_name_1984, ai_0);
}

void SaveStopLoss(int ai_0) {
   GlobalVariableSet(g_var_name_1992, ai_0);
}

void SaveAlertTimeFrame(int ai_0) {
   GlobalVariableSet(g_var_name_2000, ai_0);
}

void SaveEntryTimeFrame(int ai_0) {
   GlobalVariableSet(g_var_name_2008, ai_0);
}

void SaveExitTimeFrameProfit(int ai_0) {
   GlobalVariableSet(g_var_name_2016, ai_0);
}

void SaveHighBid(double ad_0) {
   GlobalVariableSet(g_var_name_2024, ad_0);
}

void SaveLowBid(double ad_0) {
   GlobalVariableSet(g_var_name_2032, ad_0);
}

void SaveHighAsk(double ad_0) {
   GlobalVariableSet(g_var_name_2040, ad_0);
}

void SaveLowAsk(double ad_0) {
   GlobalVariableSet(g_var_name_2048, ad_0);
}

void SaveTradeSet(int ai_0) {
   GlobalVariableSet(g_var_name_2056, ai_0);
}

void HedgeSaveFirst_TP_Level(int ai_0) {
   if (ai_0 == 1) {
      GlobalVariableSet(g_var_name_2080, 10.0);
      return;
   }
   GlobalVariableSet(g_var_name_2080, -10.0);
}

void HedgeSaveFirst_Trigger_Level(int ai_0) {
   if (ai_0 == 1) {
      GlobalVariableSet(g_var_name_2088, 10.0);
      return;
   }
   GlobalVariableSet(g_var_name_2088, -10.0);
}

void HedgeSaveAlertTimeFrame(int ai_0) {
   GlobalVariableSet(g_var_name_2128, ai_0);
}

void HedgeSaveEntryTimeFrame(int ai_0) {
   GlobalVariableSet(g_var_name_2136, ai_0);
}

void HedgeSaveExitTimeFrameProfit(int ai_0) {
   GlobalVariableSet(g_var_name_2144, ai_0);
}

void HedgeSaveInternalTakeProfit(int ai_0) {
   GlobalVariableSet(g_var_name_2096, ai_0);
}

void HedgeSaveTP_Level1(int ai_0) {
   GlobalVariableSet(g_var_name_2104, ai_0);
}

void HedgeSaveInternalStopLoss(int ai_0) {
   GlobalVariableSet(g_var_name_2112, ai_0);
}

void HedgeSaveStopLoss(int ai_0) {
   GlobalVariableSet(g_var_name_2120, ai_0);
}

void SaveIntSTprice(double ad_0) {
   GlobalVariableSet(g_var_name_2072, ad_0);
}

void SaveIntTPprice(double ad_0) {
   GlobalVariableSet(g_var_name_2064, ad_0);
}

void HedgeSaveIntSTprice(double ad_0) {
   GlobalVariableSet(g_var_name_2160, ad_0);
}

void HedgeSaveIntTPprice(double ad_0) {
   GlobalVariableSet(g_var_name_2152, ad_0);
}