CyberEA Template has a money management tool integrated. This tool get the lot size for the positions based on risk and stop loss settings.
Extenal variables of MoneyManagement tool:
- UseMoneyManagement: Set to true if you want to use automatic calculation of lot size.
- UseMicroLots: Set to true if you are going to trade in 0.01 lots steps. IMPORTANT: Set it to true when you trade in 0.01 step, even if you set UseMoneyManagement=false.
- MaximumRisk: Enter your max. risk. Example: 5 mean you accept a loss of 5% of your account per trade.
- FixedLots: Enter the number of lots per trade when UseMoneyManagement=false.
How stoploss affects to automatic calculation of lot size?
I will explain with an example.
If you set an stoploss of 150 pips and a MaximumRisk=5, this mean that if stoploss is hitted the max. loss will be 5% of your balance and the lot size will be calculated based on this. So if you set stoploss of 50 pips the lot size will be greater than if you set 150pips stop loss for the same max risk.
The MoneyManegement code: The loop is a double function that must be called when you need calculated the lot size, for example in OrderSend function you have to write LotsRisk(int sl) instead a number of lots. In our case we set the stop loss with the variable StopLoss so we have to write LotsRisk(StopLoss) when lot size calculation is needed.
double LotsRisk(int sl)
{
if (UseMicroLots == false)
{
if (!UseMoneyManagement)
{
LotSize = NormalizeDouble(FixedLots,1);
if (LotSize < 0.1) LotSize = 0.1;
if (Kumo == 1) Print("Position lot size: " + LotSize);
}
if (UseMoneyManagement)
{
LotSize = NormalizeDouble(AccountFreeMargin() * MaximumRisk / 1000.0 / sl,1);
if (LotSize < 0.1) LotSize = 0.1;
if (Kumo == 1) Print("Position lot size: " + LotSize);
}
}
if (UseMicroLots == true)
{
if (!UseMoneyManagement)
{
LotSize = NormalizeDouble(FixedLots,2);
if (LotSize < 0.01) LotSize = 0.01;
if (Kumo == 1) Print("Position lot size: " + LotSize);
}
if (UseMoneyManagement)
{
LotSize = NormalizeDouble(AccountFreeMargin() * MaximumRisk / 1000.0 / sl,2);
if (LotSize < 0.01) LotSize = 0.01;
if (Kumo == 1) Print("Position lot size: " + LotSize);
}
}
return(LotSize);
}