# Lot Size Calculator

292

Hello

For a while I've been using the following lot size calculation in EAs, and more recently in a script I can drag onto a chart for it to do the lot size calc for manual trades automatically.  My trading partner said, 'wow and it handles situations where the base currency is GBP and the pair for calculation are for example AUDNZD where neither of the pair are the same as the base currency?'.  Now I'm suddenly in need of a sanity check that this is right.  I'm 99.9% sure it's OK other than the couple of points I've put in my own code, but I'd really appreciate someone on a higher plane than I reassuring me that I'm doing this right for all FX pairs (and would this work for Oil also?):

```double ReturnLots(double _risk, double sl, double tickVal, double price) {
double minlot = MarketInfo(Symbol(), MODE_MINLOT);
double maxlot = MarketInfo(Symbol(), MODE_MAXLOT);
double leverage = AccountLeverage();
double lotsize = MarketInfo(Symbol(), MODE_LOTSIZE);
double stoplevel = MarketInfo(Symbol(), MODE_STOPLEVEL);

string cur = AccountInfoString(ACCOUNT_CURRENCY);
double bal = AccountInfoDouble(ACCOUNT_BALANCE);                           # just noticed I should be using equity not the available balance?
_risk = _risk / 100;
double numerator   = bal * _risk;
double denominator = sl  * tickVal;

if(numerator <= 0 || denominator <= 0) {
return(1);                                                             # just noticed this should be -1
} else {
double result = numerator / denominator;
if(result < minlot) {
result = minlot;
} else if(result > maxlot) {
result = maxlot;
}
return(result);
}
}
```
Moderator
31608

strontiumDog:

Hello

For a while I've been using the following lot size calculation in EAs, and more recently in a script I can drag onto a chart for it to do the lot size calc for manual trades automatically.  My trading partner said, 'wow and it handles situations where the base currency is GBP and the pair for calculation are for example AUDNZD where neither of the pair are the same as the base currency?'.  Now I'm suddenly in need of a sanity check that this is right.  I'm 99.9% sure it's OK other than the couple of points I've put in my own code, but I'd really appreciate someone on a higher plane than I reassuring me that I'm doing this right for all FX pairs (and would this work for Oil also?):

• Why is tickVal a parameter and not updated inside the function ? if the profit currency is not the same as account currency, tick value is always changing, you need to be sure to use the current value.
• Your function is not independent, it works only if sl is a number of ticks. For example, on a 5 digits broker, if your stoploss is 40 pips, you need a parameter sl=400, is it the case ?
• If your calculated value is below minimum lot, you return minimum lot, that means your real risk is superior to "_risk", is it acceptable ?
15086

• You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
• Account Balance * percent = RISK = |OrderOpenPrice - OrderStopLoss| * OrderLots * DeltaPerlot (Note OOP-OSL includes the SPREAD)
• Do NOT use TickValue by itself - DeltaPerlot
• You must normalize lots properly and check against min and max.
• You must also check FreeMargin to avoid stop out
1. just noticed I should be using equity not the available balance?
Executive decision, but I "don't count my chickens before they're hatched."
2. I agree with angevoyageur on minimum lot. If you use it, your risk could be much higher than acceptable. It means you should skip the trade.