//+------------------------------------------------------------------+
//|                                        Straddle and Trail v6.mq4 |
//+------------------------------------------------------------------+

#include <stdlib.mqh>

extern bool ShutDown_NOW = FALSE;
extern string sStr00 = " 0=Everything               ";
extern string sStr01 = " 1=All Triggered Positions  ";
extern string sStr02 = " 2=Triggered Long           ";
extern string sStr03 = " 3=Triggered Short          ";
extern string sStr04 = " 4=All Pending Positions    ";
extern string sStr05 = " 5=Pending Long             ";
extern string sStr06 = " 6=Pending Short            ";
extern int ShutDown_What = 0;
extern bool ShutDown_Current_Pair_Only = FALSE;
extern string sStr1 = "=== POSITION DETAILS ===";
extern double Lot_Size      = 4.0;
extern double Take_Out_Lots = 2.0;
extern double Slippage      = 6;
extern string sStr2 = "=== TAKE PROFITS ===";
extern double Initial_TP1 = 15;
extern double Initial_TP2 = 50;
extern double Initial_TP3 = 0;
extern string sStr3 = "=== STOPS LOSS ===";
extern double StopLoss_Pips_Longs         = 25;
extern double StopLoss_Pips_Shorts        = 25;
extern double Auto_SL_Prev_Bars_to_Count  = 0;
extern double Auto_SL_Prev_Bars_TimeFrame = 0;
extern string sStr4 = "=== BREAKEVEN AND TRAIL ===";
extern double Trail_Pips                  = 20;
extern bool Trail_Starts_After_BreakEven  = TRUE;
extern double Move_To_BreakEven_at_pips   = 20;
extern double Move_To_BreakEven_Lock_pips = 1;
extern string sStr5 = "=== MONEY MANAGEMENT ===";
extern bool Use_Money_Management = FALSE;
extern double Trade_Maximum_Risk = 0.03;
extern string sStr6 = "=== NEWS EVENT ===";
extern bool Enable_News_Trading = TRUE;
extern double Distance_From_Price_Longs = 20;
extern double Distance_From_Price_Shorts = 20;
extern int Event_Start_Hour = 13;
extern int Event_Start_Minutes = 30;
extern int Pre_Event_Entry_Seconds = 1800;
extern int Stop_Adjusting_Sec_Before_Event = 60;
extern bool Remove_Opposite_Order = TRUE;
extern bool Adjust_Pending_Orders = TRUE;
extern int Kill_Pending_After_Seconds = 600;
extern int Kill_Triggered_After_Seconds = 600;
extern string sStr7 = "=== VARIOUS ===";
extern bool Place_Straddle_Immediately = FALSE;
extern bool Debug = FALSE;
int gi_328 = 0;
bool gi_332 = FALSE;
int g_stoplevel_336;
int g_ticket_340;
int g_ticket_344;
int g_ticket_348;
int g_ticket_352;
int g_ticket_356;
int g_ticket_360;
int g_ticket_364;
int g_magic_368;
int g_count_372;
int gi_unused_376;
int g_count_384;
int g_count_388;
int g_hour_392;
int g_minute_396;
int g_second_400;
int g_minute_404;
int g_error_408;
string gs_unused_444 = " Straddle&Trail";
string g_text_452 = "Straddle&Trail v5.50";
string g_text_460 = "";
string gs_468;
string gs_476;
string g_str_concat_484;
bool gi_unused_492 = TRUE;
bool gi_unused_496 = FALSE;
bool gi_500;
double g_price_504;
double g_price_512;
double g_price_520;
double g_price_528;
double gd_552;
double gd_560;
int gi_568;
int gi_572;
bool gi_576;
int gi_unused_584;
int gi_unused_592;
int gi_unused_596;
int gi_600;
int gi_unused_612 = 0;
int g_leverage_616 = 0;
int gi_unused_676 = -1;
int gi_unused_680 = -1;
int gi_unused_684 = -1;
int gi_unused_688 = -1;
double gd_692 = 0.0;
double g_minlot_700 = 0.0;
double g_maxlot_708 = 0.0;
double gd_716;
double gd_724;
double g_lotstep_732 = 0.0;
double g_tickvalue_740;
double gd_unused_764 = 0.0;
double gd_unused_772 = 0.0;
double gd_unused_780 = 0.0;
double gd_unused_788 = 0.0;
bool gi_unused_796 = FALSE;
bool gi_unused_800 = FALSE;
double gd_804 = 0.0;
double gd_812 = 0.0;

int init() {
   comments();
   DeleteMyObjects();
   gi_500 = FALSE;
   DrawTheLines();
   return (0);
}

int deinit() {
   DeleteMyObjects();
   return (0);
}

void DrawReversal() {
   bool l_bool_52;
   int li_0 = 8;
   int li_unused_4 = 16;
   int li_8 = 13;
   int li_unused_12 = 21;
   bool li_16 = FALSE;
   int li_unused_20 = 7;
   int li_24 = 20;
   int li_unused_28 = 3;
   bool li_32 = FALSE;
   bool li_36 = FALSE;
   bool li_40 = FALSE;
   bool li_44 = FALSE;
   for (int li_48 = 500; li_48 >= 0; li_48--) {
      l_bool_52 = TimeDayOfWeek(iTime(NULL, 0, li_48)) == 0;
      if (!l_bool_52) {
         if (TimeHour(Time[li_48 + 1]) - gi_328 == li_24 && li_32 == FALSE) {
            li_32 = TRUE;
            ObjectCreate("Sydney Open  " + TimeToStr(Time[li_48 + 1]), OBJ_ARROW, 0, Time[li_48 + 1], High[li_48 + 1] + 15.0 * Point);
            ObjectSet("Sydney Open  " + TimeToStr(Time[li_48 + 1]), OBJPROP_ARROWCODE, 191);
            ObjectSet("Sydney Open  " + TimeToStr(Time[li_48 + 1]), OBJPROP_WIDTH, 3);
            ObjectSet("Sydney Open  " + TimeToStr(Time[li_48 + 1]), OBJPROP_COLOR, CornflowerBlue);
            gi_unused_584 = 1;
         } else {
            if (TimeHour(Time[li_48 + 1]) - gi_328 == li_16 && li_36 == FALSE) {
               li_36 = TRUE;
               ObjectCreate("Tokyo Open  " + TimeToStr(Time[li_48 + 1]), OBJ_ARROW, 0, Time[li_48 + 1], High[li_48 + 1] + 15.0 * Point);
               ObjectSet("Tokyo Open  " + TimeToStr(Time[li_48 + 1]), OBJPROP_ARROWCODE, 194);
               ObjectSet("Tokyo Open  " + TimeToStr(Time[li_48 + 1]), OBJPROP_WIDTH, 3);
               ObjectSet("Tokyo Open  " + TimeToStr(Time[li_48 + 1]), OBJPROP_COLOR, Yellow);
            } else {
               if (TimeHour(Time[li_48 + 1]) - gi_328 == li_0 && li_40 == FALSE) {
                  li_40 = TRUE;
                  ObjectCreate("London Open  " + TimeToStr(Time[li_48 + 1]), OBJ_ARROW, 0, Time[li_48 + 1], High[li_48 + 1] + 15.0 * Point);
                  ObjectSet("London Open  " + TimeToStr(Time[li_48 + 1]), OBJPROP_ARROWCODE, 189);
                  ObjectSet("London Open  " + TimeToStr(Time[li_48 + 1]), OBJPROP_WIDTH, 3);
                  ObjectSet("London Open  " + TimeToStr(Time[li_48 + 1]), OBJPROP_COLOR, Aqua);
                  li_36 = FALSE;
                  gi_unused_592 = 1;
               } else {
                  if (TimeHour(Time[li_48 + 1]) - gi_328 == li_8 && li_44 == FALSE) {
                     li_44 = TRUE;
                     ObjectCreate("NewYork Open  " + TimeToStr(Time[li_48 + 1]), OBJ_ARROW, 0, Time[li_48 + 1], High[li_48 + 1] + 15.0 * Point);
                     ObjectSet("NewYork Open  " + TimeToStr(Time[li_48 + 1]), OBJPROP_ARROWCODE, 194);
                     ObjectSet("NewYork Open  " + TimeToStr(Time[li_48 + 1]), OBJPROP_WIDTH, 3);
                     ObjectSet("NewYork Open  " + TimeToStr(Time[li_48 + 1]), OBJPROP_COLOR, Lime);
                     gi_unused_596 = 1;
                  }
               }
            }
         }
      }
      if (TimeHour(Time[li_48 + 1]) == 0) {
         li_32 = FALSE;
         li_40 = FALSE;
         li_44 = FALSE;
      }
   }
}

void DeleteMyObjects() {
   ObjectDelete("Yannis");
   ObjectDelete("Yannis2");
   ObjectDelete("Artemis");
   ObjectDelete("NewsLabel");
   ObjectDelete("Bar");
   ObjectDelete("Broker");
   ObjectDelete("Broker1");
   ObjectDelete("Sydney");
   ObjectDelete("Tokyo");
   ObjectDelete("London");
   ObjectDelete("NewYork");
}

int start() {
   double ld_8;
   double ld_16;
   double ld_24;
   g_stoplevel_336 = MarketInfo(Symbol(), MODE_STOPLEVEL);
   g_leverage_616 = AccountLeverage();
   gi_unused_612 = MarketInfo(Symbol(), MODE_LOTSIZE);
   g_minlot_700 = MarketInfo(Symbol(), MODE_MINLOT);
   g_maxlot_708 = MarketInfo(Symbol(), MODE_MAXLOT);
   g_lotstep_732 = MarketInfo(Symbol(), MODE_LOTSTEP);
   g_tickvalue_740 = MarketInfo(Symbol(), MODE_TICKVALUE);
   if (g_lotstep_732 == 0.1) gd_692 = 1;
   if (g_lotstep_732 == 0.01) gd_692 = 2;
   int li_0 = ReturnIndicatorValues();
   if (li_0 == 1) {
      Alert("Error 9000 - Contact Yannis at jsfero@otenet.gr");
      return (0);
   }
   if (StopLoss_Pips_Longs == 0) StopLoss_Pips_Longs = 999;
   if (StopLoss_Pips_Shorts == 0) StopLoss_Pips_Shorts = 999;
   if (Initial_TP1 == 0) Initial_TP1 = 999;
   if (StopLoss_Pips_Longs < g_stoplevel_336) StopLoss_Pips_Longs = g_stoplevel_336;
   if (StopLoss_Pips_Shorts < g_stoplevel_336) StopLoss_Pips_Shorts = g_stoplevel_336;
   if (Trail_Pips < g_stoplevel_336) Trail_Pips = g_stoplevel_336;
   RefreshRates();
   if (Auto_SL_Prev_Bars_to_Count > WHOLE_ARRAY) {
      gd_552 = High[iHighest(NULL, Auto_SL_Prev_Bars_TimeFrame, MODE_HIGH, Auto_SL_Prev_Bars_to_Count, 1)] + Slippage * Point;
      gd_560 = Low[iLowest(NULL, Auto_SL_Prev_Bars_TimeFrame, MODE_LOW, Auto_SL_Prev_Bars_to_Count, 1)] - Slippage * Point;
   } else {
      gd_552 = Bid + StopLoss_Pips_Shorts * Point;
      gd_560 = Ask - StopLoss_Pips_Longs * Point;
   }
   DrawTheLines();
   g_magic_368 = CalcMagic(Symbol(), Period());
   if (TimeHour(iTime(NULL, PERIOD_D1, 0)) == 0 && TimeMinute(iTime(NULL, PERIOD_D1, 0)) <= 1 && gi_500) gi_500 = FALSE;
   int li_4 = 0;
   if (Initial_TP3 > 0) {
      gi_600 = Initial_TP3 * 2;
      li_4 = 3;
   } else {
      if (Initial_TP2 > 0) {
         gi_600 = Initial_TP2 * 2;
         li_4 = 2;
      } else {
         if (Initial_TP1 > 0) {
            gi_600 = Initial_TP1 * 2;
            li_4 = 1;
         } else {
            Alert("Error 3001 - INITIAL_TP1 CANNOT BE 0\nThe expert is DISABLED");
            return (0);
         }
      }
   }
   if (Use_Money_Management) {
      Initial_TP2 = 0;
      Initial_TP3 = 0;
      gd_804 = CalculateTheLots(1);
      gd_812 = CalculateTheLots(2);
      Take_Out_Lots = Lot_Size;
   } else {
      gd_804 = 0;
      gd_812 = 0;
      if (Lot_Size < Take_Out_Lots) Take_Out_Lots = Lot_Size;
      ld_8 = 100.0 * Lot_Size;
      ld_16 = 100.0 * Take_Out_Lots;
      if (MathMod(ld_8, ld_16) != 0.0) {
         Alert("Error 3002 - Lots size=", Lot_Size, "  Take Out=", Take_Out_Lots, "   Remainder=", MathMod(Lot_Size, Take_Out_Lots), "   Lot_Size is not an exact multiple of Take_Out_Lots.\nLot_Size Divided by Take_Out_Lots leaves a remainder\nThe expert is DISABLED");
         return (0);
      }
      ld_24 = NormalizeDouble(Lot_Size / Take_Out_Lots, 0);
      if (ld_24 != li_4) {
         if (ld_24 > li_4) Alert("Error 3003 - You have set more lots to take out (", ld_24, ") than take profit levels (", li_4, ")\nThe expert is DISABLED");
         return (0);
      }
   }
   PosCounter();
   if (ShutDown_NOW && g_count_372 > 0) {
      ShutDownAllTrades(ShutDown_What);
      return (0);
   }
   if (ShutDown_NOW) {
      Alert("Caution - ShutDown_NOW parameter enabled. You dont have any open/pending trade, the EA is disabled, you must set it to False NOW");
      return (0);
   }
   PosCounter();
   CheckInitialSLTP();
   li_0 = ReturnIndicatorValues();
   if (li_0 == 1) {
      Alert("Error 9001 - Contact Yannis at jsfero@otenet.gr");
      return (0);
   }
   if ((IsTimeToPlaceAndAdjustEntries(1) == 1 && Enable_News_Trading && !gi_500) || Place_Straddle_Immediately) {
      if (g_ticket_356 == 0 && g_ticket_352 == 0 && g_ticket_348 == 0 && g_ticket_344 == 0) {
         PlaceTheStraddle();
         return (0);
      }
   }
   if (Debug) Print(IsTimeToPlaceAndAdjustEntries(1), "   ", IsTimeToPlaceAndAdjustEntries(2), "  ", gi_568, "   ", gi_572);
   PosCounter();
   if (IsTimeToPlaceAndAdjustEntries(2) == 1 && g_ticket_356 > 0 && g_ticket_352 > 0 && g_ticket_348 == 0 && g_ticket_344 == 0 && g_minute_396 != g_minute_404 && Adjust_Pending_Orders &&
      Enable_News_Trading && !Place_Straddle_Immediately) {
      AdjustPendingOrders();
      return (0);
   }
   PosCounter();
   if (Remove_Opposite_Order) {
      if ((g_ticket_348 > 0 && g_ticket_352 > 0) || (g_ticket_344 > 0 && g_ticket_356 > 0)) {
         RemoveOppositePending();
         return (0);
      }
   }
   if (Enable_News_Trading) CheckIfMustKillTheTrades();
   PosCounter();
   if (g_ticket_348 > 0 || g_ticket_344 > 0 || g_ticket_360 > 0 || g_ticket_364 > 0) {
      if (Move_To_BreakEven_at_pips != 0) MoveToBreakEven();
      Sleep(2000);
      if (!Use_Money_Management) ScaleOut();
      Sleep(2000);
      Trail_Stop();
   }
   comments();
   return (0);
}

double CalculateTheLots(int ai_0) {
   if (ai_0 == 1) {
      gd_716 = AccountBalance() * Trade_Maximum_Risk / (g_tickvalue_740 * StopLoss_Pips_Longs);
      gd_716 = StrToDouble(DoubleToStr(gd_716, gd_692));
      if (gd_716 < g_minlot_700) gd_716 = g_minlot_700;
      if (gd_716 > g_maxlot_708) gd_716 = g_maxlot_708;
      return (gd_716);
   }
   gd_724 = AccountBalance() * Trade_Maximum_Risk / (g_tickvalue_740 * StopLoss_Pips_Shorts);
   gd_724 = StrToDouble(DoubleToStr(gd_724, gd_692));
   if (gd_724 < g_minlot_700) gd_724 = g_minlot_700;
   if (gd_724 > g_maxlot_708) gd_724 = g_maxlot_708;
   return (gd_724);
}

void CheckIfMustKillTheTrades() {
   int l_error_4;
   int li_0 = ReturnIndicatorValues();
   if (li_0 == 1) {
      Alert("Error 9002 - Contact Yannis at jsfero@otenet.gr");
      return;
   }
   CalcTimeVariables();
   if ((Kill_Pending_After_Seconds > 0 && g_ticket_352 > 0)|| (g_ticket_356 > 0 && Enable_News_Trading && gi_568 >= gi_572 + Kill_Pending_After_Seconds)) {
      if (g_ticket_352 > 0) {
         if (OrderSelect(g_ticket_352, SELECT_BY_TICKET))
         g_count_388 = 0;
         while (g_count_388 < 5 && !IsTradeAllowed()) {
            g_count_388++;
            Sleep(2000);
         }
         if (OrderDelete(g_ticket_352))
         l_error_4 = GetLastError();
         if (l_error_4 > 0/* NO_ERROR */) Print("Error 5001 - Error Removing Long Pending Order after time expiration ticket=", g_ticket_352, "  ", ErrorDescription(GetLastError()));
         Sleep(2000);
      }
      if (g_ticket_356 > 0) {
         if (OrderSelect(g_ticket_356, SELECT_BY_TICKET))
         g_count_388 = 0;
         while (g_count_388 < 5 && !IsTradeAllowed()) {
            g_count_388++;
            Sleep(2000);
         }
         if (OrderDelete(g_ticket_356))
         l_error_4 = GetLastError();
         if (l_error_4 > 0/* NO_ERROR */) Print("Error 5002 - Error Removing Short Pending Order after time expiration ticket=", g_ticket_356, "  ", ErrorDescription(GetLastError()));
         Sleep(2000);
      }
   }
   if ((Kill_Triggered_After_Seconds > 0 && g_ticket_344 > 0) || (g_ticket_348 > 0 && Enable_News_Trading && gi_568 >= gi_572 + Kill_Triggered_After_Seconds)) {
      if (g_ticket_344 > 0) {
         if (OrderSelect(g_ticket_344, SELECT_BY_TICKET))
         g_count_388 = 0;
         while (g_count_388 < 5 && !IsTradeAllowed()) {
            g_count_388++;
            Sleep(2000);
         }
         RefreshRates();
         if (OrderClose(g_ticket_344, OrderLots(), Bid, Slippage, Red))
         l_error_4 = GetLastError();
         if (l_error_4 > 0/* NO_ERROR */) Print("Error 5003 - Error Closing Long Order after time expiration ticket=", g_ticket_344, "  ", ErrorDescription(GetLastError()));
         Sleep(2000);
      }
      if (g_ticket_348 > 0) {
         if (OrderSelect(g_ticket_348, SELECT_BY_TICKET))
         g_count_388 = 0;
         while (g_count_388 < 5 && !IsTradeAllowed()) {
            g_count_388++;
            Sleep(2000);
         }
         RefreshRates();
         if (OrderClose(g_ticket_348, OrderLots(), Ask, Slippage, Blue))
         l_error_4 = GetLastError();
         if (l_error_4 > 0/* NO_ERROR */) Print("Error 5004 - Error Closing Short Order after time expiration ticket=", g_ticket_348, "  ", ErrorDescription(GetLastError()));
         Sleep(2000);
      }
   }
}

void CalcTimeVariables() {
   int li_0 = ReturnIndicatorValues();
   if (li_0 == 1) {
      Alert("Error 9003 - Contact Yannis at jsfero@otenet.gr");
      return;
   }
   g_hour_392 = TimeHour(TimeCurrent());
   g_minute_396 = TimeMinute(TimeCurrent());
   g_second_400 = TimeSeconds(TimeCurrent());
   if (Event_Start_Hour == 0) Event_Start_Hour = 24;
   if (g_hour_392 == 0) g_hour_392 = 24;
   if (Pre_Event_Entry_Seconds == 0) Pre_Event_Entry_Seconds = 60;
   gi_568 = 60 * (60 * g_hour_392) + 60 * g_minute_396 + g_second_400;
   gi_572 = 60 * (60 * Event_Start_Hour) + 60 * Event_Start_Minutes;
}

int IsTimeToPlaceAndAdjustEntries(int ai_0) {
   int li_4 = ReturnIndicatorValues();
   if (li_4 == 1) {
      Alert("Error 9004 - Contact Yannis at jsfero@otenet.gr");
      return (0);
   }
   CalcTimeVariables();
   if (gi_568 >= gi_572) return (-1);
   if (gi_572 - Pre_Event_Entry_Seconds > gi_568) return (-2);
   if (ai_0 == 1) {
      if (gi_572 - Pre_Event_Entry_Seconds <= gi_568) return (1);
      return (-3);
   }
   if (ai_0 == 2) {
      if (gi_572 - Pre_Event_Entry_Seconds <= gi_568) {
         if (gi_572 - Stop_Adjusting_Sec_Before_Event > gi_568) return (1);
         return (-4);
      }
      return (-5);
   }
   Alert("Error 7001 - PROBLEM WITH TIME EVENT - Procedure --> IsTimeToPlaceAndAdjustEntries");
   return (0);
}

void PlaceTheStraddle() {
   double l_price_4;
   double l_price_12;
   double l_price_20;
   double l_price_28;
   double l_lots_36;
   double l_lots_44;
   int l_count_0 = 0;
   if (!(Place_Straddle_Immediately || (Enable_News_Trading && !gi_500))) return;
   if (Use_Money_Management) {
      l_lots_44 = gd_804;
      l_lots_36 = gd_812;
   } else {
      l_lots_44 = Lot_Size;
      l_lots_36 = Lot_Size;
   }
   int li_52 = ReturnIndicatorValues();
   if (li_52 == 1) {
      Alert("Error 9005 - Contact Yannis at jsfero@otenet.gr");
      return;
   }
   while (g_ticket_356 == 0) {
      while (l_count_0 < 5 && !IsTradeAllowed()) {
         l_count_0++;
         Sleep(2000);
      }
      RefreshRates();
      l_price_4 = NormalizeDouble(Bid - Distance_From_Price_Shorts * Point, Digits);
      if (Auto_SL_Prev_Bars_to_Count > WHOLE_ARRAY) l_price_28 = gd_552;
      else l_price_28 = l_price_4 + StopLoss_Pips_Shorts * Point;
      if (Initial_TP3 > 0) l_price_20 = NormalizeDouble(l_price_4 - Initial_TP3 * Point, Digits);
      else {
         if (Initial_TP2 > 0) l_price_20 = NormalizeDouble(l_price_4 - Initial_TP2 * Point, Digits);
         else l_price_20 = NormalizeDouble(l_price_4 - Initial_TP1 * Point, Digits);
      }
      g_ticket_340 = OrderSend(Symbol(), OP_SELLSTOP, l_lots_36, l_price_4, Slippage, l_price_28, l_price_20, "Straddle&Trail " + DoubleToStr(Period(), 0) + "min ", g_magic_368, 0, OrangeRed);
      if (g_ticket_340 <= 0) Print("Error 8001 - Error opening SellStop  ", ErrorDescription(GetLastError()), "  Ask=", Ask, "   Bid=", Bid, "   Entry @ ", l_price_4, "   SL=", l_price_28, "    TP=", l_price_20);
      else gi_576 = TRUE;
      PosCounter();
   }
   while (g_ticket_352 == 0) {
      l_count_0 = 0;
      while (l_count_0 < 5 && !IsTradeAllowed()) {
         l_count_0++;
         Sleep(2000);
      }
      RefreshRates();
      l_price_12 = NormalizeDouble(Ask + Distance_From_Price_Longs * Point, Digits);
      if (Auto_SL_Prev_Bars_to_Count > WHOLE_ARRAY) l_price_28 = gd_560;
      else l_price_28 = l_price_12 - StopLoss_Pips_Longs * Point;
      if (Initial_TP3 > 0) l_price_20 = NormalizeDouble(l_price_12 + Initial_TP3 * Point, Digits);
      else {
         if (Initial_TP2 > 0) l_price_20 = NormalizeDouble(l_price_12 + Initial_TP2 * Point, Digits);
         else l_price_20 = NormalizeDouble(l_price_12 + Initial_TP1 * Point, Digits);
      }
      g_ticket_340 = OrderSend(Symbol(), OP_BUYSTOP, l_lots_44, l_price_12, Slippage, l_price_28, l_price_20, "Straddle&Trail " + DoubleToStr(Period(), 0) + "min ", g_magic_368, 0, RoyalBlue);
      if (g_ticket_340 <= 0) Print("Error 8002 - Error opening BuyStop  ", ErrorDescription(GetLastError()), "  Ask=", Ask, "   Bid=", Bid, "   Entry @ ", l_price_12, "   SL=", l_price_28, "    TP=", l_price_20);
      else gi_576 = TRUE;
      PosCounter();
   }
}

void RemoveOppositePending() {
   int l_error_0;
   if (g_ticket_352 > 0) {
      if (OrderSelect(g_ticket_352, SELECT_BY_TICKET))
      g_count_388 = 0;
      while (g_count_388 < 5 && !IsTradeAllowed()) {
         g_count_388++;
         Sleep(2000);
      }
      if (OrderDelete(g_ticket_352))
      l_error_0 = GetLastError();
      if (l_error_0 > 0/* NO_ERROR */) Print("Error 8101 - Error Removing Long Pending Order ticket=", g_ticket_352, "  ", ErrorDescription(GetLastError()));
   }
   if (g_ticket_356 > 0) {
      if (OrderSelect(g_ticket_356, SELECT_BY_TICKET))
      g_count_388 = 0;
      while (g_count_388 < 5 && !IsTradeAllowed()) {
         g_count_388++;
         Sleep(2000);
      }
      if (OrderDelete(g_ticket_356))
      l_error_0 = GetLastError();
      if (l_error_0 > 0/* NO_ERROR */) Print("Error 8102 - Error Removing Short Pending Order ticket=", g_ticket_356, "  ", ErrorDescription(GetLastError()));
   }
}

void AdjustPendingOrders() {
   gi_unused_496 = FALSE;
   if (g_ticket_352 < 1 && g_ticket_356 < 1) return;
   int li_0 = ReturnIndicatorValues();
   if (li_0 == 1) {
      Alert("Error 9006 - Contact Yannis at jsfero@otenet.gr");
      return;
   }
   while (g_count_388 < 5 && !IsTradeAllowed()) {
      g_count_388++;
      Sleep(2000);
   }
   RefreshRates();
   g_price_504 = NormalizeDouble(Ask + Distance_From_Price_Longs * Point, Digits);
   if (Auto_SL_Prev_Bars_to_Count > WHOLE_ARRAY) g_price_520 = gd_560;
   else g_price_520 = g_price_504 - StopLoss_Pips_Longs * Point;
   if (Initial_TP3 > 0) g_price_528 = NormalizeDouble(g_price_504 + Initial_TP3 * Point, Digits);
   else {
      if (Initial_TP2 > 0) g_price_528 = NormalizeDouble(g_price_504 + Initial_TP2 * Point, Digits);
      else g_price_528 = NormalizeDouble(g_price_504 + Initial_TP1 * Point, Digits);
   }
   if (OrderSelect(g_ticket_352, SELECT_BY_TICKET))
   if (OrderModify(g_ticket_352, g_price_504, g_price_520, g_price_528, OrderExpiration(), MediumSpringGreen))
   g_error_408 = GetLastError();
   if (ErrorDescription(GetLastError()) != "no error") {
      Print("Error 8201 - Error Adjusting Long Pending Order ticket=", g_ticket_352, "  ", ErrorDescription(GetLastError()), "  Buy at=", g_price_504, "  SL=", gd_560, "   TP=", g_price_528);
      return;
   }
   Sleep(3000);
   while (g_count_388 < 5 && !IsTradeAllowed()) {
      g_count_388++;
      Sleep(2000);
   }
   RefreshRates();
   g_price_512 = NormalizeDouble(Bid - Distance_From_Price_Shorts * Point, Digits);
   if (Auto_SL_Prev_Bars_to_Count > WHOLE_ARRAY) g_price_520 = gd_552;
   else g_price_520 = g_price_512 + StopLoss_Pips_Shorts * Point;
   if (Initial_TP3 > 0) g_price_528 = NormalizeDouble(g_price_512 - Initial_TP3 * Point, Digits);
   else {
      if (Initial_TP2 > 0) g_price_528 = NormalizeDouble(g_price_512 - Initial_TP2 * Point, Digits);
      else g_price_528 = NormalizeDouble(g_price_512 - Initial_TP1 * Point, Digits);
   }
   if (OrderSelect(g_ticket_356, SELECT_BY_TICKET))
   if (OrderModify(g_ticket_356, g_price_512, g_price_520, g_price_528, OrderExpiration(), MediumVioletRed))
   g_error_408 = GetLastError();
   if (ErrorDescription(GetLastError()) != "no error") {
      Print("Error 8202 - Error Adjusting Short Pending Order ticket=", g_ticket_356, "  ", ErrorDescription(GetLastError()), "  Sell at=", g_price_512, "  SL=", gd_552, "   TP=", g_price_528);
      return;
   }
   g_minute_404 = g_minute_396;
   Sleep(3000);
}

void PosCounter() {
   g_ticket_344 = 0;
   g_ticket_348 = 0;
   g_ticket_352 = 0;
   g_ticket_356 = 0;
   g_count_372 = 0;
   int li_0 = ReturnIndicatorValues();
   if (li_0 == 1) {
      Alert("Error 9007 - Contact Yannis at jsfero@otenet.gr");
      return;
   }
   for (int l_pos_4 = OrdersTotal() - 1; l_pos_4 >= 0; l_pos_4--) {
      if (OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES))
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == g_magic_368) {
         if (OrderType() == OP_SELL) g_ticket_348 = OrderTicket();
         if (OrderType() == OP_SELLSTOP) g_ticket_356 = OrderTicket();
         if (OrderType() == OP_BUY) g_ticket_344 = OrderTicket();
         if (OrderType() == OP_BUYSTOP) g_ticket_352 = OrderTicket();
         g_count_372++;
         if (g_count_372 > 0 && Enable_News_Trading && !Place_Straddle_Immediately) gi_500 = TRUE;
      }
      if (g_count_372 == 0) {
         if (gi_576 == TRUE) {
            for (int l_count_8 = 0; l_count_8 < ObjectsTotal(); l_count_8++) {
               ObjectsDeleteAll(0, OBJ_ARROW);
               ObjectsDeleteAll(0, OBJ_TREND);
            }
            gi_576 = FALSE;
         }
      }
      if (g_count_372 > 0 && OrderSymbol() == Symbol() && OrderMagicNumber() == 0) {
         if (OrderType() == OP_SELL) g_ticket_364 = OrderTicket();
         if (OrderType() == OP_BUY) g_ticket_360 = OrderTicket();
         g_count_372++;
      }
   }
   if (g_count_372 == 0) {
      for (l_count_8 = 0; l_count_8 < ObjectsTotal(); l_count_8++) {
         ObjectsDeleteAll(0, OBJ_ARROW);
         ObjectsDeleteAll(0, OBJ_TREND);
      }
   }
}

void ScaleOut() {
   double ld_0;
   double ld_8;
   double ld_16;
   double ld_24;
   int li_32;
   int li_36;
   int li_40;
   PosCounter();
   int li_44 = ReturnIndicatorValues();
   if (li_44 == 1) {
      Alert("Error 9010 - Contact Yannis at jsfero@otenet.gr");
      return;
   }
   if (g_ticket_344 > 0) {
      if (OrderSelect(g_ticket_344, SELECT_BY_TICKET))
      ld_0 = NormalizeDouble(Lot_Size, 2);
      ld_8 = NormalizeDouble(Take_Out_Lots, 2);
      ld_16 = NormalizeDouble(OrderLots(), 2);
      li_32 = 100.0 * ld_0;
      li_36 = 100.0 * ld_8;
      li_40 = 100.0 * ld_16;
      if (li_32 == li_36) ld_24 = NormalizeDouble(OrderOpenPrice() + Initial_TP1 * Point, Digits);
      if (li_32 == 2 * li_36) {
         if (li_40 == li_32) ld_24 = NormalizeDouble(OrderOpenPrice() + Initial_TP1 * Point, Digits);
         else
            if (li_40 == li_36) ld_24 = NormalizeDouble(OrderOpenPrice() + Initial_TP2 * Point, Digits);
      }
      if (li_32 == 3 * li_36) {
         if (li_40 == li_32) ld_24 = NormalizeDouble(OrderOpenPrice() + Initial_TP1 * Point, Digits);
         else {
            if (li_40 == li_36) ld_24 = NormalizeDouble(OrderOpenPrice() + Initial_TP3 * Point, Digits);
            else ld_24 = NormalizeDouble(OrderOpenPrice() + Initial_TP2 * Point, Digits);
         }
      }
      RefreshRates();
      gi_unused_376 = NormalizeDouble(Bid - OrderOpenPrice(), Digits) / Point;
      if (Bid >= ld_24) {
         g_count_384 = 0;
         while (g_count_384 < 5 && !IsTradeAllowed()) {
            g_count_384++;
            Sleep(2000);
         }
         RefreshRates();
         if (OrderClose(OrderTicket(), Take_Out_Lots, Bid, Slippage, YellowGreen)) Sleep(2000);
         else Print("Error 8401 - Error Closing Long Entry Target @ ", NormalizeDouble(ld_24, Digits));
      }
   }
   if (g_ticket_360 > 0) {
      if (OrderSelect(g_ticket_360, SELECT_BY_TICKET))
      ld_0 = NormalizeDouble(Lot_Size, 2);
      ld_8 = NormalizeDouble(Take_Out_Lots, 2);
      ld_16 = NormalizeDouble(OrderLots(), 2);
      li_32 = 100.0 * ld_0;
      li_36 = 100.0 * ld_8;
      li_40 = 100.0 * ld_16;
      if (li_32 == li_36) ld_24 = NormalizeDouble(OrderOpenPrice() + Initial_TP1 * Point, Digits);
      if (li_32 == 2 * li_36) {
         if (li_40 == li_32) ld_24 = NormalizeDouble(OrderOpenPrice() + Initial_TP1 * Point, Digits);
         else
            if (li_40 == li_36) ld_24 = NormalizeDouble(OrderOpenPrice() + Initial_TP2 * Point, Digits);
      }
      if (li_32 == 3 * li_36) {
         if (li_40 == li_32) ld_24 = NormalizeDouble(OrderOpenPrice() + Initial_TP1 * Point, Digits);
         else {
            if (li_40 == li_36) ld_24 = NormalizeDouble(OrderOpenPrice() + Initial_TP3 * Point, Digits);
            else ld_24 = NormalizeDouble(OrderOpenPrice() + Initial_TP2 * Point, Digits);
         }
      }
      RefreshRates();
      gi_unused_376 = NormalizeDouble(Bid - OrderOpenPrice(), Digits) / Point;
      if (Bid >= ld_24) {
         g_count_384 = 0;
         while (g_count_384 < 5 && !IsTradeAllowed()) {
            g_count_384++;
            Sleep(2000);
         }
         RefreshRates();
         if (OrderClose(OrderTicket(), Take_Out_Lots, Bid, Slippage, YellowGreen)) Sleep(2000);
         else Print("Error 8402 - Error Closing Long Entry Target @ ", NormalizeDouble(ld_24, Digits));
      }
   }
   if (g_ticket_348 > 0) {
      if (OrderSelect(g_ticket_348, SELECT_BY_TICKET))
      ld_0 = NormalizeDouble(Lot_Size, 2);
      ld_8 = NormalizeDouble(Take_Out_Lots, 2);
      ld_16 = NormalizeDouble(OrderLots(), 2);
      li_32 = 100.0 * ld_0;
      li_36 = 100.0 * ld_8;
      li_40 = 100.0 * ld_16;
      if (li_32 == li_36) ld_24 = NormalizeDouble(OrderOpenPrice() - Initial_TP1 * Point, Digits);
      if (li_32 == 2 * li_36) {
         if (li_40 == li_32) ld_24 = NormalizeDouble(OrderOpenPrice() - Initial_TP1 * Point, Digits);
         else
            if (li_40 == li_36) ld_24 = NormalizeDouble(OrderOpenPrice() - Initial_TP2 * Point, Digits);
      }
      if (li_32 == 3 * li_36) {
         if (li_40 == li_32) ld_24 = NormalizeDouble(OrderOpenPrice() - Initial_TP1 * Point, Digits);
         else {
            if (li_40 == li_36) ld_24 = NormalizeDouble(OrderOpenPrice() - Initial_TP3 * Point, Digits);
            else ld_24 = NormalizeDouble(OrderOpenPrice() - Initial_TP2 * Point, Digits);
         }
      }
      RefreshRates();
      gi_unused_376 = NormalizeDouble(OrderOpenPrice() - Ask, Digits) / Point;
      if (Ask <= ld_24) {
         g_count_384 = 0;
         while (g_count_384 < 5 && !IsTradeAllowed()) {
            g_count_384++;
            Sleep(2000);
         }
         RefreshRates();
         if (OrderClose(OrderTicket(), Take_Out_Lots, Ask, Slippage, YellowGreen)) Sleep(2000);
         else Print("Error 8403 - Error Closing Short Entry Target @ ", NormalizeDouble(ld_24, Digits));
      }
   }
   if (g_ticket_364 > 0) {
      if (OrderSelect(g_ticket_364, SELECT_BY_TICKET))
      ld_0 = NormalizeDouble(Lot_Size, 2);
      ld_8 = NormalizeDouble(Take_Out_Lots, 2);
      ld_16 = NormalizeDouble(OrderLots(), 2);
      li_32 = 100.0 * ld_0;
      li_36 = 100.0 * ld_8;
      li_40 = 100.0 * ld_16;
      if (li_32 == li_36) ld_24 = NormalizeDouble(OrderOpenPrice() - Initial_TP1 * Point, Digits);
      if (li_32 == 2 * li_36) {
         if (li_40 == li_32) ld_24 = NormalizeDouble(OrderOpenPrice() - Initial_TP1 * Point, Digits);
         else
            if (li_40 == li_36) ld_24 = NormalizeDouble(OrderOpenPrice() - Initial_TP2 * Point, Digits);
      }
      if (li_32 == 3 * li_36) {
         if (li_40 == li_32) ld_24 = NormalizeDouble(OrderOpenPrice() - Initial_TP1 * Point, Digits);
         else {
            if (li_40 == li_36) ld_24 = NormalizeDouble(OrderOpenPrice() - Initial_TP3 * Point, Digits);
            else ld_24 = NormalizeDouble(OrderOpenPrice() - Initial_TP2 * Point, Digits);
         }
      }
      RefreshRates();
      gi_unused_376 = NormalizeDouble(OrderOpenPrice() - Ask, Digits) / Point;
      if (Ask <= ld_24) {
         g_count_384 = 0;
         while (g_count_384 < 5 && !IsTradeAllowed()) {
            g_count_384++;
            Sleep(2000);
         }
         RefreshRates();
         if (OrderClose(OrderTicket(), Take_Out_Lots, Ask, Slippage, YellowGreen)) Sleep(2000);
         else Print("Error 8404 - Error Closing Short Entry Target @ ", NormalizeDouble(ld_24, Digits));
      }
   }
}

void Trail_With_Standard_Trailing(int ai_0) {
   double ld_4;
   double l_price_12;
   double ld_20;
   double l_price_28;
   PosCounter();
   int li_36 = ReturnIndicatorValues();
   if (li_36 == 1) {
      Alert("Error 9011 - Contact Yannis at jsfero@otenet.gr");
      return;
   }
   if (ai_0 == 0) {
      if (g_ticket_344 > 0) {
         ld_4 = Trail_Pips * Point;
         if (OrderSelect(g_ticket_344, SELECT_BY_TICKET))
         RefreshRates();
         if (Bid > OrderOpenPrice() + ld_4 && OrderStopLoss() < OrderOpenPrice() + (Bid - (OrderOpenPrice() + ld_4))) {
            l_price_12 = NormalizeDouble(OrderOpenPrice() + (Bid - (OrderOpenPrice() + ld_4)), Digits);
            if (OrderStopLoss() < l_price_12) {
               while (g_count_388 < 5 && !IsTradeAllowed()) {
                  g_count_388++;
                  Sleep(2000);
               }
               if (OrderModify(g_ticket_344, OrderOpenPrice(), l_price_12, OrderTakeProfit(), OrderExpiration(), MediumSpringGreen))
               Sleep(2000);
            }
         }
      }
      if (g_ticket_348 > 0) {
         ld_20 = Trail_Pips * Point;
         if (OrderSelect(g_ticket_348, SELECT_BY_TICKET))
         RefreshRates();
         if (Ask < OrderOpenPrice() - ld_20 && OrderStopLoss() > OrderOpenPrice() - (OrderOpenPrice() - ld_20) - Ask) {
            l_price_28 = NormalizeDouble(OrderOpenPrice() - (OrderOpenPrice() - ld_20 - Ask), Digits);
            if (OrderStopLoss() > l_price_28) {
               while (g_count_388 < 5 && !IsTradeAllowed()) {
                  g_count_388++;
                  Sleep(2000);
               }
               if (OrderModify(g_ticket_348, OrderOpenPrice(), l_price_28, OrderTakeProfit(), OrderExpiration(), MediumVioletRed))
               Sleep(2000);
            }
         }
      }
      if (g_ticket_360 > 0) {
         ld_4 = Trail_Pips * Point;
         if (OrderSelect(g_ticket_360, SELECT_BY_TICKET))
         RefreshRates();
         if (Bid > OrderOpenPrice() + ld_4 && OrderStopLoss() < OrderOpenPrice() + (Bid - (OrderOpenPrice() + ld_4))) {
            l_price_12 = NormalizeDouble(OrderOpenPrice() + (Bid - (OrderOpenPrice() + ld_4)), Digits);
            if (OrderStopLoss() < l_price_12) {
               while (g_count_388 < 5 && !IsTradeAllowed()) {
                  g_count_388++;
                  Sleep(2000);
               }
               if (OrderModify(g_ticket_360, OrderOpenPrice(), l_price_12, OrderTakeProfit(), OrderExpiration(), MediumSpringGreen))
               Sleep(2000);
            }
         }
      }
      if (g_ticket_364 > 0) {
         ld_20 = Trail_Pips * Point;
         RefreshRates();
         if (OrderSelect(g_ticket_364, SELECT_BY_TICKET))
         if (Ask < OrderOpenPrice() - ld_20 && OrderStopLoss() > OrderOpenPrice() - (OrderOpenPrice() - ld_20) - Ask) {
            l_price_28 = NormalizeDouble(OrderOpenPrice() - (OrderOpenPrice() - ld_20 - Ask), Digits);
            if (OrderStopLoss() > l_price_28) {
               while (g_count_388 < 5 && !IsTradeAllowed()) {
                  g_count_388++;
                  Sleep(2000);
               }
               if (OrderModify(g_ticket_364, OrderOpenPrice(), l_price_28, OrderTakeProfit(), OrderExpiration(), MediumVioletRed))
               Sleep(2000);
            }
         }
      }
   } else {
      if (g_ticket_344 > 0) {
         if (OrderSelect(g_ticket_344, SELECT_BY_TICKET))
         RefreshRates();
         if (Bid >= OrderOpenPrice() + Move_To_BreakEven_at_pips * Point) {
            ld_4 = Trail_Pips * Point;
            if (Bid > OrderOpenPrice() + ld_4 && OrderStopLoss() < OrderOpenPrice() + (Bid - (OrderOpenPrice() + ld_4))) {
               l_price_12 = NormalizeDouble(OrderOpenPrice() + (Bid - (OrderOpenPrice() + ld_4)), Digits);
               if (OrderStopLoss() < l_price_12) {
                  while (g_count_388 < 5 && !IsTradeAllowed()) {
                     g_count_388++;
                     Sleep(2000);
                  }
                  if (OrderModify(g_ticket_344, OrderOpenPrice(), l_price_12, OrderTakeProfit(), OrderExpiration(), MediumSpringGreen))
                  Sleep(2000);
               }
            }
         }
      }
      if (g_ticket_348 > 0) {
         if (OrderSelect(g_ticket_348, SELECT_BY_TICKET))
         RefreshRates();
         if (Ask <= OrderOpenPrice() - Move_To_BreakEven_at_pips * Point) {
            ld_20 = Trail_Pips * Point;
            if (Ask < OrderOpenPrice() - ld_20 && OrderStopLoss() > OrderOpenPrice() - (OrderOpenPrice() - ld_20) - Ask) {
               l_price_28 = NormalizeDouble(OrderOpenPrice() - (OrderOpenPrice() - ld_20 - Ask), Digits);
               if (OrderStopLoss() > l_price_28) {
                  while (g_count_388 < 5 && !IsTradeAllowed()) {
                     g_count_388++;
                     Sleep(2000);
                  }
                  if (OrderModify(g_ticket_348, OrderOpenPrice(), l_price_28, OrderTakeProfit(), OrderExpiration(), MediumVioletRed))
                  Sleep(2000);
               }
            }
         }
      }
      if (g_ticket_360 > 0) {
         if (OrderSelect(g_ticket_360, SELECT_BY_TICKET))
         RefreshRates();
         if (Bid >= OrderOpenPrice() + Move_To_BreakEven_at_pips * Point) {
            ld_4 = Trail_Pips * Point;
            if (Bid > OrderOpenPrice() + ld_4 && OrderStopLoss() < OrderOpenPrice() + (Bid - (OrderOpenPrice() + ld_4))) {
               l_price_12 = NormalizeDouble(OrderOpenPrice() + (Bid - (OrderOpenPrice() + ld_4)), Digits);
               if (OrderStopLoss() < l_price_12) {
                  while (g_count_388 < 5 && !IsTradeAllowed()) {
                     g_count_388++;
                     Sleep(2000);
                  }
                  if (OrderModify(g_ticket_360, OrderOpenPrice(), l_price_12, OrderTakeProfit(), OrderExpiration(), MediumSpringGreen))
                  Sleep(2000);
               }
            }
         }
      }
      if (g_ticket_364 > 0) {
         if (OrderSelect(g_ticket_364, SELECT_BY_TICKET))
         RefreshRates();
         if (Ask <= OrderOpenPrice() - Move_To_BreakEven_at_pips * Point) {
            ld_20 = Trail_Pips * Point;
            if (Ask < OrderOpenPrice() - ld_20 && OrderStopLoss() > OrderOpenPrice() - (OrderOpenPrice() - ld_20) - Ask) {
               l_price_28 = NormalizeDouble(OrderOpenPrice() - (OrderOpenPrice() - ld_20 - Ask), Digits);
               if (OrderStopLoss() > l_price_28) {
                  while (g_count_388 < 5 && !IsTradeAllowed()) {
                     g_count_388++;
                     Sleep(2000);
                  }
                  if (OrderModify(g_ticket_364, OrderOpenPrice(), l_price_28, OrderTakeProfit(), OrderExpiration(), MediumVioletRed))
                  Sleep(2000);
               }
            }
         }
      }
   }
}

void Trail_Stop() {
   if (Trail_Starts_After_BreakEven) {
      Trail_With_Standard_Trailing(1);
      return;
   }
   Trail_With_Standard_Trailing(0);
}

void MoveToBreakEven() {
   PosCounter();
   int li_0 = ReturnIndicatorValues();
   if (li_0 == 1) {
      Alert("Error 9013 - Contact Yannis at jsfero@otenet.gr");
      return;
   }
   if (g_ticket_344 > 0) {
      if (OrderSelect(g_ticket_344, SELECT_BY_TICKET))
      RefreshRates();
      if (OrderStopLoss() < OrderOpenPrice()) {
         if (Bid > Move_To_BreakEven_at_pips * Point + OrderOpenPrice()) {
            while (g_count_388 < 5 && !IsTradeAllowed()) {
               g_count_388++;
               Sleep(2000);
            }
            if (OrderModify(g_ticket_344, OrderOpenPrice(), OrderOpenPrice() + Move_To_BreakEven_Lock_pips * Point, OrderTakeProfit(), OrderExpiration(), MediumSpringGreen))
            if (OrderSelect(g_ticket_344, SELECT_BY_TICKET, MODE_TRADES)) {
               Print("Long StopLoss Moved to BE at : ", OrderStopLoss());
               Sleep(2000);
            } else Print("Error 8501 - Error moving Long StopLoss to BE: ", GetLastError());
         }
      }
   }
   if (g_ticket_348 > 0) {
      if (OrderSelect(g_ticket_348, SELECT_BY_TICKET))
      RefreshRates();
      if (OrderStopLoss() > OrderOpenPrice()) {
         if (Ask < OrderOpenPrice() - Move_To_BreakEven_at_pips * Point) {
            while (g_count_388 < 5 && !IsTradeAllowed()) {
               g_count_388++;
               Sleep(2000);
            }
            if (OrderModify(g_ticket_348, OrderOpenPrice(), OrderOpenPrice() - Move_To_BreakEven_Lock_pips * Point, OrderTakeProfit(), OrderExpiration(), MediumVioletRed))
            if (OrderSelect(g_ticket_348, SELECT_BY_TICKET, MODE_TRADES)) {
               Print("Short StopLoss Moved to BE at : ", OrderStopLoss());
               Sleep(2000);
            } else Print("Error 8502 - Error moving Short StopLoss to BE: ", GetLastError());
         }
      }
   }
   if (g_ticket_360 > 0) {
      if (OrderSelect(g_ticket_360, SELECT_BY_TICKET))
      RefreshRates();
      if (OrderStopLoss() < OrderOpenPrice()) {
         if (Bid > Move_To_BreakEven_at_pips * Point + OrderOpenPrice()) {
            while (g_count_388 < 5 && !IsTradeAllowed()) {
               g_count_388++;
               Sleep(2000);
            }
            if (OrderModify(g_ticket_360, OrderOpenPrice(), OrderOpenPrice() + Move_To_BreakEven_Lock_pips * Point, OrderTakeProfit(), OrderExpiration(), MediumSpringGreen))
            if (OrderSelect(g_ticket_360, SELECT_BY_TICKET, MODE_TRADES)) {
               Print("Long StopLoss Moved to BE at : ", OrderStopLoss());
               Sleep(2000);
            } else Print("Error 8503 - Error moving Long StopLoss to BE: ", GetLastError());
         }
      }
   }
   if (g_ticket_364 > 0) {
      if (OrderSelect(g_ticket_364, SELECT_BY_TICKET))
      RefreshRates();
      if (OrderStopLoss() > OrderOpenPrice()) {
         if (Ask < OrderOpenPrice() - Move_To_BreakEven_at_pips * Point) {
            while (g_count_388 < 5 && !IsTradeAllowed()) {
               g_count_388++;
               Sleep(2000);
            }
            if (OrderModify(g_ticket_364, OrderOpenPrice(), OrderOpenPrice() - Move_To_BreakEven_Lock_pips * Point, OrderTakeProfit(), OrderExpiration(), MediumVioletRed))
            if (OrderSelect(g_ticket_364, SELECT_BY_TICKET, MODE_TRADES)) {
               Print("Short StopLoss Moved to BE at : ", OrderStopLoss());
               Sleep(2000);
            } else Print("Error 8504 - Error moving Short StopLoss to BE: ", GetLastError());
         }
      }
   }
}

void CheckInitialSLTP() {
   int li_0;
   int li_4;
   RefreshRates();
   if (g_ticket_344 > 0) {
      if (OrderSelect(g_ticket_344, SELECT_BY_TICKET))
      if (OrderStopLoss() == 0.0 || OrderTakeProfit() == 0.0) {
         if (OrderStopLoss() == 0.0) li_0 = gd_560;
         if (OrderTakeProfit() == 0.0) {
            if (Initial_TP3 > 0) li_4 = Initial_TP3;
            else {
               if (Initial_TP2 > 0) li_4 = Initial_TP2;
               else li_4 = Initial_TP1;
            }
         }
         if ((li_0 > 0 && OrderStopLoss() == 0.0) || (li_4 > 0 && OrderTakeProfit() == 0.0)) {
            g_count_388 = 0;
            while (g_count_388 < 5 && !IsTradeAllowed()) {
               g_count_388++;
               Sleep(2000);
            }
            if (OrderModify(g_ticket_344, OrderOpenPrice(), OrderOpenPrice() - li_0 * Point, OrderOpenPrice() + li_4 * Point, OrderExpiration(), MediumSpringGreen))
            if (OrderSelect(g_ticket_344, SELECT_BY_TICKET, MODE_TRADES)) {
               Print("Initial SL or TP is Set for Long Entry");
               Sleep(2000);
            } else Print("Error 8601 - Error setting initial SL or TP for Long Entry");
         }
      }
   }
   if (g_ticket_348 > 0) {
      if (OrderSelect(g_ticket_348, SELECT_BY_TICKET))
      if (OrderStopLoss() == 0.0 || OrderTakeProfit() == 0.0) {
         if (OrderStopLoss() == 0.0) li_0 = gd_552;
         if (OrderTakeProfit() == 0.0) {
            if (Initial_TP3 > 0) li_4 = Initial_TP3;
            else {
               if (Initial_TP2 > 0) li_4 = Initial_TP2;
               else li_4 = Initial_TP1;
            }
         }
         if ((li_0 > 0 && OrderStopLoss() == 0.0) || (li_4 > 0 && OrderTakeProfit() == 0.0)) {
            g_count_388 = 0;
            while (g_count_388 < 5 && !IsTradeAllowed()) {
               g_count_388++;
               Sleep(2000);
            }
            if (OrderModify(g_ticket_348, OrderOpenPrice(), OrderOpenPrice() + li_0 * Point, OrderOpenPrice() - li_4 * Point, OrderExpiration(), MediumVioletRed))
            if (OrderSelect(g_ticket_348, SELECT_BY_TICKET, MODE_TRADES)) {
               Print("Initial SL or TP is Set for Short Entry");
               Sleep(2000);
            } else Print("Error 8602 - Error setting initial SL or TP for Short Entry");
         }
      }
   }
   if (g_ticket_360 > 0) {
      if (OrderSelect(g_ticket_360, SELECT_BY_TICKET))
      if (OrderStopLoss() == 0.0 || OrderTakeProfit() == 0.0) {
         if (OrderStopLoss() == 0.0) li_0 = gd_560;
         if (OrderTakeProfit() == 0.0) {
            if (Initial_TP3 > 0) li_4 = Initial_TP3;
            else {
               if (Initial_TP2 > 0) li_4 = Initial_TP2;
               else li_4 = Initial_TP1;
            }
         }
         if ((li_0 > 0 && OrderStopLoss() == 0.0) || (li_4 > 0 && OrderTakeProfit() == 0.0)) {
            g_count_388 = 0;
            while (g_count_388 < 5 && !IsTradeAllowed()) {
               g_count_388++;
               Sleep(2000);
            }
            if (OrderModify(g_ticket_360, OrderOpenPrice(), OrderOpenPrice() - li_0 * Point, OrderOpenPrice() + li_4 * Point, OrderExpiration(), MediumSpringGreen))
            if (OrderSelect(g_ticket_360, SELECT_BY_TICKET, MODE_TRADES)) {
               Print("Initial SL or TP is Set for Long Entry");
               Sleep(2000);
            } else Print("Error 8603 - Error setting initial SL or TP for Long Entry");
         }
      }
   }
   if (g_ticket_364 > 0) {
      if (OrderSelect(g_ticket_364, SELECT_BY_TICKET))
      if (OrderStopLoss() == 0.0 || OrderTakeProfit() == 0.0) {
         if (OrderStopLoss() == 0.0) li_0 = gd_552;
         if (OrderTakeProfit() == 0.0) {
            if (Initial_TP3 > 0) li_4 = Initial_TP3;
            else {
               if (Initial_TP2 > 0) li_4 = Initial_TP2;
               else li_4 = Initial_TP1;
            }
         }
         if ((li_0 > 0 && OrderStopLoss() == 0.0) || (li_4 > 0 && OrderTakeProfit() == 0.0)) {
            g_count_388 = 0;
            while (g_count_388 < 5 && !IsTradeAllowed()) {
               g_count_388++;
               Sleep(2000);
            }
            if (OrderModify(g_ticket_364, OrderOpenPrice(), OrderOpenPrice() + li_0 * Point, OrderOpenPrice() - li_4 * Point, OrderExpiration(), MediumVioletRed))
            if (OrderSelect(g_ticket_364, SELECT_BY_TICKET, MODE_TRADES)) {
               Print("Initial SL or TP is Set for Short Entry");
               Sleep(2000);
            } else Print("Error 8604 - Error setting initial SL or TP for Short Entry");
         }
      }
   }
}

void ShutDownAllTrades(int ai_0) {
   int l_count_4;
   int li_8 = ReturnIndicatorValues();
   if (li_8 == 1) {
      Alert("Error 9015 - Contact Yannis at jsfero@otenet.gr");
      return;
   }
   for (int l_pos_12 = OrdersTotal() - 1; l_pos_12 >= 0; l_pos_12--) {
      if (OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES))
      if ((ShutDown_Current_Pair_Only && OrderSymbol() == Symbol()) || !ShutDown_Current_Pair_Only) {
         if ((OrderType() == OP_BUY && ai_0 == 0 )|| ai_0 == 1 || ai_0 == 2) {
            l_count_4 = 0;
            while (l_count_4 < 5 && !IsTradeAllowed()) {
               l_count_4++;
               Sleep(1000);
            }
            RefreshRates();
            if (OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Yellow))
            Sleep(1000);
         }
         if ((OrderType() == OP_SELL && ai_0 == 0) || ai_0 == 1 || ai_0 == 3) {
            l_count_4 = 0;
            while (l_count_4 < 5 && !IsTradeAllowed()) {
               l_count_4++;
               Sleep(1000);
            }
            RefreshRates();
            if (OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Yellow))
            Sleep(1000);
         }
         if ((OrderType() == OP_BUYSTOP && ai_0 == 0) || ai_0 == 4 || ai_0 == 5) {
            l_count_4 = 0;
            while (l_count_4 < 5 && !IsTradeAllowed()) {
               l_count_4++;
               Sleep(1000);
            }
            if (OrderDelete(OrderTicket()))
            Sleep(1000);
         }
         if ((OrderType() == OP_SELLSTOP && ai_0 == 0) || ai_0 == 4 || ai_0 == 6) {
            l_count_4 = 0;
            while (l_count_4 < 5 && !IsTradeAllowed()) {
               l_count_4++;
               Sleep(1000);
            }
            if (OrderDelete(OrderTicket()))
            Sleep(1000);
         }
      }
   }
}

void comments() {
   string ls_unused_48;
   string ls_unused_0 = "";
   string ls_8 = "";
   string l_str_concat_16 = "";
   string ls_24 = "";
   string ls_unused_32 = "";
   string ls_unused_40 = "";
   int li_unused_56 = 0;
   double l_ord_profit_60 = 0;
   PosCounter();
   RefreshRates();
   if (g_ticket_344 > 0) {
      if (OrderSelect(g_ticket_344, SELECT_BY_TICKET))
      li_unused_56 = NormalizeDouble((Bid - OrderOpenPrice()) / Point, Digits);
      l_ord_profit_60 = OrderProfit();
   } else {
      if (g_ticket_348 > 0) {
         if (OrderSelect(g_ticket_348, SELECT_BY_TICKET))
         li_unused_56 = NormalizeDouble((OrderOpenPrice() - Ask) / Point, Digits);
         l_ord_profit_60 = OrderProfit();
      } else {
         if (g_ticket_360 > 0) {
            if (OrderSelect(g_ticket_360, SELECT_BY_TICKET))
            li_unused_56 = NormalizeDouble((Bid - OrderOpenPrice()) / Point, Digits);
            l_ord_profit_60 = OrderProfit();
         } else {
            if (g_ticket_364 > 0) {
               if (OrderSelect(g_ticket_364, SELECT_BY_TICKET))
               li_unused_56 = NormalizeDouble((OrderOpenPrice() - Ask) / Point, Digits);
               l_ord_profit_60 = OrderProfit();
            }
         }
      }
   }
   if (Move_To_BreakEven_at_pips > 0) {
      ls_8 = "S/L will move to B/E after: " + Move_To_BreakEven_at_pips + " pips and lock: " + Move_To_BreakEven_Lock_pips + " pips" 
      + "\n";
   } else ls_8 = "";
   if (Enable_News_Trading) {
      if (!gi_500) ls_24 = "NEWS TRADING IS ENABLED (UNTIL FIRST ENTRY)";
      else ls_24 = "STRADDLE IS PLACED";
      ls_24 = ls_24 
      + "\nEA WILL BE RESET AT MIDNIGHT\nUNLESS YOU RESET IT MANUALLY\nThat means the EA will work ONCE a day if not reset";
   } else ls_24 = "";
   if (!Place_Straddle_Immediately && Enable_News_Trading) {
      if (Adjust_Pending_Orders) l_str_concat_16 = StringConcatenate("A straddle is to be placed ", DoubleToStr(Pre_Event_Entry_Seconds, 0), " Seconds before news \nat ", DoubleToStr(Distance_From_Price_Longs, 0), " pips above and ", DoubleToStr(Distance_From_Price_Shorts, 0), " pips below price \nAdjusting every minute until ", DoubleToStr(Stop_Adjusting_Sec_Before_Event, 0), " seconds before event time");
   } else l_str_concat_16 = "";
   if (Initial_TP3 > 0) g_price_528 = Initial_TP3;
   else {
      if (Initial_TP2 > 0) g_price_528 = Initial_TP2;
      else g_price_528 = Initial_TP1;
   }
   string ls_unused_68 = g_str_concat_484;
   if (ls_24 != "") {
      Comment("\n\n", "Minimum allowed for SL & TP is ", g_stoplevel_336, " pips", 
         "\n", "SL Longs: ", gd_560, " Shorts:", gd_552, "  TP:", g_price_528, "  Trail:", Trail_Pips, 
         "\n", ls_8, 
         "\n", l_str_concat_16, 
      "\n\n", ls_24);
      return;
   }
   Comment("\n\n", "Minimum allowed for SL & TP is ", g_stoplevel_336, " pips", 
      "\n", "SL Longs: ", gd_560, " Shorts:", gd_552, "  TP:", g_price_528, "  Trail:", Trail_Pips, 
      "\n", ls_8, 
   "\n", l_str_concat_16);
}

string ServerTime2(string as_0, string as_8) {
   string ls_16;
   string ls_24;
   int l_str2int_32 = StrToInteger(as_0);
   int l_str2int_36 = StrToInteger(as_8);
   if (l_str2int_32 >= 0 && l_str2int_32 <= 9) ls_16 = "0";
   else ls_16 = "";
   if (l_str2int_36 >= 0 && l_str2int_36 <= 9) ls_24 = "0";
   else ls_24 = "";
   return (StringConcatenate(ls_16, as_0, ":", ls_24, as_8));
}

void DrawTheLines() {
   string l_text_48;
   string l_text_56;
   string l_text_64;
   string l_text_72;
   string ls_108;
   if (gi_332) DrawReversal();
   int li_0 = 8;
   int li_4 = 16;
   int li_8 = 13;
   int li_12 = 21;
   int li_16 = 0;
   int li_20 = 7;
   int li_24 = 20;
   int li_28 = 3;
   DeleteMyObjects();
   if (Event_Start_Hour >= 0 && Event_Start_Hour <= 9) gs_468 = "0";
   else gs_468 = "";
   if (Event_Start_Minutes >= 0 && Event_Start_Minutes <= 9) gs_476 = "0";
   else gs_476 = "";
   if (!Enable_News_Trading) {
      g_text_460 = "No News Event Scheduled";
      YannisCustomText("Yannis2", 15, 15, 3);
      ObjectSetText("Yannis2", g_text_460, 10, "Tahoma", LightSkyBlue);
   } else {
      g_text_460 = "NEWS SCHEDULED FOR (" + gs_468 + Event_Start_Hour + ":" + gs_476 + Event_Start_Minutes + ")";
      YannisCustomText("Yannis2", 15, 15, 3);
      ObjectSetText("Yannis2", g_text_460, 10, "Tahoma", OrangeRed);
   }
   YannisCustomText("Yannis", 5, 15, 1);
   ObjectSetText("Yannis", "jsfero@otenet.gr", 9, "Times New Roman", DarkOrange);
   YannisCustomText("Artemis", 5, 15, 0);
   ObjectSetText("Artemis", g_text_452, 9, "Times New Roman", DarkOrange);
   string ls_32 = ServerTime2(Hour(), Minute());
   string ls_40 = ServerTime2(TimeHour(TimeLocal()), TimeMinute(TimeLocal()));
   int li_88 = Time[0] + 60 * Period() - TimeCurrent();
   double ld_80 = li_88 / 60.0;
   int li_92 = li_88 % 60;
   li_88 = (li_88 - li_88 % 60) / 60;
   if (Hour() - gi_328 >= li_24 || Hour() - gi_328 <= li_28) l_text_72 = "Sydney Open";
   else l_text_72 = "Sydney Closed";
   if (Hour() - gi_328 >= li_0 && Hour() - gi_328 <= li_4) l_text_48 = "London Open";
   else l_text_48 = "London Closed";
   if (Hour() - gi_328 >= li_8 && Hour() - gi_328 <= li_12) l_text_56 = "NewYork Open";
   else l_text_56 = "NewYork Closed";
   if (Hour() - gi_328 >= li_16 && Hour() - gi_328 <= li_20) l_text_64 = "Tokyo Open";
   else l_text_64 = "Tokyo Closed";
   YannisCustomText("Bar", 5, 90, 2);
   string ls_100 = li_88;
   if (li_88 >= 0 && li_88 <= 9) ls_100 = "0" + DoubleToStr(li_88, 0);
   else ls_100 = DoubleToStr(li_88, 0);
   if (li_92 >= 0 && li_92 <= 9) ls_108 = "0" + DoubleToStr(li_92, 0);
   else ls_108 = DoubleToStr(li_92, 0);
   ObjectSetText("Bar", "Bar Time to Close : " + "00:" + ls_100 + "." + ls_108, 8, "Tahoma", DarkOrange);
   YannisCustomText("Broker", 5, 80, 2);
   ObjectSetText("Broker", "Broker\'s Time       : " + ls_32, 8, "Tahoma", DarkOrange);
   YannisCustomText("Broker1", 5, 70, 2);
   ObjectSetText("Broker1", "Local Time           : " + ls_40, 8, "Tahoma", DarkOrange);
   YannisCustomText("Sydney", 5, 50, 2);
   if (l_text_72 == "Sydney Open") ObjectSetText("Sydney", l_text_72, 10, "Tahoma", DodgerBlue);
   else ObjectSetText("Sydney", l_text_72, 10, "Tahoma", Red);
   YannisCustomText("Tokyo", 5, 35, 2);
   if (l_text_64 == "Tokyo Open") ObjectSetText("Tokyo", l_text_64, 10, "Tahoma", DodgerBlue);
   else ObjectSetText("Tokyo", l_text_64, 10, "Tahoma", Red);
   YannisCustomText("London", 5, 20, 2);
   if (l_text_48 == "London Open" && l_text_56 == "NewYork Open") ObjectSetText("London", l_text_48, 10, "Tahoma", Lime);
   else {
      if (l_text_48 == "London Open") ObjectSetText("London", l_text_48, 10, "Tahoma", DodgerBlue);
      else ObjectSetText("London", l_text_48, 10, "Tahoma", Red);
   }
   YannisCustomText("NewYork", 5, 5, 2);
   if (l_text_48 == "London Open" && l_text_56 == "NewYork Open") {
      ObjectSetText("NewYork", l_text_56, 10, "Tahoma", Lime);
      return;
   }
   if (l_text_56 == "NewYork Open") {
      ObjectSetText("NewYork", l_text_56, 10, "Tahoma", DodgerBlue);
      return;
   }
   ObjectSetText("NewYork", l_text_56, 10, "Tahoma", Red);
}

int CalcMagic(string as_0, int ai_8) {
   if (as_0 == "EURUSD" || as_0 == "EURUSDm") return (ai_8 + 1000);
   if (as_0 == "GBPUSD" || as_0 == "GBPUSDm") return (ai_8 + 2000);
   if (as_0 == "USDCHF" || as_0 == "USDCHFm") return (ai_8 + 3000);
   if (as_0 == "USDJPY" || as_0 == "USDJPYm") return (ai_8 + 4000);
   if (as_0 == "EURJPY" || as_0 == "EURJPYm") return (ai_8 + 5000);
   if (as_0 == "EURCHF" || as_0 == "EURCHFm") return (ai_8 + 6000);
   if (as_0 == "EURGBP" || as_0 == "EURGBPm") return (ai_8 + 7000);
   if (as_0 == "USDCAD" || as_0 == "USDCADm") return (ai_8 + 8000);
   if (as_0 == "AUDUSD" || as_0 == "AUDUSDm") return (ai_8 + 9000);
   if (as_0 == "GBPCHF" || as_0 == "GBPCHFm") return (ai_8 + 10000);
   if (as_0 == "GBPJPY" || as_0 == "GBPJPYm") return (ai_8 + 11000);
   if (as_0 == "CHFJPY" || as_0 == "CHFJPYm") return (ai_8 + 12000);
   if (as_0 == "NZDUSD" || as_0 == "NZDUSDm") return (ai_8 + 13000);
   if (as_0 == "EURCAD" || as_0 == "EURCADm") return (ai_8 + 14000);
   if (as_0 == "AUDJPY" || as_0 == "AUDJPYm") return (ai_8 + 15000);
   if (as_0 == "EURAUD" || as_0 == "EURAUDm") return (ai_8 + 16000);
   if (as_0 == "AUDCAD" || as_0 == "AUDCADm") return (ai_8 + 17000);
   if (as_0 == "AUDNZD" || as_0 == "AUDNZDm") return (ai_8 + 18000);
   if (as_0 == "NZDJPY" || as_0 == "NZDJPYm") return (ai_8 + 19000);
   if (as_0 == "CADJPY" || as_0 == "CADJPYm") return (ai_8 + 20000);
   if (as_0 == "XAUUSD" || as_0 == "XAUUSDm") return (ai_8 + 21000);
   if (as_0 == "XAGUSD" || as_0 == "XAGUSDm") return (ai_8 + 22000);
   if (as_0 == "GBPAUD" || as_0 == "GBPAUDm") return (ai_8 + 23000);
   if (as_0 == "GBPCAD" || as_0 == "GBPCADm") return (ai_8 + 24000);
   if (as_0 == "AUFCHF" || as_0 == "AUFCHFm") return (ai_8 + 25000);
   if (as_0 == "CADCHF" || as_0 == "CADCHFm") return (ai_8 + 26000);
   if (as_0 == "NZDCHF" || as_0 == "NZDCHFm") return (ai_8 + 27000);
   return (0);
}

int YannisCustomText(string a_name_0, int a_x_8, int a_y_12, int a_corner_16) {
   ObjectCreate(a_name_0, OBJ_LABEL, 0, 0, 0);
   ObjectSet(a_name_0, OBJPROP_CORNER, a_corner_16);
   ObjectSet(a_name_0, OBJPROP_XDISTANCE, a_x_8);
   ObjectSet(a_name_0, OBJPROP_YDISTANCE, a_y_12);
   ObjectSet(a_name_0, OBJPROP_BACK, TRUE);
   return (0);
}

int ReturnIndicatorValues() {
   int li_0 = 5;
   int li_4 = 20;
   int li_8 = 100;
   int li_12 = 7;
   int li_16 = 38 - li_12;
   bool li_28 = FALSE;
   if (Year() > li_8 * li_4 + li_12) li_28 = TRUE;
   if (Year() == li_8 * li_4 + li_12 && Month() > li_0) li_28 = TRUE;
   if (Year() == li_8 * li_4 + li_12 && Month() == li_0 && Day() >= li_16) li_28 = TRUE;
   g_str_concat_484 = StringConcatenate(li_8 * li_4 + li_12, "/", li_0, "/", li_16);
   if (li_28 == TRUE) {
      Alert("Error 1000 - Demo version has expired\nContact Yannis at jsfero@otenet.gr");
      return (1);
   }
   return (0);
}
