72 lines
No EOL
1.8 KiB
Markdown
72 lines
No EOL
1.8 KiB
Markdown
# Smart Lot Calculator for MQL5
|
|
|
|
Smart Lot Calculator is an open-source MQL5 library for calculating position size according to the amount of money a trader is willing to risk.
|
|
|
|
The library uses MetaTrader 5's native `OrderCalcProfit()` function to estimate the potential loss between the entry price and the Stop Loss price in the account currency.
|
|
|
|
## Features
|
|
|
|
- Calculates volume by risk percentage
|
|
- Calculates volume by a fixed monetary risk
|
|
- Supports balance, equity and free-margin risk bases
|
|
- Uses the broker's own symbol calculation settings
|
|
- Supports buy and sell calculations
|
|
- Respects minimum and maximum volume
|
|
- Respects the symbol volume step
|
|
- Rounds volume down to avoid exceeding the selected risk
|
|
- Provides clear error messages
|
|
- Does not open or manage trades
|
|
|
|
## Repository structure
|
|
|
|
```text
|
|
Include/SmartLotCalculator.mqh
|
|
Scripts/SmartLotCalculatorExample.mq5
|
|
Docs/Usage.md
|
|
```
|
|
|
|
## Basic example
|
|
|
|
```cpp
|
|
#include "../Include/SmartLotCalculator.mqh"
|
|
|
|
void OnStart()
|
|
{
|
|
MqlTick tick;
|
|
|
|
if(!SymbolInfoTick(_Symbol, tick))
|
|
return;
|
|
|
|
double entry_price = tick.ask;
|
|
double stop_price = entry_price - (500 * _Point);
|
|
string error = "";
|
|
|
|
double volume = CSmartLotCalculator::CalculateByRiskPercent(
|
|
_Symbol,
|
|
ORDER_TYPE_BUY,
|
|
entry_price,
|
|
stop_price,
|
|
1.0,
|
|
RISK_BASE_EQUITY,
|
|
error
|
|
);
|
|
|
|
if(volume <= 0.0)
|
|
{
|
|
Print("Calculation error: ", error);
|
|
return;
|
|
}
|
|
|
|
Print("Recommended volume: ", DoubleToString(volume, 2));
|
|
}
|
|
```
|
|
|
|
## Important
|
|
|
|
This project is a position-sizing reference tool. It does not guarantee profits, prevent losses or provide trading signals.
|
|
|
|
Always verify the calculated volume before trading. Symbol specifications and trading conditions can differ between brokers.
|
|
|
|
## License
|
|
|
|
MIT License. |