MQL4 Indicator help

 
I am having an exorbitant amount of trouble incorporating my custom Indicator into an EA. The Indicator calculates 3 values and no matter how I try to get the EA to retrieve the indicator values at the conclusion of a bar, the EA always gets data that differs from the indicators calculations. Can someone please point me in the right direction? The EA properly getting the indicator data is crucial to what I am trying to make. Here is the indicator code: can someone please help
//+------------------------------------------------------------------+
//|         RL10, RL30 with Custom PSAR Indicator                   |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 clrRed
#property indicator_width1 2
#property indicator_color2 clrBlue
#property indicator_width2 2
#property indicator_color3 clrGreen
#property indicator_width3 2

input int lengthRL10 = 10;       // Length for the 10-period regression line
input int lengthRL30 = 30;       // Length for the 30-period regression line
input double psarStep = 0.02;    // PSAR step
input double psarMax = 0.2;      // PSAR maximum

double regBufferRL10[]; // Buffer for RL10 values
double regBufferRL30[]; // Buffer for RL30 values
double psarBuffer[];    // Buffer for PSAR values

//+------------------------------------------------------------------+
//| Initialization                                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   SetIndexBuffer(0, regBufferRL10, INDICATOR_DATA); // RL10 buffer
   SetIndexStyle(0, DRAW_LINE);
   IndicatorShortName("RL10 & RL30 with Custom PSAR");

   SetIndexBuffer(1, regBufferRL30, INDICATOR_DATA); // RL30 buffer
   SetIndexStyle(1, DRAW_LINE);

   SetIndexBuffer(2, psarBuffer, INDICATOR_DATA); // PSAR buffer
   SetIndexStyle(2, DRAW_ARROW); // Use arrows for PSAR
   SetIndexArrow(2, 159); // ASCII code for a small circle

   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Deinitialization                                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   // No file to close
}

//+------------------------------------------------------------------+
//| Calculation                                                      |
//+------------------------------------------------------------------+
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 < MathMax(lengthRL10, lengthRL30)) return(0); // Ensure enough data

   int limit = rates_total - prev_calculated;
   if (prev_calculated > 0) limit++; // To recalculate the last bar

   int i; // Declare 'i' once to avoid redeclaration

   for (i = limit - 1; i >= 0; i--)
   {
      // RL10 Calculation
      regBufferRL10[i] = CalculateRegression(close, i, lengthRL10);

      // RL30 Calculation
      regBufferRL30[i] = CalculateRegression(close, i, lengthRL30);

      // Custom PSAR Calculation based on RL10
      psarBuffer[i] = CalculatePSAR(i, regBufferRL10);
   }

   return(rates_total);
}

//+------------------------------------------------------------------+
//| Regression Line Calculation                                      |
//+------------------------------------------------------------------+
double CalculateRegression(const double &data[], int startIndex, int length)
{
   double sumX = 0.0, sumY = 0.0, sumXY = 0.0, sumXX = 0.0;

   for (int j = 0; j < length; j++)
   {
      double x = j;
      double y = data[startIndex + j];
      sumX += x;
      sumY += y;
      sumXY += x * y;
      sumXX += x * x;
   }

   double denominator = length * sumXX - sumX * sumX;
   if (denominator == 0.0) return EMPTY_VALUE;

   double slope = (length * sumXY - sumX * sumY) / denominator;
   double intercept = (sumY - slope * sumX) / length;

   return intercept;
}

//+------------------------------------------------------------------+
//| Custom PSAR Calculation                                          |
//+------------------------------------------------------------------+
double CalculatePSAR(int index, const double &rlBuffer[])
{
   static double psar = 0.0;
   static double af = psarStep;
   static double ep = 0.0;
   static bool uptrend = true;

   if (index == 0)
   {
      psar = rlBuffer[index];
      ep = rlBuffer[index];
      uptrend = true;
      af = psarStep;
      return psar;
   }

   double previousPsar = psar;

   if (uptrend)
   {
      psar = previousPsar + af * (ep - previousPsar);
      if (rlBuffer[index] < psar)
      {
         uptrend = false;
         psar = ep;
         af = psarStep;
         ep = rlBuffer[index];
      }
      else if (rlBuffer[index] > ep)
      {
         ep = rlBuffer[index];
         af = MathMin(af + psarStep, psarMax);
      }
   }
   else
   {
      psar = previousPsar + af * (ep - previousPsar);
      if (rlBuffer[index] > psar)
      {
         uptrend = true;
         psar = ep;
         af = psarStep;
         ep = rlBuffer[index];
      }
      else if (rlBuffer[index] < ep)
      {
         ep = rlBuffer[index];
         af = MathMin(af + psarStep, psarMax);
      }
   }

   return psar;
}
 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
YFOE: I am having an exorbitant amount of trouble incorporating my custom Indicator into an EA. The Indicator calculates 3 values and no matter how I try to get the EA to retrieve the indicator values at the conclusion of a bar, the EA always gets data that differs from the indicators calculations. Can someone please point me in the right direction? The EA properly getting the indicator data is crucial to what I am trying to make. Here is the indicator code: can someone please help

There is no need to convert or incorporate an indicator's code into an EA. To use a custom indicator in an EA, simply use the iCustom() function to call on it.

If you are already doing that, then show your code in your EA that calls it so that we can help. Remember to show all relevant code.

iCustom - Technical Indicators - MQL4 Reference
iCustom - Technical Indicators - MQL4 Reference
  • docs.mql4.com
iCustom - Technical Indicators - MQL4 Reference
 
YFOE: I am having an exorbitant amount of trouble incorporating my custom Indicator into an EA

Don't try to do that. There are no buffers, no IndicatorCounted() or prev_calculated in EAs. No way to know if older bars have changed or been added (history update.)

Just get the value(s) of the indicator(s) into EA/indicator (using iCustom) and do what you want with it.
          (MT4) Detailed explanation of iCustom - MQL4 programming forum (2017)


You should encapsulate your custom indicator iCustom calls to make your code self-documenting:
          take candle color Hekin Ashi - MQL4 and MetaTrader 4 #8-10 or #1 (2018)

Not compiled, not tested, just typed.

enum CIbufers{clRL10, clRL30, clPsar}
class CI{
 public:
   void CI(int lengthRL10 = 10, int lengthRL30 = 30, double psarStep = 0.02, double psarMax = 0.2)
                             : mRL10(lengthRL10), mRL30(lengthRL30), mStep(psarStep), mMax(psarMax){}
    double read(CLbuffers b, int shift){
       return iCustom(_Symbol, _Period, "CIname",
                      mRL10, mRL30, MStep, mMax, 
                      b, shift);
   }
 private:
   int mRL10, mRL30; double mStep, mMax;
}
Not compiled, not tested, just typed.