Text not updating when starting metatrader

To add comments, please log in or register
marketcycle17
75
marketcycle17  

I use ObjectSetString() to show a text on the chart but it's not being painted. The only time it starts to paint the text is when I switch timeframe (and even then, it takes about 1 second for the timeframe to switch, which never happens otherwise).

Instead of painting the text it shows the word "Label". Look at the screenshot to see what happens when I start MT5.

I also checked the log files and it says this: "zero divide in 'GSR.mq5' (33,102)".

What needs to be changed in the code? Indicator and screenshot is attached.


Edit: seems mostly the problem would be the iClose() in OnInit(). So it can't get price data?

Documentation on MQL5: Object Functions / ObjectSetString
Documentation on MQL5: Object Functions / ObjectSetString
  • www.mql5.com
The function sets the value of the corresponding object property. The object property must be of the string type. There are 2 variants of the function. [in]  Modifier of the specified property.  It denotes the number of the level in Fibonacci tools and in the graphical object Andrew's pitchfork. The numeration of levels starts from zero. The...
Files:
Vladimir Karputov
Moderator
214607
Vladimir Karputov  

Corrected code:

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2020,CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0

#property description "Shows present gold-silver-ratio. Formula: ratio = goldprice / silverprice.\n"
#property description "You need to have gold and silver symbolname visible in market watch window for this to work. Also the symbolname in the code of iClose() has to be the same as in market watch."

input color             Label_Color = clrOrange;
input ENUM_BASE_CORNER  Corner      = CORNER_RIGHT_UPPER;

string objname="GSR: ";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   ObjectCreate(0,objname,OBJ_LABEL,0,0,0);
   ObjectSetInteger(0,objname,OBJPROP_CORNER,Corner);
   ObjectSetInteger(0,objname,OBJPROP_XDISTANCE,10);
   ObjectSetInteger(0,objname,OBJPROP_YDISTANCE,2);
   ObjectSetString(0,objname,OBJPROP_FONT,"Arial Black");
   ObjectSetInteger(0,objname,OBJPROP_COLOR,Label_Color);
   ObjectSetInteger(0,objname,OBJPROP_FONTSIZE,10);
   ENUM_ANCHOR_POINT Anchor = ANCHOR_LEFT_UPPER;
   switch(Corner)
     {
      case CORNER_LEFT_UPPER:
         Anchor=ANCHOR_LEFT_UPPER;
         break;
      case CORNER_RIGHT_UPPER:
         Anchor=ANCHOR_RIGHT_UPPER;
         break;
      case CORNER_LEFT_LOWER:
         Anchor=ANCHOR_LEFT_LOWER;
         break;
      case CORNER_RIGHT_LOWER:
         Anchor=ANCHOR_RIGHT_LOWER;
         break;
     }
   ObjectSetInteger(0,objname,OBJPROP_ANCHOR,Anchor);
//--- get price
   double close_XAUUSD_d1_0=iClose("XAUUSD",PERIOD_D1,0);
   double close_XAGUSD_d1_0=iClose("XAGUSD",PERIOD_D1,0);
   ObjectSetString(0,objname,OBJPROP_TEXT,"GSR: ");
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Indicator deinitialization function                              |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ObjectDelete(0,objname);
  }
//+------------------------------------------------------------------+
//| 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& spreads[])
  {
   if(prev_calculated==0)
      return(rates_total);
//--- get price
   double close_XAUUSD_d1_0=iClose("XAUUSD",PERIOD_D1,0);
   double close_XAGUSD_d1_0=iClose("XAGUSD",PERIOD_D1,0);
   if(close_XAUUSD_d1_0==0.0 || close_XAGUSD_d1_0==0.0)
      return(rates_total);
   ObjectSetString(0,objname,OBJPROP_TEXT,"GSR: "+DoubleToString((close_XAUUSD_d1_0/close_XAGUSD_d1_0),2));
//---
   return(rates_total);
  }
//+------------------------------------------------------------------+


Result:


Files:
GSR_indi.mq5 4 kb
marketcycle17
75
marketcycle17  
Thank you Vladimir, works perfect!
To add comments, please log in or register