Problem-Drawing Pivots on History

 

Hi guys, this is my first post on this forum, actually, on any forum I guess. Anyway I’m trying to draw pivots on the platform’s history and it seems to work fine but I added some H1 data dating from 2004 and when it reaches about 44,000 bars I get some weird behavior. I’m using a script because I was just interested in historical events, it’s a very simple code but I can’t see where I got it wrong. Any help appreciated.

string newDay(int barID)
 {
   string ND;
   int error;
   
   ND = StringSubstr(TimeToStr(iTime(NULL, 0, barID)), 5, 5);
   
   error=GetLastError(); 
   if(error>0)
     Print("error on newDay(",error,"): ", ErrorDescription(error));
   
   return (ND);
 }
 
 void objDoer(string name, double level, color col, int cur, datetime pdTime)
 {
   ObjectCreate(name, OBJ_TREND, 0, pdTime, level, iTime(NULL, 0, cur), level);  
   ObjectSet(name, OBJPROP_RAY, false);
   ObjectSet(name, OBJPROP_COLOR, col);
 }
 
 void drawHline(int barId, int current, int prev, int pastB) //Draws a horizontal line
 {
   //BarId and current are both i 
   int error;
   int i;
   string nom = barId;
   
   datetime PDT = iTime(NULL, 0, prev);      //Previous Day Time. Used to draw.
   
   datetime PPDT = iTime(NULL, 0, pastB);      //Past Previous Day Time. Used to calculate levels. 
   int PDB = iBarShift(NULL, PERIOD_D1, PPDT); //Previous Day Bar
   
   double pH = iHigh(NULL,PERIOD_D1, PDB);    //Previous High
   double pL = iLow(NULL,PERIOD_D1, PDB);     //Previous Low
   double pC = iClose(NULL,PERIOD_D1, PDB);   //Previous Close
   
   double PP = ( pH  + pC + pL )/3;
   double R1 = 2 * PP - pL;
   double S1 = 2 * PP - pH;
   double R2 = PP + (R1 - S1);
   double S2 = PP - (R1 - S1);
   double R3 = pH + 2*(PP - pL);     
   double S3 = pL - 2*(pH - PP);
   
   double Mr1 = (R1 + PP)/2;
   double Mr2 = (R2 + R1)/2;
   double Mr3 = (R3 + R2)/2;
   double Ms1 = (PP + S1)/2;
   double Ms2 = (S1 + S2)/2;
   double Ms3 = (S2 + S3)/2;
      
   //Pivot Point, Support and Resistance Levels Calculated with iFunct to get access to prev day bar   
   objDoer("PP_"+nom, PP, Blue, current, PDT);      //Pivot Point
   objDoer("R1_"+nom, R1, Lime, current, PDT);      //R1
   objDoer("S1_"+nom, S1, Red, current, PDT);       //S1
   objDoer("R2_"+nom, R2, Lime, current, PDT);      //R2
   objDoer("S2_"+nom, S2, Red, current, PDT);       //S2
   objDoer("R3_"+nom, R3, DarkGreen, current, PDT); //R3 
   objDoer("S3_"+nom, S3, Maroon, current, PDT);    //S3
   
   //Middle Points  
   objDoer("Mr1_"+nom, Mr1, PaleTurquoise, current, PDT);   //Mr1
   objDoer("Mr2_"+nom, Mr2, PaleTurquoise, current, PDT);   //Mr2
   objDoer("Mr3_"+nom, Mr3, PaleTurquoise, current, PDT);   //Mr3
   objDoer("Ms1_"+nom, Ms1, PaleTurquoise, current, PDT);   //Ms1    
   objDoer("Ms2_"+nom, Ms2, PaleTurquoise, current, PDT);   //Ms2   
   objDoer("Ms3_"+nom, Ms3, PaleTurquoise, current, PDT);   //Ms3
   
   error=GetLastError();
   if(error>0)
     Print("error on drawHline(",error,"): ", ErrorDescription(error));
 } 

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
   
      int dayA[3] = {0,0,0};
      string ndayArray[2]={"",""};
      int i;
      int error;
   
      for(i=44000; i>0; i--)
      { 
         ndayArray[0] = newDay(i);
         
         if (ndayArray[0] != ndayArray[1])//  
         {
            if (dayA[2] == 0)             //First Encounter 
               dayA[2] = i;
            else if (dayA[1] == 0)        //Second Encounter 
               dayA[1] = i;
            else                          //All other encounters
            {     
               dayA[0] = i;
               drawHline(i, dayA[0], dayA[1], dayA[2]);
               dayA[2] = dayA[1];
               dayA[1] = dayA[0];
            }
         }
         ndayArray[1] = ndayArray[0];
       }
       error=GetLastError();
    
       if(error>0)
         Print("error on barOrg(",error,"): ", ErrorDescription(error));
   
   //---   
   return(0);
  }
 
envelop:

but I added some H1 data dating from 2004

That's great . . . as you are using PERIOD_D1 I assume you also added some D1 data for the same date range ?
 
Oh man, your right. Thanks RaptorUK! So that fixed it up. For anyone interested I'll attach the script.
Reason: