#include <stdlib.mqh>
#define PAUSE      100
int ticket1, ticket2, tsh, tim_min, tim1, tim2, sectrend, TS=25;        double vScale, sup_trnd_, sup_trend;
datetime tim3, tim4;          bool t_138=false, st_bull_, st_bull=false, t_rsi51_, t_rsi515, t_rsi515o;     //, t_suptr=false

int OnInit() {
//   EventSetTimer(30);
   return(INIT_SUCCEEDED); }

void OnTick() {/* }

void OnDeinit(const int reason) {
   EventKillTimer(); }

void OnTimer() { */
   if (Hour()*60 + Minute() > tim_min && ! (Minute() % Period())) { tim_min = Hour()*60 + Minute();          st_bull_ = st_bull;        sup_trnd_ = sup_trend;
//Alert("******* DrawArrow_HA() Minute() - " + Minute() + " - " + Seconds() + " - " + iCustom(NULL, 0, "super-trend_.ex4", 10, 4.0, 0, 0) + " - " + iCustom(NULL, 0, "super-trend_.ex4", 10, 4.0, 1, 0) + " *******");
      if (iCustom(NULL, 0, "super-trend_.ex4", 10, 4.0, 0, 0) != 2147483647) DrawArrow_HA((sup_trend=iCustom(NULL, 0, "super-trend_.ex4", 10, 4.0, 0, 0)), 0, 159, 1);
      if (iCustom(NULL, 0, "super-trend_.ex4", 10, 4.0, 1, 0) != 2147483647) DrawArrow_HA((sup_trend=iCustom(NULL, 0, "super-trend_.ex4", 10, 4.0, 1, 0)), 0, 159, 0);
      if (iCustom(NULL, 0, "super-trend_.ex4", 10, 4.0, 0, 0) == 2147483647 && iCustom(NULL, 0, "super-trend_.ex4", 10, 4.0, 1, 0) == 2147483647)
         if (sup_trend < Bid) DrawArrow_HA(sup_trend, 0, 159, 1);       else  DrawArrow_HA(sup_trend, 0, 159, 0); }
//   if (Minute() % 5 > 1) t_suptr = false;

   vScale = (WindowPriceMax() - WindowPriceMin()) / (!WindowPriceMax()? 1.154: WindowPriceMax()) / 1.154 / (1.154-1.061);
   bool trend=(iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 0) > iCustom(NULL, 0, "Parabolic.ex4", 0, 0) &&                                                                       // (1)
      iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 1) > iCustom(NULL, 0, "Parabolic.ex4", 0, 1) && iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 0) > iHigh(NULL, 0, 0)) ||
      (iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 0) < iCustom(NULL, 0, "Parabolic.ex4", 0, 0) &&
      iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 1) < iCustom(NULL, 0, "Parabolic.ex4", 0, 1) && iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 0) < iLow(NULL, 0, 0)),
      Bull=iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 0) < iLow(NULL, 0, 0), t_adx, signal;          string LineName;           int cpt=0;
      t_adx = iCustom(NULL, 0, "New folder\\ADX.ex4", 7, 1, 0) > 9.2 || iCustom(NULL, 0, "New folder\\ADX.ex4", 14, 1, 0) > 14.2;
      t_rsi51_ = t_rsi515;       t_rsi515 = (m3_RSI(0, 3) > 31 && m3_RSI(0, 3) < 69);
      if (t_rsi51_ && ! t_rsi515) t_rsi515o = true;             if (t_rsi515o && t_rsi515) t_rsi515o = false;

//   trend = trend? (Bull && iCustom(NULL, 0, "Parabolic.ex4", 0, 2) > iClose(NULL, 0, 2)) || (! Bull && iCustom(NULL, 0, "Parabolic.ex4", 0, 2) < iClose(NULL, 0, 2)): false;     // (2)
if (Day() == 28 && Hour() > 16 && Hour() < 18 && Minute() > 8 && Minute() < 16) Print("*****1* " + IntegerToString(trend) + " *****");
if (trend) Drawn_Point(WindowPriceMax()-1000 * vScale*Point, 0, 159, 1);
   signal = trend? (Bull && iCustom(NULL, 0, "Parabolic.ex4", 0, 1) < iClose(NULL, 0, 1)) || (! Bull && iCustom(NULL, 0, "Parabolic.ex4", 0, 1) > iClose(NULL, 0, 1)): false;    // (3a)
if (Day() == 28 && Hour() > 16 && Hour() < 18 && Minute() > 8 && Minute() < 16) Print("*****2* " + IntegerToString(signal) + " *****");
if (signal) Drawn_Point(WindowPriceMax()-1500 * vScale*Point, 0, 159, 2);
   signal = signal? (Bull && iCustom(NULL, 0, "Parabolic.ex4", 0, 2) > iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 2) &&                                                         // (6)
      iCustom(NULL, 0, "Parabolic.ex4", 0, 4) > iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 4)) ||
      (! Bull && iCustom(NULL, 0, "Parabolic.ex4", 0, 2) < iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 2) &&
      iCustom(NULL, 0, "Parabolic.ex4", 0, 4) < iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 4)): false;
if (Day() == 28 && Hour() > 16 && Hour() < 18 && Minute() > 8 && Minute() < 16) Print("*****3* " + IntegerToString(signal) + " *****");
if (signal) Drawn_Point(WindowPriceMax()-2000 * vScale*Point, 0, 159, 3);
   if (((iCustom(NULL, 0, "Parabolic.ex4", 0, 2) > iClose(NULL, 0, 2) && iCustom(NULL, 0, "Parabolic.ex4", 0, 1) < iClose(NULL, 0, 1)) || (iCustom(NULL, 0,                      // (3b)
   "Parabolic.ex4", 0, 2) < iClose(NULL, 0, 2) && iCustom(NULL, 0, "Parabolic.ex4", 0, 1) > iClose(NULL, 0, 1))) && tim1 < Hour()*60 + Minute() && ! (Minute() % Period())) {
      tim2 = tim1;         tim1 = Hour()*60 + Minute();        Drawn_Point(WindowPriceMax()-4000 * vScale*Point, 0, 159, 7);           t_138 = false;
      if (TS == 25 && (! t_adx || t_rsi515)) for (int i=OrdersTotal() - 1; i >= 0; i--) if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == 5555544 &&
               OrderClose(OrderTicket(), OrderLots(), OrderType()? MarketInfo(OrderSymbol(), MODE_ASK): MarketInfo(OrderSymbol(), MODE_BID), 15, clrNONE)) {} }

   if (st_bull_ != st_bull && (t_adx || ! t_rsi515) && TimeCurrent() - tim3 > 900) for (i = OrdersTotal() - 1; i >= 0; i--) if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) &&    // close all
         OrderMagicNumber() == 5555544 && OrderClose(OrderTicket(), OrderLots(), OrderType()? MarketInfo(OrderSymbol(), MODE_ASK): MarketInfo(OrderSymbol(), MODE_BID), 15, clrNONE)) {}

   signal = signal? fabs(iCustom(NULL, 0, "Parabolic.ex4", 0, 2) - iCustom(NULL, 0, "Parabolic.ex4", 0, 1)) <                                                                    // (5)
      fabs(iCustom(NULL, 0, "Parabolic.ex4", 0, 1) - iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 1)) * 5: false;
if (Day() == 28 && Hour() > 16 && Hour() < 18 && Minute() > 8 && Minute() < 16) Print("*****4* " + IntegerToString(signal) + " *****");
if (signal) Drawn_Point(WindowPriceMax()-2500 * vScale*Point, 0, 159, 4);
   signal = signal? ((Bull && iClose(NULL,0,0) > iCustom(NULL, 0, "Parabolic.ex4", 0, 2)) || (! Bull && iClose(NULL,0,0) < iCustom(NULL, 0, "Parabolic.ex4", 0, 2))) &&          // (4)
      fabs(iClose(NULL,0,0) - iCustom(NULL, 0, "Parabolic.ex4", 0, 2)) < fabs(iCustom(NULL, 0, "Parabolic.ex4", 0, 2) - iCustom(NULL, 0, "Parabolic.ex4", 0, 1)) * 2.0 / 5.0: false;
if (Day() == 28 && Hour() > 16 && Hour() < 18 && Minute() > 8 && Minute() < 16) Print("*****5* " + IntegerToString(signal) + " *****");
if (signal) Drawn_Point(WindowPriceMax()-3000 * vScale*Point, 0, 159, 5);     if (signal && (t_adx || ! t_rsi515)) Drawn_Point(WindowPriceMax()-3500 * vScale*Point, 0, 159, 6);

   double TP2;    if (sup_trnd_ != sup_trend) for (i = OrdersTotal() - 1; i >= 0; i--) if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderStopLoss() == sup_trnd_ && OrderModify(OrderTicket(),
            OrderOpenPrice(), sup_trend, OrderTakeProfit(), 0, clrNONE)) {}                                                                                                      // modif SL
   if (OrderSelect(ticket1, SELECT_BY_TICKET) && OrderCloseTime() && OrderSelect(ticket2, SELECT_BY_TICKET) && ! OrderCloseTime() && (Bull? OrderOpenPrice() > OrderStopLoss():
      OrderOpenPrice() < OrderStopLoss()) && OrderModify(ticket2, OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0, clrNONE)) {}
   if ((t_adx || ! t_rsi515) && OrderSelect(ticket2, SELECT_BY_TICKET) && ! OrderCloseTime() &&
   fabs(iClose(NULL, 0, 0) - OrderTakeProfit()) < 15 && OrderModify(ticket2, OrderOpenPrice(), OrderStopLoss(), (TP2=(OrderOpenPrice() + 1.6 * (OrderTakeProfit()-OrderOpenPrice()))), 0, clrNONE))
      GlobalVariableSet(OrderSymbol() + (ticket2 > 500? StringSubstr(IntegerToString(ticket2), StringLen(IntegerToString(ticket2)) - 4): IntegerToString(ticket2)) + "_TP2", TP2);
   if ((t_adx || ! t_rsi515) && ticket2 > 0) Print("******* (t_adx || ! t_rsi515) " + IntegerToString(ticket2) + " - " + OrderSelect(ticket2, SELECT_BY_TICKET) +
      " - " + OrderCloseTime() + " - " + DoubleToStr(iClose(NULL, 0, 0), Digits) + " - " + DoubleToStr(OrderTakeProfit(), Digits) + " - " + DoubleToStr(TP2, Digits) + " *******");

   Print("******* (st_bull_ != st_bull && ! t_adx) " + t_rsi515 + " - " + t_adx + " - " + DoubleToStr(iCustom(NULL, 0, "New folder\\ADX.ex4", 7, 1, 0), 2) + " - " +
      DoubleToStr(iCustom(NULL, 0, "New folder\\ADX.ex4", 14, 1, 0), 2) + " - " + st_bull_ + " - " + st_bull + " *******");         // if (st_bull_ != st_bull && (! t_adx || t_rsi515)) f_End_EA();
   if (signal) Print("***** OK! " + (! Bull) + " - " + DoubleToStr(iCustom(NULL, 0, "New folder\\ADX.ex4", 7, 1, 0), 2) + " - " +
      DoubleToStr(iCustom(NULL, 0, "New folder\\ADX.ex4", 14, 1, 0), 2) + " - " + DoubleToStr(fabs(iCustom(NULL, 0, "Parabolic.ex4", 0, 2) - iCustom(NULL, 0, "Parabolic.ex4", 0, 1)), 1) + " *****");

   if ((signal && t_adx && ! t_rsi515 && fabs(iCustom(NULL, 0, "Parabolic.ex4", 0, 2) - iCustom(NULL, 0, "Parabolic.ex4", 0, 1)) > 10.0) ||
   t_138 || (st_bull_ != st_bull && (! t_adx || t_rsi515))) { int err;           bool tst=false;
      for (i = OrdersTotal() - 1; i >= 0 && ! tst; i --) tst = OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == 5555544;     if (tst) return;        if (! t_138) TS = 25;

      double LM=iClose(NULL, 0, 0), TP1, SL;          SL = Bull? sup_trend < LM? fmax(sup_trend, iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 1)): iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 1):
         sup_trend > LM? fmin(sup_trend, iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 1)): iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 1);       TP1 = 1.0 * (LM - SL) + LM;      TP2 = 1.6 * (LM - SL) + LM;
      if (st_bull_ != st_bull && (! t_adx || t_rsi515)) { SL = sup_trend;     TP1 = 1.0 * (LM - SL) + LM;      TP2 = 1.6 * (LM - SL) + LM;      TS = 50;       if (st_bull != Bull) Bull = ! Bull; }
//Alert("*********** LM = " + DoubleToStr(LM, Digits) + " - SL = " + DoubleToStr(SL, Digits) + " - TP = " + DoubleToStr(TP1, Digits) + " - " + Bull + " ***********");

      while(tim1 - tim2 > 2 && TimeCurrent() - tim3 > 130 && cpt < 7) { ticket1 = OrderSend(NULL, (! Bull) + (MQLInfoInteger(MQL_TESTER) && t_138? 2: 0), 0.01, LM, 5, SL, TP1, NULL, 5555544, 0, clrNONE);
         ticket2 = OrderSend(NULL, (! Bull) + (MQLInfoInteger(MQL_TESTER) && t_138? 2: 0), 0.01, LM, 5, SL, TP2, NULL, 5555544, 0, clrNONE);
         if (ticket2 > 0) { GlobalVariableSet(Symbol() + (ticket2 > 500? StringSubstr(IntegerToString(ticket2), StringLen(IntegerToString(ticket2)) - 4): IntegerToString(ticket2)) + "_TP1", TP1);
            GlobalVariableSet(Symbol() + (ticket2 > 500? StringSubstr(IntegerToString(ticket2), StringLen(IntegerToString(ticket2)) - 4): IntegerToString(ticket2)) + "_TP2", TP2); }
         if (ticket1 > 0) { tim3 = TimeCurrent();        /* Print("***** " + DoubleToStr(fabs(iClose(NULL,0,0) - iCustom(NULL, 0, "Parabolic.ex4", 0, 2)), Digits) + " - " +
               DoubleToStr(fabs(iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 0) - iCustom(NULL, 0, "Parabolic.ex4", 0, 0)) * 2.0 / 3.0, Digits) + " *****"); */          t_138 = false;
            if (ObjectCreate(0, LineName="H_Line_"+DoubleToStr(TP1, Digits)+" "+IntegerToString(Time[0]), OBJ_HLINE, 0, 0, TP1)) ObjectSet(LineName, OBJPROP_COLOR, clrLightGreen); }
         else { Print("****err* " + (! Bull) + " - " + DoubleToStr(LM, Digits) + " - " + DoubleToStr(SL, Digits) + " - " + DoubleToStr(TP1, Digits) + " - " + (err=GetLastError()) + " *****");
            if (err == 138) { t_138 = true;     Drawn_Point(WindowPriceMax()-3500 * vScale*Point, 0, 159, ! Bull? 8: 9);        RefreshRates();         Sleep(PAUSE); } }        cpt ++; }
      if (TimeCurrent() - tim3 > 130) Print("****tck* " + IntegerToString(ticket1) + " - " + IntegerToString(tim1) + " - " + IntegerToString(tim2) + " - " + TimeToStr(tim3, TIME_MINUTES) + " *****"); }
//      if (Hour() == 19 && Minute() == 15) f_End_EA();

   if (DrawArrow_chk(WindowPriceMax()-50 * vScale*Point,0, 1)) {}
   if (TimeCurrent() - tsh > 30) if (OrderSelect(ticket1, SELECT_BY_TICKET) && ! OrderCloseTime()) TrailingStop(0, 30);
      else  if (OrderSelect(ticket2, SELECT_BY_TICKET) && ! OrderCloseTime()) TrailingStop(1, fmax(30, round(1.7 * iCustom(NULL, 0, "New folder\\ADX.ex4", 7, 1, 0)) * 10.0));
   tst = false;      for (i = OrdersTotal() - 1; i >= 0 && ! tst; i --) tst = OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == 5555544;
   if (OrdersTotal() && tst) tim4 = TimeCurrent();             if ((! OrdersTotal() || ! tst) && TimeCurrent() - tim4 > 310) ObjectsDelete(0, ""); }
//   Comment("* ADX-7 = " + DoubleToStr(iCustom(NULL, PERIOD_M30, "New folder\\ADX.ex4", 7, 1, 0), 1) + ", ADX-14 = " + DoubleToStr(iCustom(NULL, PERIOD_M30, "New folder\\ADX.ex4", , 1, 0), 1) + " *"); }

void OnDeinit(const int reason) { string str;
   for(int n = ObjectsTotal()-1; n >= 0; n -- ){ str = ObjectName(n);
   if (ObjectType(str) != OBJ_TREND) continue;
   if (StringGetChar(str, 0) != '#') continue;
   int tick=StringSubstr(str, 1, StringFind(str, " ") - 1);
   ObjectSet(str, OBJPROP_COLOR, OrderSelect(tick, SELECT_BY_TICKET)? OrderProfit() > 0? clrAqua: clrKhaki: clrWhite);
/*   ObjectSet(str, OBJPROP_WIDTH, OrderSelect(tick, SELECT_BY_TICKET)? OrderProfit() > 0? 2: 2: 1);*/ } }

void TrailingStop(int mode,int OffsetPoints) { bool tst1, tst2, tst3=false, tst4=false;      string temp, LineName;//     int TS=25;
   for (int i=OrdersTotal() - 1; i >= 0; i--) if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == 5555544) {
      double LM=OrderOpenPrice(), SL=OrderStopLoss(), SL2, TP1=! mode? OrderTakeProfit():
            GlobalVariableGet(OrderSymbol() + (ticket2 > 500? StringSubstr(IntegerToString(OrderTicket()), StringLen(IntegerToString(OrderTicket())) - 4): IntegerToString(ticket2)) + "_TP1"),
         TP2=GlobalVariableGet(OrderSymbol() + (ticket2 > 500? StringSubstr(IntegerToString(OrderTicket()), StringLen(IntegerToString(OrderTicket())) - 4): IntegerToString(ticket2)) + "_TP2"),
         SLL=NormPrice(LM + (TP1 - LM) * TS / 100 + (OrderType() == OP_BUY? -1: 1) * (15.0 * MarketInfo(OrderSymbol(),MODE_POINT)), OrderSymbol()),
         SLL2=NormPrice(TP1 + (TP2 - TP1) * TS / 100 + (OrderType() == OP_BUY? -1: 1) * (15.0 * MarketInfo(OrderSymbol(),MODE_POINT)), OrderSymbol());
      if (ObjectCreate(0, LineName="H_Line_"+DoubleToStr(SLL, Digits)+" "+IntegerToString(Time[0]), OBJ_HLINE, 0, 0, LM + (TP1-LM) * TS/100)) ObjectSet(LineName, OBJPROP_COLOR, clrSilver);
//if (! ((Minute() - tim_min) % 2) && Seconds() < 5)
      Print("*** TrailingStop"+mode+" (0) " + OrderTicket()+" - "+OrderSymbol()+" - "+(OrderType()==OP_BUY? "OP_BUY": "OP_SELL")+" - LM: "+DoubleToStr(OrderOpenPrice(),MarketInfo(OrderSymbol(),
   MODE_DIGITS)) + " - SL: " + DoubleToStr(OrderStopLoss(), MarketInfo(OrderSymbol(),MODE_DIGITS)) + " - TP: " + DoubleToStr(OrderTakeProfit(), MarketInfo(OrderSymbol(), MODE_DIGITS)) +
   " - TP1: " + DoubleToStr(TP1, MarketInfo(OrderSymbol(),MODE_DIGITS)) + " - TP2: " + DoubleToStr(TP2, MarketInfo(OrderSymbol(),MODE_DIGITS)) + " - SLL: " + (mode ? DoubleToStr(SLL,
   MarketInfo(OrderSymbol(), MODE_DIGITS)) + " - (1-global) " + (OrderType() == OP_BUY? NormPrice(MarketInfo(OrderSymbol(),MODE_BID) - OrderStopLoss(),OrderSymbol()) > NormPrice(OffsetPoints *
   MarketInfo(OrderSymbol(), MODE_POINT), OrderSymbol()) && (((tst1=0) && NormPrice(MarketInfo(OrderSymbol(),MODE_BID), OrderSymbol()) > TP1) || (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_BID),
   OrderSymbol()) > TP1 + (TP2 - TP1) * TS / 100))): NormPrice(OrderStopLoss() - MarketInfo(OrderSymbol(),MODE_ASK),OrderSymbol()) > NormPrice(OffsetPoints * MarketInfo(OrderSymbol(),MODE_POINT),
   OrderSymbol()) && (((tst1=0) && NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol()) < TP1) || (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_ASK),OrderSymbol()) < TP1+(TP2-TP1)* TS / 100)))) +
   " -1-g " + (OrderType() == OP_BUY? NormPrice(MarketInfo(OrderSymbol(),MODE_BID) - OrderStopLoss(),OrderSymbol()): NormPrice(OrderStopLoss() - MarketInfo(OrderSymbol(),MODE_ASK),OrderSymbol())) + " -1-d " +
   NormPrice(OffsetPoints * MarketInfo(OrderSymbol(),MODE_POINT),OrderSymbol()) + " - (tst2) " + (OrderType()==OP_BUY? (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_BID),OrderSymbol()) > TP1+(TP2-TP1)*TS/100)):
   (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol()) < TP1 + (TP2 - TP1) * TS / 100))) + " -2-g " + (OrderType() == OP_BUY? NormPrice(MarketInfo(OrderSymbol(),MODE_BID), OrderSymbol()):
   NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol())) + " -2-d " + NormPrice(TP1 + (TP2 - TP1) * TS / 100, OrderSymbol()) + " -TS: " + IntegerToString(TS) : DoubleToStr(SLL,
   MarketInfo(OrderSymbol(), MODE_DIGITS)) + " - (1-global) " + (OrderType() == OP_BUY? NormPrice(MarketInfo(OrderSymbol(),MODE_BID) - OrderStopLoss(),OrderSymbol()) > NormPrice(OffsetPoints *
   MarketInfo(OrderSymbol(), MODE_POINT), OrderSymbol()) && (((tst1=0) && NormPrice(MarketInfo(OrderSymbol(),MODE_BID), OrderSymbol()) > TP1) || (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_BID),
   OrderSymbol()) > LM + (TP1 - LM) * TS / 100))): NormPrice(OrderStopLoss() - MarketInfo(OrderSymbol(),MODE_ASK),OrderSymbol()) > NormPrice(OffsetPoints * MarketInfo(OrderSymbol(),MODE_POINT),
   OrderSymbol()) && (((tst1=0) && NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol()) < TP1) || (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_ASK),OrderSymbol()) < LM+(TP1-LM)* TS / 100)))) +
   " -1-g " + (OrderType() == OP_BUY? NormPrice(MarketInfo(OrderSymbol(),MODE_BID) - OrderStopLoss(),OrderSymbol()): NormPrice(OrderStopLoss() - MarketInfo(OrderSymbol(),MODE_ASK),OrderSymbol())) + " -1-d " +
   NormPrice(OffsetPoints * MarketInfo(OrderSymbol(),MODE_POINT),OrderSymbol()) + " - (tst2) " + (OrderType()==OP_BUY? (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_BID),OrderSymbol()) > LM+(TP1-LM)*TS/100)):
   (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol()) < LM + (TP1 - LM) * TS / 100))) + " -2-g " + (OrderType() == OP_BUY? NormPrice(MarketInfo(OrderSymbol(),MODE_BID), OrderSymbol()):
   NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol())) + " -2-d " + NormPrice(LM + (TP1 - LM) * TS / 100, OrderSymbol()) + " -TS: " + IntegerToString(TS)) + " ***");
if ((mode && ((OrderType()==OP_BUY && NormPrice(MarketInfo(OrderSymbol(),MODE_BID)-OrderStopLoss(),OrderSymbol()) > NormPrice(OffsetPoints*MarketInfo(OrderSymbol(),MODE_POINT),OrderSymbol()) &&
(((tst1=0) && NormPrice(MarketInfo(OrderSymbol(),MODE_BID), OrderSymbol()) > TP1) || (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_BID), OrderSymbol()) > TP1 + (TP2 - TP1) * TS / 100)))) ||
(OrderType() == OP_SELL && NormPrice(OrderStopLoss() - MarketInfo(OrderSymbol(),MODE_ASK),OrderSymbol()) > NormPrice(OffsetPoints * MarketInfo(OrderSymbol(),MODE_POINT),OrderSymbol()) &&
(((tst1=0) && NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol()) < TP1) || (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol()) < TP1 + (TP2 - TP1) * TS / 100)))))) ||
(! mode && ((OrderType()==OP_BUY && NormPrice(MarketInfo(OrderSymbol(),MODE_BID)-OrderStopLoss(),OrderSymbol()) > NormPrice(OffsetPoints*MarketInfo(OrderSymbol(),MODE_POINT),OrderSymbol()) &&
(((tst1=0) && NormPrice(MarketInfo(OrderSymbol(),MODE_BID), OrderSymbol()) > TP1) || (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_BID), OrderSymbol()) > LM + (TP1 - LM) * TS / 100)))) ||
(OrderType() == OP_SELL && NormPrice(OrderStopLoss() - MarketInfo(OrderSymbol(),MODE_ASK),OrderSymbol()) > NormPrice(OffsetPoints * MarketInfo(OrderSymbol(),MODE_POINT),OrderSymbol()) &&
(((tst1=0) && NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol()) < TP1) || (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol()) < LM + (TP1 - LM) * TS / 100)))))))
//   if (! ((Minute() - tim_min) % 2) && Seconds() < 5) {// tst4 = true;
      Print("*** TrailingStop"+mode+" (1) " + OrderTicket()+" - "+OrderSymbol()+" - "+(OrderType()==OP_BUY? "OP_BUY": "OP_SELL")+" - LM: "+DoubleToStr(OrderOpenPrice(),MarketInfo(OrderSymbol(),
   MODE_DIGITS)) + " - SL: " + DoubleToStr(OrderStopLoss(), MarketInfo(OrderSymbol(),MODE_DIGITS)) + " - TP: " + DoubleToStr(OrderTakeProfit(), MarketInfo(OrderSymbol(), MODE_DIGITS)) +
   " - TP1: " + DoubleToStr(TP1, MarketInfo(OrderSymbol(),MODE_DIGITS)) + " - TP2: " + DoubleToStr(TP2, MarketInfo(OrderSymbol(),MODE_DIGITS)) + " - SLL: " + (mode ? DoubleToStr(SLL,
   MarketInfo(OrderSymbol(), MODE_DIGITS)) + " - (1-global) " + (OrderType() == OP_BUY? NormPrice(MarketInfo(OrderSymbol(),MODE_BID) - OrderStopLoss(),OrderSymbol()) > NormPrice(OffsetPoints *
   MarketInfo(OrderSymbol(), MODE_POINT), OrderSymbol()) && (((tst1=0) && NormPrice(MarketInfo(OrderSymbol(),MODE_BID), OrderSymbol()) > TP1) || (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_BID),
   OrderSymbol()) > TP1 + (TP2 - TP1) * TS / 100))): NormPrice(OrderStopLoss() - MarketInfo(OrderSymbol(),MODE_ASK),OrderSymbol()) > NormPrice(OffsetPoints * MarketInfo(OrderSymbol(),MODE_POINT),
   OrderSymbol()) && (((tst1=0) && NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol()) < TP1) || (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_ASK),OrderSymbol()) < TP1+(TP2-TP1)* TS / 100)))) +
   " -1-g " + (OrderType() == OP_BUY? NormPrice(MarketInfo(OrderSymbol(),MODE_BID) - OrderStopLoss(),OrderSymbol()): NormPrice(OrderStopLoss() - MarketInfo(OrderSymbol(),MODE_ASK),OrderSymbol())) + " -1-d " +
   NormPrice(OffsetPoints * MarketInfo(OrderSymbol(),MODE_POINT),OrderSymbol()) + " - (tst2) " + (OrderType()==OP_BUY? (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_BID),OrderSymbol()) > TP1+(TP2-TP1)*TS/100)):
   (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol()) < TP1 + (TP2 - TP1) * TS / 100))) + " -2-g " + (OrderType() == OP_BUY? NormPrice(MarketInfo(OrderSymbol(),MODE_BID), OrderSymbol()):
   NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol())) + " -2-d " + NormPrice(TP1 + (TP2 - TP1) * TS / 100, OrderSymbol()) + " -TS: " + IntegerToString(TS) : DoubleToStr(SLL,
   MarketInfo(OrderSymbol(), MODE_DIGITS)) + " - (1-global) " + (OrderType() == OP_BUY? NormPrice(MarketInfo(OrderSymbol(),MODE_BID) - OrderStopLoss(),OrderSymbol()) > NormPrice(OffsetPoints *
   MarketInfo(OrderSymbol(), MODE_POINT), OrderSymbol()) && (((tst1=0) && NormPrice(MarketInfo(OrderSymbol(),MODE_BID), OrderSymbol()) > TP1) || (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_BID),
   OrderSymbol()) > LM + (TP1 - LM) * TS / 100))): NormPrice(OrderStopLoss() - MarketInfo(OrderSymbol(),MODE_ASK),OrderSymbol()) > NormPrice(OffsetPoints * MarketInfo(OrderSymbol(),MODE_POINT),
   OrderSymbol()) && (((tst1=0) && NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol()) < TP1) || (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_ASK),OrderSymbol()) < LM+(TP1-LM)* TS / 100)))) +
   " -1-g " + (OrderType() == OP_BUY? NormPrice(MarketInfo(OrderSymbol(),MODE_BID) - OrderStopLoss(),OrderSymbol()): NormPrice(OrderStopLoss() - MarketInfo(OrderSymbol(),MODE_ASK),OrderSymbol())) + " -1-d " +
   NormPrice(OffsetPoints * MarketInfo(OrderSymbol(),MODE_POINT),OrderSymbol()) + " - (tst2) " + (OrderType()==OP_BUY? (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_BID),OrderSymbol()) > LM+(TP1-LM)*TS/100)):
   (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol()) < LM + (TP1 - LM) * TS / 100))) + " -2-g " + (OrderType() == OP_BUY? NormPrice(MarketInfo(OrderSymbol(),MODE_BID), OrderSymbol()):
   NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol())) + " -2-d " + NormPrice(LM + (TP1 - LM) * TS / 100, OrderSymbol()) + " -TS: " + IntegerToString(TS)) + " ***");// }
      if ((mode && (OrderType() == OP_BUY && NormPrice(MarketInfo(OrderSymbol(),MODE_BID) - OrderStopLoss(),OrderSymbol()) > NormPrice(OffsetPoints * MarketInfo(OrderSymbol(),MODE_POINT),OrderSymbol()) &&
      (((tst1=0) && NormPrice(MarketInfo(OrderSymbol(),MODE_BID), OrderSymbol()) > TP1) || (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_BID), OrderSymbol()) > TP1 + (TP2 - TP1) * TS / 100))))) ||
      (! mode && (OrderType() == OP_BUY && NormPrice(MarketInfo(OrderSymbol(),MODE_BID) - OrderStopLoss(),OrderSymbol()) > NormPrice(OffsetPoints * MarketInfo(OrderSymbol(),MODE_POINT),OrderSymbol()) &&
      (((tst1=0) && NormPrice(MarketInfo(OrderSymbol(),MODE_BID), OrderSymbol()) > TP1) || (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_BID), OrderSymbol()) > LM + (TP1 - LM) * TS / 100)))))) {
Alert(temp=("*** TrailingStop"+mode+" (2) " + OrderTicket() + " - " + OrderSymbol() + " - OP: " + DoubleToStr(OrderOpenPrice(), MarketInfo(OrderSymbol(),MODE_DIGITS)) + " - SL: " +
   DoubleToStr(OrderStopLoss(), MarketInfo(OrderSymbol(),MODE_DIGITS)) + " - TP: " + DoubleToStr(OrderTakeProfit(), MarketInfo(OrderSymbol(),MODE_DIGITS)) + " - TP1: " +
   DoubleToStr(TP1, MarketInfo(OrderSymbol(),MODE_DIGITS)) + " - TP2: " + DoubleToStr(TP2, MarketInfo(OrderSymbol(),MODE_DIGITS)) + " - SLL: " +
   DoubleToStr(SLL, MarketInfo(OrderSymbol(),MODE_DIGITS)) + " - " + tst1 + " - " + tst2 + " ***"));           // v TP2 > TP1 && 
         if (OrderModify(OrderTicket(), OrderOpenPrice(), tst1 && ! tst2? NormPrice(MarketInfo(OrderSymbol(),MODE_BID) - OffsetPoints * MarketInfo(OrderSymbol(),MODE_POINT), OrderSymbol()):
            tst2? (SL2=NormPrice(MarketInfo(OrderSymbol(),MODE_BID) - OffsetPoints * MarketInfo(OrderSymbol(),MODE_POINT),OrderSymbol())) > SLL? SL2: SLL:
            OrderStopLoss(), OrderTakeProfit(),OrderExpiration(),clrNONE) == false) Alert("Cannot modify the SL due to: ", ErrorDescription(GetLastError()));
         else  tst3 = TRUE; }
      else if ((mode && (OrderType() == OP_SELL && NormPrice(OrderStopLoss() - MarketInfo(OrderSymbol(),MODE_ASK),OrderSymbol()) > NormPrice(OffsetPoints * MarketInfo(OrderSymbol(),MODE_POINT),OrderSymbol()) &&
      (((tst1=0) && NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol()) < TP1) || (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol()) < TP1 + (TP2 - TP1) * TS / 100))))) ||
      (! mode && (OrderType() == OP_SELL && NormPrice(OrderStopLoss() - MarketInfo(OrderSymbol(),MODE_ASK),OrderSymbol()) > NormPrice(OffsetPoints * MarketInfo(OrderSymbol(),MODE_POINT),OrderSymbol()) &&
      (((tst1=0) && NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol()) < TP1) || (tst2=(NormPrice(MarketInfo(OrderSymbol(),MODE_ASK), OrderSymbol()) < LM + (TP1 - LM) * TS / 100)))))) {
Alert(temp=("*** TrailingStop"+mode+" (3) " + OrderTicket() + " - " + OrderSymbol() + " - OP: " + DoubleToStr(OrderOpenPrice(), MarketInfo(OrderSymbol(),MODE_DIGITS)) + " - SL: " +
   DoubleToStr(OrderStopLoss(), MarketInfo(OrderSymbol(),MODE_DIGITS)) + " - TP: " + DoubleToStr(OrderTakeProfit(), MarketInfo(OrderSymbol(),MODE_DIGITS)) + " - TP1: " +
   DoubleToStr(TP1, MarketInfo(OrderSymbol(),MODE_DIGITS)) + " - TP2: " + DoubleToStr(TP2, MarketInfo(OrderSymbol(),MODE_DIGITS)) + " - SLL: " +
   DoubleToStr(SLL, MarketInfo(OrderSymbol(),MODE_DIGITS)) + " - " + tst1 + " - " + tst2 + " ***"));           // v TP2<TP1 && TP2>0.0 && 
         if (OrderModify(OrderTicket(),OrderOpenPrice(), tst1 && ! tst2? NormPrice(MarketInfo(OrderSymbol(),MODE_ASK) + OffsetPoints * MarketInfo(OrderSymbol(),MODE_POINT),OrderSymbol()):
            tst2? (SL2=NormPrice(MarketInfo(OrderSymbol(),MODE_ASK) + OffsetPoints * MarketInfo(OrderSymbol(),MODE_POINT),OrderSymbol())) < SLL? SL2: SLL:
            OrderStopLoss(), OrderTakeProfit(),OrderExpiration(),clrNONE) == false) Alert("Cannot modify the SL due to: ", ErrorDescription(GetLastError()));
         else  tst3 = TRUE; } }            tsh = TimeCurrent();         // if (tst4) 
}

bool DrawArrow_chk(double LinePrice,int BarNum,int I) { int tmfr=Period();
   string stmfr= tmfr==PERIOD_M1? "M1": tmfr==PERIOD_M5? "M5": tmfr==PERIOD_M15? "M15": tmfr==PERIOD_M30? "M30": 
      tmfr==PERIOD_H1? "H1": tmfr==PERIOD_H4? "H4": tmfr==PERIOD_D1? "D1": tmfr==PERIOD_W1? "W1": "MN1", 
      ArrowName="EA_Check-"+stmfr+"-"+IntegerToString(I)+"_"+IntegerToString(BarNum)+" "+IntegerToString(Time[BarNum])+" "+DoubleToStr(LinePrice);
   if (! ObjectCreate(ArrowName,OBJ_ARROW,0,Time[BarNum],LinePrice+(I==2?500*vScale*Point:0))) return false;
   ObjectSet(ArrowName, OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet(ArrowName, OBJPROP_ARROWCODE, I? 252: 251);
   ObjectSet(ArrowName, OBJPROP_COLOR, I==1? clrSilver: clrSkyBlue);
   ObjectSet(ArrowName, OBJPROP_ARROWCODE, I==3? 251: 252);
   ObjectSet(ArrowName, OBJPROP_COLOR, I==1 || I>=4? I==1 && OrdersTotal()? clrGold: clrSilver: clrSkyBlue);
   ObjectSet(ArrowName, OBJPROP_SELECTABLE, 1);
   ObjectSet(ArrowName, OBJPROP_SELECTED, 0);
   ObjectSet(ArrowName, OBJPROP_WIDTH, I==5? 1: 2);
   if (I < 2) ObjectSet(ArrowName, OBJPROP_BACK, 1);
//   ObjectSet(ArrowName, OBJPROP_YDISTANCE, 55);
   return true; }

double NormPrice(double Price,string Symb) { return NormalizeDouble(Price, MarketInfo(Symb, MODE_DIGITS)); }

void f_End_EA() { int zero=0;    Print(1 / zero); }

void Drawn_Point(double LinePrice,int BarNum,int Code,int Color) {
   string ArrowName = "Test_Arr("+IntegerToString(Color)+")_"+IntegerToString(Code)+"-"+LinePrice+" "+IntegerToString(Time[BarNum]);
   ObjectCreate(ArrowName,OBJ_ARROW,0,Time[BarNum],LinePrice);
   ObjectSet(ArrowName, OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet(ArrowName, OBJPROP_ARROWCODE, Code);
   ObjectSet(ArrowName, OBJPROP_COLOR, Color==1? clrYellow: Color==2? clrGreen: Color==3? clrBlue: Color==4? clrMagenta: Color==5? clrLimeGreen:
      Color==6? clrWhite: Color==7? clrGray: Color==8? clrOrangeRed: clrLime);
   ObjectSet(ArrowName, OBJPROP_SELECTABLE, 1);
   ObjectSet(ArrowName, OBJPROP_SELECTED, 0);
   ObjectSet(ArrowName, OBJPROP_WIDTH, Code == 159? 1: 2); }

void ObjectsDelete(int P, string str) {
   int obj_total=ObjectsTotal(), i=0, j=0;      string name;
   for (int pos=0;pos<obj_total;pos++) { name = ObjectName(pos);
     if (pos && ((str!="" && StringSubstr(name,0,StringLen(str)+4)=="EA_"+str+"-") || (str=="" && (StringSubstr(name,0,8) == "EA_Test_" ||
         StringSubstr(name, 0, 9) == "H-A_Test_" || StringSubstr(name, 0, 7) == "H_Line_")))) {
         j ++;   if (ObjectDelete(name)) { i ++;  pos --; } } }
   if (P) Print (pos, " objects, DELETED: ", i, " / ", j, " / ", obj_total);
   return; }

void DrawArrow_HA(double LinePrice,int BarNum,int Code,bool Bullish) { st_bull = Bullish;     return;
   string ArrowName = "HA_Test_Arr"+(Bullish?"-Up_":"-Down_")+IntegerToString(Code)+"-"+LinePrice+" "+IntegerToString(Time[BarNum]);
   ObjectCreate(ArrowName,OBJ_ARROW,0,Time[BarNum],LinePrice);
   ObjectSet(ArrowName, OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet(ArrowName, OBJPROP_ARROWCODE, Code);
   ObjectSet(ArrowName, OBJPROP_COLOR, Bullish? clrLime: clrPink);
   ObjectSet(ArrowName, OBJPROP_SELECTABLE, 0);
   ObjectSet(ArrowName, OBJPROP_SELECTED, 0);
   ObjectSet(ArrowName, OBJPROP_WIDTH, Code == 159? 1: 2); }

double m3_RSI (ENUM_TIMEFRAMES per,int nb) {
   return((iCustom(NULL, per, "RSI.ex4", 0, 1) + iCustom(NULL, per, "RSI.ex4", 0, 2) +
      (nb == 3 ? iCustom(NULL, per, "RSI.ex4", 0, 3) : 0.0)) / nb); }

/* Parabolic (-1) / iMA == HLOC / iMA (<- coté)                      (1)
   Parabolic (-2) -> extérieur ET                                    (2)
   Paraboloc (-1) -> intérieur + == / iMA                            (3)
   iClose / Parabolic (-2) -> extérieur                              (4)
   fabs(Parabolic(-2) - Parabolic(-1)) < fabs(Parabolic(-1) - iMA)   (5)
   Parabolic (-4) / iMA == Parabolic (-2) / iMA                      (6)
   -> signal Buy / Sell
   SL = Parabolic (-1), TP1 = 1.6 * SL, TP2 = 2.6 * SL */