The lot calculation indicator appeared one step smaller.
142.999… The value inside the computer for 1.43 ÷ 0.01 | Six bugs that were present | 0 items found due to compilation errors |
I am lab.aiueo, creating and selling MT4/MT5 indicators on GogoJungle.
RM-LOT is an MT4 indicator that calculates the optimal lot size when you drag the stop-loss line on the chart. It also displays on the panel the margin maintenance requirement and the price at which a margin call would occur before you place the order.
Because it outputs monetary figures, I manually verified the numbers on the panel in a demo account one by one before displaying them. I tested only long, only short, and very small lot sizes.
While developing, I encountered six bugs. Each passed compilation, and the panel showed plausible numbers.
1The lot always appeared one step smaller
In a scenario where it should have been 1.43 lots, the panel showed 1.42.
The cause was a line that truncates the lot to the broker’s step (0.01). 1.43 ÷ 0.01 becomes 142.9999… in the computer. Truncating this yields 142. Multiply by 0.01 gives 1.42.
Fix: add a very small number before truncating.
// 1e-8 is to counter floating point rounding errors. Without this, 1.43/0.01 becomes 142.9999…
// originally, a lot of 1.43 would drop to 1.42
g_lot = MathFloor(rawLot / lotStep + 1e-8) * lotStep;
It shifts downward slightly, so it’s not a loss-inducing bug. Still, for a tool that claims to show the exact calculated lot, being off by one step is unacceptable.
2In a dollar account, cents disappeared
When displaying monetary amounts, decimals were truncated. In a yen account this isn’t a concern, but a dollar account would show, for example, 12.85 dollars as 12 dollars.
I fixed the display to match the account’s currency.
3When changing settings, the stop loss line returned to its original position
After aligning the stop loss line and changing risk percentage and other settings, the line returned to its initial position.
In MT4, changing settings restarts the indicator. When it ended, all drawn lines were erased, which caused this.
After analyzing why it ended, I decided to preserve the line when adjusting settings, recompiling, or changing timeframes.
void OnDeinit(const int reason)
{
// Keep price lines on parameter changes, recompiles, and timeframe changes.
// Prevents carefully set stop loss from resetting with every operation
if(reason == REASON_PARAMETERS || reason == REASON_RECOMPILE ||
reason == REASON_CHARTCHANGE)
{
// Delete objects except price lines (ENTRY・SL・TP)
...
return;
}
ObjectsDeleteAll(0, PREFIX);
}
When removing the indicator from the chart, I delete all as before.
4Lines couldn’t be dragged as-is
In MT4, a line on the chart must be in an “selected” state to drag it. There was an extra step of selecting by double-clicking before moving it.
I made the lines stay in the selected state from the start. You can choose which lines are draggable via settings.
5When the account isn’t connected, take-profit shows as “0.0 pips”
In a disconnected state (balance 0), the panel displayed the distance to take profit as “0.0 pips.”
The cause was the order of calculations. I first checked, “If balance is 0, stop here,” then calculated pips and risk-reward afterward.
Pips and risk-reward depend only on price. They are independent of account status. I reordered calculations so price-derived values are computed before account verification.
6Setting defaults caused color-detection to fail
The stop-out level (percentage) was initially entered manually. Later, I changed the default to “0 = read broker settings automatically.”
Then, the red indicator for near-stopout stopped working because it directly read the setting's value (0).
Stop-out level can be read automatically from MT4
Related to item 6, I had one assumption corrected: I thought MT4 wouldn’t provide the stop-out level, so users had to enter it manually.
In fact, MT4 provides a function to read it.
// If StopOutLevel > 0 then prefer manual specification; 0 means auto-fetch broker setting
// (manual spec remains in case broker doesn’t return a value)
if(StopOutLevel > 0.0)
{
g_lcLevelUsed = StopOutLevel;
g_lcModeUsed = 0;
g_lcKnown = true;
}
else
{
g_lcLevelUsed = (double)AccountStopoutLevel();
g_lcModeUsed = AccountStopoutMode();
g_lcKnown = (g_lcLevelUsed > 0.0);
}
The manual entry setting is kept for brokers that don’t return a value.
Common across the six fixes
- None of them caused compilation errors
- All displayed plausible numbers on the panel
- Found by manual verification or after performing a different operation (changing settings, being disconnected)
A tool that outputs monetary numbers cannot be verified just by looking at the screen. I recalculated every number by hand.
This article on the initial lot issue is part of a series about MQL errors in AI-related code.
Why not make it a tool solely for lot calculation
There are many free tools that only calculate the lot. So I consolidated the post-trade account status (margin maintenance, price of margin call, and the number of consecutive losses until capital halves) into a single panel.
Details of this decision are written in the article I decided not to create, in the next piece.
RM-LOT is priced as the cheapest among my paid products. The current price is 980 yen.
Prices as of November 14, 2026. May change.
Sales: 4 units from August to September. 0 units in October.
What I decide as the developer
- Always verify monetary numbers by hand before displaying them. Include long, short, and very small lots
- Add rounding error compensation before truncation
- Price-derived values should be calculated before account verification
- Keep user-adjusted lines even if the indicator restarts due to setting changes
- When changing defaults, verify every place reading that value directly
- Before deciding that MT4 can’t provide a value, search for the function
RM-LOT
By simply dragging the stop-loss line, it shows the exact lot size you would need, along with the margin maintenance rate after entry, the price at which margin call would occur, and how many consecutive losses it can withstand, all before placing an order. It does not place orders.
RM-LOT (MT4) | See the sales page |
This article is a record of my own development process. The code excerpts shown illustrate concepts and do not guarantee operation. Calculations for lot size and margin maintenance can vary by broker, so please verify amounts yourself before placing orders. The indicators described here are tools to aid market analysis and do not guarantee profit. Exercise investment judgment at your own risk.
Thank you for reading until the end.