# How to Draw a Reference Rectangle Every 10 Pips

415

```//+------------------------------------------------------------------+
//|                                                       bgColorChanges10_20_30_40_50.mq4 |
//|                      Copyright ?2009, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+

#property indicator_chart_window
#define ONDA_BEGINS   0
#define ONDA_CONTAINS 1

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//----
for (int ix = 0; ix < nLines; ix++) // delete my horizontal lines
{
ObjectDelete("tensLines"+ix);
}

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
int nLines = 40;                                   // Number of total line to draw
double lineInterval = 0.0010;                      // Interval between lines
double normPrice = NormalizeDouble(Close[1],3);    // Current price is rounded to nearest "10"

for (int ix = 0; ix < nLines; ix++)                // Loop span number of times
{
if(ObjectFind("tensLines"+ix) < 0)
ObjectCreate("tensLines"+ix, OBJ_HLINE, 0, 0, normPrice+((ix-(nLines/2))*lineInterval));    // Place half above and half below the current price

else ObjectSet("tensLines"+ix, OBJPROP_PRICE1, normPrice+((ix-(nLines/2))*lineInterval));
ObjectSet("tensLines"+ix,OBJPROP_COLOR,DarkSlateGray);      // Make the lines look better
}

//----
return(0);
}
//+------------------------------------------------------------------+

void ObjectNameDeleteAll(string name, int where=ONDA_BEGINS, int type=EMPTY)
{
for(int iObj=ObjectsTotal()-1; iObj >= 0; iObj--)
{
string on   = ObjectName(iObj);
int    iPos = StringFind(on, name);
if (iPos < 0)                         continue;
if (iPos > 0 && where == ONDA_BEGINS) continue;
if      (type == EMPTY))         ObjectDelete(on);
else if (type == ObjectType(on)) ObjectDelete(on);
}
}

//---```

It doesn't work.  Many errors.  What needs to be fixed?

Also, when it goes higher timeframe, I want it to be changed,

===> 1Min ~ 30 Min ==> every 10 pips

===> 1 Hour  ======>  every 50 pips

===>  4 Hour, Daily ======> every 100 pips

===> Weekly, Monthly ===> every 1000 pips

Any help? Rectangles are desired instead of lines which can cause confusion with Resistance and Support Lines.

Moderator
10378

I think that it is better to create the objects in init and then move them in the main program.

This may give you some ideas to work with

As it draws alternate rectangles, the highest level is missing. You may decide to draw additional rectangles with alternate colours

Moderator
10378

```#property copyright "GumRai"
#property version   "1.00"
#property strict
#property indicator_chart_window
//--- input parameters
input int nLines = 40;              //Number of levels to draw
input double lineInterval = 0.0010; //Basic Interval between levels
input color  RCol=clrAquamarine;    //Colour

string obname="RectLevel";
double step;
datetime BarTime=0;
int RoundFactor;
int halfLines;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
if(Digits==5 || Digits==3)
RoundFactor=Digits-2;
else
RoundFactor=Digits-1;

switch(Period())
{
case PERIOD_M1:   step=lineInterval;      break;
case PERIOD_M5:   step=lineInterval;      break;
case PERIOD_M15:  step=lineInterval;      break;
case PERIOD_M30:  step=lineInterval;      break;
case PERIOD_H1:   step=lineInterval*5;    break;
case PERIOD_H4:   step=lineInterval*10;   break;
case PERIOD_D1:   step=lineInterval*10;   break;
case PERIOD_W1:   step=lineInterval*100;  break;
case PERIOD_MN1:  step=lineInterval*100;  break;
}

halfLines=(int)MathCeil(nLines*0.5);
for (int ix = 1; ix <= halfLines; ix++)                // Loop span number of times
{
ObjectCreate(0,obname+(string)ix,OBJ_RECTANGLE,0,0,0,0,0);
ObjectSetInteger(0,obname+(string)ix,OBJPROP_COLOR,RCol);
}
//---
return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
for (int ix = 1; ix <= halfLines; ix++)
ObjectDelete(0,obname+(string)ix);
}

//+------------------------------------------------------------------+
//| 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[],
{
if(Time[0]!=BarTime)
{
BarTime=Time[0];
double normPrice = NormalizeDouble(Close[1],RoundFactor);    // Current price is rounded to nearest "10"
double first=normPrice-step*halfLines;
datetime T2=Time[0]+PeriodSeconds()*100;
for (int ix = 1; ix <= halfLines; ix++)
{
ObjectSet(obname+(string)ix,OBJPROP_PRICE1,first);
ObjectSet(obname+(string)ix,OBJPROP_PRICE2,first+step);
ObjectSet(obname+(string)ix,OBJPROP_TIME2,T2);
first+=step*2;
}

}

//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
```
415

It doesn't exactly draw 10 pips in a round number.  It chooses arbitrary...
Moderator
10378

paul.seldon:
It doesn't exactly draw 10 pips in a round number.  It chooses arbitrary...

Wow!

Is that really the way that you respond when somebody takes the time and tries to help you? Complain that it doesn't do exactly as you expect?

It draws at levels rounded to 10 pips on my chart!

GumRai:

This may give you some ideas to work with

Means that it is intended to give you some ideas, not intended as your complete solution.

415

Oh, I wish it could carry the tone~ with all respect ~ ^_^

Thank you ~ but how can I apply this??? I don't know.

Moderator
10378

paul.seldon:

Thank you ~ but how can I apply this??? I don't know.