4200 Object Already Exists Error for Chart Object.

 

Right, really beginning to lose my patience here, why do I keep getting the error 4200 - object already exists on the "MovingText" chart label when the chart label doesn't actually exist in the first place?

The text object does not already exist, hence why I'm placing it on the chart. 

//+------------------------------------------------------------------+
//|                                                       object.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

MessageBox("Please drop this onto any chart with an open position ","Information",MB_OK | MB_ICONINFORMATION);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
for(int s = OrdersTotal()-1; s>= 0; s--){
   
      if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES)){
      
         ObjectCreate("MovingText", OBJ_TEXT, 0,0,0);
         
         if(!ObjectCreate("MovingText", OBJ_TEXT, 0,0,0)){
         
            Print("Error "+IntegerToString(GetLastError(),0));
         
         }
         
         ObjectCreate(
         
            0,

                "ClosePosition",

                        OBJ_BUTTON,

                                0,

                                        0,

                                                0
   
         );
    
         ObjectSetInteger(0,"ClosePosition", OBJPROP_XDISTANCE, 0);
  
            ObjectSetInteger(0,"ClosePosition", OBJPROP_XSIZE, 400);
        
               ObjectSetInteger(0,"ClosePosition", OBJPROP_YDISTANCE, 20);
        
                  ObjectSetInteger(0,"ClosePosition", OBJPROP_YSIZE, 50);
        
                     ObjectSetInteger(0,"ClosePosition", OBJPROP_CORNER,CORNER_LEFT_UPPER);
         
                        ObjectSetInteger(0,"ClosePosition", OBJPROP_COLOR,clrWhite);
         
                           if(OrderProfit() > 0.01){
                           
                              ObjectSetInteger(0,"ClosePosition", OBJPROP_BGCOLOR, clrGreen);
                              
                                 ObjectSetString(0,"ClosePosition", OBJPROP_TEXT,"Close Ticket #"+IntegerToString(OrderTicket(),0)+" with profit "+AccountCurrency()+DoubleToString(OrderProfit(),2));
               
                           }
            
                           else {
                           
                              ObjectSetInteger(0,"ClosePosition", OBJPROP_BGCOLOR,clrCrimson);
                              
                                 ObjectSetString(0,"ClosePosition", OBJPROP_TEXT,"Close Ticket #"+IntegerToString(OrderTicket(),0)+" with loss "+AccountCurrency()+DoubleToString(OrderProfit(),2));
                           
                           }
         
                           ObjectSetInteger(0,"ClosePosition", OBJPROP_FONTSIZE,10);
                  
                              ObjectCreate(
                              
                                 0,
                                 
                                    "pip_indicator",
                                    
                                       OBJ_LABEL,
                                       
                                          0,
                                          
                                             0,
                                             
                                                0
                              );
                             
                              ObjectSetInteger(0,"pip_indicator", OBJPROP_XDISTANCE, 20);
                    
                                 ObjectSetInteger(0,"pip_indicator", OBJPROP_XSIZE, 400);
                          
                                    ObjectSetInteger(0,"pip_indicator", OBJPROP_YDISTANCE, 100);
                          
                                       ObjectSetInteger(0,"pip_indicator", OBJPROP_YSIZE, 50);
                          
                                          ObjectSetInteger(0,"pip_indicator", OBJPROP_CORNER,CORNER_LEFT_UPPER);
                           
                                             //ObjectSetInteger(0,"pip_indicator", OBJPROP_COLOR,clrWhite);
                                          
                                               ObjectSetString(0,"pip_indicator", OBJPROP_FONT,"Impact");
                                                      
                                                   ObjectSetInteger(0,"pip_indicator", OBJPROP_BGCOLOR, clrNONE);
                                                   
                                                      ObjectSetInteger(0,"pip_indicator", OBJPROP_FONTSIZE,30);
                                                      
                                                         if(OrderType() == OP_BUY){
                                                         
                                                            ObjectSetString(0,"pip_indicator", OBJPROP_TEXT,Symbol()+" "+DoubleToString((Bid - OrderOpenPrice())/Point,0));
                                                            
                                                               ObjectSetText("MovingText",Symbol()+" "+DoubleToString((Bid - OrderOpenPrice())/Point,0),20,"Impact",Red);

                                                         }
                                                         
                                                         else {
                                                         
                                                            ObjectSetString(0,"pip_indicator", OBJPROP_TEXT,Symbol()+" "+DoubleToString((OrderOpenPrice() - Ask)/Point,0));
                                                            
                                                               ObjectSetText("MovingText",Symbol()+" "+DoubleToStr((OrderOpenPrice() - Ask)/Point,0),20,"Impact");
                                                         
                                                         }
                                                         
                                                         if (OrderProfit()>= 0.01){
                                                         
                                                            ObjectSetInteger(0,"pip_indicator", OBJPROP_COLOR,clrGreen); 
                                                            
                                                               ObjectSetInteger(0,"MovingText", OBJPROP_COLOR,clrGreen); 
                                                         
                                                         } else {
                                                         
                                                            ObjectSetInteger(0,"pip_indicator", OBJPROP_COLOR,clrCrimson); 
                                                            
                                                               ObjectSetInteger(0,"MovingText", OBJPROP_COLOR,clrCrimson); 
                                                         
                                                         }
                                                         
                                 
                                    ObjectMove("MovingText",0,Time[0],Bid);                           
      
      } //if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
      
      else {
      
         MessageBox("OrderSelect Error #"+IntegerToString(GetLastError(),0),"Error",MB_OK | MB_ICONERROR);
      
      } //if(!OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
      
   }//for(int s = OrdersTotal()-1; s>= 0; s--)
   
  }
//+------------------------------------------------------------------+
 
Todd Peter Gilbey: why do I keep getting the error 4200 - object already exists on the "MovingText" chart label when the chart label doesn't actually exist in the first place?
      if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES)){
      
         ObjectCreate("MovingText", OBJ_TEXT, 0,0,0);

You are creating those objects for every open order on every tick. Of course, they exist after the first tick first order.

 
You can check first with ObjectFind()
Documentation on MQL5: Object Functions / ObjectFind
Documentation on MQL5: Object Functions / ObjectFind
  • www.mql5.com
ObjectFind - Object Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Reason: