# Calculating position size

349

Hello

Im using this code snippet to calculate position size

Can anyone spot anything wrong with this code, either logically or programmatically?

Thanks!

```/* ==========================================================================
|
|   Function :      calcVolume
|
========================================================================== */
double calcVolume(double acctBal, double risk, string sym, int stopLoss)
{
double amtToRisk = acctBal * (risk * 0.01);

double lAmt = 0.0;
string baseCur = StringSubstr(sym, 0, 3); //Base Currency
string ccyCur = StringSubstr(sym, 3, 3);  //Counter Currency

if (AccountCurrency()==ccyCur) {
lAmt = amtToRisk;
Print("HG-lib2:1. Rate "+sym,": ",MarketInfo(sym, MODE_BID));
} else if (AccountCurrency()==baseCur) {
lAmt = amtToRisk * MarketInfo(sym, MODE_BID);
Print("HG-lib2:2. Rate "+sym,": ",MarketInfo(sym, MODE_BID));
} else {
if (MarketInfo(ccyCur+AccountCurrency(), MODE_BID) != 0.0) {
lAmt = amtToRisk / MarketInfo(ccyCur+AccountCurrency(), MODE_BID);
Print("HG-lib2:3. Rate "+ccyCur+AccountCurrency(),": ",MarketInfo(ccyCur+AccountCurrency(), MODE_BID));
} else if (MarketInfo(AccountCurrency()+ccyCur, MODE_BID) != 0.0) {
lAmt = amtToRisk * MarketInfo(AccountCurrency()+ccyCur, MODE_BID);
Print("HG-lib2:4. Rate "+AccountCurrency()+ccyCur,": ",MarketInfo(AccountCurrency()+ccyCur, MODE_BID));
}
}
double pos1 = (lAmt / stopLoss) ;
double posSize;
posSize = (ccyCur=="JPY") ? NormalizeDouble(MathRound(pos1/10)/100,4) : NormalizeDouble(MathRound(pos1*10)/100,4);

return MathMin(MarketInfo(sym, MODE_MAXLOT)-MarketInfo(sym, MODE_MINLOT), posSize);
}
```
15934

HarriMQL5: Can anyone spot anything wrong with this code, either logically
1. ```   lAmt = amtToRisk / MarketInfo(ccyCur+AccountCurrency(), MODE_BID);
:
double pos1 = (lAmt / stopLoss) ;```
What do you think currency/bid/pips means? Nothing! Babel!
2. In code
• 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/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around \$10/pip)
• 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
3. Use a GUI: Indicators: 'Money Manager Graphic Tool' indicator by 'takycard' Forum - Page 5