Trader’s Tech – Writing Your Own EA Part 52 – Generic Start Code

Forex RobotIf 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 51 we discussed the init() function of our Generic Start Code. This time we’re going to look at the deinit() function and the other generic functions I like to use to start off my EAs.

All my initial deinit() function includes is a call to the DeleteAllObjects() function. That way when you close your EA or change time frames, it will delete all the objects you’ve drawn on the chart, reducing confusion as to what was left and what wasn’t. Often I also include a call to a function that will write a status file (we’ll talk about writing files in a future installment), but, since the status file writing function will be unique to the EA, I don’t include that call in the generic code.

The DeleteAllObjects() function finds and deletes all objects with a name that starts with the prefix associated with each EA. As I mentioned, I use an EA-specific prefix in all my EAs for the names of GVs and chart objects. That makes it easy to identify those objects belonging to our current EA. If you’re observant, you may notice that this function doesn’t conform to some of my normal coding standards. That’s because I found this function in a public domain code library somewhere and just never bothered to make it follow my standards (I probably should do that.)  There’s nothing wrong with not reinventing the wheel. 😉

void DeleteAllObjects()
   {
   int objs = ObjectsTotal();
   string name;
   for(int cnt=ObjectsTotal()-1;cnt>=0;cnt--)
      {
      name=ObjectName(cnt);
      if (StringFind(name,Prefix,0)>-1) 
         ObjectDelete(name);
      WindowRedraw();
      }
   } //void DeleteAllObjects()

 

The DeleteAllObjects() function uses that to identify the objects for the current EA and deletes them. It uses the following built-in MQL4 functions:

ObjectsTotal() – returns the total number of objects on the current chart

ObjectName() – returns the name of the chart with the specified index (cnt)

StringFind() – returns the location of the specified string (in our case the prefix) within the larger string (name).

ObjectDelete() – deletes the named object.

WindowRedraw() – This redraws the current chart to be sure the object changes get displayed. With all the bar movement and so on, a chart can have remnants of objects messing up the place. This will delete and redraw the chart with the objects that belong.

It’s funny, but this is the first time I’ve really paid any attention to this function. This may be a good time for me to make it conform to my other code. I’ve said it before, I like things neat and orderly. You know, you’ve got to fill all the squares in the waffle with syrup, right?

So, let’s start by replacing the StringFind() function as this may not be the safest way to find the string that will always be at the beginning of the name (hence, Prefix.) A better way might be:

if(StringSubstr(name,0,StringLen(Prefix)) == Prefix)

 

That code segment may be safer in that it will only return names that find the prefix at the beginning of the object name.

StringSubstr() is a string function that returns a substring of the named string, in our case starting with 0 and going for the length of the string Prefix

StringLen() – returns the length of the named string – in our case, Prefix

Another thing, it’s a little bit goofy to assign a variable for the ObjectsTotal() and then never use it.

And one more thing. Why don’t we put the WindowsRedraw() function outside the for() loop so it only redraws when we’re done. No need to redraw the chart at every single deletion, that just makes a bunch of flickering and unnecessary processor activity.

Something I want to point out that you may have thought was weird, but is actually genius (I wish I could claim it as my own ;)), the for() statement starts at the end of the list and works backward

 

   for(int cnt=ObjectsTotal()-1;cnt>=0;cnt--)

 

There’s method in that madness. When an object is deleted, MT4 re-orders the index numbers of the remaining objects to fill in the gap, so if you were to start by deleting the object with the index of 1, then the object with the index of 2 will become the new object with index 1. If you were then to increment your counter (cnt would then equal 2), it would completely skip over the next object with the new index. So starting from the highest number would keep all the other objects from being renumbered until after you’ve checked them for the prefix.

Here is the code modified to conform:

void DeleteAllObjects()
   {
   int i,TotalObjects = ObjectsTotal();
   string name;
  for(i=TotalObjects-1;i>=0;i--)
      {
      name=ObjectName(i);
      if(StringSubstr(name,0,StringLen(Prefix)) == Prefix)
         ObjectDelete(name);
      }
   WindowRedraw();
   } //void DeleteAllObjects()

 

And, of course, I always use i & j for my counters (k & l if I need more) so I had to fix that. I think that standard goes back to my earliest programming days. Back when all variables were a single character. And yes, we did have rocks back then. 😉

And that wraps another installment. Tomorrow we’ll continue on the Generic Start Code.

Thanks for your attention and please follow me on Twitter and LinkedIn.

Tim

 

here_for_you_001

1359774937_facebook 1359774984_linkedin 1359774949_twitter 1359774960_google 1359774973_rss 1360027635_youtube

Connect_With_Winners_Edge

Get trade set ups everyday!

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