bool Bull=false, t_Bull=false, t_Sent=false, t_rsi515=false, t_adx=false;
int ticket;          double/* sup_trnd_,*/ sup_trend;

int OnInit() {
   return(INIT_SUCCEEDED); }

void OnTick() { bool Buy, Sell;
   if (floor(TimeCurrent() / 60) > tim_min && ! (Minute() % Period())) { tim_min = floor(TimeCurrent() / 60);        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, 5.0, 0, 0) != 2147483647) DrawArrow_HA((sup_trend=iCustom(NULL, 0, "super-trend_.ex4", 10, 5.0, 0, 0)), 0, 159, 1);
      if (iCustom(NULL, 0, "super-trend_.ex4", 10, 5.0, 1, 0) != 2147483647) DrawArrow_HA((sup_trend=iCustom(NULL, 0, "super-trend_.ex4", 10, 5.0, 1, 0)), 0, 159, 0);
      if (iCustom(NULL, 0, "super-trend_.ex4", 10, 5.0, 0, 0) == 2147483647 && iCustom(NULL, 0, "super-trend_.ex4", 10, 5.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;

   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) { Bull = true;      t_Bull = true;       t_Sent = true; } }      // { tim1 = TimeCurrent();

   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) { Bull = false;     t_Bull = false;      t_Sent = true; } }      // { tim1 = TimeCurrent();

   if (Bull && t_Sent && Ask < iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 0)) {
      ticket = ! OrderClose(ticket, 0.01, Ask, 15, clrNONE);      if (! ticket) t_Sent = false; }

   if (! Bull && t_Sent && Bid > iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 0)) {
      ticket = ! OrderClose(ticket, 0.01, Bid, 15, clrNONE);      if (! ticket) t_Sent = false; }

   if (t_Bull && ! t_Sent && Bid > iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 0) && (t_adx || ! t_rsi515) && sup_trend < iLow(NULL, 0, 1) && sup_trend < Bid) {
      ticket = OrderSend(Symbol(), 0, 0.01, Ask, 15, 0.99 * Ask, 0.0, NULL, 0, 0, clrNONE);
      if (ticket > 0) { Bull = true;      t_Bull = true;       t_Sent = true; } }

   if (! t_Bull && ! t_Sent && Ask < iMA(NULL, 0, 100, 0, MODE_EMA, PRICE_CLOSE, 0) && (t_adx || ! t_rsi515) && sup_trend > iHigh(NULL, 0, 1) && sup_trend > Ask) {
      ticket = OrderSend(Symbol(), 1, 0.01, Bid, 15, 1.01 * Bid, 0.0, NULL, 0, 0, clrNONE);
      if (ticket > 0) { Bull = false;     t_Bull = false;      t_Sent = true; } }

  }

void OnDeinit(const int reason) {
 }

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