How to Calculate Position Size in MQL4: Why Most Traders Get It Wrong (And How to Fix It)
Camovia Tray Team · 2026-09-09
One of the most common misconceptions among new traders is that position sizing is simply about picking a random lot size and hoping for the best. The reality is far more dangerous: without a proper risk-based position sizing calculation, you're essentially gambling with your account. The classic mistake is assuming that because you're using MetaTrader 4, the platform will somehow protect you from over-leveraging. It won't. That's entirely on you.
The fundamental rule of risk management is to risk only a small, fixed percentage of your account on any single trade—typically between 1% and 2% . This prevents a string of losses from wiping out your capital. The formula itself is straightforward, but executing it correctly in MQL4 requires careful attention to detail, especially when dealing with different brokers and currency pairs.
The Core Formula: Breaking It Down
At its simplest, the algorithm for calculating position size in MQL4 is:
Lots = (AccountBalance * RiskPercentage) / (StopLossInPips * PipValue)
However, this simple equation hides several complexities. Let's break down each component:
- AccountBalance(): This native function returns your current account balance .
- RiskPercentage: This is your predetermined risk per trade, expressed as a decimal (e.g., 1% becomes 0.01).
- StopLossInPips: The distance, in pips, from your entry price to your stop-loss level.
- PipValue: This is where things get tricky.
MarketInfo(Symbol(), MODE_TICKVALUE)returns the tick value, but this is often misused. A common mistake is failing to account for brokers using 5-digit pricing. For 5-digit brokers,MODE_TICKVALUEreturns the value of a pipette (1/10 of a pip), not a full pip . To correct this, you must multiply the tick value by 10 ifDigitsis 3 or 5.
The MQL4 Implementation: A Function That Works
Here's a robust MQL4 function that handles these complexities:
// Function to calculate position size
double CalculateLotSize(double riskPercentage, double stopLossPips) {
double lotSize = 0.0;
// Get the value of a tick
double tickValue = MarketInfo(Symbol(), MODE_TICKVALUE);
// Adjust for 5-digit brokers (pipette to pip)
if (Digits == 3 || Digits == 5) {
tickValue *= 10;
}
// Calculate the risk amount in account currency
double riskAmount = AccountBalance() * (riskPercentage / 100.0);
// Calculate the lot size
lotSize = riskAmount / (tickValue * stopLossPips);
// Normalize to the broker's lot step
double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
lotSize = MathFloor(lotSize / lotStep) * lotStep;
// Ensure the lot size is within allowed limits
double minLot = MarketInfo(Symbol(), MODE_MINLOT);
double maxLot = MarketInfo(Symbol(), MODE_MAXLOT);
if (lotSize < minLot) lotSize = minLot;
if (lotSize > maxLot) lotSize = maxLot;
return NormalizeDouble(lotSize, 2);
} This function addresses several critical pitfalls: it corrects for pipette values, respects the broker's lot step increments, and ensures you don't trade below the minimum lot size .
From Code to Execution: The Real-World Challenge
While having the code is essential, the real challenge lies in integrating this calculation seamlessly into your trading workflow. Many traders find themselves constantly switching between their chart and the terminal to manually input calculated lot sizes—a process that's both time-consuming and prone to errors. If you're manually checking your calculator or spreadsheet every time you enter a trade, you're working harder, not smarter.
This is where tools that streamline the experience come into play. After you've embedded your position sizing logic into your EA or script, you need a way to monitor your open positions and manage them efficiently without being glued to the MT4 terminal. Constantly alt-tabbing to check your risk exposure or to close a trade distracts you from what really matters: market analysis.
For instance, if you have an EA that uses the above logic to open trades based on ATR stops or EMA crossovers , you'll want to monitor the resulting positions conveniently. Camovia Tray addresses this exact friction point. It turns your MT4 into a tray tool, allowing you to check live quotes and manage your open positions—viewing unrealized P&L and even closing trades—directly from your system tray . This means you can verify that your EA's position sizing logic was executed correctly and monitor its performance without needing to bring the entire MT4 window to the foreground.
Pitfalls to Avoid: What Most Guides Don't Tell You
Even with a solid function, there are several subtle traps that can break your calculation:
- TICKVALUE Reliability:
MODE_TICKVALUEcan be unreliable for non-FX instruments like indices or commodities. For these, you may need to calculate the tick value manually based on the contract size . - Account Currency Mismatches: If your account currency isn't the same as the currency pair's base, you need to adjust for exchange rates .
- Free Margin Checks: Your risk calculation should also check
AccountFreeMargin()to ensure you have enough margin for the trade; otherwise, your broker may reject the order. - Stop Loss Placement: Some traders mistakenly base their stop loss on arbitrary pip distances rather than technical levels. Your stop loss should be placed where the trade thesis breaks, not where your risk percentage dictates, even if that means the risk is smaller .
A Word on Progressive Sizing
While fixed percentage risk is the standard, some advanced strategies use progressive sizing based on account growth. For example, the method of progressive investment suggests that the lot size for each new order should be proportional to the free margin available, growing with profitable trades and shrinking with losses . While this can be implemented in MQL4, it introduces additional complexity and isn't recommended for beginners.
Conclusion: Risk First, Profit Second
Mastering position size calculation in MQL4 isn't just about writing correct code—it's about internalizing a risk-first mindset. The most sophisticated EA in the world will eventually blow up an account if it doesn't have proper position sizing logic. Start with a simple, risk-based calculation, test it thoroughly on a demo account, and gradually incorporate the complexities mentioned above. And once your logic is set, make sure your trading environment supports efficient monitoring and execution, so you can stay focused on your strategy rather than buried in terminal windows.
Turn MT5 / MT4 into a Tray Tool
Check quotes, manage positions, and hide in one click.
Download Camovia TrayFrequently Asked Questions
Are my quotes and positions uploaded anywhere?
No. Quote and position data is read 100% from your local MT5/MT4 terminal and never leaves your computer. See the privacy policy for details.
Which systems and terminals are supported?
Windows 10 / 11, with MetaTrader 5 or MetaTrader 4 (installed and logged in; MT4 needs the bridge EA attached once).
Is the MT4 bridge EA safe? What do I need to enable?
Yes. The bridge EA (CamoviaBridge) is bundled with the app - it only reads quotes/positions locally and executes close commands; no DLLs, no data uploads. Closing positions requires turning on AutoTrading in the MT4 toolbar.
How much does it cost? Is there a free trial?
Subscription pricing starts at $2.49/month (also $6.99/3 months, $13.49/6 months, $23.99/year), all plans with full features. New users can try it free before subscribing.
Related Articles
Best MT5 Tray Quotes: Can You Really Check Live Prices Without Opening the Terminal?
Discover the best MT5 tray quotes solution. Camovia Tray lets you check live prices & manage positions from the system tray without opening the terminal.
How to Watch MT5 Prices Directly from the System Tray
Tired of opening your terminal every time you want an MT5 quote? Put prices in the system tray - hover and see. Camovia Tray keeps an eye on the market for you.
How to Hide MT4 Window: A Complete Guide for Discreet Trading
Learn how to hide MT4 window effectively with Camovia Tray. Discover tray-based trading that keeps quotes and positions accessible without the visible terminal.
The MT5 System Tray Myth: Why "Not Working" Usually Means You're Looking in the Wrong Place
Transform your MT5 into a tray tool with Camovia Tray. Check live quotes, manage positions, and close trades instantly from the Windows system tray. No main window needed.
How to Hide Your MT5 Window from the Taskbar and Alt+Tab
Want to hide the MT5 window from the taskbar and Alt+Tab? Camovia Tray hides it in one click - and you can still check quotes and positions from the tray.