MQL4 round numbers Indicator

Hello everybody,

Today I want to share with you another area. I had small order for MQL4 indicator for round numbers for Metatrader4 or as it is named MT4. The task was to draw lines at screen with some step at any currency pair. In order to implement it I started first of all Googling. Then I found almost implemented indicator at forexfactory just without option for modification steps. After wondering about code purpose of variables I created the following code:

//**************************************************
//*  RoundNr.mq4 (No Copyright)                    *
//*                                                *
//*  Draws horizontal lines at round price levels  *
//*                                                *
//*  Written by: Totoro @ forexfactory.com         *
//*  Modified by Yuriy Zaletskyy                   *
//**************************************************
#property indicator_chart_window
extern double LineSpace     = 1; // 1 unit = 0.01 of basic value (e.g. 1 USD cent)
extern color  LineColor     = Turquoise;
extern int    LineStyle     = 2;
extern string LineStyleInfo = "0=Solid,1=Dash,2=Dot,3=DashDot,4=DashDotDot";
extern string LineText      = "RoundNr ";
extern double stepSize = 0.1;
extern string stepSizeComment = "step size for jpy = 0.1, for others 0.001";
extern int digitsAfterComma = 2;
extern string digitsAfterCommaComment = "2 for jpy and 3 for non jpy pair";

double LineSpaceOld;
double HighScreen;
double Tief;
bool   FirstRun = true;

int deinit()
{
   double AbSpace = stepSize*LineSpace;
   double HigherScreen    = MathRound(110*HighScreen)/100;
   double LowerScreen   = MathRound(80*Tief)/100;
   for(double i=0; i<=HigherScreen; i+=AbSpace)
   {
      if(i<LowerScreen) { continue; }
      ObjectDelete(LineText+DoubleToStr(i,digitsAfterComma));
   }
   return(0);
}

int start()
{
   if(FirstRun)
   {
      HighScreen = NormalizeDouble( High[iHighest(NULL,0,MODE_HIGH,Bars-1,0)], digitsAfterComma );
      Tief = NormalizeDouble( Low[iLowest(NULL,0,MODE_LOW,Bars-1,0)], digitsAfterComma );
      FirstRun = false;
   }
   else if(LineSpace != LineSpaceOld)
   {
      deinit();
      HighScreen = NormalizeDouble( High[iHighest(NULL,0,MODE_HIGH,Bars-1,0)], digitsAfterComma );
      Tief = NormalizeDouble( Low[iLowest(NULL,0,MODE_LOW,Bars-1,0)], digitsAfterComma );
   }
   DrawLines();
   LineSpaceOld = LineSpace;
   return(0);
}

void DrawLines()
{
   double AbSpace = stepSize*LineSpace;
   double HigherScreen    = MathRound(110*HighScreen)/100;
   double LowerScreen   = MathRound(80*Tief)/100;

   for(double i=0; i<=HigherScreen; i+=AbSpace)
   {
      if(i<LowerScreen) { continue; }
      string StringNr = DoubleToStr(i,digitsAfterComma); // digits number in object name
if (ObjectFind(LineText+StringNr) != 0) // HLine not in main chartwindow { ObjectCreate(LineText+StringNr, OBJ_HLINE, 0, 0, i); ObjectSet(LineText+StringNr, OBJPROP_STYLE, LineStyle); ObjectSet(LineText+StringNr, OBJPROP_COLOR, LineColor); } else // Adjustments { ObjectSet(LineText+StringNr, OBJPROP_PRICE1, i); ObjectSet(LineText+StringNr, OBJPROP_STYLE, LineStyle); ObjectSet(LineText+StringNr, OBJPROP_COLOR, LineColor); } } WindowRedraw(); }

Enjoy.

No Comments

Add a Comment