Trader’s Tech – Writing Your Own EA Part 79 – 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 78 we began our discussion about graphical chart objects that are attached directly to the bars or candles of our chart. In this installment, we’ll continue that talk.

In the course of writing trade management EAs, I discovered a rather annoying – but not impossible to overcome – issue with MT4. There is absolutely no way of renaming a line. You must delete and recreate it. A little background is in order.

I wanted to allow the user to create an “Action Line”. A line at which something programmatically takes place, order entry, stop loss lock in, etc.  I didn’t want the user to have to enter a specific name, I just wanted the user to have to be sure of the line color. Then I would detect the presence of a line of that specific color, then change the color and the line name to make it easier for the EA to manipulate the line. But I couldn’t change the line name.

So I wrote the following code to “rename” the line:

bool RenameLine(string oldName, string newName, color newColor)
   string oName = StringConcatenate(Prefix,"line_",oldName);
   string nName = StringConcatenate(Prefix,"line_",newName);
   bool Success=false;
   int Type;
   double Price1, Price2;
   datetime Time1, Time2;

   if(ObjectFind(oName) > -1)
      Type = ObjectType(oName);
      if(Type == OBJ_HLINE)
         Price1 = ObjectGet(oName,OBJPROP_PRICE1);
         Success = ObjectCreate(nName,Type,0,0,Price1);
         }  //if(ObjectType(oldName) == OBJ_HLINE)

      if(Type == OBJ_VLINE)
         Time1 = ObjectGet(oName,OBJPROP_TIME1);
         Success = ObjectCreate(nName,Type,0,Time1,0);
         }  //if(ObjectType(oName) == OBJ_VLINE)

      if(Type == OBJ_TREND)
         Price1 = ObjectGet(oName,OBJPROP_PRICE1);
         Time1 = ObjectGet(oName,OBJPROP_TIME1);
         Price2 = ObjectGet(oName,OBJPROP_PRICE2);
         Time2 = ObjectGet(oName,OBJPROP_TIME2);
         Success = ObjectCreate(nName,Type,0,Time1,Price1,Time2,Price2);
         }  //if(ObjectType(oName) == OBJ_TREND)

         Print("RenameLine() Line ",oName," renamed ",nName);
         }  //if(Success)
      }  //if(ObjectFind(oldName) > -1)
      Print("RenameLine() Old Line (",oName,") Not Found");

   }  //bool RenameLine(string oldName, string newName, color newColor)


We start by renaming the oldName and newName parameters to conform with our line naming standards. You may have other standards for this so you can adjust these lines to suit your programming. In addition we declare the variables we need to hold the old line’s information and we initialize a boolean variable Success which will return the success status of our function.

Next we try to find the old line. If it exists, we can proceed with our renaming. If not we generate an error in the experts log and move on.

After determining the existence of the old line we need to get some information about it.

First we determine the line type. MT4 has three different line types:

OBJ_HLINE – horizontal line with only a price element.

OBJ_VLINE – vertical line with only a time element.

OBJ_TREND – a sloping line that is described by two points each with a time and price element.

We then pick up the necessary coordinate information for each type and create the new object. The ObjectCreate() function returns a boolean based upon its success, so we can set our boolean variable Success to the result of this function. Then we can set the color of the new line using the ObjectSet() function. If there is any other information about the new line that you would like to (e.g. line width, line style, etc.) set, use the ObjectSet() function to change that too.

After being assured of the success of creating the new line, we delete the old line and Print() a line to the experts log about our success (I know, programmers can be real braggarts at times. ;))

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