I thought it might be useful if there was a thread for "common code". There are a few for compilations of indicators and EAs, but none for snippets of code (as I can see).
Personally, most of my EAs are generally the same, there's the entry conditions, the order management loop, the only difference is what the entry condition is, and various functions to find out about the conditions.
It might also help improve our programming, I know lots of my code isn't optimal because what I have is "good enough", and so I haven't got around to improving it.
Anyway, thought I'd show you what I mean (might be best to in future have them in mql files, especially as things get more complex):
/////////////////////////////////////////////////
//Code so anything in first braces, or after is only called on a new bar.
extern int BarTime = 0;
if(Time[0] != BarTime)
{
BarTime = Time[0];
// whole EA could go here if you wanted, but can go afterwards.
}
else
{
return(0);
}
//////////////////////////////////////////////////////
//Money Management (sure this is awful, but seems to work).
extern int BalanceRisk = 0.01;
double Stop = Ask-100*Point;//NewLow;
double Entry = Ask;
double Risk = MathAbs(Stop-Entry);
double Lots = 0.1;
double TickValue = MarketInfo(Symbol(), MODE_TICKVALUE);
while( ((Lots+0.1)*TickValue*Risk)/Point < AccountBalance()*BalanceRisk)
{
Lots = Lots+0.1;
}
//////////////////////////////////
Obviously there are lots of variants upon these, but that's roughly how I do it atm.
Cheers
TraderHotch
Personally, most of my EAs are generally the same, there's the entry conditions, the order management loop, the only difference is what the entry condition is, and various functions to find out about the conditions.
It might also help improve our programming, I know lots of my code isn't optimal because what I have is "good enough", and so I haven't got around to improving it.
Anyway, thought I'd show you what I mean (might be best to in future have them in mql files, especially as things get more complex):
/////////////////////////////////////////////////
//Code so anything in first braces, or after is only called on a new bar.
extern int BarTime = 0;
if(Time[0] != BarTime)
{
BarTime = Time[0];
// whole EA could go here if you wanted, but can go afterwards.
}
else
{
return(0);
}
//////////////////////////////////////////////////////
//Money Management (sure this is awful, but seems to work).
extern int BalanceRisk = 0.01;
double Stop = Ask-100*Point;//NewLow;
double Entry = Ask;
double Risk = MathAbs(Stop-Entry);
double Lots = 0.1;
double TickValue = MarketInfo(Symbol(), MODE_TICKVALUE);
while( ((Lots+0.1)*TickValue*Risk)/Point < AccountBalance()*BalanceRisk)
{
Lots = Lots+0.1;
}
//////////////////////////////////
Obviously there are lots of variants upon these, but that's roughly how I do it atm.
Cheers
TraderHotch