//+------------------------------------------------------------------+
//|                                                       Spread.mq4 |
//|                             Copyright © 2009-2016, Andriy Moraru |
//|                                        http://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009-2016, Andriy Moraru, www.EarnForex.com"
#property link      "http://www.earnforex.com"
#property version   "1.05"
#property strict

#property description "Spread - displays current spread in the chart window."
#property description "Modifiable font parameters, location and normalization."
#property description "edited"

#property indicator_chart_window

input bool UseCustomPipSize = false; // UseCustomPipSize: if true, pip size will be based on DecimalPlaces input parameter.
extern int DecimalPlaces = 4; // DecimalPlaces: how many decimal places in a pip?
input double AlertIfSpreadAbove = 0; // AlertIfSpreadAbove: if > 0 alert will sound when sprea above the value.

input color font_color = clrDarkGray;
input int font_size = 14;
input string font_face = "Verdana";
input ENUM_BASE_CORNER corner = CORNER_LEFT_LOWER;
input int spread_distance_x = 10;
input int spread_distance_y = 10;
input bool DrawTextAsBackground = false; //DrawTextAsBackground: if true, the text will be drawn as background.

int n_digits = 0;
double divider = 1;
bool alert_done = false;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   ObjectCreate("Spread", OBJ_LABEL, 0, 0, 0);
   ObjectSet("Spread", OBJPROP_CORNER, corner);
   ObjectSet("Spread", OBJPROP_XDISTANCE, spread_distance_x);
   ObjectSet("Spread", OBJPROP_YDISTANCE, spread_distance_y);
   ObjectSet("Spread", OBJPROP_BACK, DrawTextAsBackground);
   
   if (!UseCustomPipSize) {
      if (Digits==1) DecimalPlaces=0;
      if (Digits==2) DecimalPlaces=1;
      if (Digits==3) DecimalPlaces=2;
      if (Digits==4) DecimalPlaces=4;
      if (Digits==5) DecimalPlaces=4;
      }

//   if (UseCustomPipSize)
//   {
      divider = MathPow(0.1, DecimalPlaces) / Point;
      n_digits = (int)MathAbs(MathLog10(divider));
//   }
   
   
   
   double spread = MarketInfo(Symbol(), MODE_SPREAD);
   OutputSpread(spread);

   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   ObjectDelete("Spread");
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   RefreshRates();
   
   double spread = (Ask - Bid) / Point;
   OutputSpread(spread);
   
   if (AlertIfSpreadAbove > 0)
   {
      if (NormalizeSpread(spread) < AlertIfSpreadAbove) alert_done = false;
      else if (!alert_done)
      {
         PlaySound("alert.wav");
         alert_done = true;
      }
   }
   return(0);
}

void OutputSpread(double spread)
{
   ObjectSetText("Spread", "Spread: " + DoubleToString(NormalizeSpread(spread), n_digits), font_size, font_face, font_color);
}

double NormalizeSpread(double spread)
{
   return(NormalizeDouble(spread / divider, n_digits));
}
//+------------------------------------------------------------------+