Showing alert from logic sequence

 

Hi,

I need help to fix my code. My intention is to identify trend with alert or some object text based on Moving Averages for several bars (TrendMinDurationBars). Here is the idea:

  • "Trend Up" when MA[shift n] <= MA[shift ...] <= MA[shift 2] <= MA[shift 1]
  • "Trend Down" when MA[shift n] >= MA[shift ...] >= MA[shift 2] >= MA[shift 1]
  • "Trend Up bias" when one of logic from trend up is false (example: MA[shift 4] >= MA[shift 3] <= MA[shift 2] <= MA[shift 1])
  • "Trend Down bias" when one of logic from trend down is false (example: MA[shift 4] <= MA[shift 3] >= MA[shift 2] >= MA[shift 1]

Here's the picture:

Trend MA

And here's is my code:

int TrendMinDurationBar    = 5,
    SlowPeriod  = 14;

int OnInit()
{
   ObjectCreate("Trend", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSet("Trend", OBJPROP_XDISTANCE, 25);
   ObjectSet("Trend", OBJPROP_YDISTANCE, 25);
   ObjectSet("Trend", OBJPROP_CORNER, 3);
   
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
   ObjectDelete("Trend");
}

void OnTick()
{
    if (NewBar(PERIOD_M5) == true) 
    {
        if (MA("Up", PERIOD_M5) == true) {ObjectSetText("Trend", "Trend Up good", 14, "Impact", Green); Alert("TUG");}
        else if (MA("Up", PERIOD_M5) == false) {ObjectSetText("Trend", "Trend Up bias", 14, "Impact", Orange); Alert("TUB");}
        else ObjectSetText("Trend", "No trend", 14, "Impact", Gray);
    }
}

bool MA(string Direction, int TF)
{  
   bool up;
   for (int i = TrendMinDurationBar; i > 1; i--)
      {
         double SlowN = NormalizeDouble(iMA(Symbol(), TF, SlowPeriod, 0, MODE_SMA, PRICE_OPEN, i), Digits),
            SlowNmin1 = NormalizeDouble(iMA(Symbol(), TF, SlowPeriod, 0, MODE_SMA, PRICE_OPEN, i-1), Digits);
         
         if (Direction == "Up")
            {
               if (SlowN <= SlowNmin1) {up = true; return(true);}
               else if (SlowN > SlowNmin1) up = false;
            }
         if (up == false) break;
      }
   return(false);
}

bool NewBar(int TF)
{
    static datetime lastbar = 0;
    datetime curbar = iTime(Symbol(), TF, 0);

    if (lastbar != curbar)
        {
            lastbar = curbar; 
            return(true);
        }
    else return(false);
}

The code for showing Trend Down not yet written due to main problem still exist, which is, I cannot show correct alert or text "Trend Up" when the algorithm sequence is true, instead it's showing "Trend Up bias", and vice versa, when the algorithm sequence should be "bias", instead it shows "Trend Up good".

Can anyone help me to fix the code, please?

 
Anyone help, please...
Reason: