Navigation:Home > Content >

FE_MOD_2.009.mq4

Time: 2014-11-04 | Download file:FE_MOD_2.009.mq4

//-----------------------------------------------------------------------------------//
//                         FEM Version 2.009 by ApMSoft                              //
//--- Last update 01/09/2012 --------------------------- E-mail : apmsoft@mail.ru ---//
// Fix 0.1 Fixed Multiplier (fun0 remake 20/06/2012)                                 //
// Fix 0.2 Fixed ECN orders SL/TP error (29/06/2012), not complety                   //
// Fix 0.2 Settings for Augmenter on & off from settings                             //
// Fix 0.3 Negative Basket Protection, low margin check, auto magic number, chartcom //
// Fix 0.4 ECN always true, Fixed 3/5 digits processing, CheckStoplossZero    18/07  //
// Fix 0.5 "Trade context is busy" fix, CheckStoplossZero deleted             29/07  //
// Fix 0.5a "TCB" fix update, Orders Modify cycle modified                    09/08  //
// ver 006 Free SWB TF                                                        20/08  //
// ver 008 RealTP from OpenPrice, new NBP based on Breakeven, new lvls calc   01/09  //
// ver 009 Light code opt, trade one direction, nbp 2.1, minor corrections    23/10  //
//-----------------------------------------------------------------------------------//

#property copyright "2012 ApMSoft"
#property link      "www.no-url.com"

extern string EA_Name = "FEM 2.009";
extern string _________ = "Input a unique magic number for each chart";
extern int MagicNumber = 0;
extern double BaseLotSize = 0.01;
extern int ExecutionPoint = 0;
extern double BasketTakeProfit = 0.0;
extern int BasketStopLoss = 0;
extern double Multiplier = 1.0;
extern int MaximumBuyLevels = 0;
extern int MaximumSellLevels = 0;
extern bool Negative_Basket_Protection = FALSE;
extern int NBP = 0;
extern int slippage = 3;
extern double BailOutPct = 100.0;
extern bool Chicken_Out = FALSE;
extern bool FreezeAfterTP = FALSE;
extern bool FreezeAfterTPScheduler = FALSE;
extern bool ShowBreakeven = FALSE;
extern bool AugmenterOn = TRUE;
extern string ____________ = "Augmenter";
extern double Multiplier_1 = 0.0;
extern double BasketTakeProfit_1 = 0.0;
extern double Multiplier_2 = 0.0;
extern double BasketTakeProfit_2 = 0.0;
extern double Multiplier_3 = 0.0;
extern double BasketTakeProfit_3 = 0.0;
extern double Multiplier_4 = 0.0;
extern double BasketTakeProfit_4 = 0.0;
extern double Multiplier_5 = 0.0;
extern double BasketTakeProfit_5 = 0.0;
extern double Multiplier_6 = 0.0;
extern double BasketTakeProfit_6 = 0.0;
extern double Multiplier_7 = 0.0;
extern double BasketTakeProfit_7 = 0.0;
extern double Multiplier_8 = 0.0;
extern double BasketTakeProfit_8 = 0.0;
extern double Multiplier_9 = 0.0;
extern double BasketTakeProfit_9 = 0.0;
extern double Multiplier_10 = 0.0;
extern double BasketTakeProfit_10 = 0.0;
extern double Multiplier_11 = 0.0;
extern double BasketTakeProfit_11 = 0.0;
extern double Multiplier_12 = 0.0;
extern double BasketTakeProfit_12 = 0.0;
extern double Multiplier_13 = 0.0;
extern double BasketTakeProfit_13 = 0.0;
extern double Multiplier_14 = 0.0;
extern double BasketTakeProfit_14 = 0.0;
extern double Multiplier_15 = 0.0;
extern double BasketTakeProfit_15 = 0.0;
extern double Multiplier_16 = 0.0;
extern double BasketTakeProfit_16 = 0.0;
extern double Multiplier_17 = 0.0;
extern double BasketTakeProfit_17 = 0.0;
extern double Multiplier_18 = 0.0;
extern double BasketTakeProfit_18 = 0.0;
extern double Multiplier_19 = 0.0;
extern double BasketTakeProfit_19 = 0.0;
extern double Multiplier_20 = 0.0;
extern double BasketTakeProfit_20 = 0.0;
extern double Multiplier_21 = 0.0;
extern double BasketTakeProfit_21 = 0.0;
extern double Multiplier_22 = 0.0;
extern double BasketTakeProfit_22 = 0.0;
extern double Multiplier_23 = 0.0;
extern double BasketTakeProfit_23 = 0.0;
extern double Multiplier_24 = 0.0;
extern double BasketTakeProfit_24 = 0.0;
extern double Multiplier_25 = 0.0;
extern double BasketTakeProfit_25 = 0.0;
extern string ___________ = "Freeze After TP Scheduler";
extern int SUN_StartHour = 0, SUN_StartMinute = 0, SUN_EndHour = 0, SUN_EndMinute = 0;
extern int MON_StartHour = 0, MON_StartMinute = 0, MON_EndHour = 0, MON_EndMinute = 0;
extern int TUE_StartHour = 0, TUE_StartMinute = 0, TUE_EndHour = 0, TUE_EndMinute = 0;
extern int WED_StartHour = 0, WED_StartMinute = 0, WED_EndHour = 0, WED_EndMinute = 0;
extern int THU_StartHour = 0, THU_StartMinute = 0, THU_EndHour = 0, THU_EndMinute = 0;
extern int FRI_StartHour = 0, FRI_StartMinute = 0, FRI_EndHour = 0, FRI_EndMinute = 0;
extern int SAT_StartHour = 0, SAT_StartMinute = 0, SAT_EndHour = 0, SAT_EndMinute = 0;
extern string _____ = "SWB indicator settings";
extern bool Use_SWB_indicator = false;
extern bool      use_bb=true;
extern int       bb_period=20;
extern int       bb_deviation=2;
extern int       bb_shift=0;
extern bool      use_stoch=true;
extern int       k=5;
extern int       d=3;
extern int       slowing=3;
extern int       price_field=0;
extern int       stoch_shift=0;
extern int       lo_level=30;
extern int       up_level=70;
extern bool      use_rsi=true;
extern int       rsi_period=12;
extern int       rsi_shift=0;
extern int       lower=30;
extern int       upper=70;
double gd_944;
bool gi_952 = FALSE;
double gd_956;
double gd_964 = 0.0;
int gi_EP = 0;
int gi_BSL = 0;
int gi_980 = 300;
int gi_984 = 5000;
int gi_988 = 0;
int gi_992 = 0;
double gd_996 = 100.0;
int gi_1004 = 600;
double gd_1008;
bool gi_1016;
int gi_1020 = 7;
int gi_1024 = 0;
int gi_1028 = MODE_LWMA;
int gi_1032 = PRICE_WEIGHTED;
double gd_1036 = 0.25;
double gd_1044 = 0.2;
double gd_1052 = 1.01;
int gi_1060 = 0;
int gi_1064;
double gd_1068;
int gi_1076;
bool gi_1080 = FALSE;
string gs_1084;
int gi_1100 = 0;
int gi_1104 = 1;
int gi_1108 = 3;
int gi_1112 = 250;
string gs_1116;
bool gi_1124;
bool gi_1128;
bool gi_1132;
bool gi_1136;
int gi_1140;
int gi_1144;
string gs__hedged_1148 = " hedged";
int gi_1156;
bool gi_1160 = FALSE;
//string gs_1164 = "12345678901234567890123456789012345678901234567890ABCDEFGHIJKLMNOP";
double gda_setTP[27];
double gda_setMULT[27];
double gda_1188[4];
int gi_1192;
int gLevel[2]; //SE for chart_comment
double gPoint = 0.0001; // Fix for 3/5 Digits
int SignalPeriod = PERIOD_H1;
// double Last_TP[2]; // Fix 0.5 to store TP if TCB

void f0_9() {
   gd_944 = 0;
   int li_0 = OrdersHistoryTotal();
   for (int li_4 = 0; li_4 < li_0; li_4++) {
      OrderSelect(li_4, SELECT_BY_POS, MODE_HISTORY);
      if (OrderMagicNumber() == MagicNumber) gd_944 += OrderProfit() + OrderSwap() + OrderCommission();
   }
}

int f0_5() {
   double ld_0 = MarketInfo(Symbol(), MODE_MINLOT);
   for (int li_8 = 0; ld_0 < 1.0; li_8++) ld_0 = 10.0 * ld_0;
   return (li_8);
}

double f0_2(double ad_0) {
   double ld_8;
   double ld_16 = AccountEquity() - gi_984;
   double ld_24 = gi_988;
   double ld_32 = gi_992;
   if (gi_988 == 0 || gi_992 == 0) ld_8 = ad_0;
   else {
      ld_24 = gi_984 * ld_24 / 100.0;
      Print("tmp=" + ld_16 + ",AccountEquity()=" + AccountEquity() + ",InitEquity=" + gi_984);
      ld_32 /= 100.0;
      if (ld_16 > 0.0) ld_16 = MathPow(ld_32 + 1.0, ld_16 / ld_24);
      else {
         if (ld_16 < 0.0) ld_16 = MathPow(1 - ld_32, MathAbs(ld_16 / ld_24));
         else ld_16 = 1;
      }
      ld_8 = NormalizeDouble(ad_0 * ld_16, f0_5());
      if (ld_8 < MarketInfo(Symbol(), MODE_MINLOT)) ld_8 = MarketInfo(Symbol(), MODE_MINLOT);
   }
   if (ld_8 < 0.0) Print("ERROR tmp=" + ld_16 + ",a=" + ld_24 + ",b=" + ld_32 + ",AccountEquity()=" + AccountEquity());
   return (ld_8);
}

int deinit() {
   return (0);
}

int init() {
   if (IsTesting()) SignalPeriod = PERIOD_M30;
   if (Period() != PERIOD_M30 && !Use_SWB_indicator) {
      Print("ERROR -- FE_MOD should be attached to " + Symbol() + " 30 minute chart window");
      Alert("ERROR -- FE_MOD should be attached to " + Symbol() + " 30 minute chart window");
   }
   updateMagicNumber();
   MathSrand(ExecutionPoint+BasketTakeProfit*Multiplier); // Fix 0.5 for Un-synk
   /*gsa_92[0] = AccountNumber();
   gsa_92[1] = KEY;
   gsa_92[2] = User;
   gsa_92[3] = Register_Number;
   gi_76 = fun1(gsa_92, ArraySize(gsa_92), 0.1, gs_1164, IsDemo());*/
   gd_956 = Multiplier;
   if (Digits == 3) {gPoint = 0.01;} else {
      if (Digits == 5) {gPoint = 0.0001; } else {
      gPoint = Point; }
      }
   gi_EP = ExecutionPoint;
   gi_BSL = BasketStopLoss;
   gda_setTP[0] = BasketTakeProfit_1;
   gda_setTP[1] = BasketTakeProfit_2;
   gda_setTP[2] = BasketTakeProfit_3;
   gda_setTP[3] = BasketTakeProfit_4;
   gda_setTP[4] = BasketTakeProfit_5;
   gda_setTP[5] = BasketTakeProfit_6;
   gda_setTP[6] = BasketTakeProfit_7;
   gda_setTP[7] = BasketTakeProfit_8;
   gda_setTP[8] = BasketTakeProfit_9;
   gda_setTP[9] = BasketTakeProfit_10;
   gda_setTP[10] = BasketTakeProfit_11;
   gda_setTP[11] = BasketTakeProfit_12;
   gda_setTP[12] = BasketTakeProfit_13;
   gda_setTP[13] = BasketTakeProfit_14;
   gda_setTP[14] = BasketTakeProfit_15;
   gda_setTP[15] = BasketTakeProfit_16;
   gda_setTP[16] = BasketTakeProfit_17;
   gda_setTP[17] = BasketTakeProfit_18;
   gda_setTP[18] = BasketTakeProfit_19;
   gda_setTP[19] = BasketTakeProfit_20;
   gda_setTP[20] = BasketTakeProfit_21;
   gda_setTP[21] = BasketTakeProfit_22;
   gda_setTP[22] = BasketTakeProfit_23;
   gda_setTP[23] = BasketTakeProfit_24;
   gda_setTP[24] = BasketTakeProfit_25;
   gda_setTP[25] = BasketTakeProfit;
   gda_setTP[26] = BasketTakeProfit_1;
   gda_setMULT[0] = Multiplier_1;
   gda_setMULT[1] = Multiplier_2;
   gda_setMULT[2] = Multiplier_3;
   gda_setMULT[3] = Multiplier_4;
   gda_setMULT[4] = Multiplier_5;
   gda_setMULT[5] = Multiplier_6;
   gda_setMULT[6] = Multiplier_7;
   gda_setMULT[7] = Multiplier_8;
   gda_setMULT[8] = Multiplier_9;
   gda_setMULT[9] = Multiplier_10;
   gda_setMULT[10] = Multiplier_11;
   gda_setMULT[11] = Multiplier_12;
   gda_setMULT[12] = Multiplier_13;
   gda_setMULT[13] = Multiplier_14;
   gda_setMULT[14] = Multiplier_15;
   gda_setMULT[15] = Multiplier_16;
   gda_setMULT[16] = Multiplier_17;
   gda_setMULT[17] = Multiplier_18;
   gda_setMULT[18] = Multiplier_19;
   gda_setMULT[19] = Multiplier_20;
   gda_setMULT[20] = Multiplier_21;
   gda_setMULT[21] = Multiplier_22;
   gda_setMULT[22] = Multiplier_23;
   gda_setMULT[23] = Multiplier_24;
   gda_setMULT[24] = Multiplier_25;
   gda_setMULT[25] = Multiplier;
   gda_setMULT[26] = Multiplier_1;
   int li_0 = fun0(gda_setTP, gda_setMULT, Digits, 0, gPoint, gda_1188, 0, IsDemo());
   if (li_0 >= 0) {
      gd_964 = gda_1188[0];
      gd_1068 = gda_1188[1];
      gd_956 = gda_1188[2];
   }
   gi_1076 = MathRound((-MathLog(MarketInfo(Symbol(), MODE_LOTSTEP))) / 2.302585093);
   gi_1124 = FALSE;
   gi_1128 = FALSE;
   gi_1132 = FALSE;
   gi_1136 = FALSE;
   gi_1140 = -1;
   gi_1016 = FALSE;
   gi_1144 = -1;
   /*gs_1116 = "approved";
   if (gi_76 == 0) {
      Print("Your Register number: " + gs_1164 + ", please input valid KEY.");
      Alert("Your Register number: " + gs_1164);
   }*/
   return (0);
}

int f0_11(bool ai_0 = FALSE, double nbp = 0.0) {
   int li_4;
   double ld_8;
   double ld_16 = 0;
   double ld_24 = 0;
   string ls_32 = "";
   bool li_40 = TRUE;
   if (TimeCurrent() - gi_1064 < 60) return (0);
   if (ai_0 && (!gi_1132)) return (0);
   if (!GlobalVariableCheck("PERMISSION")) {
      GlobalVariableSet("PERMISSION", TimeCurrent());
  //  if (!SupportECN) {
  //     if (ai_0) {
  //        if (OrderSelect(gi_1140, SELECT_BY_TICKET)) ld_24 = OrderTakeProfit() - MarketInfo(Symbol(), MODE_SPREAD) * gPoint;
  //     } else ld_16 = Ask + gd_964 * gPoint;
  //  }
      if (gi_1160 == FALSE) {
         if (ai_0) ld_8 = NormalizeDouble(f0_10(1) * gd_1052, 2);
         else ld_8 = f0_2(gd_1008);
      } else {
         if (f0_3(0) > 0.0) ld_8 = NormalizeDouble(f0_3(0) * gd_956, 2);
         else ld_8 = NormalizeDouble(BaseLotSize, 2);
      }
      
      // No money check
      if (!MarginEnoughCheck(ld_8)) {Sleep(1000); return(false);}// Fix 0.06a No money check
      
      // Fix 0.5 "TradeContextBusy"
      for (int tmp = 0; tmp < 6; tmp++) {
         if (IsTradeContextBusy())
            {Sleep(3000);
             Print("**** TCB: OpenBuy on " + Symbol() + " waiting to trade contest " + tmp + " attempt(s)");}
             else break;}      
      if (!IsTradeAllowed()) {GlobalVariableDel("PERMISSION"); return(false);}
      RefreshRates(); //Need b'cose possible waiting to trade contest fix 0.5
      
  //    if (BasketStopLoss > 0) ld_24 = Ask - gi_BSL * gPoint;
  //    ld_16 = Ask + gd_964 * gPoint; // Fix 0.2
  //    if (nbp > 0.0) ld_16 = nbp; // NBP
  //    if (!SupportECN) {
  //       li_4 = OrderSend(Symbol(), OP_BUY, ld_8, NormalizeDouble(Ask, Digits), slippage, NormalizeDouble(ld_24, Digits), NormalizeDouble(ld_16, Digits), EA_Name + ls_32,
  //          MagicNumber, 0, Green);
  //    } else {
         li_4 = OrderSend(Symbol(), OP_BUY, ld_8, NormalizeDouble(Ask, Digits), slippage, 0, 0, EA_Name + ls_32, MagicNumber, 0, Green);
         Sleep(1000);
  //     if (li_4 >= 0) OrderModify(li_4, 0, NormalizeDouble(ld_24, Digits), NormalizeDouble(ld_16, Digits), 0, Black);
  //  }
      gi_1064 = TimeCurrent();
      if (li_4 != -1) {
         if (!ai_0) {
            gi_1140 = li_4;
            f0_15("BUY hedgedTicket=" + gi_1140);
         } else {
            f0_15("BUY FEM_ticket=" + li_4);
            gi_1144 = 0;
         }
      } else {
         f0_15("failed sell");
         li_40 = FALSE;
      }
   }
   GlobalVariableDel("PERMISSION");
   //Last_TP[0] = ld_16;   
   return (li_40);
}

int f0_12(bool ai_0 = FALSE, double nbp = 0.0) {
   int li_4;
   double ld_8;
   double ld_16 = 0;
   double ld_24 = 0;
   string ls_32 = "";
   bool li_40 = TRUE;
   if (TimeCurrent() - gi_1064 < 60) return (0);
   if (ai_0 && (!gi_1136)) return (0);
   if (!GlobalVariableCheck("PERMISSION")) {
      GlobalVariableSet("PERMISSION", TimeCurrent());
  //  if (!SupportECN) {
  //     if (ai_0) {
  //        if (OrderSelect(gi_1140, SELECT_BY_TICKET)) ld_24 = OrderTakeProfit() + MarketInfo(Symbol(), MODE_SPREAD) * gPoint;
  //     } else ld_16 = Bid - gd_964 * gPoint;
  //  }
      if (gi_1160 == FALSE) {
         if (ai_0) ld_8 = NormalizeDouble(f0_10(0) * gd_1052, 2);
         else ld_8 = f0_2(gd_1008);
      } else {
         if (f0_6(0) > 0.0) ld_8 = NormalizeDouble(f0_6(0) * gd_956, 2);
         else ld_8 = NormalizeDouble(BaseLotSize, 2);
      }
      
      // No money check
      if (!MarginEnoughCheck(ld_8)) {Sleep(1000); return(false);}// Fix 0.06a No money check
      
      // Fix 0.5 "TradeContextBusy"
      for (int tmp = 0; tmp < 6; tmp++) {
         if (IsTradeContextBusy())
            {Sleep(3000);
             Print("**** TCB: OpenDeal on " + Symbol() + " waiting to trade contest " + tmp + " attempt(s)");}
             else break;}      
      if (!IsTradeAllowed()) {GlobalVariableDel("PERMISSION"); return(false);}
      RefreshRates(); //Need b'cose possible waiting to trade contest fix 0.5      
      
  //  if (BasketStopLoss > 0) ld_24 = Bid + gi_BSL * gPoint;
  //  ld_16 = Bid - gd_964 * gPoint; // Fix 0.2
  //  if (nbp > 0.0) ld_16 = nbp; // NBP
  //  if (!SupportECN) {
  //     li_4 = OrderSend(Symbol(), OP_SELL, ld_8, NormalizeDouble(Bid, Digits), slippage, NormalizeDouble(ld_24, Digits), NormalizeDouble(ld_16, Digits), EA_Name + ls_32,
  //        MagicNumber, 0, Pink);
  //  } else {
         li_4 = OrderSend(Symbol(), OP_SELL, ld_8, NormalizeDouble(Bid, Digits), slippage, 0, 0, EA_Name + ls_32, MagicNumber, 0, Pink);
         Sleep(1000);
  //     if (li_4 >= 0) OrderModify(li_4, NormalizeDouble(OrderOpenPrice(), Digits), NormalizeDouble(ld_24, Digits), NormalizeDouble(ld_16, Digits), 0, Black);
  //  }
      gi_1064 = TimeCurrent();
      if (li_4 != -1) {
         if (!ai_0) {
            gi_1140 = li_4;
            f0_15("SELL hedgedTicket=" + gi_1140);
         } else {
            f0_15("SELL FEM_ticket=" + li_4);
            gi_1144 = 1;
         }
      } else {
         f0_15("failed sell");
         li_40 = FALSE;
      }
   }
   GlobalVariableDel("PERMISSION");
   //Last_TP[1] = ld_16;
   return (li_40);
}

void f0_14(int ai_0, int ai_4) {
   if (MaximumBuyLevels == 0 && ai_0 == 0) return;
   int li_8 = 0;
   double ld_12 = 0;
   double ld_20 = 0;
   double ld_28 = 0;
   double ld_36 = 0;
   int li_44 = -1;
   int li_48 = 0;
   int li_52 = 0;
   int li_56 = 0;
   for (li_52 = 0; li_52 < OrdersTotal(); li_52++) {
      if (OrderSelect(li_52, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY) {
            li_56++;
            if (OrderOpenTime() > li_8) {
               li_8 = OrderOpenTime();
               ld_12 = OrderOpenPrice();
               li_44 = OrderType();
               li_48 = OrderTicket();
               ld_28 = OrderTakeProfit();
               ld_36 = OrderStopLoss();
            }
            if (OrderLots() > ld_20) ld_20 = OrderLots();
         }
      }
   }
   //int li_60 = MathRound(MathLog(ld_20 / BaseLotSize) / MathLog(gd_956)) + 1.0;
   //if (li_60 < 0) li_60 = 0;
   int li_60 = li_56;
   gd_1008 = NormalizeDouble(BaseLotSize * MathPow(gd_956, li_60), gi_1076);
   if (li_60 == 0 && f0_7() == 1) {
      if (FreezeAfterTP == FALSE && gi_952 == FALSE) f0_11();
      else
         if (ai_0 > 0) f0_11();
   } else {
      if (ld_12 - Ask > ExecutionPoint * gd_1068 && ld_12 > 0.0 && li_56 < MaximumBuyLevels) {
         if (!(f0_11())) return;
         return;} // if (ord_op_price - Ask > Execut...
   }
   
   if (li_56 == 0) return; // if mult_level == 0
   
   // Manage Pyramid
   
   double nbpTP=0;
   static int nbpBT;
   if (Negative_Basket_Protection == TRUE && ld_28 == 0) {nbpTP = GetBreakeven(OP_BUY); nbpBT = TimeCurrent();}
   if (ld_28 != 0 && TimeCurrent() - nbpBT > 3600) {nbpTP = GetBreakeven(OP_BUY); nbpBT = TimeCurrent();}

   if (ld_28 == 0) ld_28 = ld_12 + gd_964 * gPoint; //tp
   if (ld_36 == 0) ld_36 = ld_12 - gi_BSL * gPoint; //sl

   if (nbpTP > 0 && Negative_Basket_Protection == TRUE) { // Авто NBP на базе безубытка Umeku
      double autoNBP_TP = NormalizeDouble(nbpTP + NBP * gPoint, Digits);
      if (autoNBP_TP > ld_28)   {
         Print(" **** Auto-NBP work on Buy " + Symbol() + " aNBP: " + DoubleToStr(autoNBP_TP, Digits) + " oTP: " + DoubleToStr(ld_28, Digits));
         ld_28 = autoNBP_TP;    }                       } //nbp>0
   
   //if (ld_28 == 0) ld_28 = Last_TP[0]; //tp
      
   for (li_52 = 0; li_52 < OrdersTotal(); li_52++) {
      if (GlobalVariableCheck("PERMISSION")) break; //Fix 0.5 "TradeContextBusy"
      if (OrderSelect(li_52, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderMagicNumber() != MagicNumber || OrderType() != OP_BUY || ld_36 == 0.0 || ld_28 == 0.0 || OrderSymbol() != Symbol()) continue;
         if (NormalizeDouble(OrderTakeProfit(),Digits) == NormalizeDouble(ld_28, Digits) && NormalizeDouble(OrderStopLoss(), Digits) == NormalizeDouble(ld_36,Digits)) continue;
         OrderModify(OrderTicket(), NormalizeDouble(OrderOpenPrice(), Digits), NormalizeDouble(ld_36, Digits), NormalizeDouble(ld_28, Digits), 0, Pink);
         Sleep(1000);
      }
   }
}


void f0_13(int ai_0, int ai_4) {
   if (MaximumSellLevels == 0 && ai_4 == 0) return;
   int li_8 = 0;
   double ld_12 = 0;
   double ld_20 = 0;
   double ld_28 = 0;
   double ld_36 = 0;
   int li_44 = -1;
   int li_48 = 0;
   int li_52 = 0;
   int li_56 = 0;
   for (li_52 = 0; li_52 < OrdersTotal(); li_52++) {
      if (OrderSelect(li_52, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderMagicNumber() == MagicNumber && OrderType() == OP_SELL) {
            li_56++;
            if (OrderOpenTime() > li_8) {
               li_8 = OrderOpenTime();
               ld_12 = OrderOpenPrice();
               li_44 = OrderType();
               li_48 = OrderTicket();
               ld_28 = OrderTakeProfit();
               ld_36 = OrderStopLoss();
            }
            if (OrderLots() > ld_20) ld_20 = OrderLots();
         }
      }
   }
   //int li_60 = MathRound(MathLog(ld_20 / BaseLotSize) / MathLog(gd_956)) + 1.0;
   //if (li_60 < 0) li_60 = 0;
   int li_60 = li_56;
   gd_1008 = NormalizeDouble(BaseLotSize * MathPow(gd_956, li_60), gi_1076);
   if (li_60 == 0 && f0_7() == -1) {
      if (FreezeAfterTP == FALSE && gi_952 == FALSE) f0_12();
      else
         if (ai_4 > 0) f0_12();
   } else {
      if (Bid - ld_12 > ExecutionPoint * gd_1068 && ld_12 > 0.0 && li_56 < MaximumSellLevels) {
         if (!(f0_12())) return;
         return;  } // if Bid - ld_12 > ExecutionPoint * g...
   }

   if (li_56 == 0) return;
   
   // Manage Pyramid
   
   double nbpTP=0;
   static int nbpST;
   if (Negative_Basket_Protection == TRUE && ld_28 == 0) {nbpTP = GetBreakeven(OP_SELL); nbpST = TimeCurrent();}
   if (ld_28 != 0 && TimeCurrent() - nbpST > 3600) {nbpTP = GetBreakeven(OP_BUY); nbpST = TimeCurrent();}

   if (ld_28 == 0) ld_28 = ld_12 - gd_964 * gPoint; //tp
   if (ld_36 == 0) ld_36 = ld_12 + gi_BSL * gPoint; //sl

   if (nbpTP > 0 && Negative_Basket_Protection == TRUE) { // Авто NBP на базе безубытка Umeku
      double autoNBP_TP = NormalizeDouble(nbpTP - NBP * gPoint, Digits);
      if (autoNBP_TP < ld_28)   {
         Print(" **** Auto-NBP work on Sell " + Symbol() + " aNBP: " + DoubleToStr(autoNBP_TP, Digits) + " oTP: " + DoubleToStr(ld_28, Digits));
         ld_28 = autoNBP_TP;    }                       } //nbp>0

   //if (ld_28 == 0) ld_28 = Last_TP[1]; //tp
   
   for (li_52 = 0; li_52 < OrdersTotal(); li_52++) {
      if (GlobalVariableCheck("PERMISSION")) break; //Fix 0.5 "TradeContextBusy"
      if (OrderSelect(li_52, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderMagicNumber() != MagicNumber || OrderType() != OP_SELL || ld_36 == 0.0 || ld_28 == 0.0 || OrderSymbol() != Symbol()) continue;
         if (NormalizeDouble(OrderTakeProfit(),Digits) == NormalizeDouble(ld_28, Digits) && NormalizeDouble(OrderStopLoss(), Digits) == NormalizeDouble(ld_36,Digits)) continue;
         OrderModify(OrderTicket(), NormalizeDouble(OrderOpenPrice(), Digits), NormalizeDouble(ld_36, Digits), NormalizeDouble(ld_28, Digits), 0, Pink);
         Sleep(1000);
      }
   }
}

int f0_8(string as_0) {
   int li_8;
   int li_12 = OrdersTotal();
   if (li_12 == 0) return (0);
   double ld_16 = 0;
   for (int li_24 = li_12 - 1; li_24 >= 0; li_24--) {
      if (OrderSelect(li_24, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == as_0 && OrderMagicNumber() == MagicNumber) {
            if (OrderType() == OP_BUY) {
               RefreshRates();
               li_8 = OrderClose(OrderTicket(), OrderLots(), Bid, slippage, Red);
               continue;
            }
            if (OrderType() == OP_SELL) {
               RefreshRates();
               li_8 = OrderClose(OrderTicket(), OrderLots(), Ask, slippage, Green);
            }
         }
      }
   }
   return (1);
}

int start() {

   if (IsTesting() && AccountLeverage() < 400) return(0);

   double ld_0;
   double ld_8;
   double ld_16;   
   if (Chicken_Out == TRUE) {
      f0_8(Symbol());
      return (0);
   }
   
   if (Period() != PERIOD_M30 && !Use_SWB_indicator) return (0);
   
   if (FreezeAfterTPScheduler == TRUE) gi_952 = f0_4();
   
   int li_24 = 0;
   int li_28 = 0;
   for (int li_32 = 0; li_32 < OrdersTotal(); li_32++) {
      if (OrderSelect(li_32, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderMagicNumber() == MagicNumber) {
            if (StringFind(OrderComment(), gs__hedged_1148) == -1) {
               if (OrderType() == OP_BUY) {
                  li_24++;
                  continue;
               }
               if (OrderType() == OP_SELL) li_28++;
            }
         }
      }
   }
   
   gLevel[0] = li_24; //SE ApM
   gLevel[1] = li_28; //SE ApM   
   
   /*if (li_24 >= gi_1060) {
      if (!gi_1136) {
         f0_15("Allow long hedge! trades=" + li_24 + ",TradesDeep=" + gi_1060);
         gi_1136 = TRUE;
      }
   }
   if (li_28 >= gi_1060) {
      if (!gi_1132) {
         f0_15("Allow short hedge! trades=" + li_28 + ",TradesDeep=" + gi_1060);
         gi_1132 = TRUE;
      }
   }*/
   
   bool li_36 = FALSE;
   if ((100 - BailOutPct) * AccountBalance() / 100.0 >= AccountEquity()) {
      f0_15("AccountBalance=" + AccountBalance() + ",AccountEquity=" + AccountEquity());
      gi_1016 = TRUE;
      li_36 = TRUE;
   }
   if ((gd_996 + 100.0) * AccountBalance() / 100.0 <= AccountEquity()) gi_1016 = TRUE;
   if (gi_1016) {
      for (int li_40 = OrdersTotal() - 1; li_40 >= 0; li_40--) {
         if (OrderSelect(li_40, SELECT_BY_POS)) {
            if (OrderMagicNumber() == MagicNumber) {
               f0_15("close #" + OrderTicket());
               if (!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), MarketInfo(Symbol(), MODE_SPREAD), White)) {
                  f0_15("error");
                  return (0);
               }
            }
         }
      }
      gi_1016 = FALSE;
      if (li_36) {
         Sleep(1000 * gi_1004);
         li_36 = FALSE;
      }
      gi_1136 = FALSE;
      gi_1132 = FALSE;
   }
   
   // Реализуем паузу в каждой копии, чтобы избежать синхронности в работе. Fix 0.5 TCB
   double Rnd = 20 + (1-20)*MathRand()/32767; //Число от 1 до 20
   Sleep(Rnd*50);

   gi_1192 = fun0(gda_setTP, gda_setMULT, Digits, li_24, gPoint, gda_1188, li_24, IsDemo());
   if (gi_1192 >= 0) {
         gd_964 = gda_1188[0];
         gd_1068 = gda_1188[1];
         gd_956 = gda_1188[2];
         if (gi_1192 == 0) gi_1160 = FALSE;
         else gi_1160 = TRUE;
      }
   f0_14(li_24, li_28); // Manage Buy
   gi_1192 = fun0(gda_setTP, gda_setMULT, Digits, li_28, gPoint, gda_1188, li_28, IsDemo());
   if (gi_1192 >= 0) {
         gd_964 = gda_1188[0];
         gd_1068 = gda_1188[1];
         gd_956 = gda_1188[2];
         if (gi_1192 == 0) gi_1160 = FALSE;
         else gi_1160 = TRUE;
      }
   f0_13(li_24, li_28); // Manage Sell
      /*gi_1192 = fun0(gda_setTP, gda_setMULT, Digits, 0, gPoint, gda_1188, 0, IsDemo());
      if (gi_1192 >= 0) {
         gd_964 = gda_1188[0];
         gd_1068 = gda_1188[1];
         gd_956 = gda_1188[2];
         if (gi_1192 == 0) gi_1160 = FALSE;
         else gi_1160 = TRUE;
      }
      if ((!FreezeAfterTP) && !gi_952) {
         if (gi_1128) {
            gi_1192 = fun0(gda_setTP, gda_setMULT, Digits, li_24, gPoint, gda_1188, li_24, IsDemo());
            if (gi_1192 >= 0) {
               gd_964 = gda_1188[0];
               gd_1068 = gda_1188[1];
               gd_956 = gda_1188[2];
               if (gi_1192 == 0) gi_1160 = FALSE;
               else gi_1160 = TRUE;
            }
            if (f0_11(1)>0) gi_1128 = FALSE;
         }
         if (gi_1124) {
            gi_1192 = fun0(gda_setTP, gda_setMULT, Digits, li_28, gPoint, gda_1188, li_28, IsDemo());
            if (gi_1192 >= 0) {
               gd_964 = gda_1188[0];
               gd_1068 = gda_1188[1];
               gd_956 = gda_1188[2];
               if (gi_1192 == 0) gi_1160 = FALSE;
               else gi_1160 = TRUE;
            }
            if (f0_12(1)>0) gi_1124 = FALSE;
         }
      }/*/
    ChartComment();
    return (0);
}

void ChartComment() {
   if (IsTesting() && (!IsVisualMode()) || IsOptimization()) return; // Taken from 3.0 to faster history
   string ls_0 = DoubleToStr(f0_1(2), 2);
   if (!IsTesting()) f0_9();
   string s1="", s2="", s6="";
   if (FreezeAfterTP) s1 = "\nFreezeAfterTP is enabled.";
   if (FreezeAfterTPScheduler == TRUE && f0_4() == 1) s2 = "\nFreezeAfterTPScheduler: ON"; else s2 = "";
   string s4="\nBuy level: " + gLevel[0] + " Sell level: " + gLevel[1];
   if (ShowBreakeven) {
       s6 = "\nBETP ";
       if (gLevel[0] > 1) s6 = s6 + "Buy: " + DoubleToStr(GetBreakeven(OP_BUY), Digits) + " / ";
       if (gLevel[1] > 1) s6 = s6 + "Sell: " + DoubleToStr(GetBreakeven(OP_SELL), Digits);
                       }
   
   Comment(" \nFE_MOD 2.009" , 
      "\nAccount Leverage  :  " + "1 : " + AccountLeverage(),
      "\nAccount Type  :  " + AccountServer(), 
      "\nServer Time  :  " + TimeToStr(TimeCurrent(), TIME_SECONDS), 
      "\nAccount Equity  = ", AccountEquity(), 
      "\nFree Margin     = ", AccountFreeMargin(), 
      "\nDrawdown  :  ", ls_0, "%", 
      "\nTotal Profit/Loss = ", gd_944, "\n",
      s1, s2, s4, s6,
      "\n");
}

double f0_1(int ai_0) {
   double ld_4;
   if (ai_0 == 2) {
      ld_4 = (AccountEquity() / AccountBalance() - 1.0) / (-0.01);
      if (ld_4 <= 0.0) return (0);
      return (ld_4);
   }
   if (ai_0 == 1) {
      ld_4 = 100.0 * (AccountEquity() / AccountBalance() - 1.0);
      if (ld_4 <= 0.0) return (0);
      return (ld_4);
   }
   return (0.0);
}

int fun0(double& a0[], double& a1[], int a2, int a3, double a4, double& a5[], double a6, int a7)
{ int v8;
  double v9, v12, v16;
  v8 = 0;

  v9 = a0[25]; //take profit standart
  v16 = a1[25]; // mult standart
 
  if ( a0[a3] <= 0.0 ) v9 = a0[25]; else v9 = a0[a3];
  if ( a1[a3] > 0.0 ) {v16 = a1[a3]; v8 = 1;}
    
  // Standart original 2.0 trade logic's
  if (!AugmenterOn)
      {if (a3 == 0 || a3 > 1) {v16 = a1[25];}}
    
//LABEL_10:
  if ( a1[1] > 0.0
    || a1[2] > 0.0
    || a1[3] > 0.0
    || a1[4] > 0.0
    || a1[5] > 0.0
    || a1[6] > 0.0
    || a1[7] > 0.0
    || a1[8] > 0.0
    || a1[9] > 0.0
    || a1[10] > 0.0
    || a1[11] > 0.0
    || a1[12] > 0.0
    || a1[13] > 0.0
    || a1[14] > 0.0
    || a1[15] > 0.0
    || a1[16] > 0.0
    || a1[17] > 0.0
    || a1[18] > 0.0
    || a1[19] > 0.0
    || a1[20] > 0.0
    || a1[21] > 0.0
    || a1[22] > 0.0
    || a1[23] > 0.0
    || a1[24] > 0.0 )
    v8 = 1;
  a5[0] = v9; // Fix 3/5
  a5[1] = gPoint; // Fix 3/5
  a5[2] = v16;
  a5[3] = a6;
  return (v8 == 1);
}
   
int f0_7() { //int signal()
  int buy = 1, sell = -1;
  if (!Use_SWB_indicator) {
      double ld_0 = iSAR(NULL, SignalPeriod, gd_1036, gd_1044, 0);
      double ld_8 = iMA(NULL, SignalPeriod, gi_1020, gi_1024, gi_1028, gi_1032, 0);
      if (ld_0 > ld_8) return (sell);
      if (ld_0 < ld_8) return (buy);
      return (0);}
      
  ////////////// 0.2b Signal from SWB Grid 4.1.0.7 //////////////////////
  double upBB=iBands(Symbol(),0,bb_period,bb_deviation,0,PRICE_CLOSE,MODE_UPPER,bb_shift);
  double loBB=iBands(Symbol(),0,bb_period,bb_deviation,0,PRICE_CLOSE,MODE_LOWER,bb_shift);
  double stoch=iStochastic(Symbol(),0,k,d,slowing,MODE_SMA,price_field,MODE_SIGNAL,stoch_shift);
  double rsi=iRSI(Symbol(),0,rsi_period,PRICE_CLOSE,rsi_shift);
  if(use_bb && use_stoch && use_rsi)
  {
     if(High[bb_shift]>upBB && stoch>up_level && rsi>upper) return(sell);
     if(Low[bb_shift]upBB && stoch>up_level) return(sell);
     if(Low[bb_shift]upBB) return(sell);
     if(Low[bb_shift]up_level && rsi>upper) return(sell);
     if(stochup_level) return(sell);
     if(stochupBB && rsi>upper) return(sell);
     if(Low[bb_shift]upper) return(sell);
     if(rsi= li_20 && li_16 < li_24))) return (1);
      return (0);
   }
   if (li_20 > li_24) {
      if (!(!(li_16 >= li_20 || li_16 < li_24))) return (1);
      return (0);
   }
   return (0);
}

bool MarginEnoughCheck(double lot) { // No money check ))
         if (AccountFreeMargin() < 1 ) return(false); // Low margin
         if (AccountFreeMargin() < lot * MarketInfo(Symbol(), MODE_MARGINREQUIRED)) return(false); //No margin to open order
   return(true);
}

void updateMagicNumber() {
   int li_0;
   string ls_12;
   int lia_20[256];
   for (int li_4 = 0; li_4 < 256; li_4++) {
      li_0 = li_4;
      for (int l_count_8 = 0; l_count_8 < 8; l_count_8++) {
         if (li_0 & 1 > 0) li_0 = li_0 >> 1 ^ (-306674912);
         else li_0 >>= 1;
      }
      lia_20[li_4] = li_0;
   }
   if (MagicNumber == 0) {
      ls_12 = Symbol() + 0;
      li_0 = -1;
      for (li_4 = StringLen(ls_12) - 1; li_4 >= 0; li_4--) li_0 = li_0 >> 8 ^ lia_20[li_0 ^ StringGetChar(ls_12, li_4) & 255];
      MagicNumber = li_0 ^ (-1) & EMPTY_VALUE;
   }
   Print("Using magic number: " + MagicNumber);
}

double f0_10(int ai_0) {
   for (int li_4 = OrdersTotal() - 1; li_4 >= 0; li_4--) {
      if (OrderSelect(li_4, SELECT_BY_POS)) {
         if (OrderMagicNumber() == MagicNumber) {
            if (StringFind(OrderComment(), gs__hedged_1148) == -1) {
               f0_15("GetLastLotSize " + ai_0 + ",OrderLots()=" + OrderLots());
               return (OrderLots());
            }
         }
      }
   }
   f0_15("GetLastLotSize " + ai_0 + " wasnt found");
   return (0);
}

double f0_3(int ai_0) {
   for (int li_4 = OrdersTotal() - 1; li_4 >= 0; li_4--) {
      if (OrderSelect(li_4, SELECT_BY_POS)) {
         if (OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY) {
            if (StringFind(OrderComment(), gs__hedged_1148) == -1) {
               f0_15("GetLastLotSize " + ai_0 + ",OrderLots()=" + OrderLots());
               return (OrderLots());
            }
         }
      }
   }
   f0_15("GetLastLotSize " + ai_0 + " wasnt found");
   return (0);
}

double f0_6(int ai_0) {
   for (int li_4 = OrdersTotal() - 1; li_4 >= 0; li_4--) {
      if (OrderSelect(li_4, SELECT_BY_POS)) {
         if (OrderMagicNumber() == MagicNumber && OrderType() == OP_SELL) {
            if (StringFind(OrderComment(), gs__hedged_1148) == -1) {
               f0_15("GetLastLotSize " + ai_0 + ",OrderLots()=" + OrderLots());
               return (OrderLots());
            }
         }
      }
   }
   f0_15("GetLastLotSize " + ai_0 + " wasnt found");
   return (0);
}

void f0_15(string as_0) {}

// New [3] functions: for new NBP & Calculate Breakeven Point (01/09/2012)

double GetBreakeven(int type) { // (magic, operation 0-buy, 1-sell)
   double BE_TP;
   double ld_32;
   double point_40;
   int order_total_20 = OrdersTotal();
   int ticket_16 = fGBE2(MagicNumber, type, 0);
   if (OrderSelect(ticket_16, SELECT_BY_TICKET)) {
      BE_TP = OrderOpenPrice();
      ld_32 = -1;
      point_40 = MarketInfo(OrderSymbol(), MODE_POINT);
      while (ld_32 < 0.0) {
         if (type == OP_BUY) BE_TP += point_40;
         else BE_TP -= point_40;
         ld_32 = 0;
         for (int pos_12 = 0; pos_12 < order_total_20; pos_12++) {
            if (OrderSelect(pos_12, SELECT_BY_POS)) {
               if (type == OrderType())
                  if (OrderMagicNumber() == MagicNumber) ld_32 += fGBE1(OrderLots(), OrderOpenPrice(), BE_TP, type, OrderSymbol()) + OrderCommission() + OrderSwap();
            }
         }
      }
   }
   return (BE_TP);
}

double fGBE1(double ad_0, double ad_8, double ad_16, int ai_24, string a_symbol_28) {
   double ld_36 = MarketInfo(a_symbol_28, MODE_TICKVALUE) / MarketInfo(a_symbol_28, MODE_TICKSIZE);
   if (ai_24 == 0) return (ld_36 * ad_0 * (ad_16 - ad_8));
   if (ai_24 == 1) return (ld_36 * ad_0 * (ad_8 - ad_16));
   return (0);
}

int fGBE2(int a_magic_0, int a_cmd_4, bool ai_8) {
   int ticket_12;
   int order_total_20 = OrdersTotal();
   for (int pos_16 = 0; pos_16 < order_total_20; pos_16++) {
      if (OrderSelect(pos_16, SELECT_BY_POS)) {
         if (a_cmd_4 == OrderType()) {
            if (OrderMagicNumber() == a_magic_0) {
               ticket_12 = OrderTicket();
               if (ai_8) return (ticket_12);
            }
         }
      }
   }
   return (ticket_12);
}

Recommend