//+------------------------------------------------------------------+
//|                                              UCS-LRS.mq5         |
//|                     Simple Linear Regression Slope               |
//|       Converted from PineScript by UCSgears - Version 2           |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   3
#property indicator_label1  "Zero Line"
#property indicator_label2  "Linear Regression Slope"
#property indicator_label3  "Average Slope"
#property indicator_color1  clrGray
#property indicator_color2  clrBlue
#property indicator_color3  clrGray
#property indicator_width2  2
#property indicator_type2   DRAW_HISTOGRAM

input int clen = 50;   // Curve Length
input int slen = 5;    // Slope Length
input int glen = 13;   // Signal Length

double lrcBuffer[];
double slrsBuffer[];
double alrsBuffer[];

double lrs;
double slrs;
double alrs;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0, lrcBuffer);
   SetIndexBuffer(1, slrsBuffer);
   SetIndexBuffer(2, alrsBuffer);
   
   PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_LINE);
   PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_HISTOGRAM);
   PlotIndexSetInteger(2, PLOT_DRAW_TYPE, DRAW_LINE);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   if (rates_total < clen) return(0);

   for (int i = prev_calculated; i < rates_total; i++)
     {
      // Linear Regression Curve
      lrcBuffer[i] = iMA(NULL, 0, clen, 0, MODE_LINEAR, PRICE_CLOSE, i);

      // Linear Regression Slope
      lrs = lrcBuffer[i] - lrcBuffer[i - 1];

      // Smooth Linear Regression Slope
      slrs = iMAOnArray(lrcBuffer, rates_total, slen, 0, MODE_EMA, i);

      // Signal Linear Regression Slope
      alrs = iMAOnArray(slrsBuffer, rates_total, glen, 0, MODE_SMA, i);

      slrsBuffer[i] = slrs;
      alrsBuffer[i] = alrs;

      // Setting color based on conditions
      if (lrs > alrs && lrs > 0)
         PlotIndexSetInteger(1, PLOT_LINE_COLOR, i, clrGreen);
      else if (lrs < alrs && lrs < 0)
         PlotIndexSetInteger(1, PLOT_LINE_COLOR, i, clrRed);
      else
         PlotIndexSetInteger(1, PLOT_LINE_COLOR, i, clrBlue);
     }

   return(rates_total);
  }
//+------------------------------------------------------------------+
