# Calculating lot size based on Risk

22

I am currently testing an EA which I have converted from MQL4 code to new MQL5 code. Things generally work but the lot size calculation (based on a fixed risk percentage) is off:

```double subGetLotSize (const string sSymbol, const double dRiskPercent, const bool bAccountIsMicro=false) export
{
double dPrice        = 0.0;
double dMarginPerLot = 0.0;

return 0.0;
}

if (!OrderCalcMargin(ORDER_TYPE_BUY, sSymbol, 1.0, dPrice, dMarginPerLot)) {
return 0.0;
}

if (dMarginPerLot <= 0.0) {
return 0.0;
}

double dLots          = 0.0;
double dAccountMargin = AccountInfoDouble (ACCOUNT_MARGIN_FREE);
double dRiskMargin    = dAccountMargin * (dRiskPercent/100);
double dStepVol       = SymbolInfoDouble (sSymbol, SYMBOL_VOLUME_STEP);

dLots = NormalizeDouble (dRiskMargin/dMarginPerLot, 2);
dLots = dStepVol * NormalizeDouble(dLots/dStepVol, 0);
Print ("GETLOTSIZE: Margin=" + DoubleToString(dAccountMargin) + ", RiskMargin=" + DoubleToString(dRiskMargin) + ", MarginPerLot=" + DoubleToString(dMarginPerLot) +
", RiskPrecent=" + DoubleToString(dRiskPercent) + ", StepVol=" + DoubleToString(dStepVol) + ",Lots=" + DoubleToString(dLots));

double dMinVol = SymbolInfoDouble(sSymbol, SYMBOL_VOLUME_MIN);
if (dLots < dMinVol) {
dLots = dMinVol;
}

double dMaxVol = SymbolInfoDouble(sSymbol, SYMBOL_VOLUME_MAX);
if (dLots > dMaxVol) {
dLots = dMaxVol;
}

return dLots;
}```

When my EA runs, I get this output:

```2019.02.14 21:00:00.435 ADV_MA_Cross (#US30,M15)        GETLOTSIZE: Margin=10075.85000000, RiskMargin=503.79250000, MarginPerLot=45.13000000, RiskPrecent=5.00000000, StepVol=1.00000000,Lots=11.00000000
```

However, when running in a Demo account against US30, the margin for the opened position (11 Lots) is 1240 EUR (approx 1400 USD).

The logic my function is using seems quite simple/basic and I'm not seeing where it would be going wrong. Could anybody give me a hint as to what's causing this problem/discrepancy?

Thanks a lot

22

TheRealMorris:

I am currently testing an EA which I have converted from MQL4 code to new MQL5 code. Things generally work but the lot size calculation (based on a fixed risk percentage) is off:

When my EA runs, I get this output:

However, when running in a Demo account against US30, the margin for the opened position (11 Lots) is 1240 EUR (approx 1400 USD).

The logic my function is using seems quite simple/basic and I'm not seeing where it would be going wrong. Could anybody give me a hint as to what's causing this problem/discrepancy?

Thanks a lot

If anybody has any ideas, I'd appreciate a hint. I've been looking at this for hours and can't find the problem (it's probably something stupid) but I'm stuck :-(
373

TheRealMorris:
If anybody has any ideas, I'd appreciate a hint. I've been looking at this for hours and can't find the problem (it's probably something stupid) but I'm stuck :-(
Ideas about what? What did you expect?
22

lippmaje:
Ideas about what? What did you expect?
Ideas about why this code results in a lot size which is not appropriate for the value of the dRiskPercent parameter ... I'm stumped on this one ...
373

Code looks ok. Sounds like you're testing on two different accounts. Post some logs/screenshots.
22

lippmaje:
Code looks ok. Sounds like you're testing on two different accounts. Post some logs/screenshots.
Thanks for giving it a look. Following your suggestion, I tested this code on two other brokers and there it works fine. So it's something weird about my broker but I still have no idea what's causing it.