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

Forex Robot 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 82 we continued our discussion about detecting price action in an Action Rectangle. We’re going to do more of the same here.

After our code correction in the last installment, we can proceed. Oh, no we can’t! Man, I wonder what I was smoking the other day when I wrote this code. Two more lines that are wacky. They’ll pass the compiler syntax test, but definitely won’t work (I tested them just to be sure.)  I’ll present the corrected lines here, then proceed with the article.

This line:

if(Price1 > Bid > Price2)


Should be:

if(Price1 > Bid && Bid > Price2)


Also the opposite of that line below it:

if(Price1 < Bid < Price2)


Should be:

if(Price1 < Bid && Bid < Price2)


Sorry, sometimes my fingers get ahead of my brain when I’m typing.  That’s why we do bench checks of the code before we compile it. It saves much time later in the development process.

We’ve established that the current object is of type RECTANGLE and is the proper color. We now need to see if price action is in or near the rectangle.

As you know a rectangle can be described by either of the pairs of opposite corner points. And each point is described by two parameters, Price and Time. We’ll start by collecting the two points that determine the location of the current rectangle. Price1, Time1, Price2 and Time2.

Next we’ll determine if the current price is between Price1 and Price2. MT4 doesn’t specify which of the two points is the lower and which is the higher, that actually depends with which point you started drawing your rectangle, so our first job is to determine which is above and which is below, then find out if current price is between them. Hence the first test:

if(Price1 < Price2)


If that statement is true, we test if the current market price is between those numbers:

if(Price1 < Bid && Bid < Price2)


If the current market price is between the rectangle prices, we can set our boolean global variable fPIARPriceInRect to true. If it isn’t true, we fall to the next test; to determine if price action is NEAR the rectangle. This test is a little trickier, but still straightforward. In plain english the test is: “If the absolute value of the difference between Price1 and Bid is less than NearRectPips pips OR if the absolute value of the difference between Price2 and Bid is less than NearRectPips pips” then we can set our boolean global variable fPIARPriceNearRect to true.

First, what is absolute value? The MathAbs() function takes a negative number and makes it positive and leaves a positive number untouched. So we end up with the positive of whatever calculation is provided to the function. That allows us to compare it to the NearRectPips parameter that was sent to our PriceInActionRectangle() function. We don’t have to first determine if the Bid is above or below Price1 or Price2. In some cases, it’s just a lazy man’s way of coding. In this particular case, we know that the Bid is not between Price1 and Price2 and that Price1 < Price2, so the Bid is either below Price1 or above Price2. But taking the absolute value just saves a lot of figuring…. Necessity is the mother of invention and laziness is the father. 😉 Plus we can just duplicate the code in the next section instead of having to figure it all out again.

I use this particular construct quite a bit when comparing double type variables to each other. If I want to determine if a line has been moved, I compare the new price to the old price by subtracting them, taking the absolute value and seeing if that exceeds 1 pip:

if(MathAbs(Price1 - Price2) / AdjPoint > 1)

And, of course, you’ll remember the ubiquitous AdjPoint which I use to relate Price to Pips (which allows for both 4- and 5- digit brokers.) You can actually see the code to calculate AdjPoint here.

Here’s a little coding trick I’ve been known to use. I use a double global variable called Zero and set it to 0.001. When I have to determine if a double Price is equal to zero, if I use this code:

if(Price == 0)


I’m asking for problems because after some calculations, Price could be equal to 0.00000001, which for my purposes is zero, but as far as Mr. Always Exact (the computer)is concerned, this is NOT zero. So instead I use the following code:

if(Price < Zero)


The code is easy to read and makes it’s point to the programmer, but still alleviates issues with infinitesimally small numbers. Plus, 0.001 is less than any Forex pair price I use. As with anything like this that I do, if any of you have a better solution or a situation you think it might not work, I would love to hear about it. We should always be learning….

We’ll pick this up next time right at this point. Thanks for your attention and please follow me on Twitter and LinkedIn.

The following two tabs change content below.
Winners Edge Trading was founded in 2009 and is working to create the most current and useful Forex information and training available on the internet.

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news