# is the LotsOptimized func in the Moving Average MT4 sample EA even sane?

440

I am looking at the LotsOptimized function in the sample EA "Moving Average.mq4", this is a EA which comes with every installation of MT4 and is obviously intended as a sample/template for people to learn how to write EAs...

And I can not get it through my head how can this code be part of the install, what can it teach a newbie when it's full of.... let's say "inconsistencies"...

Here's the function

```extern double Lots               = 0.1;
...
//+------------------------------------------------------------------+
double LotsOptimized()
{
double lot=Lots;
int    orders=HistoryTotal();     // history orders total
int    losses=0;                  // number of losses orders without a break
//---- select lot size
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//---- calcuulate number of losses orders without a break
if(DecreaseFactor>0)
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
//----
if(OrderProfit()>0) break;
if(OrderProfit()<0) losses++;
}
if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
}
//---- return lot size
if(lot<0.1) lot=0.1;
return(lot);
}```

So the first thing we see in the body of the function is that the value of the extern var 'Lots' is assigned to the local var 'lot':

```   double lot=Lots;
```

Right after that we overwrite the value of lot with calculations based on AccountFreeMargin. So what's the point of assigning Lots to it? What's the point of having the extern Lots for that matter? it DOES NOT DO ANYTHING - because it's only use is to be assigned to 'lots' - which is then immediately overwritten with another value.

So... up till now we have

1. a useless assignment statement;
2. an extern which is not used.

... not exactly a code to use as guidelines...

Let's look at the formula which is used to calculate the value of lots:

```   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
```

So the intention was probably to calculate maximum number of lots based on risk (risk being a percentage of AccountFreeMargin expressed as a double, so 0.05 would be 5% risk). What is the meaning of 1000.0 in this formula?? I don't know. Probably nobody knows. Because it is meaningless.

If we wanted to get the number of lots that we can open as a percentage of the free margin then the proper way of doing it would be:

1. Calculate the percentage of Free Margin that we want to put at risk:

`double free_marg_at_risk = AccountFreeMargin()*MaximumRisk;`

2. Calculate how many lots we can open with this margin:

```double marg_one_lot = AccountFreeMargin() - AccountFreeMarginCheck( symbol, oper, 1 );
double lots = free_marg_at_risk / marg_one_lot;
```

So to summarize - I don't know how this "sample" EA ended up in the MT4 code samples but IMO it is hardly a sample of anything... except bad coding. I think it needs to be revised (and in the mean time - removed from the MT4 samples codebase as to not to confuse MQL novice programmers looking for lot calculation code samples).

Moderator
18021

4x4ever:

I am looking at the LotsOptimized function in the sample EA "Moving Average.mq4", this is a EA which comes with every installation of MT4 and is obviously intended as a sample/template for people to learn how to write EAs...

And I can not get it through my head how can this code be part of the install, what can it teach a newbie when it's full of.... let's say "inconsistencies"...

Nobody has a Monopoly on writing bad code . . . we all can, and do, do it.
440

Well... you CAN'T, sorry. You are the Gods of the MT4 universe and your code is supposed to be pure perfection

Seriously though - including bad code in the examples is more of a disservice than a service... If you can't do it right - nobody can. After all these are EXAMPLES! People, especially novices, will take for granted that code provided by the company that created the MT4 platform and MQL language is clean, reliable and generally speaking - good to follow as coding guidelines and simply as copy-paste snippets in their own code.

Moderator
18021

4x4ever:

Well... you CAN'T, sorry. You are the Gods of the MT4 universe and your code is supposed to be pure perfection

Don't get me mixed up with MetaQuotes . . . I am just a User like you who helps out here for no reward.
440

oh... ok, then you CAN write bad code, no problem ... but you probably don't, anyway ... :)

Sorry - I thought you are with MQ because it says you are a 'Moderator' under your avatar...

6374

4x4ever:

• a useless assignment statement;
• an extern which is not used.

You have to see that code as an example

It might be that time this code was made the leverage brokers were offering was all the same

don't know might be the 1000.0 did had a meaning that time

like digitnotation was also pip related

and commission / swap was something that has no influence

ECN accounts we didn't have

now we know why we don't have to use

`if(Volume[0]>1) return;`

and why we have to count down closing trades

Also this code is not giving a good example how to open trades

```   if(Open[1]>ma && Close[1]<ma)
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
return;
}
if(Open[1]<ma && Close[1]>ma)
{
return;
}```

if it fails GetLastError() is not called....

but for testing and changing to learn ( like put in stoploss and takeprofit with trailing function ) you can find out how to code better

and that is also how you can look at the most of the code that is written inside Code Base

not all coders that have published inside CodeBase are experts writing a program

440

Yes, there's probably many reasons why this code was put there at some point in the past... The important part is we all agree it is not a very good code so hopefully this post will draw the attention of some decision-maker at MetaQuotes and they will decide it's a shame to ship their software with this code as example and fix it ... or at the very least remove it.