#include <stdlib.mqh>
#define PAUSE      100
int tsh, tim_min, tim1, tim2, mn=0, nj=0;       double vScale;       datetime tim3, tim4;       bool updn;

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

void OnTick() { int ticket1, ticket2;/* }

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

void OnTimer() { */
   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) &&
      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)) || nj < 6,
      Bull=iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 0) < iLow(NULL, 0, 0), t_adx, signal;          string LineName;
      t_adx = iCustom(NULL, PERIOD_M30, "New folder\\ADX.ex4", 1, 7) > 10 && iCustom(NULL, PERIOD_M30, "New folder\\ADX.ex4", 1, 14) > 15;

//   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;
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;
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) &&
      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, "Parabolic.ex4", 0, 2) < iClose(NULL, 0, 2) && iCustom(NULL, 0, "Parabolic.ex4", 0, 1) > iClose(NULL, 0, 1))) && tim1 < Hour()*60 + Minute()) {
      tim2 = tim1;         tim1 = Hour()*60 + Minute();        Drawn_Point(WindowPriceMax()-4000 * vScale*Point, 0, 159, 7);
      updn = iCustom(NULL, 0, "Parabolic.ex4", 0, 1) < iClose(NULL, 0, 1);          nj = 0; }
      if (mn < Minute() || ! nj) if (Hour()*60 + Minute() - tim1 < 330 && updn == (iCustom(NULL, 0, "Parabolic.ex4", 0, 1) < iClose(NULL, 0, 1))) nj ++;    else  nj = 0;
   signal = signal? fabs(iCustom(NULL, 0, "Parabolic.ex4", 0, 2) - iCustom(NULL, 0, "Parabolic.ex4", 0, 1)) <
      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, fmax(2,nj+1))) || (! Bull && iClose(NULL,0,0) < iCustom(NULL, 0, "Parabolic.ex4", 0, fmax(2,nj+1)))) &&
      fabs(iClose(NULL,0,0) - iCustom(NULL,0,"Parabolic.ex4",0,fmax(2,nj+1))) < fabs(iCustom(NULL,0,"Parabolic.ex4",0,fmax(2,nj+1)) - iCustom(NULL,0,"Parabolic.ex4",0,fmax(1,nj))) * 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) Drawn_Point(WindowPriceMax()-3500 * vScale*Point, 0, 159, 6);

   if (signal) Print("***** OK! " + (! Bull) + " - " + DoubleToStr(iCustom(NULL, PERIOD_M30, "New folder\\ADX.ex4", 1, 7), 2) + " - " +
      DoubleToStr(iCustom(NULL, PERIOD_M30, "New folder\\ADX.ex4", 1, 14), 2) + " - " + DoubleToStr(fabs(iCustom(NULL, 0, "Parabolic.ex4", 0, 2) - iCustom(NULL, 0, "Parabolic.ex4", 0, 1)), 1) + " *****");
   if (signal && t_adx && fabs(iCustom(NULL, 0, "Parabolic.ex4", 0, 2) - iCustom(NULL, 0, "Parabolic.ex4", 0, 1)) > 15.0) {
      double LM=iClose(NULL,0,0), SL=iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 1), TP1=1.0*(LM-SL)+LM, TP2=1.6*(LM-SL)+LM;      int err;
      if (tim1 - tim2 > 2 && TimeCurrent() - tim3 > 150) { ticket1 = OrderSend(NULL, ! Bull, 0.01, LM, 5, SL, TP1, NULL, 0, 0, clrNONE);     tim3 = TimeCurrent();
         if (ticket1 > 0) { 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) + " *****");
            if (ObjectCreate(0, LineName="H_Line_"+DoubleToStr(TP1, Digits)+" "+IntegerToString(Time[0]), OBJ_HLINE, 0, 0, TP1)) ObjectSet(LineName, OBJPROP_COLOR, clrLightGreen); }
         else { Print("***** " + (! Bull) + " - " + DoubleToStr(LM, Digits) + " - " + DoubleToStr(SL, Digits) + " - " + DoubleToStr(TP1, Digits) + " - " + (err=GetLastError()) + " *****");
            if (err == 138) Drawn_Point(WindowPriceMax()-3500 * vScale*Point, 0, 159, 8); } }
      else  Print("***** " + IntegerToString(ticket1) + " - " + IntegerToString(tim1) + " - " + IntegerToString(tim2) + " - " + TimeToStr(tim3, TIME_MINUTES) + " *****");
/*      ticket2=OrderSend(NULL, ! Bull, 0.01, LM, 5, SL, TP2, NULL, 0, 0, clrNONE);*/ }

//   TrailingStop(1, 30, NULL);
   if (DrawArrow_chk(WindowPriceMax()-50 * vScale*Point,0, 1)) {}
   if (TimeCurrent() - tsh > 30) TrailingStop(30);       if (OrdersTotal()) tim4 = TimeCurrent();
   if (! OrdersTotal() && TimeCurrent() - tim4 > 300) ObjectsDelete(0, "");      mn = Minute(); }

void TrailingStop(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)) {
      double LM=OrderOpenPrice(), SL=OrderStopLoss(), TP1=OrderTakeProfit(), TP2=0.0, SL2,
         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 (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: " + 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) + " ***"); */
   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 ((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))))) */
if ((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()) >= NormPrice(LM+(TP1-LM)*TS/100,OrderSymbol()))))) ||
(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()) <= NormPrice(LM+(TP1-LM)*TS/100,OrderSymbol()))))))
//   if (! ((Minute() - tim_min) % 2) && Seconds() < 5) {// tst4 = true;
      Print("*** TrailingStop (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: " + 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) + " ***");// }*/
   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 (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)))) { */
      if (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()) >= NormPrice(LM+(TP1-LM)*TS/100,OrderSymbol()))))) {
Alert(temp=("*** TrailingStop (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 (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)))) { */
      else if (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()) <= NormPrice(LM+(TP1-LM)*TS/100,OrderSymbol()))))) {
Alert(temp=("*** TrailingStop (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: clrPurple);
   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; }

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