Making a Custom Indicator that draws trend lines according to MACD histogram bars

 

The Indicator I'm making works this way, it reads the Histogram bars of this custom indicator I made (MACD from TradingView). It finds the previous 4 chunks of bars separated by groups of above or below 0, then inside each group it finds the highest candle in the Price chart (if bars are above 0) and the lowest candle in the Price chart (if bars are below 0), finally it draws a trend line connecting the 2 high candles and the 2 low candles.

For now I'm trying to get the code to find and store those values, to check if the values are correct I want it to draw vertical lines at the beginning and at the end, but it draws the 4 lines at the candle (1).

#property strict
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 clrGreen
#property indicator_color2 clrRed
#property indicator_color3 clrBlue
#property indicator_color4 clrYellow

// Define the custom indicator handle
input string CustomIndicatorName = "Examples\\MACD TeamViewer";
input ENUM_TIMEFRAMES CustomIndicatorTimeframe = PERIOD_CURRENT;
input int CustomIndicatorMode = 2;

// Define arrays to store bars of the same level
double BarSet1[];
double BarSet2[];
double BarSet3[];
double BarSet4[];

int OnInit()
{
   SetIndexBuffer(0, BarSet1);
   SetIndexBuffer(1, BarSet2);
   SetIndexBuffer(2, BarSet3);
   SetIndexBuffer(3, BarSet4);

   return(INIT_SUCCEEDED);
}

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[])
{
   // Clear the arrays on each calculation
   ArrayInitialize(BarSet1, 0);
   ArrayInitialize(BarSet2, 0);
   ArrayInitialize(BarSet3, 0);
   ArrayInitialize(BarSet4, 0);

   // Loop through the buffer to find the series of bars
   double prevBarValue = iCustom(Symbol(), CustomIndicatorTimeframe, CustomIndicatorName, CustomIndicatorMode, 1);
   double currentBarValue;

   for (int shift = 1; shift < rates_total; shift++)
   {
      currentBarValue = iCustom(Symbol(), CustomIndicatorTimeframe, CustomIndicatorName, CustomIndicatorMode, shift);

      // Check if the current bar is of opposite level to the previous bar
      if ((prevBarValue > 0 && currentBarValue < 0) || (prevBarValue < 0 && currentBarValue > 0))
      {
         // Determine the series and store the bars
         if (currentBarValue > 0)
         {
            ArraySetAsSeries(BarSet1, true);
            ArraySetAsSeries(BarSet2, false);
            ArraySetAsSeries(BarSet3, false);
            ArraySetAsSeries(BarSet4, false);
            BarSet1[shift] = currentBarValue;
         }
         else
         {
            ArraySetAsSeries(BarSet1, false);
            ArraySetAsSeries(BarSet2, true);
            ArraySetAsSeries(BarSet3, false);
            ArraySetAsSeries(BarSet4, false);
            BarSet2[shift] = currentBarValue;
         }
      }
      
      if ((prevBarValue < 0 && currentBarValue > 0) || (prevBarValue > 0 && currentBarValue < 0))
      {
         // Determine the series and store the bars
         if (currentBarValue > 0)
         {
            ArraySetAsSeries(BarSet1, false);
            ArraySetAsSeries(BarSet2, false);
            ArraySetAsSeries(BarSet3, true);
            ArraySetAsSeries(BarSet4, false);
            BarSet3[shift] = currentBarValue;
         }
         else
         {
            ArraySetAsSeries(BarSet1, false);
            ArraySetAsSeries(BarSet2, false);
            ArraySetAsSeries(BarSet3, false);
            ArraySetAsSeries(BarSet4, true);
            BarSet4[shift] = currentBarValue;
         }
      }

      // Store subsequent bars of the same level
      else if (prevBarValue == currentBarValue)
      {
         BarSet1[shift] = currentBarValue;
         BarSet2[shift] = currentBarValue;
         BarSet3[shift] = currentBarValue;
         BarSet4[shift] = currentBarValue;
      }

      // Stop storing when finding an opposite bar again
      else
      {
         break;
      }

      prevBarValue = currentBarValue;
   }

   // Find the first and last index of each series
   int startBar1 = ArrayMinimum(BarSet1, WHOLE_ARRAY, 0);
   int endBar1 = ArrayMaximum(BarSet1, WHOLE_ARRAY, 0);
   int startBar2 = ArrayMinimum(BarSet2, WHOLE_ARRAY, 0);
   int endBar2 = ArrayMaximum(BarSet2, WHOLE_ARRAY, 0);
   int startBar3 = ArrayMinimum(BarSet3, WHOLE_ARRAY, 0);
   int endBar3 = ArrayMaximum(BarSet3, WHOLE_ARRAY, 0);
   int startBar4 = ArrayMinimum(BarSet4, WHOLE_ARRAY, 0);
   int endBar4 = ArrayMaximum(BarSet4, WHOLE_ARRAY, 0);

   // Draw vertical lines at the beginning and end of each series
   DrawVerticalLine("VerticalLine1", clrGreen, startBar1, endBar1);
   DrawVerticalLine("VerticalLine2", clrRed, startBar2, endBar2);
   DrawVerticalLine("VerticalLine3", clrBlue, startBar3, endBar3);
   DrawVerticalLine("VerticalLine4", clrYellow, startBar4, endBar4);

   return(rates_total);
}

// Function to draw a vertical line
void DrawVerticalLine(string name, color clr, int startBar, int endBar)
{
   if (startBar != -1 && endBar != -1)
   {
      double startTime = iTime(Symbol(), CustomIndicatorTimeframe, startBar);
      double endTime = iTime(Symbol(), CustomIndicatorTimeframe, endBar);
      double lowPrice = iLow(Symbol(), CustomIndicatorTimeframe, startBar);

      // Create a vertical line
      int line = ObjectCreate(0, name, OBJ_VLINE, 0, startTime, lowPrice);
      ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
      ObjectSetInteger(0, name, OBJPROP_RAY_LEFT, false);
      ObjectSetInteger(0, name, OBJPROP_TIME1, (int)startTime);
      ObjectSetInteger(0, name, OBJPROP_TIME2, (int)endTime);
      ObjectSetDouble(0, name, OBJPROP_PRICE1, lowPrice);
      ObjectSetDouble(0, name, OBJPROP_PRICE2, lowPrice);
   }
}