Objects on the chart keep disappearing

 

Hi I have this EA which works perfectly although i cant seem to figure out why the pivot zones and lines are disappearing. How can I ensure previous pivot points and zones remain throughout the backtest? Spent countless hours trying to figure this out but cant seem to make progress. Would appreciate the answer or at least some learning material 

datetime old_time;
string symbol_loop[] = {"GBPUSD"};

input int PercentageOfRangeAsZones = 10;

int OnInit() {


   return (INIT_SUCCEEDED);
}

void OnDeinit(const int reason) {
   // Deinitialization logic, if needed
}

void OnTick() {
   datetime GMT = iTime(_Symbol, PERIOD_W1, 1);
   if (GMT > old_time) {
      old_time = GMT;

      for (int i = 0; i < ArraySize(symbol_loop); i++) {
         string symbol = symbol_loop[i];
         // Your code for creating lines and zones here
         
                 int digits = GetDecimalPlaces(symbol);

      double tickValue = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_VALUE);
      double contractSize = SymbolInfoDouble(symbol, SYMBOL_TRADE_CONTRACT_SIZE);
      double point = tickValue / contractSize;   
      double PipValue = point * 10;
      
      
      double high1 = iHigh(symbol, PERIOD_W1, 1);
      double low1 = iLow(symbol, PERIOD_W1, 1);
      double close1 = iClose(symbol, PERIOD_W1, 1);
      
      datetime time1 = iTime(symbol, PERIOD_W1, 0);
      datetime time2 = time1 + (PeriodSeconds(PERIOD_D1)*6);
      
      //------------------------------------//
      // PIVOT LINES                        //
      //------------------------------------//    
      
      double pivotPoint = ((high1 + low1 + close1) / 3);
      string ObjName = "Pivot Point";
      createLine(ObjName, clrYellow, time1, pivotPoint, time2, pivotPoint);  
      
      double r1 = pivotPoint * 2 - low1;
      ObjName = "R1";
      createLine(ObjName, clrWhite, time1, r1, time2, r1);
      
      double r2 = pivotPoint + high1 - low1;
      ObjName = "R2";
      createLine(ObjName, clrWhite, time1, r2, time2, r2);     

      double s1 = pivotPoint * 2 - high1;
      ObjName = "S1";
      createLine(ObjName, clrWhite, time1, s1, time2, s1);
      
      double s2 = pivotPoint - (high1 - low1);
      ObjName = "S2";
      createLine(ObjName, clrWhite, time1, s2, time2, s2); 
      
      //------------------------------------//
      // PIVOT ZONES                        //
      //------------------------------------//  
      
      //Zones Calculation
      double range = NormalizeDouble(((r2 - s2) / PipValue), 0);
      double zonesize = 0.05 * range;//(PercentageOfRangeAsZones / 100) * range;
      double zonesizeinSymbolFormat = NormalizeDouble((zonesize * PipValue), digits);
      double zonesizeinSymbolFormationHalf = NormalizeDouble((zonesizeinSymbolFormat / 2), digits);
      
      //Pivot Zone 
      double pivotPointUpperZone = pivotPoint + zonesizeinSymbolFormationHalf;
      double pivotPointLowerZone = pivotPoint - zonesizeinSymbolFormationHalf;     
      string zoneName = "Pivot Point Zone";
      createZone(zoneName, clrRed, time1, pivotPointUpperZone, time2, pivotPointLowerZone);
   
      //R1 Zone
      double r1UpperZone = r1 + zonesizeinSymbolFormationHalf;      
      double r1LowerZone = r1 - zonesizeinSymbolFormationHalf;
      zoneName = "R1 Zone";
      createZone(zoneName, clrRed, time1, r1UpperZone, time2, r1LowerZone);
      
      //S1 Zone
      double s1UpperZone = s1 + zonesizeinSymbolFormationHalf;      
      double s1LowerZone = s1 - zonesizeinSymbolFormationHalf;
      zoneName = "S1 Zone";
      createZone(zoneName, clrRed, time1, s1UpperZone, time2, s1LowerZone);

      //R2 Zone
      double r2UpperZone = r2 + zonesizeinSymbolFormationHalf;      
      double r2LowerZone = r2 - zonesizeinSymbolFormationHalf;
      zoneName = "R2 Zone";
      createZone(zoneName, clrRed, time1, r2UpperZone, time2, r2LowerZone);
      
      //S2 Zone
      double s2UpperZone = s2 + zonesizeinSymbolFormationHalf;      
      double s2LowerZone = s2 - zonesizeinSymbolFormationHalf;
      zoneName = "S2 Zone";
      createZone(zoneName, clrRed, time1, s2UpperZone, time2, s2LowerZone);
      }
   }
}

void createLine(string objName, color clr, datetime time1, double price1, datetime time2, double price2) {
   ObjectCreate(0, objName, OBJ_TREND, 0, time1, price1, time2, price2);
   ObjectSetInteger(0, objName, OBJPROP_COLOR, clr);
   ObjectSetInteger(0, objName, OBJPROP_WIDTH, 3);
}

void createZone(string zoneName, color clr, datetime time1, double price1, datetime time2, double price2) {
   ObjectCreate(0, zoneName, OBJ_RECTANGLE, 0, time1, price1, time2, price2);
   ObjectSetInteger(0, zoneName, OBJPROP_FILL, clr);
   ObjectSetInteger(0, zoneName, OBJPROP_COLOR, clr);
}

int GetDecimalPlaces(const string symbol) {
   double tick_size = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE);
   int decimal_places = 0;

   while (tick_size < 1.0) {
      tick_size *= 10.0;
      decimal_places++;
   }

   return decimal_places;
}
 

You are using the same object name on each loop iteration, so in essence you are simply moving them along from the previous position to the new position.

Instead assign a unique name for each iteration. For example, you could use the bar's open time to make the object's name unique.

 
Fernando Carreiro #:

You are using the same object name on each loop iteration, so in essence you are simply moving them along from the previous position to the new position.

Instead assign a unique name for each iteration. For example, you could use the bar's open time to make the object's name unique.

Thank you. For those who may need help, by simply adding the following to the object name, the objects remain :)

+ IntegerToString(i) + "_" + TimeToString(time1, TIME_DATE | TIME_MINUTES);

I know its not part of the original question but how can i make this work for multiple currencies? As i add another pair to the symbol_loop, nothing works

 
tradingbetter #: I know its not part of the original question but how can i make this work for multiple currencies? As i add another pair to the symbol_loop, nothing works

A chart, only displays one symbol at a time, and if you are adding graphical objects to it, it should be be for the current chart symbol and no other.

If you want to generate graphical objects for other symbols, then generate them on other charts already open for those symbols, or open up new charts for them.