/*
  EA that manages your trades after 1st manually
  open trade with a matching MagicNumber.
  Developed for needs of VanDerPjer  
*/
#property link      "VanDerPjer"

extern string   = "BASIC_SETTINGS";
extern int MagicNumber = 1101;
extern int TP_Steps = 3;
extern bool Use_Buy_Level = FALSE;
extern double Buy_Level = 0;
extern bool Use_Sell_Level = FALSE;
extern double Sell_Level = 0;
extern string    = "STOPLOSS SETTINGS";
extern int SL_P1 = 4000;
extern int SL_P2 = 3000;
extern int SL_P3 = 2000;
extern int SL_P4 = 2000;
extern int SL_P5 = 2000;
extern int SL_P6 = 2000;
extern int SL_P7 = 2000;
extern int SL_P8 = 2000;
extern int SL_P9 = 2000;
extern int SL_P10 = 2000;
extern int SL_M1 = 4000;
extern int SL_M2 = 3000;
extern int SL_M3 = 2000;
extern int SL_M4 = 2000;
extern int SL_M5 = 2000;
extern int SL_M6 = 2000;
extern int SL_M7 = 2000;
extern int SL_M8 = 2000;
extern int SL_M9 = 2000;
extern int SL_M10 = 2000;
extern string     = "LOSIZE SETTINGS";
extern double LotP1 = 0.10;
extern string LotP1_only_if = "1st trade is launched";
extern string through_the_EA = "with object movement";
extern double LotP2 = 0.20;
extern double LotP3 = 0.40;
extern double LotP4 = 0.8;
extern double LotP5 = 1;
extern double LotP6 = 2;
extern double LotP7 = 3;
extern double LotP8 = 4;
extern double LotP9 = 5;
extern double LotP10 = 6;
extern double LotM2 = 0.20;
extern double LotM3 = 0.40;
extern double LotM4 = 0.8;
extern double LotM5 = 1.0;
extern double LotM6 = 2;
extern double LotM7 = 3;
extern double LotM8 = 4;
extern double LotM9 = 5;
extern double LotM10 = 6;
extern string      = "STEP SETTINGS";
extern int StepP1 = 100;
extern int StepP2 = 100;
extern int StepP3 = 100;
extern int StepP4 = 100;
extern int StepP5 = 100;
extern int StepP6 = 100;
extern int StepP7 = 100;
extern int StepP8 = 100;
extern int StepP9 = 100;
extern int StepP10 = 100;
extern int StepM1 = 100;
extern int StepM2 = 100;
extern int StepM3 = 100;
extern int StepM4 = 100;
extern int StepM5 = 100;
extern int StepM6 = 100;
extern int StepM7 = 100;
extern int StepM8 = 100;
extern int StepM9 = 100;
extern int StepM10 = 100;
extern bool USE_PARTIAL_CLOSE = TRUE;
extern string       = "PARTIAL CLOSE SETTINGS";
extern string        = "TP & SL in pips";
extern string         = "Close in % of previous Lotsize";
extern int TP_1_P1 = 70;
extern double Close_1_P1p = 50.0;
extern int TP_2_P1 = 100;
extern double Close_2_P1p = 40.0;
extern int SL_1_P1 = 70;
extern double Close_1_P1m = 50.0;
extern int SL_2_P1 = 70;
extern double Close_2_P1m = 50.0;
extern int TP_1_M1 = 70;
extern double Close_1_M1p = 50.0;
extern int TP_2_M1 = 100;
extern double Close_2_M1p = 40.0;
extern int SL_1_M1 = 70;
extern double Close_1_M1m = 50.0;
extern int SL_2_M1 = 70;
extern double Close_2_M1m = 50.0;
extern int slippage = 3;
int g_window_572;
string g_name_576 = "startLabelSell";
string g_name_584 = "startDetectMarkSell";
string g_name_592 = "startMarkSell";
string g_name_108 = "startLabelCloseAll";
string g_name_109 = "startDetectCloseAll";
string g_name_110 = "startMarkCloseAll";
string gs_unused_600 = "AutostartLabelSell";
string gs_unused_608 = "AutostartDetectMarkSell";
string g_name_616 = "startLabelBuy";
string g_name_624 = "startDetectMarkBuy";
string g_name_632 = "startMarkBuy";
string gs_unused_640 = "AutostartLabelBuy";
string gs_unused_648 = "AutostartDetectMarkBuy";
double gda_656[10];
double gda_660[10];
double gda_664[10];
double gda_668[10];
double gda_672[1];
double gda_676[1];
double gda_680[1];
double gda_684[1];
double gda_688[1];
double gda_692[1];
int gi_696 = 0;
int gi_700 = 0;
int gi_704 = 0;
bool gi_708 = FALSE;
bool gi_712 = FALSE;
bool gi_716 = FALSE;
bool gi_720 = FALSE;
bool gi_unused_724 = FALSE;
bool gi_unused_728 = FALSE;
bool gi_unused_732 = FALSE;
bool gi_unused_736 = FALSE;
bool gi_unused_740 = FALSE;
bool gi_unused_744 = FALSE;
bool gi_unused_748 = FALSE;
bool gi_unused_752 = FALSE;
bool gi_unused_756 = FALSE;
bool gi_unused_760 = FALSE;
bool gi_unused_764 = FALSE;
bool gi_unused_768 = FALSE;
bool gi_unused_772 = FALSE;
bool gi_unused_776 = FALSE;
bool gi_unused_780 = FALSE;
bool gi_unused_784 = FALSE;
bool gi_788 = FALSE;
bool gi_792 = FALSE;
bool gi_796 = FALSE;
bool gi_800 = FALSE;
bool gi_unused_804 = FALSE;
bool gi_unused_808 = FALSE;
bool gi_unused_812 = FALSE;
bool gi_unused_816 = FALSE;
bool gi_unused_820 = FALSE;
bool gi_unused_824 = FALSE;
bool gi_unused_828 = FALSE;
bool gi_unused_832 = FALSE;
bool gi_unused_836 = FALSE;
bool gi_unused_840 = FALSE;
bool gi_unused_844 = FALSE;
bool gi_unused_848 = FALSE;
bool gi_unused_852 = FALSE;
bool gi_unused_856 = FALSE;
bool gi_unused_860 = FALSE;
bool gi_unused_864 = FALSE;
bool gi_868 = FALSE;
bool gi_872 = FALSE;
bool gi_876 = FALSE;
bool gi_880 = FALSE;
bool gi_884 = FALSE;
bool gi_888 = FALSE;
bool gi_892 = TRUE;

int init() {
   return (0);
}

int deinit() {
   return (0);
}

int LastTicket() {
   int l_ticket_0 = 0;
   int li_16 = OrdersTotal() - 1;
   for (int l_pos_12 = li_16; l_pos_12 >= 0; l_pos_12--) {
      OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES);
      if (OrderTicket() > l_ticket_0 && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()) l_ticket_0 = OrderTicket();
   }
   return (l_ticket_0);
}

int OpenTradesBuy() {
   int l_count_8 = 0;
   int l_ord_total_4 = OrdersTotal();
   for (int l_pos_0 = 0; l_pos_0 < l_ord_total_4; l_pos_0++) {
      OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES);
      if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) l_count_8++;
   }
   return (l_count_8);
}

int OpenTradesSell() {
   int l_count_8 = 0;
   int l_ord_total_4 = OrdersTotal();
   for (int l_pos_0 = 0; l_pos_0 < l_ord_total_4; l_pos_0++) {
      OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES);
      if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) l_count_8++;
   }
   return (l_count_8);
}

int OpenTradesTotal() {
   int li_ret_0 = OpenTradesBuy() + OpenTradesSell();
   return (li_ret_0);
}

int LastType() {
   int l_ticket_0 = 0;
   int l_cmd_8 = 2;
   int li_16 = OrdersTotal() - 1;
   for (int l_pos_12 = li_16; l_pos_12 >= 0; l_pos_12--) {
      OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES);
      if (OrderTicket() > l_ticket_0 && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()) {
         l_ticket_0 = OrderTicket();
         l_cmd_8 = OrderType();
      }
   }
   return (l_cmd_8);
}

double LastBuyPrice() {
   int l_ticket_8;
   double ld_0 = 0;
   double l_ord_open_price_12 = 0;
   int li_24 = OrdersTotal() - 1;
   for (int l_pos_20 = 0; l_pos_20 <= li_24; l_pos_20++) {
      OrderSelect(l_pos_20, SELECT_BY_POS, MODE_TRADES);
      if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol() && OrderTicket() > ld_0) {
         l_ticket_8 = OrderTicket();
         l_ord_open_price_12 = OrderOpenPrice();
      }
   }
   return (l_ord_open_price_12);
}

double LastSellPrice() {
   int l_ticket_8;
   double ld_0 = 0;
   double l_ord_open_price_12 = 0;
   int li_24 = OrdersTotal() - 1;
   for (int l_pos_20 = 0; l_pos_20 <= li_24; l_pos_20++) {
      OrderSelect(l_pos_20, SELECT_BY_POS, MODE_TRADES);
      if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol() && OrderTicket() > ld_0) {
         l_ticket_8 = OrderTicket();
         l_ord_open_price_12 = OrderOpenPrice();
      }
   }
   return (l_ord_open_price_12);
}

int start() {

 int buylevel = 0;
   if (Close[0] > Buy_Level && Use_Buy_Level == 1) buylevel = 1;
   else buylevel = 0;
   int selllevel = 0;
   if (Close[0] < Sell_Level && Use_Sell_Level == 1) selllevel = 1;
   else selllevel = 0;
   string Level;
   if (buylevel == 1) Level = "Above Buy Level";
   if (selllevel == 1) Level = "Below Sell Level";
   int l_ticket_0;
   int l_ticket_4;
   int l_ticket_8;
   int l_ticket_12;
   int l_ticket_16;
   int l_ticket_20;
   int l_ticket_24;
   int l_ticket_28;
   int l_ticket_32;
   int l_ticket_36;
   int l_ticket_40;
   int l_ticket_44;
   int l_ticket_48;
   int l_ticket_52;
   int l_ticket_56;
   int l_ticket_60;
   if (gi_892 == TRUE) {
      ResetArrays();
      gi_892 = FALSE;
   }
   if (gi_704 < OpenTradesBuy() && LastType() == 0) {
      Print("New Long position on " + Symbol() + " opened!");
      gda_656[gi_704] = LastTicket();
      gda_664[gi_704] = LastBuyPrice();
   } else {
      if (gi_700 < OpenTradesSell() && LastType() == 1) {
         Print("New Short position on " + Symbol() + " opened!");
         gda_660[gi_700] = LastTicket();
         gda_668[gi_700] = LastSellPrice();
      }
   }
   if (gi_696 < OpenTradesTotal() && gi_876 == FALSE) {
      gda_680[0] = ShortSL();
      gi_876 = TRUE;
   }
   if (gi_880 == FALSE && ShortTP() != 0.0) {
      gda_684[0] = ShortTP();
      gi_880 = TRUE;
   }
   if (gi_696 < OpenTradesTotal() && gi_868 == FALSE) {
      gda_672[0] = LongSL();
      gi_868 = TRUE;
   }
   if (gi_872 == FALSE && LongTP() != 0.0) {
      gda_676[0] = LongTP();
      gi_872 = TRUE;
   }
   if (gi_696 < OpenTradesTotal() && gi_884 == FALSE) {
      gda_688[0] = StartPrice();
      gi_884 = TRUE;
   }
   if (gi_696 < OpenTradesTotal() && gi_888 == FALSE) {
      gda_692[0] = StartType();
      gi_888 = TRUE;
   }
   gi_696 = OpenTradesTotal();
   gi_700 = OpenTradesSell();
   gi_704 = OpenTradesBuy();
   if (OpenTradesTotal() < gi_696) {
      Closeall();
      gi_696 = 0;
      gi_700 = 0;
      gi_704 = 0;
      ResetArrays();
      SetBoolsBack();
      Print("All Orders Closed due to position loss!");
   }
   if (USE_PARTIAL_CLOSE == TRUE && gda_688[0] != 0.0 && gda_692[0] == 0.0 && gi_708 == FALSE && Bid >= gda_664[0] + Point * TP_1_P1) {
      if (OrderSelect(gda_656[0], SELECT_BY_TICKET) == TRUE) l_ticket_0 = OrderTicket();
      if (OrderClose(l_ticket_0, OrderLots() * Close_1_P1p / 100.0, Bid, slippage * tpslmult1() * Point, Red) == 1) {
         gi_708 = TRUE;
         Print("Order Partially Closed on " + Symbol());
         gda_656[0] = LastTicket();
      }
   }
   if (USE_PARTIAL_CLOSE == TRUE && gda_688[0] != 0.0 && gda_692[0] == 0.0 && gi_712 == FALSE && Bid >= gda_664[0] + Point * TP_2_P1) {
      if (OrderSelect(gda_656[0], SELECT_BY_TICKET) == TRUE) l_ticket_4 = OrderTicket();
      if (OrderClose(l_ticket_4, OrderLots() * Close_2_P1p / 100.0, Bid, slippage * tpslmult1() * Point, Red) == 1) {
         gi_712 = TRUE;
         Print("Order Partially Closed on " + Symbol());
         gda_656[0] = LastTicket();
      }
   }
   if (USE_PARTIAL_CLOSE == TRUE && gda_688[0] != 0.0 && gda_692[0] == 0.0 && gi_716 == FALSE && Bid <= gda_664[0] - Point * SL_1_P1) {
      if (OrderSelect(gda_656[0], SELECT_BY_TICKET) == TRUE) l_ticket_8 = OrderTicket();
      if (OrderClose(l_ticket_8, OrderLots() * Close_1_P1m / 100.0, Bid, slippage * tpslmult1() * Point, Red) == 1) {
         gi_716 = TRUE;
         Print("Order Partially Closed on " + Symbol());
         gda_656[0] = LastTicket();
      }
   }
   if (USE_PARTIAL_CLOSE == TRUE && gda_688[0] != 0.0 && gda_692[0] == 0.0 && gi_720 == FALSE && Bid <= gda_664[0] - Point * SL_2_P1) {
      if (OrderSelect(gda_656[0], SELECT_BY_TICKET) == TRUE) l_ticket_12 = OrderTicket();
      if (OrderClose(l_ticket_12, OrderLots() * Close_2_P1m / 100.0, Bid, slippage * tpslmult1() * Point, Red) == 1) {
         gi_720 = TRUE;
         Print("Order Partially Closed on " + Symbol());
         gda_656[0] = LastTicket();
      }
   }
   if (USE_PARTIAL_CLOSE == TRUE && OpenTradesBuy() != 0 && gda_688[0] != 0.0 && gda_692[0] == 1.0 && gi_788 == FALSE && Bid >= gda_664[0] + Point * TP_1_M1) {
      if (OrderSelect(gda_656[0], SELECT_BY_TICKET) == TRUE) l_ticket_16 = OrderTicket();
      if (OrderClose(l_ticket_16, OrderLots() * Close_1_M1p / 100.0, Bid, slippage * tpslmult1() * Point, Red) == 1) {
         gi_788 = TRUE;
         Print("Order Partially Closed on " + Symbol());
         gda_656[0] = LastTicket();
      }
   }
   if (USE_PARTIAL_CLOSE == TRUE && OpenTradesBuy() != 0 && gda_688[0] != 0.0 && gda_692[0] == 1.0 && gi_792 == FALSE && Bid >= gda_664[0] + Point * TP_2_M1) {
      if (OrderSelect(gda_656[0], SELECT_BY_TICKET) == TRUE) l_ticket_20 = OrderTicket();
      if (OrderClose(l_ticket_20, OrderLots() * Close_2_M1p / 100.0, Bid, slippage * tpslmult1() * Point, Red) == 1) {
         gi_792 = TRUE;
         Print("Order Partially Closed on " + Symbol());
         gda_656[0] = LastTicket();
      }
   }
   if (USE_PARTIAL_CLOSE == TRUE && OpenTradesBuy() != 0 && gda_688[0] != 0.0 && gda_692[0] == 1.0 && gi_796 == FALSE && Bid <= gda_664[0] - Point * SL_1_M1) {
      if (OrderSelect(gda_656[0], SELECT_BY_TICKET) == TRUE) l_ticket_24 = OrderTicket();
      if (OrderClose(l_ticket_24, OrderLots() * Close_1_M1m / 100.0, Bid, slippage * tpslmult1() * Point, Red) == 1) {
         gi_796 = TRUE;
         Print("Order Partially Closed on " + Symbol());
         gda_656[0] = LastTicket();
      }
   }
   if (USE_PARTIAL_CLOSE == TRUE && OpenTradesBuy() != 0 && gda_688[0] != 0.0 && gda_692[0] == 1.0 && gi_800 == FALSE && Bid <= gda_664[0] - Point * SL_2_M1) {
      if (OrderSelect(gda_656[0], SELECT_BY_TICKET) == TRUE) l_ticket_28 = OrderTicket();
      if (OrderClose(l_ticket_28, OrderLots() * Close_2_M1m / 100.0, Bid, slippage * tpslmult1() * Point, Red) == 1) {
         gi_800 = TRUE;
         Print("Order Partially Closed on " + Symbol());
         gda_656[0] = LastTicket();
      }
   }
   if (USE_PARTIAL_CLOSE == TRUE && gda_688[0] != 0.0 && gda_692[0] == 1.0 && gi_708 == FALSE && Ask <= gda_668[0] - Point * TP_1_P1) {
      if (OrderSelect(gda_660[0], SELECT_BY_TICKET) == TRUE) l_ticket_32 = OrderTicket();
      if (OrderClose(l_ticket_32, OrderLots() * Close_1_P1p / 100.0, Ask, slippage * tpslmult1() * Point, Red) == 1) {
         gi_708 = TRUE;
         Print("Order Partially Closed on " + Symbol());
         gda_660[0] = LastTicket();
      }
   }
   if (USE_PARTIAL_CLOSE == TRUE && gda_688[0] != 0.0 && gda_692[0] == 1.0 && gi_712 == FALSE && Ask <= gda_668[0] - Point * TP_2_P1) {
      if (OrderSelect(gda_660[0], SELECT_BY_TICKET) == TRUE) l_ticket_36 = OrderTicket();
      if (OrderClose(l_ticket_36, OrderLots() * Close_2_P1p / 100.0, Ask, slippage * tpslmult1() * Point, Red) == 1) {
         gi_712 = TRUE;
         Print("Order Partially Closed on " + Symbol());
         gda_660[0] = LastTicket();
      }
   }
   if (USE_PARTIAL_CLOSE == TRUE && gda_688[0] != 0.0 && gda_692[0] == 1.0 && gi_716 == FALSE && Ask >= gda_668[0] + Point * SL_1_P1) {
      if (OrderSelect(gda_660[0], SELECT_BY_TICKET) == TRUE) l_ticket_40 = OrderTicket();
      if (OrderClose(l_ticket_40, OrderLots() * Close_1_P1m / 100.0, Ask, slippage * tpslmult1() * Point, Red) == 1) {
         gi_716 = TRUE;
         Print("Order Partially Closed on " + Symbol());
         gda_660[0] = LastTicket();
      }
   }
   if (USE_PARTIAL_CLOSE == TRUE && gda_688[0] != 0.0 && gda_692[0] == 1.0 && gi_720 == FALSE && Ask >= gda_668[0] + Point * SL_2_P1) {
      if (OrderSelect(gda_660[0], SELECT_BY_TICKET) == TRUE) l_ticket_44 = OrderTicket();
      if (OrderClose(l_ticket_44, OrderLots() * Close_2_P1m / 100.0, Ask, slippage * tpslmult1() * Point, Red) == 1) {
         gi_720 = TRUE;
         Print("Order Partially Closed on " + Symbol());
         gda_660[0] = LastTicket();
      }
   }
   if (USE_PARTIAL_CLOSE == TRUE && OpenTradesSell() != 0 && gda_688[0] != 0.0 && gda_692[0] == 0.0 && gi_788 == FALSE && Ask <= gda_668[0] - Point * TP_1_M1) {
      if (OrderSelect(gda_660[0], SELECT_BY_TICKET) == TRUE) l_ticket_48 = OrderTicket();
      if (OrderClose(l_ticket_48, OrderLots() * Close_1_M1p / 100.0, Ask, slippage * tpslmult1() * Point, Red) == 1) {
         gi_788 = TRUE;
         Print("Order Partially Closed on " + Symbol());
         gda_660[0] = LastTicket();
      }
   }
   if (USE_PARTIAL_CLOSE == TRUE && OpenTradesSell() != 0 && gda_688[0] != 0.0 && gda_692[0] == 0.0 && gi_792 == FALSE && Ask <= gda_668[0] - Point * TP_2_M1) {
      if (OrderSelect(gda_660[0], SELECT_BY_TICKET) == TRUE) l_ticket_52 = OrderTicket();
      if (OrderClose(l_ticket_52, OrderLots() * Close_2_M1p / 100.0, Ask, slippage * tpslmult1() * Point, Red) == 1) {
         gi_792 = TRUE;
         Print("Order Partially Closed on " + Symbol());
         gda_660[0] = LastTicket();
      }
   }
   if (USE_PARTIAL_CLOSE == TRUE && OpenTradesSell() != 0 && gda_688[0] != 0.0 && gda_692[0] == 0.0 && gi_796 == FALSE && Ask >= gda_668[0] + Point * SL_1_M1) {
      if (OrderSelect(gda_660[0], SELECT_BY_TICKET) == TRUE) l_ticket_56 = OrderTicket();
      if (OrderClose(l_ticket_56, OrderLots() * Close_1_M1m / 100.0, Ask, slippage * tpslmult1() * Point, Red) == 1) {
         gi_796 = TRUE;
         Print("Order Partially Closed on " + Symbol());
         gda_660[0] = LastTicket();
      }
   }
   if (USE_PARTIAL_CLOSE == TRUE && OpenTradesSell() != 0 && gda_688[0] != 0.0 && gda_692[0] == 0.0 && gi_800 == FALSE && Ask >= gda_668[0] + Point * SL_2_M1) {
      if (OrderSelect(gda_660[0], SELECT_BY_TICKET) == TRUE) l_ticket_60 = OrderTicket();
      if (OrderClose(l_ticket_60, OrderLots() * Close_2_M1m / 100.0, Ask, slippage * tpslmult1() * Point, Red) == 1) {
         gi_800 = TRUE;
         Print("Order Partially Closed on " + Symbol());
         gda_660[0] = LastTicket();
      }
   }
   SetStartIndicatorSell(0);
   SetStartIndicatorBuy(0);
   SetStartIndicatorCloseAll(0);
   if (DetectUserActionSell(g_name_584, g_name_592) == 1) ForcedStartSell();
   if (DetectUserActionBuy(g_name_624, g_name_632) == 1) ForcedStartBuy();
    if (Level == "Above Buy Level") ForcedLevelBuy();
   if (Level == "Below Sell Level") ForcedLevelSell();
        
   if (DetectUserActionCloseAll(g_name_109, g_name_110) == 1) {Closeall();
  
   if (Level == "Above Buy Level") ForcedLevelBuy();
   if (Level == "Below Sell Level") ForcedLevelSell();
        
      ResetArrays();
      gi_696 = 0;
      gi_700 = 0;
      gi_704 = 0;
      SetBoolsBack();}
   string ls_64 = "" 
      + "\n" 
      + "GRID EA" 
      + "\n" 
      + "\nUse Buy Level = " + Use_Buy_Level
      + "\nBuy Level = " + Buy_Level
      + "\nUse Sell Level = " + Use_Sell_Level
      + "\nSell Level = " + Sell_Level
      + "\n" + Level 
      + "\n"
      + "StartPrice = " + StartPrice() 
      + "\n" 
      + "_StartPrice[0] = " + gda_688[0] 
      + "\n" 
      + "_StartType[0] = " + gda_692[0] 
      + "\n" 
      + DoubleToStr(StartPrice() + StepP1 * Point, Digits) 
      + "\n" 
      + "LastBuyPrice = " + DoubleToStr(LastBuyPrice(), Digits) 
      + "\n" 
      + "LastSellPrice = " + DoubleToStr(LastSellPrice(), Digits) 
      + "\n" 
      + "Lastopentime = " + lastopentime() 
      + "\n" 
      + "\n" 
      + "BID   = " + Bid 
      + "\n" 
      + "LONGTP = " + LongTP() + "     TPLONG[0] = " + gda_676[0] 
      + "\n" 
      + "LONGSL = " + LongSL() 
      + "\n" 
      + "SHORTTP = " + ShortTP() + "       TPSHORT[0] = " + gda_684[0] 
      + "\n" 
      + "SHORTSL = " + ShortSL() 
      + "\n" 
      + "StartPrice = " + StartPrice() 
      + "\n" 
      + "\n" 
   + "sp = " + LastType();
   Comment(ls_64);
   ObjectCreate("1", OBJ_LABEL, g_window_572, 0, 0);
   ObjectSet("1", OBJPROP_COLOR, Red);
   ObjectSet("1", OBJPROP_XDISTANCE, 200);
   ObjectSet("1", OBJPROP_YDISTANCE, 10);
   ObjectSetText("1", "STATUS =" + DoubleToStr(totalprofit() / tpslmult1(), Digits) + " " + AccountCurrency(), 10, "Calibri", col());
   ObjectCreate("2", OBJ_LABEL, g_window_572, 0, 0);
   ObjectSet("2", OBJPROP_COLOR, Red);
   ObjectSet("2", OBJPROP_XDISTANCE, 200);
   ObjectSet("2", OBJPROP_YDISTANCE, 40);
   ObjectSetText("2", main(), 8, "Tahoma", DarkSlateGray);
   ObjectCreate("3", OBJ_LABEL, g_window_572, 0, 0);
   ObjectSet("3", OBJPROP_COLOR, Red);
   ObjectSet("3", OBJPROP_XDISTANCE, 200);
   ObjectSet("3", OBJPROP_YDISTANCE, 60);
   ObjectSetText("3", main1(), 8, "Tahoma", DarkSlateGray);
   if (LongSL() != 0.0) ObjectCreate(DoubleToStr(LongSL(), Digits), OBJ_HLINE, 0, 0, NormalizeDouble(LongSL(), Digits));
   ObjectSet(DoubleToStr(LongSL(), Digits), OBJPROP_COLOR, Red);
   ObjectSet(DoubleToStr(LongSL(), Digits), OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet(DoubleToStr(LongSL(), Digits), OBJPROP_WIDTH, 1);
   if (gda_676[0] != 0.0) ObjectCreate(DoubleToStr(gda_676[0], Digits), OBJ_HLINE, 0, 0, NormalizeDouble(gda_676[0], Digits));
   ObjectSet(DoubleToStr(gda_676[0], Digits), OBJPROP_COLOR, Lime);
   ObjectSet(DoubleToStr(gda_676[0], Digits), OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet(DoubleToStr(gda_676[0], Digits), OBJPROP_WIDTH, 3);
   if (ShortSL() != 0.0) ObjectCreate(DoubleToStr(ShortSL(), Digits), OBJ_HLINE, 0, 0, NormalizeDouble(ShortSL(), Digits));
   ObjectSet(DoubleToStr(ShortSL(), Digits), OBJPROP_COLOR, Red);
   ObjectSet(DoubleToStr(ShortSL(), Digits), OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet(DoubleToStr(ShortSL(), Digits), OBJPROP_WIDTH, 1);
   if (gda_684[0] != 0.0) ObjectCreate(DoubleToStr(gda_684[0], Digits), OBJ_HLINE, 0, 0, NormalizeDouble(gda_684[0], Digits));
   ObjectSet(DoubleToStr(gda_684[0], Digits), OBJPROP_COLOR, Lime);
   ObjectSet(DoubleToStr(gda_684[0], Digits), OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet(DoubleToStr(gda_684[0], Digits), OBJPROP_WIDTH, 3);
   if (Bid >= NormalizeDouble(ShortSL(), Digits) && ShortSL() != 0.0) {
      Closeall();
      ResetArrays();
      gi_696 = 0;
      gi_700 = 0;
      gi_704 = 0;
      SetBoolsBack();
   }
   if (Bid <= NormalizeDouble(gda_684[0], Digits) && gda_684[0] != 0.0) {
      Closeall();
      ResetArrays();
      gi_696 = 0;
      gi_700 = 0;
      gi_704 = 0;
      SetBoolsBack();
   }
   if (Bid <= NormalizeDouble(LongSL(), Digits) && LongSL() != 0.0) {
      Closeall();
      ResetArrays();
      gi_696 = 0;
      gi_700 = 0;
      gi_704 = 0;
      SetBoolsBack();
   }
   if (Bid >= NormalizeDouble(gda_676[0], Digits) && gda_676[0] != 0.0) {
      Closeall();
      ResetArrays();
      gi_696 = 0;
      gi_700 = 0;
      gi_704 = 0;
      SetBoolsBack();
   }
   if (Open2Buy() == 1) OpenBuy2x();
   if (Open2Sell() == 1) OpenSell2x();
   if (Open3Buy() == 1) OpenBuy3x();
   if (Open3Sell() == 1) OpenSell3x();
   if (Open4Buy() == 1) OpenBuy4x();
   if (Open4Sell() == 1) OpenSell4x();
   if (Open5Buy() == 1) OpenBuy5x();
   if (Open5Sell() == 1) OpenSell5x();
   if (Open6Buy() == 1) OpenBuy6x();
   if (Open6Sell() == 1) OpenSell6x();
   if (Open7Buy() == 1) OpenBuy7x();
   if (Open7Sell() == 1) OpenSell7x();
   if (Open8Buy() == 1) OpenBuy8x();
   if (Open8Sell() == 1) OpenSell8x();
   if (Open9Buy() == 1) OpenBuy9x();
   if (Open9Sell() == 1) OpenSell9x();
   if (Open10Buy() == 1) OpenBuy10x();
   if (Open10Sell() == 1) OpenSell10x();
   return (0);
}

string main() {
   string ls_ret_0 = "";
   if (OpenTradesBuy() + OpenTradesSell() == 0) ls_ret_0 = "waiting for a trade to be opened on " + Symbol();
   if (OpenTradesBuy() + OpenTradesSell() > 0) ls_ret_0 = "TRADING!";
   return (ls_ret_0);
}

string main1() {
   string ls_ret_0 = "";
   if (OpenTradesBuy() + OpenTradesSell() == 0) ls_ret_0 = "with MagicNumber: " + MagicNumber;
   return (ls_ret_0);
}

int Open2Buy() {
   bool li_ret_0;
   if (gda_692[0] == 0.0 && TP_Steps > 1 && Ask >= gda_688[0] + StepP1 * Point && OpenTradesBuy() == 1 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   if (gda_692[0] == 1.0 && Ask >= gda_688[0] + StepM1 * Point && OpenTradesBuy() == 0 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   return (li_ret_0);
}

int Open2Sell() {
   bool li_ret_0;
   if (gda_692[0] == 0.0 && Bid <= gda_688[0] - StepM1 * Point && OpenTradesSell() == 0 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   if (gda_692[0] == 1.0 && TP_Steps > 1 && Bid <= gda_688[0] - StepP1 * Point && OpenTradesSell() == 1 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   return (li_ret_0);
}

int Open3Buy() {
   bool li_ret_0;
   if (gda_692[0] == 0.0 && TP_Steps > 2 && Ask >= gda_688[0] + (StepP1 + StepP2) * Point && OpenTradesBuy() == 2 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   if (gda_692[0] == 1.0 && Ask >= gda_688[0] + (StepM1 + StepM2) * Point && OpenTradesBuy() == 1 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   return (li_ret_0);
}

int Open3Sell() {
   bool li_ret_0;
   if (gda_692[0] == 0.0 && Bid <= gda_688[0] - (StepM1 + StepM2) * Point && OpenTradesSell() == 1 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   if (gda_692[0] == 1.0 && TP_Steps > 2 && Bid <= gda_688[0] - (StepP1 + StepP2) * Point && OpenTradesSell() == 2 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   return (li_ret_0);
}

int Open4Buy() {
   bool li_ret_0;
   if (gda_692[0] == 0.0 && TP_Steps > 3 && Ask >= gda_688[0] + (StepP1 + StepP2 + StepP3) * Point && OpenTradesBuy() == 3 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   if (gda_692[0] == 1.0 && Ask >= gda_688[0] + (StepM1 + StepM2 + StepM3) * Point && OpenTradesBuy() == 2 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   return (li_ret_0);
}

int Open4Sell() {
   bool li_ret_0;
   if (gda_692[0] == 0.0 && Bid <= gda_688[0] - (StepM1 + StepM2 + StepM3) * Point && OpenTradesSell() == 2 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   if (gda_692[0] == 1.0 && TP_Steps > 3 && Bid <= gda_688[0] - (StepP1 + StepP2 + StepP3) * Point && OpenTradesSell() == 3 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   return (li_ret_0);
}

int Open5Buy() {
   bool li_ret_0;
   if (gda_692[0] == 0.0 && TP_Steps > 4 && Ask >= gda_688[0] + (StepP1 + StepP2 + StepP3 + StepP4) * Point && OpenTradesBuy() == 4 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   if (gda_692[0] == 1.0 && Ask >= gda_688[0] + (StepM1 + StepM2 + StepM3 + StepM4) * Point && OpenTradesBuy() == 3 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   return (li_ret_0);
}

int Open5Sell() {
   bool li_ret_0;
   if (gda_692[0] == 0.0 && Bid <= gda_688[0] - (StepM1 + StepM2 + StepM3 + StepM4) * Point && OpenTradesSell() == 3 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   if (gda_692[0] == 1.0 && TP_Steps > 4 && Bid <= gda_688[0] - (StepP1 + StepP2 + StepP3 + StepP4) * Point && OpenTradesSell() == 4 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   return (li_ret_0);
}

int Open6Buy() {
   bool li_ret_0;
   if (gda_692[0] == 0.0 && TP_Steps > 5 && Ask >= gda_688[0] + (StepP1 + StepP2 + StepP3 + StepP4 + StepP5) * Point && OpenTradesBuy() == 5 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   if (gda_692[0] == 1.0 && Ask >= gda_688[0] + (StepM1 + StepM2 + StepM3 + StepM4 + StepM5) * Point && OpenTradesBuy() == 4 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   return (li_ret_0);
}

int Open6Sell() {
   bool li_ret_0;
   if (gda_692[0] == 0.0 && Bid <= gda_688[0] - (StepM1 + StepM2 + StepM3 + StepM4 + StepM5) * Point && OpenTradesSell() == 4 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   if (gda_692[0] == 1.0 && TP_Steps > 5 && Bid <= gda_688[0] - (StepP1 + StepP2 + StepP3 + StepP4 + StepP5) * Point && OpenTradesSell() == 5 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   return (li_ret_0);
}

int Open7Buy() {
   bool li_ret_0;
   if (gda_692[0] == 0.0 && TP_Steps > 6 && Ask >= gda_688[0] + (StepP1 + StepP2 + StepP3 + StepP4 + StepP5 + StepP6) * Point && OpenTradesBuy() == 6 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   if (gda_692[0] == 1.0 && Ask >= gda_688[0] + (StepM1 + StepM2 + StepM3 + StepM4 + StepM5 + StepM6) * Point && OpenTradesBuy() == 5 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   return (li_ret_0);
}

int Open7Sell() {
   bool li_ret_0;
   if (gda_692[0] == 0.0 && Bid <= gda_688[0] - (StepM1 + StepM2 + StepM3 + StepM4 + StepM5 + StepM6) * Point && OpenTradesSell() == 5 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   if (gda_692[0] == 1.0 && TP_Steps > 6 && Bid <= gda_688[0] - (StepP1 + StepP2 + StepP3 + StepP4 + StepP5 + StepP6) * Point && OpenTradesSell() == 6 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   return (li_ret_0);
}

int Open8Buy() {
   bool li_ret_0;
   if (gda_692[0] == 0.0 && TP_Steps > 7 && Ask >= gda_688[0] + (StepP1 + StepP2 + StepP3 + StepP4 + StepP5 + StepP6 + StepP7) * Point && OpenTradesBuy() == 7 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   if (gda_692[0] == 1.0 && Ask >= gda_688[0] + (StepM1 + StepM2 + StepM3 + StepM4 + StepM5 + StepM6 + StepM7) * Point && OpenTradesBuy() == 6 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   return (li_ret_0);
}

int Open8Sell() {
   bool li_ret_0;
   if (gda_692[0] == 0.0 && Bid <= gda_688[0] - (StepM1 + StepM2 + StepM3 + StepM4 + StepM5 + StepM6 + StepM7) * Point && OpenTradesSell() == 6 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   if (gda_692[0] == 1.0 && TP_Steps > 7 && Bid <= gda_688[0] - (StepP1 + StepP2 + StepP3 + StepP4 + StepP5 + StepP6 + StepP7) * Point && OpenTradesSell() == 7 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   return (li_ret_0);
}

int Open9Buy() {
   bool li_ret_0;
   if (gda_692[0] == 0.0 && TP_Steps > 8 && Ask >= gda_688[0] + (StepP1 + StepP2 + StepP3 + StepP4 + StepP5 + StepP6 + StepP7 + StepP8) * Point && OpenTradesBuy() == 8 &&
      gda_688[0] != 0.0) li_ret_0 = TRUE;
   if (gda_692[0] == 1.0 && Ask >= gda_688[0] + (StepM1 + StepM2 + StepM3 + StepM4 + StepM5 + StepM6 + StepM7 + StepM8) * Point && OpenTradesBuy() == 7 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   return (li_ret_0);
}

int Open9Sell() {
   bool li_ret_0;
   if (gda_692[0] == 0.0 && Bid <= gda_688[0] - (StepM1 + StepM2 + StepM3 + StepM4 + StepM5 + StepM6 + StepM7 + StepM8) * Point && OpenTradesSell() == 7 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   if (gda_692[0] == 1.0 && TP_Steps > 8 && Bid <= gda_688[0] - (StepP1 + StepP2 + StepP3 + StepP4 + StepP5 + StepP6 + StepP7 + StepP8) * Point && OpenTradesSell() == 8 &&
      gda_688[0] != 0.0) li_ret_0 = TRUE;
   return (li_ret_0);
}

int Open10Buy() {
   bool li_ret_0;
   if (gda_692[0] == 0.0 && TP_Steps > 9 && Ask >= gda_688[0] + (StepP1 + StepP2 + StepP3 + StepP4 + StepP5 + StepP6 + StepP7 + StepP8 + StepP9) * Point && OpenTradesBuy() == 9 &&
      gda_688[0] != 0.0) li_ret_0 = TRUE;
   if (gda_692[0] == 1.0 && Ask >= gda_688[0] + (StepM1 + StepM2 + StepM3 + StepM4 + StepM5 + StepM6 + StepM7 + StepM8 + StepM9) * Point && OpenTradesBuy() == 8 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   return (li_ret_0);
}

int Open10Sell() {
   bool li_ret_0;
   if (gda_692[0] == 0.0 && Bid <= gda_688[0] - (StepM1 + StepM2 + StepM3 + StepM4 + StepM5 + StepM6 + StepM7 + StepM8 + StepM9) * Point && OpenTradesSell() == 8 && gda_688[0] != 0.0) li_ret_0 = TRUE;
   if (gda_692[0] == 1.0 && TP_Steps > 9 && Bid <= gda_688[0] - (StepP1 + StepP2 + StepP3 + StepP4 + StepP5 + StepP6 + StepP7 + StepP8 + StepP9) * Point && OpenTradesSell() == 9 &&
      gda_688[0] != 0.0) li_ret_0 = TRUE;
   return (li_ret_0);
}

void OpenBuy2x() {
   if (gda_692[0] == 0.0) OrderSend(Symbol(), OP_BUY, LotP2, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);
   if (gda_692[0] == 1.0) OrderSend(Symbol(), OP_BUY, LotM2, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);
}

void OpenSell2x() {
   if (gda_692[0] == 0.0) OrderSend(Symbol(), OP_SELL, LotM2, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);
   if (gda_692[0] == 1.0) OrderSend(Symbol(), OP_SELL, LotP2, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);
}

void OpenBuy3x() {
   if (gda_692[0] == 0.0) OrderSend(Symbol(), OP_BUY, LotP3, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);
   if (gda_692[0] == 1.0) OrderSend(Symbol(), OP_BUY, LotM3, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);
}

void OpenSell3x() {
   if (gda_692[0] == 0.0) OrderSend(Symbol(), OP_SELL, LotM3, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);
   if (gda_692[0] == 1.0) OrderSend(Symbol(), OP_SELL, LotP3, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);
}

void OpenBuy4x() {
   if (gda_692[0] == 0.0) OrderSend(Symbol(), OP_BUY, LotP4, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);
   if (gda_692[0] == 1.0) OrderSend(Symbol(), OP_BUY, LotM4, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);
}

void OpenSell4x() {
   if (gda_692[0] == 0.0) OrderSend(Symbol(), OP_SELL, LotM4, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);
   if (gda_692[0] == 1.0) OrderSend(Symbol(), OP_SELL, LotP4, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);
}

void OpenBuy5x() {
   if (gda_692[0] == 0.0) OrderSend(Symbol(), OP_BUY, LotP5, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);
   if (gda_692[0] == 1.0) OrderSend(Symbol(), OP_BUY, LotM5, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);
}

void OpenSell5x() {
   if (gda_692[0] == 0.0) OrderSend(Symbol(), OP_SELL, LotM5, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);
   if (gda_692[0] == 1.0) OrderSend(Symbol(), OP_SELL, LotP5, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);
}

void OpenBuy6x() {
   if (gda_692[0] == 0.0) OrderSend(Symbol(), OP_BUY, LotP6, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);
   if (gda_692[0] == 1.0) OrderSend(Symbol(), OP_BUY, LotM6, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);
}

void OpenSell6x() {
   if (gda_692[0] == 0.0) OrderSend(Symbol(), OP_SELL, LotM6, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);
   if (gda_692[0] == 1.0) OrderSend(Symbol(), OP_SELL, LotP6, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);
}

void OpenBuy7x() {
   if (gda_692[0] == 0.0) OrderSend(Symbol(), OP_BUY, LotP7, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);
   if (gda_692[0] == 1.0) OrderSend(Symbol(), OP_BUY, LotM7, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);
}

void OpenSell7x() {
   if (gda_692[0] == 0.0) OrderSend(Symbol(), OP_SELL, LotM7, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);
   if (gda_692[0] == 1.0) OrderSend(Symbol(), OP_SELL, LotP7, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);
}

void OpenBuy8x() {
   if (gda_692[0] == 0.0) OrderSend(Symbol(), OP_BUY, LotP8, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);
   if (gda_692[0] == 1.0) OrderSend(Symbol(), OP_BUY, LotM8, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);
}

void OpenSell8x() {
   if (gda_692[0] == 0.0) OrderSend(Symbol(), OP_SELL, LotM8, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);
   if (gda_692[0] == 1.0) OrderSend(Symbol(), OP_SELL, LotP8, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);
}

void OpenBuy9x() {
   if (gda_692[0] == 0.0) OrderSend(Symbol(), OP_BUY, LotP9, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);
   if (gda_692[0] == 1.0) OrderSend(Symbol(), OP_BUY, LotM9, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);
}

void OpenSell9x() {
   if (gda_692[0] == 0.0) OrderSend(Symbol(), OP_SELL, LotM9, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);
   if (gda_692[0] == 1.0) OrderSend(Symbol(), OP_SELL, LotP9, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);
}

void OpenBuy10x() {
   if (gda_692[0] == 0.0) OrderSend(Symbol(), OP_BUY, LotP10, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);
   if (gda_692[0] == 1.0) OrderSend(Symbol(), OP_BUY, LotM10, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);
}

void OpenSell10x() {
   if (gda_692[0] == 0.0) OrderSend(Symbol(), OP_SELL, LotM10, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);
   if (gda_692[0] == 1.0) OrderSend(Symbol(), OP_SELL, LotP10, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);
}

double LongTP() {
   double ld_ret_0 = 0;
   if (gda_692[0] == 0.0 && TP_Steps == 1) ld_ret_0 = StartPrice() + StepP1 * Point;
   if (gda_692[0] == 0.0 && TP_Steps == 2) ld_ret_0 = StartPrice() + (StepP1 + StepP2) * Point;
   if (gda_692[0] == 0.0 && TP_Steps == 3) ld_ret_0 = StartPrice() + (StepP1 + StepP2 + StepP3) * Point;
   if (gda_692[0] == 0.0 && TP_Steps == 4) ld_ret_0 = StartPrice() + (StepP1 + StepP2 + StepP3 + StepP4) * Point;
   if (gda_692[0] == 0.0 && TP_Steps == 5) ld_ret_0 = StartPrice() + (StepP1 + StepP2 + StepP3 + StepP4 + StepP5) * Point;
   if (gda_692[0] == 0.0 && TP_Steps == 6) ld_ret_0 = StartPrice() + (StepP1 + StepP2 + StepP3 + StepP4 + StepP5 + StepP6) * Point;
   if (gda_692[0] == 0.0 && TP_Steps == 7) ld_ret_0 = StartPrice() + (StepP1 + StepP2 + StepP3 + StepP4 + StepP5 + StepP6 + StepP7) * Point;
   if (gda_692[0] == 0.0 && TP_Steps == 8) ld_ret_0 = StartPrice() + (StepP1 + StepP2 + StepP3 + StepP4 + StepP5 + StepP6 + StepP7 + StepP8) * Point;
   if (gda_692[0] == 0.0 && TP_Steps == 9) ld_ret_0 = StartPrice() + (StepP1 + StepP2 + StepP3 + StepP4 + StepP5 + StepP6 + StepP7 + StepP8 + StepP9) * Point;
   if (gda_692[0] == 0.0 && TP_Steps == 10) ld_ret_0 = StartPrice() + (StepP1 + StepP2 + StepP3 + StepP4 + StepP5 + StepP6 + StepP7 + StepP8 + StepP9 + StepP10) * Point;
   return (ld_ret_0);
}

double LongSL() {
   double ld_ret_0 = 0;
   if (OpenTradesBuy() == 1 && LastBuyPrice() - SL_P1 * Point >= 0.0) ld_ret_0 = LastBuyPrice() - SL_P1 * Point;
   if (OpenTradesBuy() == 2 && LastBuyPrice() - SL_P2 * Point >= 0.0) ld_ret_0 = LastBuyPrice() - SL_P2 * Point;
   if (OpenTradesBuy() == 3 && LastBuyPrice() - SL_P3 * Point >= 0.0) ld_ret_0 = LastBuyPrice() - SL_P3 * Point;
   if (OpenTradesBuy() == 4 && LastBuyPrice() - SL_P4 * Point >= 0.0) ld_ret_0 = LastBuyPrice() - SL_P4 * Point;
   if (OpenTradesBuy() == 5 && LastBuyPrice() - SL_P5 * Point >= 0.0) ld_ret_0 = LastBuyPrice() - SL_P5 * Point;
   if (OpenTradesBuy() == 6 && LastBuyPrice() - SL_P6 * Point >= 0.0) ld_ret_0 = LastBuyPrice() - SL_P6 * Point;
   if (OpenTradesBuy() == 7 && LastBuyPrice() - SL_P7 * Point >= 0.0) ld_ret_0 = LastBuyPrice() - SL_P7 * Point;
   if (OpenTradesBuy() == 8 && LastBuyPrice() - SL_P8 * Point >= 0.0) ld_ret_0 = LastBuyPrice() - SL_P8 * Point;
   if (OpenTradesBuy() == 9 && LastBuyPrice() - SL_P9 * Point >= 0.0) ld_ret_0 = LastBuyPrice() - SL_P9 * Point;
   if (OpenTradesBuy() == 10 && LastBuyPrice() - SL_P10 * Point >= 0.0) ld_ret_0 = LastBuyPrice() - SL_P10 * Point;
   return (ld_ret_0);
}

double ShortTP() {
   double ld_ret_0 = 0;
   if (gda_692[0] == 1.0 && TP_Steps == 1) ld_ret_0 = StartPrice() - StepM1 * Point;
   if (gda_692[0] == 1.0 && TP_Steps == 2) ld_ret_0 = StartPrice() - (StepM1 + StepM2) * Point;
   if (gda_692[0] == 1.0 && TP_Steps == 3) ld_ret_0 = StartPrice() - (StepM1 + StepM2 + StepM3) * Point;
   if (gda_692[0] == 1.0 && TP_Steps == 4) ld_ret_0 = StartPrice() - (StepM1 + StepM2 + StepM3 + StepM4) * Point;
   if (gda_692[0] == 1.0 && TP_Steps == 5) ld_ret_0 = StartPrice() - (StepM1 + StepM2 + StepM3 + StepM4 + StepM5) * Point;
   if (gda_692[0] == 1.0 && TP_Steps == 6) ld_ret_0 = StartPrice() - (StepM1 + StepM2 + StepM3 + StepM4 + StepM5 + StepM6) * Point;
   if (gda_692[0] == 1.0 && TP_Steps == 7) ld_ret_0 = StartPrice() - (StepM1 + StepM2 + StepM3 + StepM4 + StepM5 + StepM6 + StepM7) * Point;
   if (gda_692[0] == 1.0 && TP_Steps == 8) ld_ret_0 = StartPrice() - (StepM1 + StepM2 + StepM3 + StepM4 + StepM5 + StepM6 + StepM7 + StepM8) * Point;
   if (gda_692[0] == 1.0 && TP_Steps == 9) ld_ret_0 = StartPrice() - (StepM1 + StepM2 + StepM3 + StepM4 + StepM5 + StepM6 + StepM7 + StepM8 + StepM9) * Point;
   if (gda_692[0] == 1.0 && TP_Steps == 10) ld_ret_0 = StartPrice() - (StepM1 + StepM2 + StepM3 + StepM4 + StepM5 + StepM6 + StepM7 + StepM8 + StepM9 + StepM10) * Point;
   return (ld_ret_0);
}

double ShortSL() {
   double ld_ret_0 = 0;
   if (OpenTradesSell() == 1 && LastSellPrice() - SL_P1 * Point > 0.0) ld_ret_0 = LastSellPrice() + SL_M1 * Point;
   if (OpenTradesSell() == 2 && LastSellPrice() - SL_P2 * Point > 0.0) ld_ret_0 = LastSellPrice() + SL_M2 * Point;
   if (OpenTradesSell() == 3 && LastSellPrice() - SL_P3 * Point > 0.0) ld_ret_0 = LastSellPrice() + SL_M3 * Point;
   if (OpenTradesSell() == 4 && LastSellPrice() - SL_P4 * Point > 0.0) ld_ret_0 = LastSellPrice() + SL_M4 * Point;
   if (OpenTradesSell() == 5 && LastSellPrice() - SL_P5 * Point > 0.0) ld_ret_0 = LastSellPrice() + SL_M5 * Point;
   if (OpenTradesSell() == 6 && LastSellPrice() - SL_P6 * Point > 0.0) ld_ret_0 = LastSellPrice() + SL_M6 * Point;
   if (OpenTradesSell() == 7 && LastSellPrice() - SL_P7 * Point > 0.0) ld_ret_0 = LastSellPrice() + SL_M7 * Point;
   if (OpenTradesSell() == 8 && LastSellPrice() - SL_P8 * Point > 0.0) ld_ret_0 = LastSellPrice() + SL_M8 * Point;
   if (OpenTradesSell() == 9 && LastSellPrice() - SL_P9 * Point > 0.0) ld_ret_0 = LastSellPrice() + SL_M9 * Point;
   if (OpenTradesSell() == 10 && LastSellPrice() - SL_P10 * Point > 0.0) ld_ret_0 = LastSellPrice() + SL_M10 * Point;
   return (ld_ret_0);
}

void Closeall() {
   int l_cmd_8;
   bool l_ord_close_12;
   int l_ord_total_0 = OrdersTotal();
   for (int l_pos_4 = l_ord_total_0 - 1; l_pos_4 >= 0; l_pos_4--) {
      OrderSelect(l_pos_4, SELECT_BY_POS);
      l_cmd_8 = OrderType();
      l_ord_close_12 = FALSE;
      switch (l_cmd_8) {
      case OP_BUY:
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) l_ord_close_12 = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), slippage, Red);
      case OP_SELL:
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) l_ord_close_12 = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), slippage, Red);
      }
      if (l_ord_close_12 == 0) {
      }
   }
   ObjectsDeleteAll(0, OBJ_HLINE);
   Print("Set is closed on " + Symbol());
   Sleep(3000);
}

double StartPrice() {
   int l_ticket_8;
   double l_ord_open_price_12;
   double ld_0 = D'09.09.2001 03:46:39';
   int li_24 = OrdersTotal() - 1;
   for (int l_pos_20 = li_24; l_pos_20 >= 0; l_pos_20--) {
      OrderSelect(l_pos_20, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() == MagicNumber && OrderTicket() < ld_0 && OrderSymbol() == Symbol()) {
         l_ticket_8 = OrderTicket();
         l_ord_open_price_12 = OrderOpenPrice();
      }
   }
   return (l_ord_open_price_12);
}

int StartType() {
   int l_ticket_8;
   double ld_0 = D'09.09.2001 03:46:39';
   int l_cmd_12 = 2;
   int li_20 = OrdersTotal() - 1;
   for (int l_pos_16 = li_20; l_pos_16 >= 0; l_pos_16--) {
      OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES);
      if (OrderTicket() < ld_0 && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()) {
         l_ticket_8 = OrderTicket();
         l_cmd_12 = OrderType();
      }
   }
   return (l_cmd_12);
}

int lastopentime() {
   int l_datetime_16;
   int l_datetime_0 = 0;
   double l_datetime_4 = 0;
   for (int l_pos_12 = OrdersTotal() - 1; l_pos_12 >= 0; l_pos_12--) {
      OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()) {
         if (OrderMagicNumber() == MagicNumber) {
            l_datetime_16 = OrderOpenTime();
            if (l_datetime_16 > l_datetime_0) {
               l_datetime_0 = l_datetime_16;
               l_datetime_4 = OrderOpenTime();
            }
         }
         return (l_datetime_4);
      }
   }
   return (0);
}

double totalprofit() {
   double ld_ret_0 = 0;
   int l_pos_8 = 0;
   bool li_12 = FALSE;
   while (li_12 == FALSE) {
      if (OrderSelect(l_pos_8, SELECT_BY_POS)) {
         if (OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()) ld_ret_0 += OrderProfit();
         l_pos_8++;
      } else li_12 = TRUE;
   }
   return (ld_ret_0);
}

int col() {
   int li_ret_0 = DarkSlateGray;
   if (totalprofit() > 0.0) li_ret_0 = Green;
   if (totalprofit() < 0.0) li_ret_0 = Crimson;
   return (li_ret_0);
}

int tpslmult1() {
   int li_ret_0;
   if (Digits == 5) li_ret_0 = 10;
   if (Digits == 3) li_ret_0 = 10;
   if (Digits == 4) li_ret_0 = 1;
   if (Digits == 2) li_ret_0 = 1;
   return (li_ret_0);
}

void SetStartIndicatorSell(int ai_0) {
   int l_x_4 = 5;
   int l_y_8 = 15;
   ObjectCreate(g_name_576, OBJ_LABEL, 0, 0, 0, 0, 0, 0, 0);
   ObjectSetText(g_name_576, "Open SELL: ", 8, "Tahoma", DarkSlateGray);
   ObjectSet(g_name_576, OBJPROP_CORNER, 2);
   ObjectSet(g_name_576, OBJPROP_XDISTANCE, l_x_4);
   ObjectSet(g_name_576, OBJPROP_YDISTANCE, l_y_8);
   ObjectCreate(g_name_584, OBJ_LABEL, 0, 0, 0, 0, 0, 0);
   ObjectSetText(g_name_584, "o", 18, "Wingdings", Crimson);
   l_x_4 = 80;
   l_y_8 = 8;
   ObjectSet(g_name_584, OBJPROP_CORNER, 2);
   ObjectSet(g_name_584, OBJPROP_XDISTANCE, l_x_4);
   ObjectSet(g_name_584, OBJPROP_YDISTANCE, l_y_8);
   ObjectCreate(g_name_592, OBJ_LABEL, 0, 0, 0, 0, 0, 0);
   l_y_8 = 10;
   if (ai_0 == 1) l_x_4 = 80;
   else l_x_4 = 108;
   ObjectSetText(g_name_592, "®", 12, "Wingdings", Crimson);
   ObjectSet(g_name_592, OBJPROP_CORNER, 2);
   ObjectSet(g_name_592, OBJPROP_XDISTANCE, l_x_4);
   ObjectSet(g_name_592, OBJPROP_YDISTANCE, l_y_8);
}

void SetStartIndicatorCloseAll(int ai_0) {
   int l_x_4 = 5;
   int l_y_8 = 75;
   ObjectCreate(g_name_108, OBJ_LABEL, 0, 0, 0, 0, 0, 0, 0);
   ObjectSetText(g_name_108, "Close ALL: ", 8, "Tahoma", DarkSlateGray);
   ObjectSet(g_name_108, OBJPROP_CORNER, 2);
   ObjectSet(g_name_108, OBJPROP_XDISTANCE, l_x_4);
   ObjectSet(g_name_108, OBJPROP_YDISTANCE, l_y_8);
   ObjectCreate(g_name_109, OBJ_LABEL, 0, 0, 0, 0, 0, 0);
   ObjectSetText(g_name_109, "o", 18, "Wingdings", Gainsboro);
   l_x_4 = 80;
   l_y_8 = 68;
   ObjectSet(g_name_109, OBJPROP_CORNER, 2);
   ObjectSet(g_name_109, OBJPROP_XDISTANCE, l_x_4);
   ObjectSet(g_name_109, OBJPROP_YDISTANCE, l_y_8);
   ObjectCreate(g_name_110, OBJ_LABEL, 0, 0, 0, 0, 0, 0);
   l_y_8 = 70;
   if (ai_0 == 1) l_x_4 = 80;
   else l_x_4 = 108;
   ObjectSetText(g_name_110, "®", 12, "Wingdings", Gainsboro);
   ObjectSet(g_name_110, OBJPROP_CORNER, 2);
   ObjectSet(g_name_110, OBJPROP_XDISTANCE, l_x_4);
   ObjectSet(g_name_110, OBJPROP_YDISTANCE, l_y_8);
}

int DetectUserActionCloseAll(string a_name_0, string a_name_8) {
   double ld_16;
   double ld_24;
   double ld_32;
   double ld_40;
   double ld_48;
   double ld_56;
   if (ObjectFind(a_name_8) > -1 && ObjectFind(a_name_0) > -1) {
      ld_16 = ObjectGet(a_name_0, OBJPROP_XDISTANCE);
      ld_24 = ld_16 + 25.0;
      ld_32 = ObjectGet(a_name_0, OBJPROP_YDISTANCE);
      ld_40 = ld_32 + 25.0;
      ld_48 = ObjectGet(a_name_8, OBJPROP_XDISTANCE) + 12.0;
      ld_56 = ObjectGet(a_name_8, OBJPROP_YDISTANCE) + 12.0;
      if (ld_16 <= ld_48 && ld_48 <= ld_24 && ld_32 <= ld_56 && ld_56 <= ld_40) return (1);
      return (0);
   }
   SetStartIndicatorCloseAll(0);
   return (0);
}


int DetectUserActionSell(string a_name_0, string a_name_8) {
   double ld_16;
   double ld_24;
   double ld_32;
   double ld_40;
   double ld_48;
   double ld_56;
   if (ObjectFind(a_name_8) > -1 && ObjectFind(a_name_0) > -1) {
      ld_16 = ObjectGet(a_name_0, OBJPROP_XDISTANCE);
      ld_24 = ld_16 + 25.0;
      ld_32 = ObjectGet(a_name_0, OBJPROP_YDISTANCE);
      ld_40 = ld_32 + 25.0;
      ld_48 = ObjectGet(a_name_8, OBJPROP_XDISTANCE) + 12.0;
      ld_56 = ObjectGet(a_name_8, OBJPROP_YDISTANCE) + 12.0;
      if (ld_16 <= ld_48 && ld_48 <= ld_24 && ld_32 <= ld_56 && ld_56 <= ld_40) return (1);
      return (0);
   }
   SetStartIndicatorSell(0);
   return (0);
}

void ForcedStartSell() {
   if (DetectUserActionSell(g_name_584, g_name_592) && OpenTradesBuy() + OpenTradesSell() == 0) OrderSend(Symbol(), OP_SELL, LotP1, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);
    if (OpenTradesBuy() + OpenTradesSell() == 0) OrderSend(Symbol(), OP_SELL, LotP1, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);

}
void ForcedLevelSell() {
  if (OpenTradesBuy() + OpenTradesSell() == 0) OrderSend(Symbol(), OP_SELL, LotP1, Bid, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Red);

}

void SetStartIndicatorBuy(int ai_0) {
   int l_x_4 = 5;
   int l_y_8 = 45;
   ObjectCreate(g_name_616, OBJ_LABEL, 0, 0, 0, 0, 0, 0, 0);
   ObjectSetText(g_name_616, "Open BUY: ", 8, "Tahoma", DarkSlateGray);
   ObjectSet(g_name_616, OBJPROP_CORNER, 2);
   ObjectSet(g_name_616, OBJPROP_XDISTANCE, l_x_4);
   ObjectSet(g_name_616, OBJPROP_YDISTANCE, l_y_8);
   ObjectCreate(g_name_624, OBJ_LABEL, 0, 0, 0, 0, 0, 0);
   ObjectSetText(g_name_624, "o", 18, "Wingdings", MediumSeaGreen);
   l_x_4 = 80;
   l_y_8 = 38;
   ObjectSet(g_name_624, OBJPROP_CORNER, 2);
   ObjectSet(g_name_624, OBJPROP_XDISTANCE, l_x_4);
   ObjectSet(g_name_624, OBJPROP_YDISTANCE, l_y_8);
   ObjectCreate(g_name_632, OBJ_LABEL, 0, 0, 0, 0, 0, 0);
   l_y_8 = 40;
   if (ai_0 == 1) l_x_4 = 80;
   else l_x_4 = 108;
   ObjectSetText(g_name_632, "®", 12, "Wingdings", MediumSeaGreen);
   ObjectSet(g_name_632, OBJPROP_CORNER, 2);
   ObjectSet(g_name_632, OBJPROP_XDISTANCE, l_x_4);
   ObjectSet(g_name_632, OBJPROP_YDISTANCE, l_y_8);
}

int DetectUserActionBuy(string a_name_0, string a_name_8) {
   double ld_16;
   double ld_24;
   double ld_32;
   double ld_40;
   double ld_48;
   double ld_56;
   if (ObjectFind(a_name_8) > -1 && ObjectFind(a_name_0) > -1) {
      ld_16 = ObjectGet(a_name_0, OBJPROP_XDISTANCE);
      ld_24 = ld_16 + 25.0;
      ld_32 = ObjectGet(a_name_0, OBJPROP_YDISTANCE);
      ld_40 = ld_32 + 25.0;
      ld_48 = ObjectGet(a_name_8, OBJPROP_XDISTANCE) + 12.0;
      ld_56 = ObjectGet(a_name_8, OBJPROP_YDISTANCE) + 12.0;
      if (ld_16 <= ld_48 && ld_48 <= ld_24 && ld_32 <= ld_56 && ld_56 <= ld_40) return (1);
      return (0);
   }
   SetStartIndicatorBuy(0);
   return (0);
}

void ForcedStartBuy() {
   if (DetectUserActionBuy(g_name_624, g_name_632) && OpenTradesBuy() + OpenTradesSell() == 0) OrderSend(Symbol(), OP_BUY, LotP1, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);
   if (OpenTradesBuy() + OpenTradesSell() == 0) OrderSend(Symbol(), OP_BUY, LotP1, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);

}
void ForcedLevelBuy() {
    if (OpenTradesBuy() + OpenTradesSell() == 0) OrderSend(Symbol(), OP_BUY, LotP1, Ask, slippage * tpslmult1(), 0, 0, 0, MagicNumber, 0, Blue);

}

void SetBoolsBack() {
   gi_708 = FALSE;
   gi_712 = FALSE;
   gi_716 = FALSE;
   gi_720 = FALSE;
   gi_unused_724 = FALSE;
   gi_unused_728 = FALSE;
   gi_unused_732 = FALSE;
   gi_unused_736 = FALSE;
   gi_unused_740 = FALSE;
   gi_unused_744 = FALSE;
   gi_unused_748 = FALSE;
   gi_unused_752 = FALSE;
   gi_unused_756 = FALSE;
   gi_unused_760 = FALSE;
   gi_unused_764 = FALSE;
   gi_unused_768 = FALSE;
   gi_unused_772 = FALSE;
   gi_unused_776 = FALSE;
   gi_unused_780 = FALSE;
   gi_unused_784 = FALSE;
   gi_788 = FALSE;
   gi_792 = FALSE;
   gi_796 = FALSE;
   gi_800 = FALSE;
   gi_unused_804 = FALSE;
   gi_unused_808 = FALSE;
   gi_unused_812 = FALSE;
   gi_unused_816 = FALSE;
   gi_unused_820 = FALSE;
   gi_unused_824 = FALSE;
   gi_unused_828 = FALSE;
   gi_unused_832 = FALSE;
   gi_unused_836 = FALSE;
   gi_unused_840 = FALSE;
   gi_unused_844 = FALSE;
   gi_unused_848 = FALSE;
   gi_unused_852 = FALSE;
   gi_unused_856 = FALSE;
   gi_unused_860 = FALSE;
   gi_unused_864 = FALSE;
   gi_868 = FALSE;
   gi_872 = FALSE;
   gi_876 = FALSE;
   gi_880 = FALSE;
   gi_884 = FALSE;
   gi_888 = FALSE;
   gi_892 = TRUE;
}

void ResetArrays() {
   ArrayInitialize(gda_660, 0);
   ArrayInitialize(gda_668, 0);
   ArrayInitialize(gda_656, 0);
   ArrayInitialize(gda_664, 0);
   ArrayInitialize(gda_660, 0);
   ArrayInitialize(gda_672, 0);
   ArrayInitialize(gda_676, 0);
   ArrayInitialize(gda_680, 0);
   ArrayInitialize(gda_684, 0);
   ArrayInitialize(gda_688, 0);
   ArrayInitialize(gda_692, 2);
   Print("ALL ARRAYS RESETED!");
}