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"...
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.
Well... you CAN'T, sorry. You are the Gods of the MT4 universe and your code is supposed to be pure perfection
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...
you're right it is "bad"
- a useless assignment statement;
- an extern which is not used.
You have to see that code as an example
It is made years ago....
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; } //---- buy conditions if(Open[1]<ma && Close[1]>ma) { res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue); 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
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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
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
... not exactly a code to use as guidelines...
Let's look at the formula which is used to calculate the value of lots:
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:
2. Calculate how many lots we can open with this margin:
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).