ObjectFind Freeze the Chart

 

Hello,


I used to update a trade panel everytick to show the current PnL of the trades.

This results to have some problems and to Freeze the tick flow sometimes, randomly.

I searched a bit and I found that is better to place a Sleep function or a timer to not call ObjectFind every tick.


That's my code, do you have any idea in how to make it more efficient?

void UpdatePanel()
  {
   if(positions != PositionsTotal())
     {
      positions = PositionsTotal();
      ObjectDelete(ChartID(),"Rectangle");
      ObjectDelete(ChartID(),"SEC");
      ObjectDelete(ChartID(),"sec_am");
      ObjectDelete(ChartID(),"Brick");
      ObjectDelete(ChartID(),"PNL");
      ObjectDelete(ChartID(),"PNL_Hedge");
      ObjectDelete(ChartID(),"PNL_bar");
      ObjectDelete(ChartID(),"PNL_Total");
      ObjectDelete(ChartID(),"Brick_Size");
      ObjectDelete(ChartID(),"NumPNL_Chain");
      ObjectDelete(ChartID(),"NumPNL_Hedge");
      ObjectDelete(ChartID(),"NumPNL_Total");
     }

   if(ObjectFind(0,"PNL") < 0)
     {
      rect_label.Create(0,"Rectangle",0,5,25,300,135);
      rect_label.BorderType(BORDER_FLAT);
      rect_label.Selectable(false);
      rect_label.BackColor(C'60,31,95');

      text_label.Create(0,"SEC",0,15,45);
      text_label.Anchor(ANCHOR_LEFT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Color(clrAliceBlue);
      text_label.Selectable(false);
      text_label.Description("Commissions & Spread:");

      text_label.Create(0,"Brick",0,15,65);
      text_label.Anchor(ANCHOR_LEFT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Color(clrAliceBlue);
      text_label.Selectable(false);
      text_label.Description("Brick Size:");

      text_label.Create(0,"PNL",0,15,85);
      text_label.Anchor(ANCHOR_LEFT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Color(clrLavender);
      text_label.Selectable(false);
      text_label.Description("PnL Current Chain:");

      text_label.Create(0,"PNL_Hedge",0,15,105);
      text_label.Anchor(ANCHOR_LEFT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Color(clrLavender);
      text_label.Selectable(false);
      text_label.Description("PnL Hedged Chains:");

      text_label.Create(0,"PNL_bar",0,15,118);
      text_label.Anchor(ANCHOR_LEFT);
      text_label.FontSize(8);
      text_label.Selectable(false);
      text_label.Color(clrLavender);
      text_label.Description("________________________________________");

      text_label.Create(0,"PNL_Total",0,15,140);
      text_label.Anchor(ANCHOR_LEFT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Color(clrAliceBlue);
      text_label.Selectable(false);
      text_label.Description("PnL Total:");
     }
   
   if(ObjectFind(0,"sec_am") < 0)
     {
      text_label.Create(0,"sec_am",0,295,45);
      text_label.Anchor(ANCHOR_RIGHT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Color(clrLavender);
      text_label.Selectable(false);
     }
   ObjectSetString(ChartID(),"sec_am",OBJPROP_TEXT,DoubleToString(GetTotalCommission(),2)+ " | " + (string)DoubleToString((SymbolInfoInteger(act_symbol,SYMBOL_SPREAD)*SymbolInfoDouble(act_symbol,SYMBOL_TRADE_TICK_SIZE)),_Digits));
 
   
   if(ObjectFind(0,"Brick_Size") < 0)
     {
      text_label.Create(0,"Brick_Size",0,295,65);
      text_label.Anchor(ANCHOR_RIGHT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Color(clrLavender);
      text_label.Selectable(false);
     }
   if(SymbolInfoInteger(act_symbol,SYMBOL_SPREAD)/(brick_size/SymbolInfoDouble(act_symbol,SYMBOL_TRADE_TICK_SIZE)) < 0.5)
      ObjectSetInteger(ChartID(),"Brick_Size",OBJPROP_COLOR,clrLavender);
   else
      ObjectSetInteger(ChartID(),"Brick_Size",OBJPROP_COLOR,C'240,113,103');
   ObjectSetString(ChartID(),"Brick_Size",OBJPROP_TEXT,DoubleToString(brick_size,_Digits));
   
   
   if(ObjectFind(0,"NumPNL_Chain") < 0)
     {
      text_label.Create(0,"NumPNL_Chain",0,295,85);
      text_label.Anchor(ANCHOR_RIGHT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Color(clrLavender);
      text_label.Selectable(false);
     }
   ObjectSetString(ChartID(),"NumPNL_Chain",OBJPROP_TEXT,DoubleToString(CheckPNL(),2) + " $  | " + DoubleToString(trade_volume,2));
   
   
   if(ObjectFind(0,"NumPNL_Hedge") < 0)
     {
      text_label.Create(0,"NumPNL_Hedge",0,295,105);
      text_label.Anchor(ANCHOR_RIGHT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Color(clrLavender);
      text_label.Selectable(false);
     }
   ObjectSetString(ChartID(),"NumPNL_Hedge",OBJPROP_TEXT,DoubleToString(CheckPNL_Hedge(),2) + " $  | " + DoubleToString(hedge_volume,2));
   
   
   if(ObjectFind(0,"NumPNL_Total") < 0)
     {
      text_label.Create(0,"NumPNL_Total",0,295,140);
      text_label.Anchor(ANCHOR_RIGHT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Selectable(false);
     }
   if(CheckPNL_Hedge()+CheckPNL() > 0)
      ObjectSetInteger(ChartID(),"NumPNL_Total",OBJPROP_COLOR,C'92,211,148');
   else
      if(CheckPNL_Hedge()+CheckPNL() == 0)
         ObjectSetInteger(ChartID(),"NumPNL_Total",OBJPROP_COLOR,clrLavender);
      else
         ObjectSetInteger(ChartID(),"NumPNL_Total",OBJPROP_COLOR,C'240,113,103');
   ObjectSetString(ChartID(),"NumPNL_Total",OBJPROP_TEXT,DoubleToString((CheckPNL_Hedge()+CheckPNL()),2) + " $  | " + DoubleToString((hedge_volume + trade_volume),2));

  }
 
arimbur: I searched a bit and I found that is better to place a Sleep function or a timer to not call ObjectFind every tick.

But that is not what your posted code does. It only runs when the position count changes. Your problem is elsewhere.

 
William Roeder #:

But that is not what your posted code does. It only runs when the position count changes. Your problem is elsewhere.

The problem is that the above code run every tick, and objectfind fails sometimes, I don't know how to fix it

That's another code I've done but still failing:

void UpdatePanel()
  {
   
   if(positions != PositionsTotal() && TimeCurrent() > timer + 5)
     {
      timer = TimeCurrent();
      positions = PositionsTotal();
      ObjectDelete(ChartID(),"Rectangle");
      ObjectDelete(ChartID(),"SEC");
      ObjectDelete(ChartID(),"sec_am");
      ObjectDelete(ChartID(),"Brick");
      ObjectDelete(ChartID(),"PNL");
      ObjectDelete(ChartID(),"PNL_Hedge");
      ObjectDelete(ChartID(),"PNL_bar");
      ObjectDelete(ChartID(),"PNL_Total");
      ObjectDelete(ChartID(),"Brick_Size");
      ObjectDelete(ChartID(),"NumPNL_Chain");
      ObjectDelete(ChartID(),"NumPNL_Hedge");
      ObjectDelete(ChartID(),"NumPNL_Total");

      rect_label.Create(0,"Rectangle",0,5,25,300,135);
      rect_label.BorderType(BORDER_FLAT);
      rect_label.Selectable(false);
      rect_label.BackColor(C'60,31,95');

      text_label.Create(0,"SEC",0,15,45);
      text_label.Anchor(ANCHOR_LEFT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Color(clrAliceBlue);
      text_label.Selectable(false);
      text_label.Description("Commissions & Spread:");

      text_label.Create(0,"Brick",0,15,65);
      text_label.Anchor(ANCHOR_LEFT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Color(clrAliceBlue);
      text_label.Selectable(false);
      text_label.Description("Brick Size:");

      text_label.Create(0,"PNL",0,15,85);
      text_label.Anchor(ANCHOR_LEFT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Color(clrLavender);
      text_label.Selectable(false);
      text_label.Description("PnL Current Chain:");

      text_label.Create(0,"PNL_Hedge",0,15,105);
      text_label.Anchor(ANCHOR_LEFT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Color(clrLavender);
      text_label.Selectable(false);
      text_label.Description("PnL Hedged Chains:");

      text_label.Create(0,"PNL_bar",0,15,118);
      text_label.Anchor(ANCHOR_LEFT);
      text_label.FontSize(8);
      text_label.Selectable(false);
      text_label.Color(clrLavender);
      text_label.Description("________________________________________");

      text_label.Create(0,"PNL_Total",0,15,140);
      text_label.Anchor(ANCHOR_LEFT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Color(clrAliceBlue);
      text_label.Selectable(false);
      text_label.Description("PnL Total:");

      text_label.Create(0,"sec_am",0,295,45);
      text_label.Anchor(ANCHOR_RIGHT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Color(clrLavender);
      text_label.Selectable(false);

      text_label.Create(0,"Brick_Size",0,295,65);
      text_label.Anchor(ANCHOR_RIGHT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Color(clrLavender);
      text_label.Selectable(false);

      text_label.Create(0,"NumPNL_Chain",0,295,85);
      text_label.Anchor(ANCHOR_RIGHT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Color(clrLavender);
      text_label.Selectable(false);

      text_label.Create(0,"NumPNL_Hedge",0,295,105);
      text_label.Anchor(ANCHOR_RIGHT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Color(clrLavender);
      text_label.Selectable(false);

      text_label.Create(0,"NumPNL_Total",0,295,140);
      text_label.Anchor(ANCHOR_RIGHT);
      text_label.Font("Verdana");
      text_label.FontSize(8);
      text_label.Selectable(false);
     }

   ObjectSetString(ChartID(),"sec_am",OBJPROP_TEXT,DoubleToString(GetTotalCommission(),2)+ " | " + (string)DoubleToString((SymbolInfoInteger(act_symbol,SYMBOL_SPREAD)*SymbolInfoDouble(act_symbol,SYMBOL_TRADE_TICK_SIZE)),_Digits));

   if(SymbolInfoInteger(act_symbol,SYMBOL_SPREAD)/(brick_size/SymbolInfoDouble(act_symbol,SYMBOL_TRADE_TICK_SIZE)) < 0.5)
      ObjectSetInteger(ChartID(),"Brick_Size",OBJPROP_COLOR,clrLavender);
   else
      ObjectSetInteger(ChartID(),"Brick_Size",OBJPROP_COLOR,C'240,113,103');
   ObjectSetString(ChartID(),"Brick_Size",OBJPROP_TEXT,DoubleToString(brick_size,_Digits));

   ObjectSetString(ChartID(),"NumPNL_Chain",OBJPROP_TEXT,DoubleToString(CheckPNL(),2) + " $  | " + DoubleToString(trade_volume,2));

   ObjectSetString(ChartID(),"NumPNL_Hedge",OBJPROP_TEXT,DoubleToString(CheckPNL_Hedge(),2) + " $  | " + DoubleToString(hedge_volume,2));

   if(CheckPNL_Hedge()+CheckPNL() > 0)
      ObjectSetInteger(ChartID(),"NumPNL_Total",OBJPROP_COLOR,C'92,211,148');
   else
      if(CheckPNL_Hedge()+CheckPNL() == 0)
         ObjectSetInteger(ChartID(),"NumPNL_Total",OBJPROP_COLOR,clrLavender);
      else
         ObjectSetInteger(ChartID(),"NumPNL_Total",OBJPROP_COLOR,C'240,113,103');
   ObjectSetString(ChartID(),"NumPNL_Total",OBJPROP_TEXT,DoubleToString((CheckPNL_Hedge()+CheckPNL()),2) + " $  | " + DoubleToString((hedge_volume + trade_volume),2));

  }