Trader’s Tech – Writing Your Own EA Part 49 – 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.

It seems I got the article number sequence fouled up. I hope that didn’t mess anyone up. I’ve renumbered the articles now so they should be correct. In Parts 47 & 48 we covered MT4’s handy Global Variables. As I mentioned, in this article I’m going to show you my generic “starter” code that I use when I start a new EA.

I absolutely despise starting a new EA. I know that sounds funny. But I like getting right to the creative process and as you know, I “write” the EA directly in the editor in “pseudocode”. The part I despise is setting up all the generic starting stuff, my “libraries” if you will. In one of my other lives, I’m a .NET programmer and I use Microsoft Visual Studio for that. It’s all set up for me every time I start a new program or web site. But with MQL4, I’ve got to set it all up. So, in the interest of automating as much of the task as possible (those of you who know me well, know that I’ll spend 10 hours automating a 10 minute project – it’s just my “thing”, whether it’s programming or just life in general – Stop laughing Nathan!) I created the EA Start Code file.

I have several functions that I include in my generic startup code. You’ve seen the Global Variable-related functions. I also have code for calculating my AdjPoint variable (as you recall, that relates Price to Pips), variables for program Title, Abbreviation and Version and quite a bit more. Here it is:

//+------------------------------------------------------------------+
//| GenericStartCode.mq4 |
//| Tim Black |
//| http://winnersedgetrading.com |
//+------------------------------------------------------------------+
#property copyright "Tim Black"
#property link "http://winnersedgetrading.com"
#property copyright "Copyright © 2012, T Black & Associates"
#property link "http://www.tbaai.com"
#include <stdlib.mqh>
#include <stderror.mqh> 
#include <OrderReliable_2011.01.07.mqh>
#include <PcntTradeSize.mqh>
string Title="GenericStartCode"; 
string Prefix="GSC_";
string Version="v0.10";
datetime ExpireDate=D'2041.11.30 00:01';
extern double RiskPcnt=1.0;
extern int MagicNumber=1111234;
string TextFont="Verdana";
int FiveDig;
int MaxInt=2147483646;
int LotDigits;
bool MarginAlert=false;
double AdjPoint;
static datetime LastTradeTime=0;
color TextColor=Goldenrod;
bool debug = true;
bool HeartBeat = true;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
   {
   //----
   Print("---------------------------------------------------------");
   Print("-----",Title," ",Version," Initializing ",Symbol(),"-----"); 
   if(Digits==5||Digits==3)
      FiveDig = 10;
   else
      FiveDig = 1;
   AdjPoint = Point * FiveDig;
   DrawVersion(); 

   if(MarketInfo(Symbol(),MODE_LOTSTEP) < 0.1)
      LotDigits = 2;
   else if(MarketInfo(Symbol(),MODE_LOTSTEP) < 1.0)
      LotDigits = 1;
   else
      LotDigits = 0;

   if(GlobalVariableCheck(StringConcatenate(Prefix,"debug")))
      {
      if(GlobalVariableGet(StringConcatenate(Prefix,"debug")) == 1)
         debug = true;
      else
         debug = false;
      }

   if(GlobalVariableCheck(StringConcatenate(Prefix,"HeartBeat")))
      {
      if(GlobalVariableGet(StringConcatenate(Prefix,"HeartBeat")) == 1)
         HeartBeat = true;
      else
         HeartBeat = false;
      }
  //---------------------------------------------------- 

  //----
  return(0);
  }  //int init()

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
   {
   //----
   DeleteAllObjects();

   //----
   return(0);
   }  //int deinit()

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()

void DrawVersion()
   {
   string name;
   name = StringConcatenate(Prefix,"Version");
   ObjectCreate(name,OBJ_LABEL,0,0,0);
   ObjectSetText(name,Version,8,TextFont,TextColor);
   ObjectSet(name,OBJPROP_CORNER,2);
   ObjectSet(name,OBJPROP_XDISTANCE,5);
   ObjectSet(name,OBJPROP_YDISTANCE,2);
   } //void DrawVersion()

void SetGV(string VarName,double VarVal)
   {
   string strVarName = StringConcatenate(Prefix,Symbol(),"_",VarName);

   GlobalVariableSet(strVarName,VarVal);
   if(debug)
      Print("###Set GV ",strVarName," Value=",VarVal);
   } //void SetGV

double GetGV(string VarName)
   {
   string strVarName = StringConcatenate(Prefix,Symbol(),"_",VarName);
   double VarVal = -99999999;

   if(GlobalVariableCheck(strVarName))
      {
      VarVal = GlobalVariableGet(strVarName);
      if(debug)
         Print("###Get GV ",strVarName," Value=",VarVal);
      }

   return(VarVal); 
   } //double GetGV(string VarName)

void HeartBeat(int TimeFrame=PERIOD_H1)
   {
   static datetime LastHeartBeat;
   datetime CurrentTime;

   if(GlobalVariableCheck(StringConcatenate(Prefix,"HeartBeat")))
      {
      if(GlobalVariableGet(StringConcatenate(Prefix,"HeartBeat")) == 1)
         HeartBeat = true;
      else
         HeartBeat = false;
   }  //void HeartBeat(int TimeFrame=PERIOD_H1)

   if(HeartBeat)
      { 
      CurrentTime = iTime(NULL,TimeFrame,0);
      if(CurrentTime > LastHeartBeat)
         {
         Print(Version," HeartBeat ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES));
         LastHeartBeat = CurrentTime;
         } //if(CurrentTime > ...
      } //if(HeartBeat)

   } //HeartBeat()
  //------------------------------------------------------

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
   {
  //----
   if(HeartBeat)
      HeartBeat();
  //----
   return(0);
   }  //int start()
//+------------------------------------------------------------------+

The way I use this is simple. I just save it with my EAs (in the Experts folder) named “EA Start Code.mq4“. When I’m ready to start a new EA, I just open this file (double-click on it in the MetaEditor Navigator Window’s Files tab) and then click File/Save As in the MetaEditor window and name it whatever I want. I do that first so I don’t forget to save it as a different file. Next I edit whatever needs editing (Name, Prefix, Version, etc.) Then I’m ready to start on my new EA. No muss, no fuss.

I know that cutting and pasting from this blog doesn’t work too well, so I’ve posted the file for download, just click here to get it.

That’s it for today.Tomorrow I’ll go into more detail about what all the code does in my EA Start Code file.

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.

Latest posts by admin (see all)

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news