Bot EA
- Lot Size with 0.1
- How to code?
- Simple risk percentage lot size calculation > Not working (yet)
European broker-dealers and Asian broker-dealers generally have different account currencies, so you have to detect that first, and then you can put some code in your EA to calculate the value of your SL:
// in globals input double MaxRiskPercent = 1.1; input int StopPips = 178; double point, pointMultiplier, tickSize, tickValue, pointValue, pipValue, accountBalance, maxRiskMoney, lotSize; // in OnInit() point = SymbolInfoDouble(BaseSymbol, SYMBOL_POINT); tickSize = SymbolInfoDouble(BaseSymbol, SYMBOL_TRADE_TICK_SIZE); tickValue = SymbolInfoDouble(BaseSymbol, SYMBOL_TRADE_TICK_VALUE); pointValue = point * (tickValue / tickSize); if(_Digits == 3 || _Digits == 5) { pipValue = pointValue * 10 * lotSize; pointMultiplier = 10; } else { pipValue = pointValue * lotSize; pointMultiplier = 1; } accountBalance = AccountInfoDouble(ACCOUNT_BALANCE); maxRiskMoney = accountBalance * MaxRiskPercent / 100; lotSize = NormalizeDouble(GetLot(ORDER_TYPE_BUY, NormalizeDouble(2.00000 - StopPips * _Point * pointMultiplier, _Digits), NormalizeDouble(2.00000, _Digits), maxRiskMoney), 2); if(pipValue * StopPips * lotSize > maxRiskMoney) { Alert("Math error! Initial StopPips and lotSize exceed max risk of $" + DoubleToString(maxRiskMoney, 2)); ExpertRemove(); } Print("lotSize: ", lotSize);
Here, I'm comparing the value of my SL to the value of my max risk. In this code snippet, pipValue * StopPips (highlighted) is what you want.
Note that NormalizeDouble() and DoubleToString() are somewhat blunt functions that round decimal digits. Those are fine for my purposes but if you want pinpoint accuracy in your math, see Fernando Carreiro's post in another thread.
- 2024.12.18
- www.mql5.com