Lot Size Library for Any Volume Step
- Bibliothèque
-
Jevon Naldo Yoshield
I build MetaTrader 5 tools I use on my own charts. Nine are on the Market.
Order Entry Panel sends market orders per click. Smart Risk and Lot Size Panel turns risk and stop into lots. Prop Firm Guard and Drawdown Lock caps my daily loss. - Version: 1.0
This library returns the largest lot whose loss at your stop does not exceed the money you allow it to risk, correct for any volume step, tick size and tick value the symbol reports.
I wrote it for the MQL5 developer who has pasted a lot-normalising block into every EA and can no longer say which copy is right. One import, one version, and a call that refuses rather than oversizing.
WHAT IT DOES
- It refuses instead of oversizing: when the smallest lot the symbol accepts would risk more than your limit, the call returns -6 instead of a bigger lot.
- Legal on any step: the size is floored onto the symbol's own step and never rounded up, so a 0.001-step symbol and a 0.25-step instrument are handled without a decimal count typed by hand.
- Three ways to state the risk: a money amount, a percentage of balance or equity, or a distance in points, plus the inverse call that says what a given volume risks.
- You cannot pass a wrong tick size: the library reads the symbol's volume and tick specification itself, at the moment of the call.
- A sentence for every code: nine error values, each with plain text that says what to check.
- No state and no orders: it returns a number, never sends, modifies or closes an order, and two calls with the same inputs in the same tick return the same value.
HOW IT WORKS
1. Put chimaster_lots.ex5 in MQL5\Libraries, under your terminal's data folder, and paste the import block below into the EA.
2. Call a sizing function with the symbol, the money you allow it to risk and the distance from entry to stop.
3. Read the number: a positive value is a size, and a negative value is an error code that CMS_ErrorText turns into a sentence.
THE TWELVE CALLS
#import "chimaster_lots.ex5"
double CMS_VolumeMin(const string symbol);
double CMS_VolumeMax(const string symbol);
double CMS_VolumeStep(const string symbol);
int CMS_VolumeDigits(const string symbol);
double CMS_NormalizeVolume(const string symbol, const double requestedVolume);
double CMS_MoneyPerLot(const string symbol, const double priceDistance);
double CMS_LotFromRiskMoney(const string symbol, const double riskMoney, const double stopDistancePrice);
double CMS_LotFromRiskPercent(const string symbol, const double riskPercent, const double stopDistancePrice, const int riskBase);
double CMS_LotFromRiskPoints(const string symbol, const double riskMoney, const double stopPoints);
double CMS_RiskOfVolume(const string symbol, const double volume, const double priceDistance);
string CMS_ErrorText(const int code);
string CMS_Version();
#import
The first four read the symbol's volume specification. CMS_MoneyPerLot turns a price distance into the loss of one lot. CMS_LotFromRiskMoney, CMS_LotFromRiskPercent and CMS_LotFromRiskPoints return the size from money, from a percentage of balance or equity, and from points; the percentage call takes 0 for balance and 1 for equity as its last argument. CMS_RiskOfVolume is the inverse. CMS_ErrorText and CMS_Version are the two string calls.
THE ERROR CODES
-1 unknown symbol, -2 volume specification unusable, -3 tick specification unusable, -4 distance not positive, -5 risk amount out of range, -6 the minimum lot risks more than you asked, -7 an input is not finite or is out of range, -8 the risk base is not 0 or 1, -9 no balance or equity to take a percentage from.
GOOD TO KNOW
- Compiled, not source, and the money figure is an estimate: you receive the compiled library, and money per lot uses the terminal's tick-value loss at the moment of the call, with no commission, swap or fee.
- No stop or margin checks: you supply the distance from entry to stop, and the library does not validate stop levels, freeze levels or margin.
- One order's worth: it clamps to the symbol's maximum volume and does not split a larger size, and it does not apply the symbol's total volume limit.
- One step from my panel: this library floors with a relative tolerance, so 0.3 lots on a 0.1-step symbol stays 0.3, where a plain floor gives 0.2. My Smart Risk and Lot Size Panel floors plainly, so the two can differ by one step there; both are legal, and neither risks more than the formula.
BEFORE YOU TRUST IT
The MQL5 compiler does not link-check an import: a missing library still compiles at 0 errors, and the failure appears when the terminal loads the code. So check the file. Paste the import block into an empty script, add Print(CMS_Version());, compile it and run it on any chart. It prints 1.00 when the terminal has loaded this build.
QUESTIONS I EXPECT
Will my EA compile without the library? Yes, and that is the trap: the compiler does not read the .ex5, so a missing file compiles cleanly and fails at load time. The version check above is the fast answer.
My broker's minimum is not a multiple of the step. Then the symbol specification is inconsistent. The library returns that minimum unchanged, and the server may reject the order when you send it.
Does it place or manage trades? No. It returns a number, and your EA decides what to do with it.
COMPATIBILITY
Any MT5 account type. Native MQL5, no DLLs.
SUPPORT
Questions through the MQL5 message system. First reply within two working days.
If you have used it, I would welcome a short review of what worked and what did not.
