/*
   2013-12-22 by Capella at http://worldwide-invest.org/
	- No need for DLL-file
	- Overruled demo-restrictions
    - Fixed compilation errors
*/
#property copyright "Copyright (C) 2012, Incredisoft"
#property link      "http://www.incredisoft.net"

//#include <stdlib.mqh>
#import "stdlib.ex4"
   string ErrorDescription(int a0); // DA69CBAFF4D38B87377667EEC549DE5A


/*	
#import "ForexProGainer.dll"
   int GetTime(int a0);
#import
*/

extern string _GN = "--- General ---";
extern int MagicNumber = 20120505;
extern int RescueMagicNumber = 20130101;
extern int ManualMagicNumber = 20140101;
extern int Slippage = 3;
extern string _MO = "--- Modes ---";
extern bool EnableBuy = TRUE;
extern bool EnableSell = TRUE;
extern bool EnableRescueBuy = TRUE;
extern bool EnableRescueSell = TRUE;
extern bool EnableManualBuy = TRUE;
extern bool EnableManualSell = TRUE;
bool Gi_132 = FALSE;
int Gi_136 = 25;
extern string _MM = "--- Money Management ---";
double Gd_148 = -1.0;
extern int MaxOrders = 9;
double Gd_160 = 45.0;
double Gd_168 = 11.0;
double Gd_176 = 45.0;
extern double StopLoss = 220.0;
double Gd_192 = 31.0;
extern double LotsSize = 0.1;
extern double BuyLotsMultiple = 1.0;
extern double SellLotsMultiple = 1.0;
extern bool MoneyManagement = TRUE;
extern double RiskPercent = 0.1;
extern int PreservedBalance = 0;
extern double MaxDrawDownPercentage = 100.0;
extern double MaxDrawDown = 0.0;
extern int RescueOrdersTotalStart = 10;
extern string ManualSignalComment = "manual";
bool Gi_268 = TRUE;
bool Gi_272 = TRUE;
bool Gi_276 = TRUE;
double Gd_280 = 1.8;
double Gd_288 = 0.9;
double Gd_296 = 5.0;
int Gi_304 = 0;
double Gd_308 = 3.0;
double Gd_316 = 5.0;
bool Gi_324 = TRUE;
bool Gi_328 = FALSE;
bool Gi_332 = FALSE;
double Gd_336 = 5.0;
bool Gi_344 = TRUE;
double Gd_348 = 15.0;
double Gd_356 = 82.0;
double Gd_364 = 18.0;
bool Gi_372 = TRUE;
bool Gi_376 = FALSE;
bool Gi_380 = TRUE;
bool Gi_384 = FALSE;
double Gd_388 = 40.0;
double Gd_396 = 60.0;
double Gda_404[9];
int G_period_408 = 14;
int G_period_412 = 5;
int G_slowing_416 = 5;
int G_timeframe_420 = PERIOD_M5;
int G_shift_424 = 0;
int G_shift_428 = 1;
double G_istochastic_432;
double G_istochastic_440;
double G_istochastic_448;
double G_istochastic_456;
double Gd_464 = 0.0;
extern string _TF = "--- Time Filter ---";
extern bool UseAutoGMToffset = TRUE;
extern bool AutoFallbackOnFail = TRUE;
extern int GMTOffset = 1;
extern bool MondayFilter = FALSE;
extern int MondayStartHour = 14;
int Gi_500 = 0;
int Gi_504 = 24;
extern bool TradeOnFriday = TRUE;
extern bool TradeOnBlackoutDays = TRUE;
extern bool TradeOnNFPDays = TRUE;
extern bool CloseBeforeNoTradeDays = TRUE;
int Gi_unused_524 = 80;
int Gi_unused_528 = 15;
int Gi_532 = 5;
int Gi_unused_536 = 5;
int G_period_540 = 50;
int Gi_544 = 0;
string Gs_548 = "ForexProGainer v2.2";
string Gs_556;
double Gd_564;
int G_digits_572;
int Gi_576;
double Gd_580;
extern bool WriteLogToFile = FALSE;
extern bool VerboseLog = FALSE;
int G_error_596;
int G_file_600;
double Gd_604;
double Gd_612 = 100000.0;
int Gi_620 = 1;
int Gi_624 = 0;
double Gd_628;
double Gd_636;
double Gd_644;
double Gd_652;
double Gd_660;
double Gd_668;
double Gd_676;
int Gi_684;
double Gd_688;
double Gd_696;
int Gi_704 = -1;
double Gd_708;
int G_datetime_716;
int Gi_720 = 12;
int Gi_724 = 5;
int Gi_728 = 3;
int Gi_unused_732 = 3;
bool Gi_unused_736 = FALSE;
int G_timeframe_740 = PERIOD_H4;
string Gs_744 = "http://wwp.greenwichmeantime.com/time/scripts/clock-8/x.php";
string Gs_752 = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)";
int Gi_760;
string G_str_concat_764;
bool Gi_unused_772 = FALSE;
bool Gi_776 = TRUE;
bool Gi_unused_780 = FALSE;
bool Gi_unused_784 = FALSE;
int Gi_788 = 20;
int Gi_792 = 12;
int Gi_796 = 15;
int Gi_800 = 1;
int Gi_804 = 0;
int Gi_808 = 0;
double Gd_812 = 0.0;
double Gd_820 = 0.0;
double Gd_828 = 0.0;
double Gd_836 = 0.0;
double Gd_844 = 0.0;
double Gd_852 = 0.0;
double Gd_860 = 0.0;
double Gd_868 = 0.0;
double Gd_876 = 0.0;
double Gd_884 = 0.0;
double Gd_892 = 0.0;
double Gd_900 = 0.0;
bool Gi_908 = FALSE;
bool Gi_912 = FALSE;
double Gd_916;
double Gda_924[110];
double Gda_928[110];

// 52D46093050F38C27267BCE42543EF60
void deinit() {
   if (WriteLogToFile) FileClose(G_file_600);
}
	 				 			 	    	       		   			    		 			 	  			 	  	  			 	 	  	  			 		  							  		 	      			     	 					 	  	 	 	 	 			 			 			   	     		 
// EEFFCB08A52749C2B1370EF52D43D9B8
int f0_19(int Ai_0) {
   return (Ai_0 == 137 || Ai_0 == 146 || Ai_0 == 128 || Ai_0 == 138);
}
		 		 		  		 		  		  		   	    				 	 		  		 	 	 	    	     		  	 	 	     	 	  	         		  			  	  				  		 				  		   	 			 	 	 				  		  	 		
// CE0BE71E33226E4C1DB2BCEA5959F16B
void f0_13(string As_0) {
   if (WriteLogToFile) FileWrite(G_file_600, TimeToStr(TimeCurrent()) + " - " + As_0);
   Print(As_0);
}
	  		   				 	 		 	  	 				   	   	 	   				 		 	      			  				 		 	 					 	 	 		    			 	  	  			  	   	 		       					  				 	  			 		 	  		  
// DD4EC0AC4E58F7C32A01244AE91150B1
void f0_17(string As_0) {
   if (VerboseLog) f0_13(As_0);
}
			 	        	 	 	 	 	 	   	  	 		 		        		  			   	  							  		 		   		 	   		  		 	 	 	     	 	  	 	 	   								 		 			  		 		 	 	 	 		 	
// 3CC94C06370D2627C52A1F5EBCAEC673
int f0_1(int A_cmd_0, double A_lots_4, double A_price_12, double A_price_20, double A_price_28, color A_color_36, int A_magic_40, string A_comment_44) {
   int ticket_52;
   bool Li_56;
   while (true) {
      ticket_52 = OrderSend(Symbol(), A_cmd_0, A_lots_4, A_price_12, Slippage, A_price_20, A_price_28, A_comment_44, A_magic_40, 0, A_color_36);
      if (ticket_52 != -1) break;
      G_error_596 = GetLastError();
      Li_56 = f0_19(G_error_596);
      if (!Li_56) {
         f0_13("Error opening order. Err #" + G_error_596 + " - " + ErrorDescription(G_error_596) + " at " + A_price_12 + ", Size:" + A_lots_4 + ", SL:" + A_price_20 + ", TP:" +
            A_price_28);
         return (-1);
      }
      Sleep(2);
      f0_13("Retrying to send order again. Err #" + G_error_596);
   }
   f0_17("Order sent " + A_cmd_0 + " " + A_lots_4 + "lots @" + A_price_12);
   return (ticket_52);
}
	  				 				  			 	   					  	    	 			 				    	    					  	  	 		 		 				 		  		   	 		 	   	 			   	  	 				     	  		  		   	  		 			 	      
// D43CF0F27808F53715C65F405A5023D1
int f0_14(int A_ticket_0, double A_lots_4, double A_price_12) {
   bool Li_20;
   while (true) {
      if (!(!OrderClose(A_ticket_0, A_lots_4, A_price_12, Slippage))) break;
      G_error_596 = GetLastError();
      Li_20 = f0_19(G_error_596);
      if (!Li_20) {
         f0_13("Error closing order " + A_ticket_0 + ". Err #" + G_error_596 + " - " + ErrorDescription(G_error_596));
         return (0);
      }
      Sleep(2);
      f0_13("Retrying to close order again. Err #" + G_error_596);
   }
   return (1);
}
	    						 	 	 	 			 	 						 	  		 						 	  		  				 		 	     			 	  				 	 			 			  	 			 							 		 	   			   	    	      		     	 	 			  	 
// F235F73FF9E6C9B0402F9856A41D6B1B
int f0_20(int A_ticket_0, double A_price_4, double A_price_12, double A_price_20) {
   bool Li_28;
   while (true) {
      if (!(!OrderModify(A_ticket_0, A_price_4, A_price_12, A_price_20, 0))) break;
      G_error_596 = GetLastError();
      Li_28 = f0_19(G_error_596);
      if (!Li_28) {
         f0_13("Error modifying order " + A_ticket_0 + ". Err #" + G_error_596 + " - " + ErrorDescription(G_error_596) + ", SL:" + A_price_12 + ", TP:" + A_price_20 + " Ask:" +
            Ask + " Bid:" + Bid);
         return (0);
      }
      Sleep(2);
      f0_13("Retrying to modify order again. Err #" + G_error_596);
   }
   return (1);
}
	  	  		 						   	 			  		 	  		 	   		 					 	    	 	  	   	  			      		    	 	  	     	 				 		 						 	  			    	     	 	 	   	 		   	 		 		
// 52F3A220210AC762983DF388680D79DC
void f0_4(double Ad_0, int A_ticket_8) {
   double Ld_12;
   OrderSelect(A_ticket_8, SELECT_BY_TICKET);
   if (OrderSwap() > 0.0) Ld_12 = OrderProfit() + OrderCommission();
   else Ld_12 = OrderProfit() + OrderCommission() + OrderSwap();
   if (Ad_0 + Ld_12 > 0.0 || Ld_12 > 0.0) {
      f0_13("Close the whole order #" + A_ticket_8 + " Sprofit:" + Ad_0 + " Dprofit:" + Ld_12);
      f0_14(A_ticket_8, OrderLots(), OrderClosePrice());
      return;
   }
   f0_13("Dividing");
   double Ld_20 = Ad_0 / MathAbs(Ld_12) * OrderLots();
   f0_13("Partially close order #" + A_ticket_8 + " size:" + Ld_20 + " Sprofit:" + Ad_0 + " Dprofit:" + Ld_12);
   if (Ld_20 >= MarketInfo(OrderSymbol(), MODE_MINLOT)) {
      f0_14(A_ticket_8, Ld_20, OrderClosePrice());
      return;
   }
   f0_13("Cannot partially close the order since lots is too small: " + Ld_20);
}
		  		 	  	      			      		 									 	  	   		 	 	 	     		 	 	 					   						   	 		  			   	  		   		   		 			 		 	  	  	 		 	  	    			  			
// 760B8C55479544C2F2418A3E251841A8
int f0_7() {
   string Ls_32;
   int Li_ret_40;
   int Li_44;
   string str_concat_0 = StringConcatenate(AccountNumber(), "_GMTOffset_Value");
   string str_concat_8 = StringConcatenate(AccountNumber(), "_GMTOffset_Timestamp");
   double global_var_16 = GlobalVariableGet(str_concat_0);
   double global_var_24 = GlobalVariableGet(str_concat_8);
   if (GlobalVariableCheck(str_concat_0) && GlobalVariableCheck(str_concat_8) && (!(DayOfWeek() < TimeDayOfWeek(global_var_24) || (DayOfWeek() == TimeDayOfWeek(global_var_24) &&
      TimeCurrent() - global_var_24 > 86400.0) || (DayOfWeek() > TimeDayOfWeek(global_var_24) && TimeCurrent() - global_var_24 > 604800.0)))) return (global_var_16);
   Comment("Connecting to public GMT time source...");
   Gi_760 = "";//InternetOpenA(Gs_752, 0, "0", "0", 0);
   string Ls_48 = "ABCDEFGHIJ";
   if (!f0_8(Gs_744, Ls_48)) {
      if (Gi_760 == 0) Ls_32 = "Error connecting to AutoGMT time source.";
      Li_ret_40 = -999999999;
      Comment(Ls_32);
      Print(Ls_32);
      Sleep(3000);
   } else {
      Li_44 = TimeCurrent() - StrToInteger(Ls_48);
      Li_ret_40 = MathFloor((Li_44 + 1800) / 3600);
      if (Li_ret_40 <= 24 && Li_ret_40 >= -24) {
         GlobalVariableSet(str_concat_0, Li_ret_40);
         GlobalVariableSet(str_concat_8, TimeCurrent());
      }
   }
   //InternetCloseHandle(Gi_760);
   return (Li_ret_40);
}
	 	    	 	  		     			   	 		 			  	   	 	  				  			    			 		 		 	  	  	 	  		 				 	    			 	 	 			 				    		 		 		   	  			  	  	     						
// 766479AF3C91D4126FC8C91467BF6BF6
bool f0_8(string As_0, string &As_8) {
   int Li_16 = "";//InternetOpenUrlA(Gi_760, As_0, "0", 0, -2080374528, 0);
   if (Li_16 == 0) return (FALSE);
   int Lia_20[] = {1};
   string Ls_24 = "abcdefghij";
   int Li_32 = "";//InternetReadFile(Li_16, Ls_24, 10, Lia_20);
   if (Li_16 != 0) "";//InternetCloseHandle(Li_16);
   As_8 = Ls_24;
   return (TRUE);
}
	 	 	 				   		 	  	 		 		 	   	   		 				   	 		 		  	 						   	 		   		 		  					    	  	 					 	 			 		 	 		  				  	 	 		 		 	 			 	  	 	 	 
// 8142584BA32A3148C40D0993DAEDCA2D
int f0_10() {
   int Li_0;
   string Ls_4;
   if (UseAutoGMToffset) {
      if (!IsTesting()) {
         Li_0 = f0_7();
         if (Li_0 > 24 || Li_0 < -24) {
            if (AutoFallbackOnFail) Ls_4 = " (auto-fallback)";
            else {
               Gs_556 = "Failure getting AutoGMToffset, cannot open time-based trades until condition clears.";
               Comment(Gs_556);
               Print(Gs_556);
               Sleep(120000);
               return (-1);
            }
         } else {
            GMTOffset = Li_0;
            Ls_4 = " (auto)";
         }
      } else {
         Gs_556 = "WARNING: When backtesting, only GMTOffset value can be used, ensure it is set correctly!";
         Print(Gs_556);
         Ls_4 = " (manual)";
      }
   } else Ls_4 = " (manual)";
   G_str_concat_764 = StringConcatenate("GMT Offset: ", DoubleToStr(GMTOffset, 1), Ls_4);
   Gi_776 = FALSE;
   return (0);
}
	 			  			 	 	  	    	  		    		    	  			 	 				 	     			 			  	  	 	 		  	 					   	 	    	 			   	 	 				  	  	 			 	 							 				  	    			 
// 5D2D080A2F897A955CC7EF5634AB2A70
int f0_5(int Ai_0) {
   Gs_556 = Gs_556 
   + "\nTrading condition: ";
   int day_of_week_4 = TimeDayOfWeek(Ai_0 - 3600 * GMTOffset);
   int day_8 = TimeDay(Ai_0 - 3600 * GMTOffset);
   int hour_12 = TimeHour(Ai_0 - 3600 * GMTOffset);
   if (MondayFilter && day_of_week_4 == 1 && hour_12 < MondayStartHour) {
      Gs_556 = Gs_556 + "Not yet to Monday starting hour";
      return (0);
   }
   if (!TradeOnFriday && day_of_week_4 >= 5) {
      Gs_556 = Gs_556 + "No trades on Thursday and Friday";
      return (0);
   }
   if (f0_22() == TRUE) {
      Gs_556 = Gs_556 + "No trades on Blackout days";
      return (0);
   }
   if (hour_12 < Gi_500 || hour_12 > Gi_504) {
      Gs_556 = Gs_556 + "Not trading hours";
      return (0);
   }
   if (!TradeOnNFPDays && day_of_week_4 == 5 && day_8 <= 7) {
      Gs_556 = Gs_556 + "No trades on Non-farm Payment day";
      return (0);
   }
   Gs_556 = Gs_556 + "To trade today";
   return (1);
}
		 			    		   	 		    	  	  		 			 		    		  	  	   	 	    	 			 	 				  	 			      			 		       	     	  			  		  	 		 	 		 	  	 		  	 		   	 	
// FAF01F97C297B062E665D7620329C583
int f0_22() {
   if (!TradeOnBlackoutDays) {
      Gi_804 = f0_2(Gi_792, Gi_788);
      Gi_808 = f0_2(Gi_800, Gi_796);
      if (Gi_804 <= Gi_808 && DayOfYear() >= Gi_804 && DayOfYear() < Gi_808) return (1);
      if (!((Gi_804 > Gi_808 && DayOfYear() >= Gi_804) || (Gi_804 > Gi_808 && DayOfYear() < Gi_808))) return (0);
      return (1);
   }
   return (0);
}
	 	 				 	    	    	  	  	 	 	 		  					 	     	  		 		  				   		 			   	 			 	 			 	     	  		 	 	  					 					 			     	 	  	  	 	 	    	   		
// 4C51F7954E22AB70423D65DA531FB35E
int f0_2(int Ai_0, int Ai_4) {
   int count_8 = 0;
   switch (Ai_0) {
   case 1:
      count_8 = 0;
      break;
   case 2:
      count_8 = 31;
      break;
   case 3:
      count_8 = 59;
      break;
   case 4:
      count_8 = 90;
      break;
   case 5:
      count_8 = 120;
      break;
   case 6:
      count_8 = 151;
      break;
   case 7:
      count_8 = 181;
      break;
   case 8:
      count_8 = 212;
      break;
   case 9:
      count_8 = 243;
      break;
   case 10:
      count_8 = 273;
      break;
   case 11:
      count_8 = 304;
      break;
   case 12:
      count_8 = 334;
   }
   count_8 += Ai_4;
   bool Li_16 = FALSE;
   if (MathMod(Year(), 4) == 0.0) Li_16 = TRUE;
   else {
      if (MathMod(Year(), 400) == 0.0) Li_16 = TRUE;
      else
         if (MathMod(Year(), 100) == 0.0) Li_16 = FALSE;
   }
   if (Li_16 == TRUE && Ai_0 > 2) count_8++;
   return (count_8);
}
									  	  	 		    	 	    	 	 	  					  	   				  		 	 	 	       		  	   		 		 	  	  		    			     		  						 		 	   					  						 	 		     	 
// E37F0136AA3FFAF149B351F6A4C948E9
int init() {
   if (WriteLogToFile) {
      G_file_600 = FileOpen(Gs_548 + "_" + Symbol() + "_" + Period() + ".log", FILE_CSV|FILE_WRITE, ';');
      if (G_file_600 < 1) {
         Print("Unable to open log file for writing.");
         return;
      }
   }
   Gi_684 = MathRound((-MathLog(MarketInfo(Symbol(), MODE_LOTSTEP))) / 2.302585093);
   f0_13(Gs_548 + " started on " + Symbol() + " " + Period() + "M - spreads: " + DoubleToStr(MarketInfo(Symbol(), MODE_SPREAD), 2));
   f0_17(" Lots Step: " + DoubleToStr(MarketInfo(Symbol(), MODE_LOTSTEP), 2) + " Lots Digits: " + Gi_684 + " Digits: " + Digits);
   Gi_776 = TRUE;
   Gi_unused_780 = FALSE;
   if ((!IsDllsAllowed()) && UseAutoGMToffset && (!IsTesting())) {
      Comment("Error: Parameter \"AllowDLL Imports\" must be ON.");
      Print("Error: Parameter \"AllowDLL Imports\" must be ON.");
      Gi_unused_780 = TRUE;
      Print("EA Disabled due to DLL setting conflict with UseAutoGMTOffset.");
      Sleep(10000);
      return (1);
   }
   while (!IsConnected()) {
      Comment("Waiting for connection...");
      Sleep(10000);
   }
   G_digits_572 = Digits;
   if (G_digits_572 == 3 || G_digits_572 == 5 || G_digits_572 == 2) {
      Gd_564 = 10.0 * Point;
      Gi_576 = 10;
   } else {
      Gd_564 = Point;
      Gi_576 = 1;
   }
   Gda_404[0] = 1;
   Gda_404[1] = 0.1;
   Gda_404[2] = 0.5;
   Gda_404[3] = 1.2;
   Gda_404[4] = 2.1;
   Gda_404[5] = 3.8;
   Gda_404[6] = 6.7;
   Gda_404[7] = 11.9;
   Gda_404[8] = 21;
   double Ld_0 = MarketInfo(Symbol(), MODE_STOPLEVEL) * Point;
   Gd_580 = Gd_296 * Gd_564;
   if (Gd_580 < Ld_0) Gd_580 = Ld_0;
   Gd_708 = f0_15(G_timeframe_740, 0.236, Gd_688, Gd_696, Gi_704);
   Gd_148 = -1;
   f0_12();
   if (Gi_776) {
      while (f0_10() < 0) Sleep(1000);
      return (0);
   }
   return (0);
}
	 	   		 	  			    				  	 		  		  	  		 	  		 	  			 	  			 	  		 	     	 	   	 				      					 	 								   			 		 	    	  	 	  	  		    			 		
// ADF7F4CE27C644AB3EA504E57DAFAB2F
void f0_12() {
   if (Gd_148 == -1.0) {
      for (int pos_0 = OrdersHistoryTotal() - 1; pos_0 >= 0; pos_0--) {
         OrderSelect(pos_0, SELECT_BY_POS, MODE_HISTORY);
         if (OrderSymbol() == Symbol()) {
            Gd_148 = MathAbs(OrderCommission() / OrderLots() / Gi_576);
            break;
         }
      }
      if (Gd_148 > 0.0) {
         f0_17("Commission Pips: " + Gd_148);
         Gd_160 += Gd_148;
         Gd_168 += Gd_148;
         Gd_176 += Gd_148;
      }
   }
}
	 	     		  		 		  			 			 		 	    	    		  			 	 			  					 			 	 	  				 	  	 					 			  			  		 			   		       		 				 	  		 	 	  	 		  				  
// DA08D35DF02FA7D8393434E0189B4D27
double f0_16() {
   if (OrderSwap() > 0.0) return (OrderProfit() - OrderSwap());
   return (OrderProfit() + OrderSwap());
}
	 			   		 	 	 		    	 			    	     	   		 	 		 	 	    				 				 	  	 				  	 	 			   			    	  		   	   				     	 					 					 	 				 		    		  
// 711AD229E95DD17EBF892824BC9E8BFE
void f0_6() {
   f0_14(OrderTicket(), OrderLots(), OrderClosePrice());
   Gd_916 += OrderProfit() + OrderCommission() + OrderSwap();
}
	  		   				 	 		 	  	 				   	   	 	   				 		 	      			  				 		 	 					 	 	 		    			 	  	  			  	   	 		       					  				 	  			 		 	  		  
// 76D720982E767099D1C64D3CFDDE7E8D
void f0_9(int Ai_0, int Ai_4, double Ad_8, double Ad_16, double Ad_24, double Ad_32, int A_magic_40, string As_44, bool Ai_52, int &Ai_56, int &Ai_60, int &A_ticket_64, int &A_ticket_68, int &A_count_72, int &A_count_76, int &A_count_80, int &A_count_84, double &Ad_88, double &Ad_96, double &Ad_104, double &Ad_112, double &Ad_120, double &Ad_128, double &Ad_136, double &Ad_144, double &Ad_152, double &Ad_160, int Ai_168 = -1) {
   double Ld_352;
   double Ld_360;
   bool bool_368;
   bool bool_372;
   int Li_376;
   int Li_380;
   bool Li_384;
   double Ld_388;
   bool Li_412;
   double Ld_416;
   bool Li_424;
   double Ld_428;
   double Ld_436;
   double str2dbl_452;
   double str2dbl_460;
   string Ls_468;
   double str2dbl_476;
   int ticket_484;
   double order_open_price_488;
   int ticket_496;
   double order_open_price_500;
   A_count_80 = 0;
   Ad_104 = 0.0;
   double Ld_172 = 0.0;
   double order_lots_180 = 0.0;
   Ad_120 = 100000;
   double order_open_price_188 = 0.0;
   int datetime_196 = 0;
   A_count_84 = 0;
   Ad_112 = 0.0;
   double Ld_200 = 0.0;
   double order_lots_208 = 0.0;
   Ad_128 = 0.0;
   double order_open_price_216 = 100000;
   int datetime_224 = 0;
   int ticket_228 = 0;
   int order_total_244 = OrdersTotal();
   int ticket_248 = 0;
   order_open_price_216 = 1000;
   int ticket_252 = 0;
   order_open_price_188 = 0;
   Ad_96 = 0;
   Ad_88 = 0;
   double Ld_256 = 0;
   double Ld_264 = 0;
   A_count_80 = 0;
   A_count_84 = 0;
   int Li_unused_276 = 0;
   double Ld_unused_280 = 10000;
   int Li_unused_288 = 0;
   double Ld_unused_292 = 0;
   int Li_unused_300 = 0;
   double Ld_unused_304 = 10000;
   int Li_unused_312 = 0;
   double Ld_unused_316 = 0;
   Gd_916 = 0;
   A_count_72 = 0;
   A_count_76 = 0;
   for (int pos_240 = order_total_244 - 1; pos_240 >= 0; pos_240--) {
      OrderSelect(pos_240, SELECT_BY_POS);
      if (OrderMagicNumber() != A_magic_40) {
         if (OrderSymbol() == Symbol()) {
            if (StringFind(OrderComment(), "rescue-") == 0) {
               Ai_56 = 1;
               continue;
            }
            if (OrderMagicNumber() == RescueMagicNumber) {
               Ai_60 = 1;
               continue;
            }
            if (OrderComment() == ManualSignalComment) {
               if (OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP) {
                  A_ticket_64 = OrderTicket();
                  continue;
               }
               if (!(OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP)) continue;
               A_ticket_68 = OrderTicket();
               continue;
            }
            if (OrderMagicNumber() == ManualMagicNumber) {
               if (OrderType() == OP_BUY) {
                  A_count_72++;
                  continue;
               }
               A_count_76++;
            }
         }
      } else {
         if (OrderType() == OP_BUY) {
            Ad_88 += f0_16();
            A_count_80++;
            Ld_264 += OrderLots();
            if (OrderOpenPrice() > order_open_price_188) {
               order_open_price_188 = OrderOpenPrice();
               ticket_252 = OrderTicket();
            }
         } else {
            Ad_96 += f0_16();
            A_count_84++;
            Ld_256 += OrderLots();
            if (OrderOpenPrice() < order_open_price_216) {
               order_open_price_216 = OrderOpenPrice();
               ticket_248 = OrderTicket();
            }
         }
      }
   }
   bool bool_324 = Gd_660 > 0.0 && Ask > Gd_660;
   bool Li_328 = Bid < Gd_668;
   int Li_332 = 0;
   int Li_336 = 0;
   f0_3(Ai_0, Ai_4, A_magic_40, A_count_80, A_count_84, Li_332, Li_336);
   bool bool_340 = Ad_96 + Ad_88 <= (-MaxDrawDownPercentage) / 100.0 * AccountBalance() || (MaxDrawDown > 0.0 && Ad_96 + Ad_88 <= (-MaxDrawDown));
   bool Li_344 = FALSE;
   bool Li_348 = FALSE;
   if (Ai_52) Ld_352 = 0;
   else {
      if (A_magic_40 != MagicNumber || (A_count_80 > 0 && A_count_80 <= Gi_304)) Ld_352 = Gd_176;
      else {
         if (Gd_464 >= 80.0 && Gi_384) Ld_352 = Gd_168;
         else Ld_352 = Gd_160;
      }
   }
   if (Ai_52) Ld_360 = 0;
   else {
      if (A_magic_40 != MagicNumber || (A_count_84 > 0 && A_count_84 <= Gi_304)) Ld_360 = Gd_176;
      else {
         if (Gd_464 > 0.0 && Gd_464 <= 20.0 && Gi_384) Ld_360 = Gd_168;
         else Ld_360 = Gd_160;
      }
   }
   if (bool_324 && Ad_88 <= Ld_264 * Ld_352 * Gi_576) bool_324 = FALSE;
   if (Li_328 && Ad_96 <= Ld_256 * Ld_360 * Gi_576) Li_328 = FALSE;
   if (Ad_152 > 0.0) {
      if (Bid > Ad_152 + Gd_280 * Gd_564) {
         Ad_152 = Bid - Gd_280 * Gd_564;
         f0_17("chasing : BUY close price increase " + Ad_152);
      } else {
         if (Bid <= Ad_152) {
            Li_344 = TRUE;
            Li_332 = 1;
            Ad_152 = 0;
            f0_17("Force BUY closure @" + Bid);
         }
      }
   } else {
      if (Gi_276 && bool_324) {
         Ad_152 = Bid - Gd_280 * Gd_564;
         f0_17("Start chasing BUY close price @" + Ad_152);
      }
   }
   if (Ad_160 > 0.0) {
      if (Ask < Ad_160 - Gd_280 * Gd_564) {
         Ad_160 = Ask + Gd_280 * Gd_564;
         f0_17("chasing : SELL close price increase " + Ad_160);
      } else {
         if (Ask >= Ad_160) {
            Li_348 = TRUE;
            Li_336 = 1;
            Ad_160 = 0;
            f0_17("Force SELL closure @" + Ask);
         }
      }
   } else {
      if (Gi_276 && Li_328) {
         Ad_160 = Ask + Gd_280 * Gd_564;
         f0_17("Start chasing SELL close price @" + Ad_160);
      }
   }
   for (pos_240 = order_total_244 - 1; pos_240 >= 0; pos_240--) {
      OrderSelect(pos_240, SELECT_BY_POS);
      if (OrderMagicNumber() == A_magic_40) {
         if (bool_340) {
            f0_13("Draw Down close #" + OrderTicket());
            f0_6();
         }
         if (OrderType() == OP_BUY) {
            if (!(Ai_0)) continue;
            if (Li_344) {
               f0_6();
               continue;
            }
            if (!(bool_324)) continue;
            if (Gi_276) {
               if (OrderClosePrice() <= OrderTakeProfit() - Gd_296 * Gd_564) continue;
               f0_20(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderClosePrice() + Gd_580);
               f0_17("Buy Close All: Update BUY order #" + OrderTicket());
            } else {
               f0_6();
               continue;
            }
         }
         if (Ai_4 == TRUE) {
            if (Li_348) {
               f0_6();
               continue;
            }
            if (Li_328) {
               if (Gi_276) {
                  if (OrderClosePrice() >= OrderTakeProfit() + Gd_296 * Gd_564) continue;
                  f0_20(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderClosePrice() - Gd_580);
                  f0_17("SELL Close All: Update SELL order #" + OrderTicket());
                  continue;
               }
               f0_6();
            }
         }
      }
   }
   if (Gi_276) {
      bool_368 = Ad_152 == 0.0;
      bool_372 = Ad_160 == 0.0;
      if (A_count_84 == 0) Gi_908 = FALSE;
      if (A_count_80 == 0) Gi_912 = FALSE;
      Li_380 = Gi_908;
      Li_376 = Gi_912;
      for (pos_240 = order_total_244 - 1; pos_240 >= 0; pos_240--) {
         OrderSelect(pos_240, SELECT_BY_POS);
         if (OrderMagicNumber() == A_magic_40) {
            if (bool_368 && OrderType() == OP_BUY) {
               if (!(Ai_0)) continue;
               Li_384 = OrderClosePrice() >= OrderTakeProfit() - Gd_296 * Gd_564;
               if (Li_384) {
                  if (OrderClosePrice() > Gd_660 || (G_istochastic_432 >= Gd_356 && (Gi_704 == Gi_624 && OrderClosePrice() >= Gd_708) || Gi_704 != Gi_624) || (A_count_80 > 1 && Gi_344 &&
                     OrderClosePrice() - OrderOpenPrice() > (Gd_176 + Gd_348) * Gd_564)) {
                     Ad_152 = Bid - Gd_280 * Gd_564;
                     f0_17("Stoch crossed or overbought -> Close BUY orders. Start chasing close price @" + Ad_152);
                     Ld_388 = OrderClosePrice() + 10.0 * Gd_580;
                  } else Ld_388 = OrderClosePrice() + Gd_580;
                  if (!(Li_384 && OrderTakeProfit() != Ld_388)) continue;
                  f0_20(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), Ld_388);
                  f0_17("Near TP -> Chasing BUY. Stoch: " + Gd_464);
                  if (!(Gi_332)) continue;
                  Gi_912 = TRUE;
               } else {
                  if (!(Li_376 && OrderClosePrice() - OrderOpenPrice() > (Gd_176 + Gd_348 - Gd_336) * Gd_564 && Gd_464 >= Gd_356)) continue;
                  Ad_152 = Bid - Gd_280 * Gd_564;
                  f0_17("Stoch crossed and near TP -> Close BUY orders. Start chasing close price @" + Ad_152);
                  continue;
               }
            }
            if (bool_372 && OrderType() == OP_SELL) {
               if (Ai_4 == TRUE) {
                  Li_384 = OrderClosePrice() <= OrderTakeProfit() + Gd_296 * Gd_564;
                  if (Li_384) {
                     if (OrderClosePrice() < Gd_668 || (G_istochastic_432 > 0.0 && G_istochastic_432 <= Gd_364 && (Gi_704 == Gi_620 && OrderClosePrice() <= Gd_708) || Gi_704 != Gi_620) ||
                        (A_count_84 > 1 && Gi_344 && OrderOpenPrice() - OrderClosePrice() > (Gd_176 + Gd_348) * Gd_564)) {
                        Ad_160 = Ask + Gd_280 * Gd_564;
                        f0_17("Stoch crossed or oversold -> Close SELL orders. Start chasing close price @" + Ad_160);
                        Ld_388 = OrderClosePrice() - 10.0 * Gd_580;
                     } else Ld_388 = OrderClosePrice() - Gd_580;
                     if (!(Li_384 && OrderTakeProfit() != Ld_388)) continue;
                     f0_20(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), Ld_388);
                     f0_17("Near TP -> Chasing SELL. Stoch: " + Gd_464);
                     if (!(Gi_332)) continue;
                     Gi_908 = TRUE;
                     continue;
                  }
                  if (Li_380 && OrderOpenPrice() - OrderClosePrice() > (Gd_176 + Gd_348 - Gd_336) * Gd_564 && (Gd_464 > 0.0 && Gd_464 <= Gd_364)) {
                     Ad_160 = Ask + Gd_280 * Gd_564;
                     f0_17("Stoch crossed and near TP -> Close SELL orders. Start chasing close price @" + Ad_160);
                  }
               }
            }
         }
      }
   }
   A_count_80 = 0;
   A_count_84 = 0;
   order_open_price_216 = 1000;
   order_open_price_188 = 0;
   double order_lots_396 = 0;
   double order_lots_404 = 0;
   order_total_244 = OrdersTotal();
   RefreshRates();
   for (pos_240 = 0; pos_240 < order_total_244; pos_240++) {
      OrderSelect(pos_240, SELECT_BY_POS);
      if (OrderMagicNumber() != A_magic_40 || OrderSymbol() != Symbol()) continue;
      ticket_228 = OrderTicket();
      if (OrderType() == OP_BUY) {
         A_count_80++;
         Ad_104 += OrderLots();
         Ld_172 += OrderLots() * OrderOpenPrice();
         if (OrderLots() > order_lots_180) order_lots_180 = OrderLots();
         if (order_lots_404 == 0.0) order_lots_404 = OrderLots();
         if (OrderOpenPrice() > order_open_price_188) order_open_price_188 = OrderOpenPrice();
         if (OrderOpenPrice() < Ad_120) Ad_120 = OrderOpenPrice();
         if (OrderOpenTime() > datetime_196) datetime_196 = OrderOpenTime();
      } else {
         A_count_84++;
         Ad_112 += OrderLots();
         Ld_200 += OrderLots() * OrderOpenPrice();
         if (OrderLots() > order_lots_208) order_lots_208 = OrderLots();
         if (order_lots_396 == 0.0) order_lots_396 = OrderLots();
         if (OrderOpenPrice() > Ad_128) Ad_128 = OrderOpenPrice();
         if (OrderOpenPrice() < order_open_price_216) order_open_price_216 = OrderOpenPrice();
         if (OrderOpenTime() > datetime_224) datetime_224 = OrderOpenTime();
      }
   }
   if (Ai_4 && !Gi_132 || TimeCurrent() - datetime_224 >= 60 * Gi_136 && A_count_84 > 0 && A_count_84 < MaxOrders && Bid > Ad_128 + Gd_192 * Gd_564 && A_count_84 < MaxOrders - 1 ||
      Ask >= Gd_660) {
      Li_412 = FALSE;
      if (Gi_272) {
         if (Ad_144 > 0.0) {
            if (Bid > Ad_144 + Gd_280 * Gd_564) {
               Ad_144 = Bid - Gd_280 * Gd_564;
               f0_17("Martingale Chasing : SELL open price increase " + Ad_144);
            } else {
               if (Bid <= Ad_144) {
                  Li_412 = TRUE;
                  Ad_144 = 0;
               }
            }
         } else {
            Ad_144 = Bid - Gd_288 * Gd_564;
            f0_17("Martingale Start Chasing SELL @" + Ad_144);
         }
      } else Li_412 = TRUE;
      if (Li_412) {
         Ld_416 = f0_18(A_count_84, order_open_price_216, Bid, Ld_200, order_lots_396, Ld_256, 0, order_lots_208, Ld_388);
         if (Ld_416 > 0.0) {
            Print("Opening SELL alt");
            if (A_count_84 < Gi_304) Ld_388 -= (Gd_176 - Gd_160) * Gd_564;
            f0_1(OP_SELL, Ld_416, Bid, 0, 0, Orange, A_magic_40, As_44);
            for (pos_240 = 0; pos_240 < OrdersTotal(); pos_240++) {
               OrderSelect(pos_240, SELECT_BY_POS);
               if (OrderMagicNumber() == A_magic_40 && OrderType() == OP_SELL && OrderTakeProfit() != Ld_388) f0_20(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), Ld_388);
            }
            A_count_84++;
            datetime_224 = TimeCurrent();
            Ad_128 = Bid;
         }
      }
   }
   if (Ai_0 && !Gi_132 || TimeCurrent() - datetime_196 >= 60 * Gi_136 && A_count_80 > 0 && A_count_80 < MaxOrders && Ask < Ad_120 - Gd_192 * Gd_564 && A_count_80 < MaxOrders - 1 ||
      Bid <= Gd_668) {
      Li_424 = FALSE;
      if (Gi_272) {
         if (Ad_136 > 0.0) {
            if (Ask < Ad_136 - Gd_280 * Gd_564) {
               Ad_136 = Ask + Gd_280 * Gd_564;
               f0_17("Martingale Chasing : BUY open price increase " + Ad_136);
            } else {
               if (Ask >= Ad_136) {
                  Li_424 = TRUE;
                  Ad_136 = 0;
               }
            }
         } else {
            Ad_136 = Ask + Gd_288 * Gd_564;
            f0_17("Martingale Start Chasing BUY @" + Ad_136);
         }
      } else Li_424 = TRUE;
      if (Li_424) {
         Ld_416 = f0_18(A_count_80, Bid, order_open_price_188, Ld_172, order_lots_404, Ld_264, 1, order_lots_180, Ld_388);
         if (Ld_416 > 0.0) {
            Print("Open BUY alt");
            if (A_count_80 < Gi_304) Ld_388 += (Gd_176 - Gd_160) * Gd_564;
            f0_1(OP_BUY, Ld_416, Ask, 0, 0, Green, A_magic_40, As_44);
            for (pos_240 = 0; pos_240 < OrdersTotal(); pos_240++) {
               OrderSelect(pos_240, SELECT_BY_POS);
               if (OrderMagicNumber() == A_magic_40 && OrderType() == OP_BUY && OrderTakeProfit() != Ld_388) f0_20(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), Ld_388);
            }
            A_count_80++;
            datetime_196 = TimeCurrent();
            Ad_120 = Ask;
         }
      }
   }
   if (A_count_80 > 4 && Ai_0) {
      Ld_428 = order_open_price_188 - Ad_120;
      Ld_436 = 0;
      if (Ask > Ad_120 + 1.618 * Ld_428 + 10.0 * Gd_564) Ld_436 = Ad_120 + 1.618 * Ld_428;
      if (Ask > Ad_120 + 1.0 * Ld_428 + 10.0 * Gd_564) Ld_436 = Ad_120 + 1.0 * Ld_428;
      if (Ask > Ad_120 + 0.618 * Ld_428 + 10.0 * Gd_564) Ld_436 = Ad_120 + 0.618 * Ld_428;
      else {
         if (Ask > Ad_120 + Ld_428 / 2.0 + 10.0 * Gd_564) Ld_436 = Ad_120 + Ld_428 / 2.0;
         else {
            if (Ask > Ad_120 + Ld_428 / 2.0 + 10.0 * Gd_564) Ld_436 = Ad_120 + Ld_428 / 2.0;
            else
               if (Ask > Ad_120 + 0.236 * Ld_428 + 10.0 * Gd_564) Ld_436 = Ad_120 + 0.236 * Ld_428;
         }
      }
      if (Ld_436 - Ad_120 < Gd_176 * Gd_564) Ld_436 = 0;
      if (Ld_436 > 0.0) {
         for (pos_240 = order_total_244 - 1; pos_240 >= 0; pos_240--) {
            OrderSelect(pos_240, SELECT_BY_POS);
            if (OrderMagicNumber() == A_magic_40 && OrderType() == OP_BUY) {
               if (order_total_244 >= MaxOrders - 1) {
                  f0_6();
                  f0_17("Max BUY orders exceed and FIB level reached -> Close order");
                  Li_332 = 1;
                  continue;
               }
               if (OrderStopLoss() < Ld_436) {
                  f0_20(OrderTicket(), OrderOpenPrice(), Ld_436, OrderTakeProfit());
                  f0_17("FIB level: Update BUY order #" + OrderTicket());
               }
            }
         }
      }
   }
   if (A_count_84 > 4 && Ai_4) {
      Ld_428 = Ad_128 - order_open_price_216;
      Ld_436 = 0;
      if (Bid < Ad_128 - 0.236 * Ld_428 - 10.0 * Gd_564) Ld_436 = Ad_128 - 0.236 * Ld_428;
      if (Bid < Ad_128 - Ld_428 / 2.0 - 10.0 * Gd_564) Ld_436 = Ad_128 - Ld_428 / 2.0;
      if (Bid < Ad_128 - Ld_428 / 2.0 - 10.0 * Gd_564) Ld_436 = Ad_128 - Ld_428 / 2.0;
      if (Bid < Ad_128 - 0.618 * Ld_428 - 10.0 * Gd_564) Ld_436 = Ad_128 - 0.618 * Ld_428;
      if (Bid < Ad_128 - 1.0 * Ld_428 - 10.0 * Gd_564) Ld_436 = Ad_128 - 1.0 * Ld_428;
      if (Bid < Ad_128 - 1.618 * Ld_428 - 10.0 * Gd_564) Ld_436 = Ad_128 - 1.618 * Ld_428;
      if (Ad_128 - Ld_436 < Gd_176 * Gd_564) Ld_436 = 0;
      if (Ld_436 > 0.0) {
         for (pos_240 = order_total_244 - 1; pos_240 >= 0; pos_240--) {
            OrderSelect(pos_240, SELECT_BY_POS);
            if (OrderMagicNumber() == A_magic_40 && OrderType() == OP_SELL) {
               if (order_total_244 >= MaxOrders - 1) {
                  f0_6();
                  Li_336 = 1;
                  f0_17("Max SELL orders exceed and FIB level reached -> Close order");
                  continue;
               }
               if (OrderStopLoss() == 0.0 || OrderStopLoss() > Ld_436) {
                  f0_20(OrderTicket(), OrderOpenPrice(), Ld_436, OrderTakeProfit());
                  f0_17("FIB level: Update SELL order #" + OrderTicket());
               }
            }
         }
      }
   }
   if (Ad_144 > 0.0 && A_count_84 == 0) {
      if (Bid > Ad_144 + Gd_280 * Gd_564) {
         Ad_144 = Bid - Gd_280 * Gd_564;
         f0_17("chasing : SELL open price increase " + Ad_144);
      } else {
         if (Bid <= Ad_144) {
            if (Ad_144 - Bid <= Gd_160 * Gd_564) {
               f0_21(Ad_16 * Ad_32, A_magic_40, As_44, A_count_84, order_lots_208);
               A_count_84++;
               datetime_224 = TimeCurrent();
               Ad_128 = Bid;
               f0_17("chasing : Open sell order ");
            } else f0_17("First SELL order : Price is too far");
            Ad_144 = 0;
         }
      }
   }
   if (Ad_136 > 0.0 && A_count_80 == 0) {
      if (Ask < Ad_136 - Gd_280 * Gd_564) {
         Ad_136 = Ask + Gd_280 * Gd_564;
         f0_17("chasing : BUY open price increase " + Ad_136);
      } else {
         if (Ask >= Ad_136) {
            if (Ask - Ad_136 <= Gd_160 * Gd_564) {
               f0_11(Ad_8 * Ad_24, A_magic_40, As_44, A_count_80, order_lots_180);
               A_count_80++;
               datetime_196 = TimeCurrent();
               Ad_120 = Ask;
               f0_17("chasing : Open buy order ");
            } else f0_17("First BUY order : Price is too far");
            Ad_136 = 0;
         }
      }
   }
   bool bool_444 = FALSE;
   bool bool_448 = FALSE;
   if (A_count_84 == 0 && Ai_4) {
      if (Ai_168 == 1) bool_444 = (Gd_660 > 0.0 && Bid > Gd_660) || A_magic_40 == ManualMagicNumber;
      else
         if (Ai_168 == -1 && SellLotsMultiple > 0.0) bool_444 = (Li_332 && Gi_372) || G_istochastic_432 >= Gd_396;
   }
   if (A_count_80 == 0 && Ai_0) {
      if (Ai_168 == 0) bool_448 = Ask < Gd_668 || A_magic_40 == ManualMagicNumber;
      else
         if (Ai_168 == -1 && Ad_24 > 0.0) bool_448 = (Li_336 && Gi_372) || G_istochastic_432 <= Gd_388;
   }
   if (bool_444 && Bid > Ad_128 + Gd_192 * Gd_564 && A_count_84 < MaxOrders) {
      if (!Gi_268) f0_21(Ad_16 * Ad_32, A_magic_40, As_44, A_count_84, order_lots_208);
      else {
         if (Ad_144 == 0.0) {
            Ad_144 = Bid - Gd_280 * Gd_564;
            f0_17("Got SELL signal D2Upper: " + Gd_660);
         }
      }
   } else {
      if (bool_448 && !Gi_132 || TimeCurrent() - datetime_196 >= 60 * Gi_136 && Ask < Ad_120 - Gd_192 * Gd_564 && A_count_80 < MaxOrders) {
         if (!Gi_268) f0_11(Ad_8 * Ad_24, A_magic_40, As_44, A_count_80, order_lots_180);
         else {
            if (Ad_136 == 0.0) {
               Ad_136 = Ask + Gd_280 * Gd_564;
               f0_17("Got BUY signal D2Lower: " + Gd_668);
            }
         }
      }
   }
   if (Gd_916 > 0.0) {
      str2dbl_452 = 0;
      str2dbl_460 = 0;
      for (pos_240 = 0; pos_240 < OrdersTotal(); pos_240++) {
         OrderSelect(pos_240, SELECT_BY_POS);
         if (OrderMagicNumber() != 0 || OrderSymbol() != Symbol() || StringFind(OrderComment(), "rescue-") != 0) continue;
         Ls_468 = StringSubstr(OrderComment(), 7);
         str2dbl_476 = StrToDouble(Ls_468);
         if (OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP) {
            str2dbl_452 = str2dbl_476;
            Print("Found rescue BUY >= " + str2dbl_452);
         } else {
            if (OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP) {
               str2dbl_460 = str2dbl_476;
               Print("Found rescue SELL <= " + str2dbl_460);
            }
         }
      }
      ticket_484 = 0;
      order_open_price_488 = 10000;
      ticket_496 = 0;
      order_open_price_500 = 0;
      for (pos_240 = 0; pos_240 < OrdersTotal(); pos_240++) {
         OrderSelect(pos_240, SELECT_BY_POS);
         if (OrderSymbol() != Symbol() || OrderMagicNumber() == MagicNumber || OrderMagicNumber() == RescueMagicNumber || OrderMagicNumber() == ManualMagicNumber) continue;
         if (OrderType() == OP_BUY) {
            if (str2dbl_452 > 0.0 && OrderOpenPrice() >= str2dbl_452 && OrderOpenPrice() < order_open_price_488) {
               ticket_484 = OrderTicket();
               order_open_price_488 = OrderOpenPrice();
            }
         } else {
            if (OrderType() == OP_SELL) {
               if (str2dbl_460 > 0.0 && OrderOpenPrice() <= str2dbl_460 && OrderOpenPrice() > order_open_price_500) {
                  ticket_496 = OrderTicket();
                  order_open_price_500 = OrderOpenPrice();
               }
            }
         }
      }
      if (ticket_484 > 0) {
         f0_4(Gd_916, ticket_484);
         Print("Closed profit: " + Gd_916 + " Last BUY #: " + ticket_484);
      } else {
         if (ticket_496 > 0) {
            f0_4(Gd_916, ticket_496);
            Print("Closed profit: " + Gd_916 + " Last SELL #: " + ticket_496);
         }
      }
   }
}
			     	   		 			 			 		  		 	  	 	    	   			 					  		 		 			   	  			  	  	 	 			 				 			  	  			    	      			 						  		 			  	 			 				  
// 52868910137DDD8B4D44A6355BF001F1
void f0_3(int Ai_0, int Ai_4, int A_magic_8, int Ai_12, int Ai_16, int &Ai_20, int &Ai_24) {
   if (Gi_328 || G_istochastic_432 >= 80.0 || G_istochastic_440 >= 80.0 || G_istochastic_432 <= 20.0 || G_istochastic_440 <= 20.0) {
      for (int pos_28 = OrdersTotal() - 1; pos_28 >= 0; pos_28--) {
         OrderSelect(pos_28, SELECT_BY_POS);
         if (OrderMagicNumber() == A_magic_8) {
            if (OrderType() == OP_SELL) {
               if (!(Ai_4)) continue;
               if (Gi_328 && Ai_16 == 1) {
                  if (OrderOpenPrice() - Gd_676 >= Gd_160 * Gd_564 && OrderClosePrice() < Gd_676 - 10.0 * Gd_564) {
                     f0_20(OrderTicket(), OrderOpenPrice(), Gd_676, OrderTakeProfit());
                     f0_17("D0 Trailing SL for SELL");
                  }
               } else {
                  if (Gd_464 > 0.0 && Gd_464 <= 20.0 && OrderStopLoss() > 0.0) {
                     f0_17("SELL Trend reversing -> Close order");
                     f0_6();
                     Ai_24 = 1;
                     continue;
                  }
               }
               if (OrderClosePrice() - OrderOpenPrice() < StopLoss * Gd_564) continue;
               f0_17("SELL stop out");
               f0_6();
               continue;
            }
            if (OrderType() == OP_BUY) {
               if (Ai_0 == TRUE) {
                  if (Gi_328 && Ai_12 == 1) {
                     if (Gd_676 - OrderOpenPrice() >= Gd_160 * Gd_564 && OrderClosePrice() > Gd_676 + 10.0 * Gd_564) {
                        f0_20(OrderTicket(), OrderOpenPrice(), Gd_676, OrderTakeProfit());
                        f0_17("D0 Trailing SL for BUY");
                     }
                  } else {
                     if (Gd_464 >= 80.0 && OrderStopLoss() > 0.0) {
                        f0_17("BUY Trend reversing -> Close order");
                        f0_6();
                        Ai_20 = 1;
                        continue;
                     }
                  }
                  if (OrderOpenPrice() - OrderClosePrice() >= StopLoss * Gd_564) {
                     f0_17("BUY stop out");
                     f0_6();
                  }
               }
            }
         }
      }
   }
}
	 				   	 	   	       	 	   		 	   		   	 	  	   	  	 	 		 	 				  				 	  			  		  			         	      						  	 	 	 		  			 	   			  	      	 	
// 087E0272828D82DD16EBCD6258ACEA68
double f0_0(int A_timeframe_0, double &Ad_unused_4, double &Ad_unused_12, double &Ad_unused_20, double &Ad_unused_28, double &Ad_36, double &Ad_44) {
   double Ld_72;
   double Ld_52 = 0.0;
   int Li_60 = G_period_540 - 1;
   double ima_64 = iMA(Symbol(), A_timeframe_0, G_period_540, Gi_544, MODE_SMA, PRICE_CLOSE, 0);
   while (Li_60 >= 0) {
      Ld_72 = iClose(Symbol(), A_timeframe_0, Li_60) - ima_64;
      Ld_52 += Ld_72 * Ld_72;
      Li_60--;
   }
   double Ld_80 = 2.0 * MathSqrt(Ld_52 / G_period_540);
   Ad_36 = ima_64 + Ld_80;
   Ad_44 = ima_64 - Ld_80;
   return (ima_64);
}
				 	     		  	 	  	  	    			 		   	     		 	  		 		 	  	   			    			     		   	 				 	  	       	   	 		 	  			   		 			  	  			   	 	  	 	 	
// F4B6F5CF98459867814015C7184CF47B
void f0_21(double Ad_0, int Ai_8, string As_12, int Ai_unused_20, double Ad_unused_24) {
   f0_12();
   double Ld_32 = Bid - 2.0 * Gd_176 * Gd_564;
   int ticket_40 = f0_1(OP_SELL, Ad_0, Bid, 0, 0, Red, Ai_8, As_12);
   OrderSelect(ticket_40, SELECT_BY_TICKET);
   f0_20(ticket_40, OrderOpenPrice(), OrderStopLoss(), Ld_32);
}
	  	  		 						   	 			  		 	  		 	   		 					 	    	 	  	   	  			      		    	 	  	     	 				 		 						 	  			    	     	 	 	   	 		   	 		 		
// 83E5F33833A4884D368C845DA42DE748
void f0_11(double Ad_0, int Ai_8, string As_12, int Ai_unused_20, double Ad_unused_24) {
   f0_12();
   double Ld_32 = Ask + 2.0 * Gd_176 * Gd_564;
   int ticket_40 = f0_1(OP_BUY, Ad_0, Ask, 0, 0, Blue, Ai_8, As_12);
   OrderSelect(ticket_40, SELECT_BY_TICKET);
   f0_20(ticket_40, OrderOpenPrice(), OrderStopLoss(), Ld_32);
}
	 	 				 	    	    	  	  	 	 	 		  					 	     	  		 		  				   		 			   	 			 	 			 	     	  		 	 	  					 					 			     	 	  	  	 	 	    	   		
// E7BE831AFE822D3863013B6DF50EC05D
double f0_18(int Ai_0, double Ad_unused_4, double Ad_unused_12, double Ad_20, double Ad_28, double Ad_36, bool Ai_44, double Ad_unused_48, double &Ad_56) {
   double price_64;
   double Ld_72;
   double Ld_ret_88;
   double Ld_96;
   if (Ai_44) {
      price_64 = Ask;
      Ad_56 = Ask + Gd_176 * Gd_564;
      Ld_72 = Ad_56 - Gd_316 * Gd_564;
      Ad_56 += Gd_308 * Gd_564;
      if (Gi_276) Ad_56 += Gd_296 * Gd_564;
   } else {
      price_64 = Bid;
      Ad_56 = Bid - Gd_176 * Gd_564;
      Ld_72 = Ad_56 + Gd_316 * Gd_564;
      Ad_56 -= Gd_308 * Gd_564;
      if (Gi_276) Ad_56 -= Gd_296 * Gd_564;
   }
   if (Gi_376) Ld_ret_88 = Gda_404[Ai_0] * LotsSize;
   else {
      Ld_96 = Ad_20 / Ad_36;
      Ld_ret_88 = (Ld_96 - Ld_72) * Ad_36 / (Ld_72 - price_64);
   }
   if (Gi_380 && Ld_ret_88 < Ad_28) Ld_ret_88 = Ad_28;
   else
      if (Ld_ret_88 < MarketInfo(Symbol(), MODE_MINLOT)) Ld_ret_88 = MarketInfo(Symbol(), MODE_MINLOT);
   return (Ld_ret_88);
}
		        	 		 	 					 	  			 	 				      	 			  	 		  	   	 				 		  		  		  	    		 		 					    				  	       		 	 			 	   		  	   	 	 						 	
// D619AD222463CC6932367514F37AB5A1
double f0_15(int A_timeframe_0, double Ad_4, double &Ad_12, double &Ad_20, int &Ai_28) {
   int Li_48;
   int Li_60;
   int Li_64;
   double Ld_68;
   double Ld_76;
   double Ld_84;
   double Ld_92;
   int Li_32 = 300;
   int ind_counted_40 = IndicatorCounted();
   double Ld_100 = 0;
   double Ld_108 = 0;
   for (int index_36 = 0; index_36 < Li_32; index_36++) {
      Gda_924[index_36] = 0;
      Gda_928[index_36] = 0;
   }
   for (int Li_52 = Li_32; Li_52 >= 0; Li_52--) {
      Ld_68 = iLow(Symbol(), A_timeframe_0, iLowest(Symbol(), A_timeframe_0, MODE_LOW, Gi_720, Li_52));
      if (Ld_68 == Ld_108) Ld_68 = 0.0;
      else {
         Ld_108 = Ld_68;
         if (iLow(Symbol(), A_timeframe_0, Li_52) - Ld_68 > Gi_724 * Point) Ld_68 = 0.0;
         else {
            for (int Li_56 = 1; Li_56 <= Gi_728; Li_56++) {
               Ld_76 = Gda_928[Li_52 + Li_56];
               if (Ld_76 != 0.0 && Ld_76 > Ld_68) Gda_928[Li_52 + Li_56] = 0.0;
            }
         }
      }
      if (iLow(Symbol(), A_timeframe_0, Li_52) == Ld_68) Gda_928[Li_52] = Ld_68;
      else Gda_928[Li_52] = 0.0;
      Ld_68 = iHigh(Symbol(), A_timeframe_0, iHighest(Symbol(), A_timeframe_0, MODE_HIGH, Gi_720, Li_52));
      if (Ld_68 == Ld_100) Ld_68 = 0.0;
      else {
         Ld_100 = Ld_68;
         if (Ld_68 - iHigh(Symbol(), A_timeframe_0, Li_52) > Gi_724 * Point) Ld_68 = 0.0;
         else {
            for (Li_56 = 1; Li_56 <= Gi_728; Li_56++) {
               Ld_76 = Gda_924[Li_52 + Li_56];
               if (Ld_76 != 0.0 && Ld_76 < Ld_68) Gda_924[Li_52 + Li_56] = 0.0;
            }
         }
      }
      if (iHigh(Symbol(), A_timeframe_0, Li_52) == Ld_68) Gda_924[Li_52] = Ld_68;
      else Gda_924[Li_52] = 0.0;
   }
   if (Li_48 == 0) {
      Ld_108 = 0;
      Ld_100 = 0;
   } else {
      Ld_108 = Ld_84;
      Ld_100 = Ld_92;
   }
   for (Li_52 = Li_32; Li_52 >= 0; Li_52--) {
      Ld_76 = 0.0;
      switch (Li_48) {
      case 0:
         if (!(Ld_108 == 0.0 && Ld_100 == 0.0)) break;
         if (Gda_924[Li_52] != 0.0) {
            Ld_100 = iHigh(Symbol(), A_timeframe_0, Li_52);
            Li_60 = Li_52;
            Li_48 = -1;
            Ld_76 = 1;
         }
         if (Gda_928[Li_52] == 0.0) break;
         Ld_108 = iLow(Symbol(), A_timeframe_0, Li_52);
         Li_64 = Li_52;
         Li_48 = 1;
         Ld_76 = 1;
         break;
      case 1:
         if (Gda_928[Li_52] != 0.0 && Gda_928[Li_52] < Ld_108 && Gda_924[Li_52] == 0.0) {
            Li_64 = Li_52;
            Ld_108 = Gda_928[Li_52];
            Ld_76 = 1;
         }
         if (!(Gda_924[Li_52] != 0.0 && Gda_928[Li_52] == 0.0)) break;
         Ld_100 = Gda_924[Li_52];
         Li_60 = Li_52;
         Li_48 = -1;
         Ld_76 = 1;
         break;
      case -1:
         if (Gda_924[Li_52] != 0.0 && Gda_924[Li_52] > Ld_100 && Gda_928[Li_52] == 0.0) {
            Li_60 = Li_52;
            Ld_100 = Gda_924[Li_52];
         }
         if (!(Gda_928[Li_52] != 0.0 && Gda_924[Li_52] == 0.0)) break;
         Ld_108 = Gda_928[Li_52];
         Li_64 = Li_52;
         Li_48 = 1;
         break;
      default:
         return/*(WARN)*/;
      }
   }
   Ad_12 = Ld_100;
   Ad_20 = Ld_108;
   if (Ld_108 == 0.0 || Ld_100 == 0.0) {
      Ai_28 = -1;
      return (0);
   }
   if (Li_60 > Li_64) {
      Ai_28 = Gi_624;
      return (Ld_108 + (Ld_100 - Ld_108) * Ad_4);
   }
   Ai_28 = Gi_620;
   return (Ld_100 - (Ld_100 - Ld_108) * Ad_4);
}
	     	  		 				  						 				   	 		  	  		 		     		 		 	 	 	 					   	 			     	 		  	  					  						 		    	 	  	 	 	     	       			  				  	
// EA2B2676C28C0DB26D39331A336C6B92
int start() {
   double istochastic_24;
   double istochastic_32;
   double istochastic_40;
   double istochastic_48;
   int datetime_56;
   int Li_68;
   int Li_72;
   double Ld_76;
   double Ld_84;
   double Ld_92;
   double Ld_100;
   double Ld_108;
   double Ld_116;
   int Li_124;
   int Li_128;
   double Ld_132;
   double Ld_140;
   double Ld_148;
   double Ld_156;
   int Li_172;
   int Li_176;
   double Ld_180;
   double Ld_188;
   int Li_196;
   double Ld_204;
   int Li_unused_212;
   double order_lots_216;
   double order_lots_224;
   double Ld_232;
   double Ld_240;
   string Ls_248;
   if (!IsTradeAllowed()) return (0);
   Gs_556 = "";
/*
   int Li_8 = GetTime(30);
   if (Li_8 < 0) {
      Comment("\nTrial period expired. ForexProGainer has stopped.\nPlease purchase a license at http://www.forexprogainer.com to continue.");
      return (0);
   }
   string Ls_0 = "\nTrial period: " + (Li_8 / 86400) + " days, " + (Li_8 / 60 / 60 % 24) + " hours left";
*/	
   if (!f0_5(TimeCurrent())) {
      if (CloseBeforeNoTradeDays) {
         for (int pos_12 = 0; pos_12 < OrdersTotal(); pos_12++) {
            OrderSelect(pos_12, SELECT_BY_POS);
            if (OrderMagicNumber() == MagicNumber) f0_14(OrderTicket(), OrderLots(), OrderClosePrice());
         }
      }
      Comment(Gs_556);
      return (0);
   }
   G_istochastic_432 = iStochastic(Symbol(), G_timeframe_420, G_period_408, G_period_412, G_slowing_416, MODE_SMA, 0, MODE_MAIN, G_shift_424);
   G_istochastic_440 = iStochastic(Symbol(), G_timeframe_420, G_period_408, G_period_412, G_slowing_416, MODE_SMA, 0, MODE_MAIN, G_shift_428);
   double Ld_16 = 0;
   if (G_istochastic_432 >= 80.0 || G_istochastic_440 >= 80.0 || G_istochastic_432 <= 20.0 || G_istochastic_440 <= 20.0) {
      G_istochastic_448 = iStochastic(Symbol(), G_timeframe_420, G_period_408, G_period_412, G_slowing_416, MODE_SMA, 0, MODE_SIGNAL, G_shift_424);
      G_istochastic_456 = iStochastic(Symbol(), G_timeframe_420, G_period_408, G_period_412, G_slowing_416, MODE_SMA, 0, MODE_SIGNAL, G_shift_428);
      istochastic_24 = G_istochastic_456;
      istochastic_32 = G_istochastic_440;
      istochastic_40 = G_istochastic_432;
      istochastic_48 = G_istochastic_448;
      if (istochastic_32 + istochastic_48 - istochastic_24 - istochastic_40 != 0.0 && (istochastic_24 > istochastic_32 && istochastic_40 > istochastic_48) || (istochastic_32 > istochastic_24 &&
         istochastic_48 > istochastic_40)) Ld_16 = (istochastic_32 * istochastic_48 - istochastic_24 * istochastic_40) / (istochastic_32 + istochastic_48 - istochastic_24 - istochastic_40);
      if (Ld_16 > 0.0 && Gd_464 != Ld_16) Gd_464 = Ld_16;
   }
   Gd_676 = f0_0(Gi_532, Gd_628, Gd_636, Gd_644, Gd_652, Gd_660, Gd_668);
   if (Gi_324) {
      datetime_56 = iTime(Symbol(), G_timeframe_740, 0);
      if (Gi_704 == -1 || datetime_56 != G_datetime_716) {
         Gd_708 = f0_15(G_timeframe_740, 0.236, Gd_688, Gd_696, Gi_704);
         G_datetime_716 = datetime_56;
      }
   } else Gi_704 = -1;
   int Li_60 = 0;
   int Li_64 = 0;
   int ticket_164 = 0;
   int ticket_168 = 0;
   if (MoneyManagement) {
      LotsSize = (AccountBalance() - PreservedBalance) * RiskPercent / 100000.0;
      if (LotsSize < MarketInfo(Symbol(), MODE_MINLOT)) LotsSize = MarketInfo(Symbol(), MODE_MINLOT);
   }
   f0_9(EnableBuy, EnableSell, LotsSize, LotsSize, BuyLotsMultiple, SellLotsMultiple, MagicNumber, Gs_548, 0, Li_60, Li_64, ticket_164, ticket_168, Li_172, Li_176, Li_68,
      Li_72, Ld_76, Ld_84, Ld_92, Ld_100, Ld_108, Ld_116, Gd_812, Gd_820, Gd_828, Gd_836, -1);
   if (Li_60 || Li_64) {
      Li_196 = -2;
      if (Li_72 >= RescueOrdersTotalStart && Bid >= Ld_116 + Gd_192 / 2.0 * Gd_564) Li_196 = 1;
      else
         if (Li_68 >= RescueOrdersTotalStart && Ask <= Ld_108 - Gd_192 / 2.0 * Gd_564) Li_196 = 0;
      if (Li_196 != -2 || Li_64) {
         f0_9(EnableRescueBuy, EnableRescueSell, LotsSize, LotsSize, BuyLotsMultiple, SellLotsMultiple, RescueMagicNumber, Gs_548 + " - Rescue", Gd_916 > 0.0, Li_60, Li_64,
            ticket_164, ticket_168, Li_172, Li_176, Li_124, Li_128, Ld_132, Ld_140, Ld_148, Ld_156, Ld_204, Ld_204, Gd_844, Gd_852, Gd_860, Gd_868, Li_196);
      }
   }
   if (ticket_164 != 0 || ticket_168 != 0 || Li_172 > 0 || Li_176 > 0) {
      Li_unused_212 = 0;
      if (ticket_164 > 0 && Li_172 == 0) Li_196 = 0;
      else {
         if (ticket_168 > 0 && Li_176 == 0) Li_196 = 1;
         else Li_196 = -2;
      }
      if ((ticket_164 > 0 && Li_172 == 0) || (ticket_168 > 0 && Li_176 == 0) || Li_172 > 0 || Li_176 > 0) {
         OrderSelect(ticket_164, SELECT_BY_TICKET);
         order_lots_216 = OrderLots();
         OrderSelect(ticket_168, SELECT_BY_TICKET);
         order_lots_224 = OrderLots();
         f0_9(EnableManualBuy, EnableManualSell, order_lots_216, order_lots_224, 1, 1, ManualMagicNumber, Gs_548 + " - " + ManualSignalComment, 0, Li_60, Li_64, ticket_164,
            ticket_168, Li_172, Li_176, Li_172, Li_176, Ld_76, Ld_84, Ld_180, Ld_188, Ld_108, Ld_116, Gd_876, Gd_884, Gd_892, Gd_900, Li_196);
      }
   }
   if (AccountBalance() > 0.0) Ld_232 = 100.0 * (-AccountProfit()) / AccountBalance();
   else Ld_232 = 0;
   if (Ld_232 > Gd_604) Gd_604 = Ld_232;
   if (AccountMargin() > 0.0) {
      Ld_240 = 100.0 * AccountEquity() / AccountMargin();
      if (Ld_240 < Gd_612) Gd_612 = Ld_240;
   }
   if (Gd_148 != -1.0) Ls_248 = "\nBroker Commission : " + DoubleToStr(Gd_148, 2) + " pips";
   else Ls_248 = "";
   Gs_556 = "\n\n--------------------------------------------------" 
      + "\n" 
      + Gs_548 + " Expert Advisor" 
      + "\nwww.forexprogainer.com" 
      + "\n--------------------------------------------------" 
      + "\nAccount Number : " + AccountNumber() 
      + "\nAccount Leverage 1:" + AccountLeverage() // + Ls_0 
      + "\nBase Lots : " + DoubleToStr(LotsSize, 2) + Ls_248 
      + "\nP/L: " + DoubleToStr(Ld_84 + Ld_76, 2) + " " + AccountCurrency() 
      + "\nDrawdown: " + DoubleToStr(Ld_232, 1) + "%" 
      + "\n--------------------------------------------------" 
      + "\n" 
      + G_str_concat_764 + Gs_556 
      + "\nOpened BUY: " + Li_68 + " positions " + DoubleToStr(Ld_92, 2) + " lots" 
   + "\nOpened SELL: " + Li_72 + " positions " + DoubleToStr(Ld_100, 2) + " lots";
   if (VerboseLog) {
      Gs_556 = Gs_556 
         + "\nMax Drawdown: " + DoubleToStr(Gd_604, 1) + "%" 
         + "\nMin Margin Level: " + DoubleToStr(Gd_612, 1) + "%" 
         + "\nOpened rescue BUY: " + Li_124 + " positions " + DoubleToStr(Ld_148, 2) + " lots" 
         + "\nOpened rescue SELL: " + Li_128 + " positions " + DoubleToStr(Ld_156, 2) + " lots" 
         + "\nOpened manual BUY: " + Li_172 + " positions " + DoubleToStr(Ld_180, 2) + " lots" 
         + "\nOpened manual SELL: " + Li_176 + " positions " + DoubleToStr(Ld_188, 2) + " lots" 
         + "\nD0 : " + DoubleToStr(Gd_676, G_digits_572) 
         + "\nD2 Upper: " + DoubleToStr(Gd_660, G_digits_572) + " - D2 Lower: " + DoubleToStr(Gd_668, G_digits_572) 
         + "\nD3 Upper: " + DoubleToStr(Gd_644, G_digits_572) + " - D3 Lower: " + DoubleToStr(Gd_652, G_digits_572) 
      + "\n\nLast Swing High: " + DoubleToStr(Gd_688, G_digits_572) + " - Low: " + DoubleToStr(Gd_696, G_digits_572) + " Expected Retracement Point: " + DoubleToStr(Gd_708,
         G_digits_572);
   }
   Comment(Gs_556);
   return (0);
}