Lines not drawing on MQL5

 

Hey everyone, I'm coding some Pivot Points for MetaTrader5 but I'm having trouble drawing the lines.
The plan is to draw 7 horizontal lines (PP, R1, S1 etc.) however only the PP line is actually being placed on the chart.
I've attached the file below. Any and all help is appreciated!


//+------------------------------------------------------------------+
//|                                       Pivot Point Calculator.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.000"
#property indicator_chart_window
#property indicator_buffers 7
#property indicator_plots   7

//--- plot Pivot
#property indicator_label1  "Pivot"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrAqua
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

//--- plot Resistance1
#property indicator_label2  "Resistance1"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrCrimson
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot Resistance2
#property indicator_label3  "Resistance2"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrCrimson
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- plot Resistance3
#property indicator_label4  "Resistance3"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrCrimson
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1
//--- plot Support1
#property indicator_label5  "Support1"
#property indicator_type5   DRAW_LINE
#property indicator_color5  clrLime
#property indicator_style5  STYLE_SOLID
#property indicator_width5  1
//--- plot Support2
#property indicator_label6  "Support2"
#property indicator_type6   DRAW_LINE
#property indicator_color6  clrLime
#property indicator_style6  STYLE_SOLID
#property indicator_width6  1
//--- plot Support3
#property indicator_label7  "Support3"
#property indicator_type7   DRAW_LINE
#property indicator_color7  clrLime
#property indicator_style7  STYLE_SOLID
#property indicator_width7  1



//--- input parameters
input string   PPMode="Floor";
//--- indicator buffers
double         PivotBuffer[];
double         Resistance1Buffer[];
double         Resistance2Buffer[];
double         Resistance3Buffer[];
double         Support1Buffer[];
double         Support2Buffer[];
double         Support3Buffer[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   Alert("NEW DEBUG!");
   double price_high = iHigh(_Symbol, _Period, 0);
   double price_low = iLow(_Symbol, _Period, 0);
   double price_close = iClose(_Symbol, _Period, 0);


   if(PPMode == "Floor")
     {
      // Define the Pivot line calculations and other information
      double PivotCalculation = (price_high + price_low + price_close) / 3;

      double R1Calculation = (2 * PivotCalculation) - price_high;
      double R2Calculation = PivotCalculation + (price_high - price_low);
      double R3Calculation = price_high + 2 * (PivotCalculation - price_low);

      double S1Calculation = (2 * PivotCalculation) - price_high;
      double S2Calculation = PivotCalculation - (price_high - price_low);
      double S3Calculation = price_low - 2 * (price_high - PivotCalculation);

      double lineTimeFrame = PERIOD_CURRENT;

      // Set index buffers
      SetIndexBuffer(0, PivotBuffer);
      SetIndexBuffer(1, Resistance1Buffer);
      SetIndexBuffer(2, Resistance2Buffer);
      SetIndexBuffer(3, Resistance3Buffer);
      SetIndexBuffer(4, Support1Buffer);
      SetIndexBuffer(5, Support2Buffer);
      SetIndexBuffer(6, Support3Buffer);

      // Draw pivot line
      string PivotLineName = "P";
      double lineHandlePP = ObjectCreate(0, PivotLineName, OBJ_HLINE, 0, 0, PivotCalculation);
      if(lineHandlePP == INVALID_HANDLE)
        {
         Print("Pivot line error: Invalid Handle.");
        }
      ObjectSetInteger(0, PivotLineName, OBJPROP_COLOR, indicator_color1);
      ObjectSetInteger(0, PivotLineName, OBJPROP_STYLE, indicator_style1);
      ObjectSetInteger(0, PivotLineName, OBJPROP_WIDTH, indicator_width1);
      Alert("P Line:" + PivotCalculation);

      // Draw R1 line
      PivotLineName = "R1";
      double lineHandleR1 = ObjectCreate(1, PivotLineName, OBJ_HLINE, 0, 0, R1Calculation);
      if(lineHandleR1 == INVALID_HANDLE)
        {
         Print("R1 line error: Invalid Handle.");
        }
      ObjectSetInteger(1, PivotLineName, OBJPROP_COLOR, indicator_color2);
      ObjectSetInteger(1, PivotLineName, OBJPROP_STYLE, indicator_style2);
      ObjectSetInteger(1, PivotLineName, OBJPROP_WIDTH, indicator_width2);
      Alert("R1 Line:" + ((2 * PivotCalculation) - price_high));

      // Draw R2 line
      PivotLineName = "R2";
      double lineHandleR2 = ObjectCreate(2, PivotLineName, OBJ_HLINE, 0, 0, R2Calculation);
      if(lineHandleR2 == INVALID_HANDLE)
        {
         Print("R2 line error: Invalid Handle.");
        }
      ObjectSetInteger(2, PivotLineName, OBJPROP_COLOR, indicator_color3);
      ObjectSetInteger(2, PivotLineName, OBJPROP_STYLE, indicator_style3);
      ObjectSetInteger(2, PivotLineName, OBJPROP_WIDTH, indicator_width3);
      Alert("R2 Line:" + (PivotCalculation + (price_high - price_low)));

      // Draw R3 line
      PivotLineName = "R3";
      double lineHandleR3 = ObjectCreate(3, PivotLineName, OBJ_HLINE, 0, 0, R3Calculation);
      if(lineHandleR3 == INVALID_HANDLE)
        {
         Print("R3 line error: Invalid Handle.");
        }
      ObjectSetInteger(3, PivotLineName, OBJPROP_COLOR, indicator_color4);
      ObjectSetInteger(3, PivotLineName, OBJPROP_STYLE, indicator_style4);
      ObjectSetInteger(3, PivotLineName, OBJPROP_WIDTH, indicator_width4);
      Alert("R3 Line:" + (price_high + 2 * (PivotCalculation - price_low)));

      // Draw S1 line
      PivotLineName = "S1";
      double lineHandleS1 = ObjectCreate(4, PivotLineName, OBJ_HLINE, 0, 0, S1Calculation);
      if(lineHandleS1 == INVALID_HANDLE)
        {
         Print("S1 line error: Invalid Handle.");
        }
      ObjectSetInteger(4, PivotLineName, OBJPROP_COLOR, indicator_color5);
      ObjectSetInteger(4, PivotLineName, OBJPROP_STYLE, indicator_style5);
      ObjectSetInteger(4, PivotLineName, OBJPROP_WIDTH, indicator_width5);
      Alert("S1 Line:" + ((2 * PivotCalculation) - price_high));

      // Draw S2 line
      PivotLineName = "S2";
      double lineHandleS2 = ObjectCreate(5, PivotLineName, OBJ_HLINE, 0, 0, S2Calculation);
      if(lineHandleS2 == INVALID_HANDLE)
        {
         Print("S2 line error: Invalid Handle.");
        }
      ObjectSetInteger(5, PivotLineName, OBJPROP_COLOR, indicator_color6);
      ObjectSetInteger(5, PivotLineName, OBJPROP_STYLE, indicator_style6);
      ObjectSetInteger(5, PivotLineName, OBJPROP_WIDTH, indicator_width6);
      Alert("S2 Line:" + (PivotCalculation - (price_high - price_low)));

      PivotLineName = "S3";
      double lineHandleS3 = ObjectCreate(6, PivotLineName, OBJ_HLINE, 0, 0, S3Calculation);
      if(lineHandleS3 == INVALID_HANDLE)
        {
         Print("S3 line error: Invalid Handle.");
        }
      ObjectSetInteger(6, PivotLineName, OBJPROP_COLOR, indicator_color7);
      ObjectSetInteger(6, PivotLineName, OBJPROP_STYLE, indicator_style7);
      ObjectSetInteger(6, PivotLineName, OBJPROP_WIDTH, indicator_width7);
      Alert("S3 Line:" + (price_low - 2 * (price_high - PivotCalculation)));
     }
//---
   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[])




  {
//---

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


 
Another thing to note: it seems as if the PP line also isn't removed when I remove the indicator. I'm probably missing something very obvious there but, again, any and all help is very much appreciated.
 
  1.      double lineHandleS1 = ObjectCreate(4, PivotLineName, OBJ_HLINE, 0, 0, S1Calculation);
          if(lineHandleS1 == INVALID_HANDLE)
            {
             Print("S1 line error: Invalid Handle.");
            }
          ObjectSetInteger(4, PivotLineName, OBJPROP_COLOR, indicator_color5);
          ObjectSetInteger(4, PivotLineName, OBJPROP_STYLE, indicator_style5);
          ObjectSetInteger(4, PivotLineName, OBJPROP_WIDTH, indicator_width5);
          Alert("S1 Line:" + ((2 * PivotCalculation) - price_high));
    
          // Draw S2 line
          PivotLineName = "S2";
          double lineHandleS2 = ObjectCreate(5, PivotLineName, OBJ_HLINE, 0, 0, S2Calculation);
    ObjectCreate does not create a handle. ObjectCreate does not return a double.
  2. 4 or 5 are not valid chart IDs.
  3. Object names must be unique.
  4. int OnInit()
      {
       Alert("NEW DEBUG!");
       double price_high = iHigh(_Symbol, _Period, 0);
       double price_low = iLow(_Symbol, _Period, 0);
    Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.


 

Thanks for your feedback! I have a few questions:

  1. Instead of using ObjectCreate, what should I use instead?
  2. Why are 4 and 5 not valid chart IDs? Is there a fixed limit?
  3. Can I reinitialize variables like this? 
    use int test = 1;
    test = 2?
  4. I am putting the code in OnCalculate but I'm concerned that it will simply generate moving lines that moves with the chart. I don't want this, I simply want to draw them as if they were lines I would put in manually. Am I worrying over nothing?
Thanks for helping me, I can understand it's frustrating!
 

Nevermind, when rewriting the code most of these issues were ironed out!

Thank you very much for the help.

Reason: