# Trader’s Tech – Writing Your Own EA Part 84 – Graphical Chart Objects

If you’re new to this series and want to check it out from the beginning, you can find that here. And look here for a list of all the programming articles.

In Part 83 we continued our discussion about detecting price action in an Action Rectangle. We’re going to do more of the same here.

You would think that with as many errors as I’ve found in the function, I would have gone through it completely and fixed everything by now. Of course not, here’s another bug fix that will replace a whole stretch of code. Replace this code:

```if(Price1 < Price2)
{
if(Price1 < Bid && Bid < Price2)
fPIARPriceInRect = true;
else if((MathAbs(Price1 - Bid) / AdjPoint < NearRectPips) || (MathAbs(Price2 - Bid) / AdjPoint < NearRectPips))
fPIARPriceNearRect = true;
}  //if(Price1 < Price2)
else
{
if(Price2 < Bid && Bid < Price1)
fPIARPriceInRect = true;
}  //else
//if not in it, near it?
if(!fPIARPriceInRect && (MathAbs(Price1 - Bid) / AdjPoint < NearRectPips || MathAbs(Price2 - Bid) / AdjPoint < NearRectPips))
fPIARPriceNearRect = true;```

With this code:

```if(Price1 < Price2)
{
if(Price1 < Bid && Bid < Price2)
fPIARPriceInRect = true;
}  //if(Price1 < Price2)
if(Price1 >= Price2)
{
if(Price2 < Bid && Bid < Price1)
fPIARPriceInRect = true;
}  //if(Price1 >= Price2)```
```//if not in it, near it?
if(!fPIARPriceInRect &&
(MathAbs(Price1 - Bid) / AdjPoint < NearRectPips ||
MathAbs(Price2 - Bid) / AdjPoint < NearRectPips))
fPIARPriceNearRect = true;```

I see what I did while writing this. I suddenly realized that I was going to have redundant code and changed the second part without removing the else if() from the first part. Truthfully, I don’t care for else statements very much. If you ever subsequently decide to change the original if() statement because you have new requirements for your code, it often invalidates the else statement. So I try to avoid the catchall else statement as much as possible. Always remember to keep your code as simple and pliable as possible when you write it.

In this case, I realized that, since we used the MathAbs() function, the test for the price action’s presence near the rectangle was the same regardless of the relative values of Price1 and Price2. All we had to do first was be sure that price action wasn’t already in the rectangle.

Which brings us to the time part of the test. If your rectangle is meant to describe significant price levels (support, resistance, etc.), the time factor may not be important, in which case you can just remove this code. Or you can make your function more ubiquitous by including a boolean parameter so you can determine when you use the function whether the time parameter is significant. I’ll leave that to your devices as a now-experienced programmer to figure out.

Since charts are based upon bars or candles, you probably don’t want to use the current time (TimeCurrent()) to test the time factor on the chart. That’s the reason I test the rectangle’s time elements against the iTime() function. iTime() returns the time of the specified bar opening. In this case the 0 bar – the currently active bar. Plus, this allows you to designate the time frame you are using for the test.

Again, another code change I’ve made in light of removing the else statement. In the time testing portion of the function, I removed the else statement and replaced it with if(Time1 >= Time2).

And, to wrap up our discussion of this function, lastly we test to see if our price is in or near the rectangle and if so, we set our string variable RectName to the name of the rectangle so it can be returned to the calling function and break out of the i loop that we are using to test the current objects. If you have removed the time code from this function, don’t forget to remove it from this if() statement as well.

You can find an updated version of the PriceInActionRectange.mqh at my GitHub.

The following two tabs change content below.