Trader’s Tech – Writing Your Own EA Part 81 – 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 80 we discussed the need to detect when price was within a zone designated by a rectangle object. In this article we’ll look at the code to do that very thing.

I’ve rewritten the code for detecting whether price action is within a graphical rectangle object or a designated color. When I looked at the code I had written a few years ago for the All-Star Entry, I realized that I had formed a lot of new standards since then, so I thought it was appropriate to rewrite it. You can find the code posted on my GitHub site as PriceInActionRectangle.mqh.


bool fPIARPriceInRect,fPIARPriceNearRect,fPIARTimeInRect;

string PriceInActionRectangle(color ActionColor=Blue,int NearRectPips=5,int TF=0)
   int i,TotalObjects=ObjectsTotal();
   string name,RectName="";
   double Price1, Price2;
   datetime Time1, Time2;
   int FiveDig;
   double AdjPoint;

      FiveDig = 10;
      FiveDig = 1;
   AdjPoint = Point * FiveDig;

   fPIARPriceInRect = false;
   fPIARPriceNearRect = false;
      //first find if there are any action rectangles
      name = ObjectName(i);
         //if there is, is price in it or near it?
         Price1 = ObjectGet(name,OBJPROP_PRICE1);
         Price2 = ObjectGet(name,OBJPROP_PRICE2);
         Time1 = ObjectGet(name, OBJPROP_TIME1);
         Time2 = ObjectGet(name,OBJPROP_TIME2);
         if(Price1 < Price2)
            if(Price1 < Bid < Price2)
               fPIARPriceInRect = true;
            else if((MathAbs(Price1 - Bid) / AdjPoint < NearRectPips) || 
                    (MathAbs(Price2 - Bid) / AdjPoint < NearRectPips))
               fPIARPriceNearRect = true;
            }  //if(Price1 < Price2)
            if(Price2 < 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;

         //if there is, is time in it?
         if(Time1 < Time2)
            if(Time1 < iTime(NULL,TF,0) < Time2)
               fPIARTimeInRect = true;
            }  //if(Time1 < Time2)
            if(Time2 < iTime(NULL,TF,0) < Time1)
               fPIARTimeInRect = true;
            }  //else
         //if price is in or near, let's jump out and end this thing
         if((fPIARPriceInRect || fPIARPriceNearRect) && fPIARTimeInRect)
            RectName = name;
            }  //if((fPIARPriceInRect || fPIARPriceNearRect) && fPIARTimeInRect)
         }  //if(ObjectGet(name,OBJPROP_COLOR))
      }  //for(i=0;i<TotalObjects;i++)

   }  //string PriceInActionRectangle(color ActionColor)


First thing you might notice, I’ve declared a few boolean variables outside of the function as global (small g) variables. That’s because I wanted to be able to return more information than I can return from a function without using globals. The boolean variables will tell whether the price and time of the current price action is actually within the confines of a rectangle of a specific color.

I’ve specified three parameters – all three with defaults, so you can call the function with no parameters if the defaults are acceptable. First is the color of the rectangle for which you’ll be looking. As I’ve mentioned before, that way you can have rectangles to your heart’s content without the EA reacting to them. I defaulted to Blue since that’s the color I used with the All-Star Entry.

The next parameter is the number of pips I defined as near the zone. The zones are very subjective, so I was fine with a little fudge factor. When I ran the strat, I allowed an entry if all other things we right and the price action was within five pips of the action rectangle so that’s my default.

The last parameter is the Time Frame. 0 means that it will react to the triangle on any time frame. This only effects the Time position of the Price Action. If the beginning of the current candle in the specified time frame is within the horizontal confines of your rectangle, the function will return a result.

In our next installment we’ll further discuss this function. 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.

Latest posts by admin (see all)

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news