Most place sizing recommendation stops at one line: threat 1% of your account, divide by your cease distance, and there may be your lot measurement. The arithmetic is correct. The issue is that the quantity it offers you usually can’t be despatched to the dealer, and when it might, it steadily dangers greater than you meant.
This submit covers what MetaTrader 5 really requires, with a labored instance and the code to do it correctly.
The calculation everybody offers you
You’ve gotten a ten,000 USD account. You might be prepared to lose 1% on this commerce, so 100 USD. You wish to purchase XAUUSD at 2,400.00 with a cease at 2,390.00, which is a distance of 10.00.
The standard system:
tons = threat in cash / (cease distance x worth of 1 level per lot)
For XAUUSD the contract is 100 ounces, so a ten.00 transfer is price 1,000 USD per lot. That offers 100 / 1000 = 0.10 tons.
Clear. And on this explicit instance it occurs to work. Change the account foreign money, the image, or the dealer, and it stops working, as a result of three issues had been assumed relatively than checked.
What the dealer really imposes
4 image properties determine whether or not your calculated quantity is actual.
- SYMBOL_TRADE_TICK_VALUE is the cash worth of 1 tick for one lot, expressed in your account foreign money. That is the quantity that makes the calculation transportable. In case your account is in EUR and also you commerce a USD-quoted image, the worth already consists of the conversion, and it strikes with the change charge.
- SYMBOL_TRADE_TICK_SIZE is the smallest worth change. It isn’t all the time equal to the purpose. On some symbols a tick is a number of factors, and a system written in factors shall be flawed by that issue.
- SYMBOL_VOLUME_MIN, SYMBOL_VOLUME_MAX and SYMBOL_VOLUME_STEP outline which volumes exist. A step of 0.01 means 0.137 tons doesn’t exist. Neither does 0.10 if the minimal is 0.25.
- SYMBOL_TRADE_STOPS_LEVEL is the minimal distance, in factors, that the dealer accepts between the present worth and a cease. Place a cease nearer than that and the order is rejected with error 10016, invalid stops. This one has nothing to do with sizing, nevertheless it decides whether or not the commerce you sized could be despatched in any respect.
The calculation that survives a dealer change
Worth of 1 level, for one lot, in account foreign money:
level worth = tick worth x (level / tick measurement)
Then:
tons = threat in cash / (cease distance in factors x level worth)
On the XAUUSD instance, with a degree of 0.01 and a tick measurement of 0.01, the purpose worth is 1.00 USD per lot. The cease is 1,000 factors away. So 100 / (1000 x 1.00) = 0.10 tons. Identical reply as earlier than, however now it holds when the account foreign money or the tick measurement adjustments.
There’s additionally a shortcut that MetaTrader 5 offers you and that just about no person makes use of. OrderCalcProfit() asks the terminal what a transfer from one worth to a different is price, for a given quantity, on a given image. It handles the contract measurement, the tick worth and the foreign money conversion for you:
double loss = 0.0;
OrderCalcProfit(ORDER_TYPE_BUY, _Symbol, 1.0, entry, cease, loss);
// loss is now the cash results of one lot shifting from entry to cease
double tons = riskMoney / MathAbs(loss);
When you solely take one factor from this submit, take this one. It removes a complete class of foreign money and contract measurement bugs.
Spherical down, by no means up
Your system offers 0.137 tons. The amount step is 0.01. You now have to decide on between 0.13 and 0.14, and the reply is to spherical down, all the time.
Rounding up appears innocent, and on this instance it takes your threat from 100 USD to about 102 USD. However the error is proportional, and on a small account with a minimal quantity of 0.01 it may be a lot worse. In case your calculation offers 0.004 tons and the minimal is 0.01, the sincere reply is to not spherical as much as the minimal. It’s that this commerce is simply too giant for this account at this cease distance. Both transfer the cease, or skip the commerce.
tons = MathFloor(tons / step) * step;
double minLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
if(tons < minLot)
return(0.0); // the commerce doesn’t match, don’t pressure it
Test the cease earlier than you measurement
Sizing a commerce whose cease the dealer will refuse is wasted work. The examine is 2 strains:
double minDistance = stopsLevel * SymbolInfoDouble(_Symbol, SYMBOL_POINT);
if(MathAbs(entry – cease) < minDistance)
return(0.0); // dealer will reject this cease
Observe that stops stage is just not mounted. Brokers widen it round information and on the session open, which is precisely when a good cease appears most engaging. A price learn at connect time and cached shall be flawed on the worst attainable second.
Placing it collectively
//| Quantity for a given cash threat, or 0 when the commerce doesn’t match. |
//+——————————————————————+
double PositionSize(const string image, const double riskMoney,
const double entry, const double cease)
//+——————————————————————+
4 checks, one name, no assumptions concerning the image or the account foreign money.
Doing it with out writing code
The calculation above has to run earlier than each commerce, and it has to learn stay image properties every time. That’s positive inside an Knowledgeable Advisor. It’s tedious if you commerce manually.
Pulsar Terminal does this for you. You set the chance, drag the cease, and it reads the tick worth, the quantity step and the stops stage from the dealer at that second, then exhibits the quantity you may really ship. The 2 failure instances above, a quantity beneath the minimal and a cease contained in the stops stage, are reported earlier than the order goes out relatively than as an error code afterwards.

What to remove. Use OrderCalcProfit() relatively than a hand written system. Spherical the quantity right down to the step, by no means up. Test the stops stage earlier than sizing, and browse it recent every time. And when the quantity comes out beneath the dealer minimal, that’s details about the commerce, not a rounding drawback to unravel.


