Broker Spread Indicator

 

Hi,

 

Is there a better way to write this indicator so that it will work for both 4 & 5 digits brokers?

 

#property copyright ""
#property link      ""

#property indicator_chart_window


extern color LabelColor = Red;

#define OBJ_NAME "MarketWatchSpread"
#define OBJ_NAME1 "MarketWatchSpread"
int market_info;
double scale = 0.1;

int init()
{
   ShowSpread();
}

int start()
{
   ShowSpread();
}

int deinit()
{
   ObjectDelete(OBJ_NAME);
}

void ShowSpread()
{
   market_info = Digits();
   if(market_info ==4 || market_info ==2 ) scale = 1; 
   static double AUDUSD, EURJPY, EURUSD, GBPJPY, GBPUSD, NZDUSD, USDCAD, USDJPY;
   static double AUDUSD1,AUDUSD2;
   static double AUDCAD, AUDJPY, AUDNZD, CHFJPY, EURAUD, EURCAD, EURCHF, EURGBP, GBPCHF, USDCHF;
   
   AUDUSD = MarketInfo("AUDUSD", MODE_SPREAD);
   EURJPY = MarketInfo("EURJPY", MODE_SPREAD);
   EURUSD = MarketInfo("EURUSD", MODE_SPREAD);
   GBPJPY = MarketInfo("GBPJPY", MODE_SPREAD);
   GBPUSD = MarketInfo("GBPUSD", MODE_SPREAD);
   NZDUSD = MarketInfo("NZDUSD", MODE_SPREAD);
   USDCAD = MarketInfo("USDCAD", MODE_SPREAD);
   USDJPY = MarketInfo("USDJPY", MODE_SPREAD);

   AUDUSD1 = MarketInfo("AUDUSD", MODE_POINT);
   AUDUSD2 = MarketInfo("AUDUSD", MODE_DIGITS);
   
  // double vpoint  = MarketInfo("EURUSD",MODE_POINT);
  // int    vdigits = MarketInfo("EURUSD",MODE_DIGITS);
  
   AUDCAD = MarketInfo("AUDCAD", MODE_SPREAD);
   AUDJPY = MarketInfo("AUDJPY", MODE_SPREAD);
   AUDNZD = MarketInfo("AUDNZD", MODE_SPREAD);
   CHFJPY = MarketInfo("CHFJPY", MODE_SPREAD);
   EURAUD = MarketInfo("EURAUD", MODE_SPREAD);
   EURCAD = MarketInfo("EURCAD", MODE_SPREAD);
   EURCHF = MarketInfo("EURCHF", MODE_SPREAD);
   EURGBP = MarketInfo("EURGBP", MODE_SPREAD);
   GBPCHF = MarketInfo("GBPCHF", MODE_SPREAD);
   USDCHF = MarketInfo("USDCHF", MODE_SPREAD);

   string sym_1 = "AUDUSD = "+ DoubleToStr(AUDUSD*scale, 2)+" pips" + "    Digits = " + DoubleToStr(AUDUSD2, 1)+ "    Points = " + DoubleToStr(AUDUSD1, 5);
   string sym_2 = "EURJPY = " + DoubleToStr(EURJPY*scale, 2)+" pips" ;
   string sym_3 = "EURUSD = " + DoubleToStr(EURUSD*scale, 2)+" pips" ;
   string sym_4 = "GBPJPY = " + DoubleToStr(GBPJPY*scale, 2)+" pips" ;
   string sym_5 = "GBPUSD = " + DoubleToStr(GBPUSD*scale, 2)+" pips" ;
   string sym_6 = "NZDUSD = " + DoubleToStr(NZDUSD*scale, 2)+" pips"       ;
   string sym_7 = "USDCAD = " + DoubleToStr(USDCAD*scale, 2)+" pips" ;
   string sym_8 = "USDJPY = " + DoubleToStr(USDJPY*scale, 2)+" pips" ;
      
   string sym_9 = "AUDCAD = " + DoubleToStr(AUDCAD*scale*10, 2)+" pips" ;
   string sym_10 = "AUDJPY = " + DoubleToStr(AUDJPY*scale*10, 2)+" pips" ;
   string sym_11 = "AUDNZD = " + DoubleToStr(AUDNZD*scale*10, 2)+" pips" ;
   string sym_12 = "CHFJPY = " + DoubleToStr(CHFJPY*scale*10, 2)+" pips" ;
   string sym_13 = "EURAUD = " + DoubleToStr(EURAUD*scale*10, 2)+" pips"       ;
   string sym_14 = "EURCAD = " + DoubleToStr(EURCAD*scale*10, 2)+" pips" ;
   string sym_15 = "EURCHF = " + DoubleToStr(EURCHF*scale, 2)+" pips" ;
   string sym_16 = "EURGBP = " + DoubleToStr(EURGBP*scale, 2)+" pips"       ;
   string sym_17 = "GBPCHF = " + DoubleToStr(GBPCHF*scale, 2)+" pips" ;
   string sym_18 = "USDCHF = " + DoubleToStr(USDCHF*scale, 2)+" pips" ;

   Comment("\n " + "\n   "+ sym_1+ "\n   "+ sym_2 + "\n   "+ sym_3 + " \n   "+ sym_4 + " \n   "  +sym_5 + " \n   " + sym_6 + " \n   " + sym_7 + " \n   "+ sym_8 +" \n   "+ sym_9 +" \n   " + sym_10 +" \n   " + sym_11 +" \n   " + sym_12  + " \n   "+sym_13 + " \n   "+sym_14 +" \n   " + sym_15 +" \n   " + sym_16  + " \n   "+sym_17 + " \n   "+sym_18 +" \n\n   " );
   
   DrawSpreadOnChart(EURUSD);

}

void DrawSpreadOnChart(double spread)
{
   string s = "EURUSD Spread: "+DoubleToStr(spread*scale, 1)+" pips" ;
   
   if(ObjectFind(OBJ_NAME) < 0)
   {
      ObjectCreate(OBJ_NAME, OBJ_LABEL, 0, 0, 0);
      ObjectSet(OBJ_NAME, OBJPROP_CORNER, 0);
      ObjectSet(OBJ_NAME, OBJPROP_YDISTANCE, 12);
      ObjectSet(OBJ_NAME, OBJPROP_XDISTANCE, 3);
      ObjectSetText(OBJ_NAME, s, 10, "FixedSys", LabelColor);
   }
   
   ObjectSetText(OBJ_NAME, s);
   
   WindowRedraw();
}

 

 

 

spread 

 
   market_info = Digits();
   if(market_info ==4 || market_info ==2 ) scale = 1; 
Digits is only the current pair. You must compute scale for each currency individually
 

You could use an array for the symbols

  string SymbolArray[]={"AUDUSD", "EURJPY", "EURUSD", "GBPJPY", "GBPUSD", "NZDUSD", "USDCAD", "USDJPY"};

  int as=ArraySize(SymbolArray);
  double points_to_pips;
  string comm="";
  for(int x=0;x<as;x++)
     {
     double symbol_spread=MarketInfo(SymbolArray[x], MODE_SPREAD);
     double dig=MarketInfo(SymbolArray[x], MODE_DIGITS);
     if(dig==3 || dig==5)
        points_to_pips=0.1;
     else
        points_to_pips=1;
     comm+=SymbolArray[x]+" = "+DoubleToStr(symbol_spread*points_to_pips,2)+" pips\n";
     
     }
  Comment(comm);
 
GumRai:

You could use an array for the symbols

 

Keith,

 

Excellent idea. Greatly appreciated.  

 
     if(dig==3 || dig==5)
       points_to_pips=0.1;
     else
        points_to_pips=1;
You've got them backwards. if dig%2==1 then points_to_pips = 10 * Point of that symbol else it's just Point (*1) of that symbol. See Problems with a calculation - MQL4 forum
 

WHRoeder:

     if(dig==3 || dig==5)
        points_to_pips=0.1;
     else
        points_to_pips=1;

 


You've got them backwards. if dig%2==1 then points_to_pips = 10 * Point of that symbol else it's just Point (*1) of that symbol. See Problems with a calculation - MQL4 forum



I think that you are confusing Point, that shows the decimal value of 1 point and a point value returned from MarketInfo which is an integer (returned as a double)
 

Attached are the screen captures of a 4-digit and a 5-digit broker. The code seems to work very well.

 

4-digit broker 

 

 

 

5-digit broker 

 
GumRai: I think that you are confusing Point, that shows the decimal value of 1 point and a point value returned from MarketInfo which is an integer (returned as a double)
Yep, per my link I was siteing pips to price (pips2dbl) and pips2points. OP is coding points to pips (1/pips2points)
 

William & Keith,

 

Can I get a final confirmation on the correct handling of the digits? I am testing with 3 different brokers: one broker is quoting 2 & 4 digits, another broker is quoting 3 & 5 digits, and yet the third broker is quoting 3 & 4 & 5 digits.  See attached pictures.

 2&4 Digits

2 and 4 digits 

 

3 & 5 Digits 

 

 

 

3, 4 & 5 Digits

 

 

 

It shouldn't matter

2,3,4 or 5 digits

because MarketInfo Digits is retrieved for each individual symbol. 

Note: there may be differences between the comment and what you see in the market watch window. This is because the comment will only be updated when the is a new tick on the chart that the code is attached to. 

 
Thanks for your reply Keith. 
Reason: