int ticket;          datetime tim1;
bool Bull=false, t_Bull=false, t_Sent=false, t_Rsi515=false, t_Adx=false, t_Pbl=false, t_Wait=false;

int OnInit() {
   return(INIT_SUCCEEDED); }

void OnTick() { bool Buy, Sell;

   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_Rsi515 = (m3_RSI(0, 2) > 31 && m3_RSI(0, 2) < 69);

   if ((Sell=(iCustom(NULL, 0, "New folder\\Manual Trade Cashpower Lifetime.ex4", 0, 0) > 0.0)) ||
   (Buy=(iCustom(NULL, 0, "New folder\\Manual Trade Cashpower Lifetime.ex4", 1, 0) > 0.0)))
      Alert("*** Manual Trade Cashpower Lifetime: " + Symbol() + " - " + IntegerToString(Period()) + " - " +
         DoubleToStr(iCustom(NULL, 0, "New folder\\Manual Trade Cashpower Lifetime.ex4", 0, 0), Digits()) + " - " +
         DoubleToStr(iCustom(NULL, 0, "New folder\\Manual Trade Cashpower Lifetime.ex4", 1, 0), Digits()) + " ***");

   if (Buy && ! Bull && (t_Adx || ! t_Rsi515)) { if (ticket > 0) {
         ticket = ! OrderClose(ticket, 0.01, Ask, 15, clrNONE);      if (! ticket) t_Sent = false; }
      ticket = OrderSend(Symbol(), 0, 0.01, Ask, 15, 0.99 * Ask, 0.0, NULL, 0, 0, clrNONE);
      if (ticket > 0) { tim1 = TimeCurrent();      Bull = true;      t_Bull = true;       t_Sent = true; } }

   if (Sell && (Bull || ! t_Sent) && (t_Adx || ! t_Rsi515)) { if (ticket > 0) {
         ticket = ! OrderClose(ticket, 0.01, Bid, 15, clrNONE);      if (! ticket) t_Sent = false; }
      ticket = OrderSend(Symbol(), 1, 0.01, Bid, 15, 1.01 * Bid, 0.0, NULL, 0, 0, clrNONE);
      if (ticket > 0) { tim1 = TimeCurrent();      Bull = false;     t_Bull = false;      t_Sent = true; } }

   if (Bull && t_Sent && (t_Wait=((Ask < iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 0) && TimeCurrent() % 300 > 290) ||
   (t_Pbl=(TimeCurrent() - tim1 > 7 * 60 * Period() && 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, 0) >
   iClose(NULL, 0, 0) && t_Adx && TimeCurrent() % 300 > 290)))))) && (TimeCurrent() % 300 > 297 || t_Wait)) {
      t_Wait = false;      ticket = ! OrderClose(ticket, 0.01, Ask, 15, clrNONE);      t_Sent = ticket; }

   if (! Bull && t_Sent && (t_Wait=((Bid > iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 0) && TimeCurrent() % 300 > 290) ||
   (t_Pbl=(TimeCurrent() - tim1 > 7 * 60 * Period() && 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, 0) <
   iClose(NULL, 0, 0) && t_Adx && TimeCurrent() % 300 > 290)))))) && (TimeCurrent() % 300 > 297 || t_Wait)) {
      t_Wait = false;      ticket = ! OrderClose(ticket, 0.01, Bid, 15, clrNONE);      t_Sent = ticket; }

   if (t_Bull && ! t_Sent && (t_Wait=((Bid > iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 0) && TimeCurrent() % 300 > 290) ||
   (t_Pbl && 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, 0) < iClose(NULL, 0, 0) && t_Adx && ! t_Rsi515 &&
   TimeCurrent() % 300 > 290))))) && (TimeCurrent() % 300 > 297 || t_Wait)) { t_Wait = false;      t_Pbl = false;
      ticket = OrderSend(Symbol(), 0, 0.01, Ask, 15, 0.99 * Ask, 0.0, NULL, 0, 0, clrNONE);
      if (ticket > 0) { Bull = true;      t_Sent = true; } }

   if (! t_Bull && ! t_Sent && (t_Wait=((Ask < iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 0) && TimeCurrent() % 300 > 290) ||
   (t_Pbl && 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, 0) > iClose(NULL, 0, 0) && t_Adx && ! t_Rsi515 &&
   TimeCurrent() % 300 > 290))))) && (TimeCurrent() % 300 > 297 || t_Wait)) { t_Wait = false;      t_Pbl = false;
      ticket = OrderSend(Symbol(), 1, 0.01, Bid, 15, 1.01 * Bid, 0.0, NULL, 0, 0, clrNONE);
      if (ticket > 0) { Bull = false;     t_Sent = true; } }

  }

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);*/ } }

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); }
