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
Thank you Vladimir, works perfect!
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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?