//+------------------------------------------------------------------+
//|                                                                  |
//|                                       Auto trend forecaster.mq4  |
//|                                                                  |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 DodgerBlue
#property indicator_color2 OrangeRed

extern int TMperiod = 8;
extern double Intensity = 1.9;
extern int SL_distance_pips = 200;
extern bool alarm_on = false;
extern int meth_smooth = MODE_SMMA;
extern int prix = PRICE_LOW;

double wma_up[];
double wma_down[];
double wma_buf[];
datetime g_time_112;


double WMA(int ai_0, int a_period_4) {
   return (iMA(NULL, 0, a_period_4, 0, meth_smooth, prix, ai_0));
}

int start() {
   double wma_temp[];
   double lda_24[];
   double ld_36;
   int counted_bars = IndicatorCounted();
   if (counted_bars < 0) return (-1);
   int ii = 1;
   int smoothing = MathFloor(MathSqrt(TMperiod));
   int per_wma = MathFloor(TMperiod / Intensity);
   int li_16 = Bars - counted_bars + TMperiod + 1;
   if (li_16 > Bars) li_16 = Bars;
   ArraySetAsSeries(wma_temp, TRUE);
   ArrayResize(wma_temp, li_16);
   ArraySetAsSeries(lda_24, TRUE);
   ArrayResize(lda_24, li_16);
   double ld_28 = Close[1];
   for (ii = 0; ii < li_16; ii++) wma_temp[ii] = 2.0 * WMA(ii, per_wma) - WMA(ii, TMperiod);
   for (ii = 0; ii < li_16 - TMperiod; ii++) wma_buf[ii] = iMAOnArray(wma_temp, 0, smoothing, 0, meth_smooth, ii);
   for (ii = li_16 - TMperiod; ii > 0; ii--) {
      lda_24[ii] = lda_24[ii + 1];
      if (wma_buf[ii] > wma_buf[ii + 1]) lda_24[ii] = 1;
      if (wma_buf[ii] < wma_buf[ii + 1]) lda_24[ii] = -1;
      if (lda_24[ii] > 0.0) {
         wma_up[ii] = wma_buf[ii];
         if (lda_24[ii + 1] < 0.0) wma_up[ii + 1] = wma_buf[ii + 1];
         if (lda_24[ii + 1] < 0.0) {
            if (ii == 1) {
               ld_36 = ld_28 - SL_distance_pips * Point;
              if( alarm_on ) DisplayAlert("UP Buy ", 0, ld_36, ld_28);
            }
         }
         wma_down[ii] = EMPTY_VALUE;
      } else {
         if (lda_24[ii] < 0.0) {
            wma_down[ii] = wma_buf[ii];
            if (lda_24[ii + 1] > 0.0) wma_down[ii + 1] = wma_buf[ii + 1];
            if (lda_24[ii + 1] > 0.0) {
               if (ii == 1) {
                  ld_36 = ld_28 + SL_distance_pips * Point;
                if( alarm_on )  DisplayAlert("DOWN Sell ", 0, ld_36, ld_28);
               }
            }
            wma_up[ii] = EMPTY_VALUE;
         }
      }
   }
   return (0);
}


void DisplayAlert(string as_0, double ad_8, double ad_16, double ad_24) {
   string ls_32;
   string ls_40;
   string ls_48;
   if (Time[0] != g_time_112) {
      g_time_112 = Time[0];
      if (ad_24 != 0.0) ls_48 = "Price " + DoubleToStr(ad_24, 4);
      else ls_48 = "";
      if (ad_8 != 0.0) ls_40 = ", TakeProfit   " + DoubleToStr(ad_8, 4);
      else ls_40 = "";
      if (ad_16 != 0.0) ls_32 = ", StopLoss   " + DoubleToStr(ad_16, 4);
      else ls_32 = "";
      Alert("Auto TREND " + as_0 + ls_48 + ls_40 + ls_32 + " ", Symbol(), ", ", Period(), " min");
   }
}

int init() {
   int _width;
   if (TMperiod == 8) _width = 2;
   else               _width = 4;
   IndicatorBuffers(3);
   string ls_4 = "(" + TMperiod + ")";
 
   SetIndexBuffer(0, wma_up);
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, _width);
   SetIndexLabel(0, "" + ls_4);
  
   SetIndexBuffer(1, wma_down);
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, _width);
   SetIndexLabel(1, "" + ls_4);

   SetIndexBuffer(2, wma_buf);
   IndicatorShortName("AUTO TREND FORECASTER");
   IndicatorDigits(MarketInfo(Symbol(), MODE_DIGITS) + 1.0);
   return (0);
}

int deinit() {
   return (0);
}
