Watch how to download trading robots for free
Find us on Twitter!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Scripts

Correlation-Aware Lot Size Calculator - script for MetaTrader 5

ali rajput
Published by:
Rana Ali
Rana Ali
  • MQL5 Developer & Algorithmic Trading Specialist at  Freelance
  • Pakistan
  • 10
Views:
63
Rating:
(2)
Published:
MQL5 Freelance Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

Correlation-Aware Lot Size Calculator

I got tired of position size calculators that only look at the trade you're about to open and ignore everything else sitting on your account. If you're already long EURUSD and you open GBPUSD too, most calculators will happily size that second trade using your full risk % again — even though the two pairs move together most of the time and you've basically doubled your bet on the dollar without realizing it.


This script tries to fix that. Before you open a new trade, it:

- Works out the normal risk-based lot size from your balance, risk %, and stop loss in points
- Goes through every position you currently have open, on any symbol
- Calculates the actual correlation between each of those symbols and the one you're about to trade, using real recent price history (not a fixed correlation table that's already outdated by the time someone publishes it)
- Checks direction too — same-direction trades on correlated pairs stack risk, opposite-direction trades on correlated pairs usually offset each other
- Adds up how much risk is already sitting in correlated positions, and shrinks your new lot size if you're close to (or past) a basket risk limit you set yourself

Everything gets printed to the Experts/Journal tab — which positions got flagged, why, and what lot size it recommends instead of your "normal" one.

A few things worth knowing before you use it:

- If a correlated position has no stop loss set, the script can't know its real dollar risk, so it warns you instead of guessing
- Correlation is calculated from the last N bars (you pick the timeframe and how many), so it reflects current market behavior — but it's still a backward-looking number and can shift over time
- This is read-only. It doesn't open, modify, or close anything on your account

Inputs:
- InpTargetSymbol — symbol you're about to trade (leave blank to use the current chart)
- InpNewTradeIsBuy — direction of the new trade
- InpRiskPercent — risk % for the new trade
- InpStopLossPoints — stop loss distance in points
- InpMaxBasketRiskPercent — max combined risk you're willing to carry across correlated positions
- InpCorrelationThreshold — how strong a correlation needs to be before it counts
- InpCorrelationBars / InpCorrelationTimeframe — controls how correlation is measured

I built this after catching myself stacking correlated pairs a few too many times without noticing until the drawdown showed up. If you only ever trade one pair at a time it won't do much for you — but if you run multiple positions across majors/minors together, it should catch things a plain lot calculator won't.

Free to use, modify, and adapt.
Prop Firm Rule Checker Prop Firm Rule Checker

Checks your trade history against common prop-firm challenge rules — profit target, max daily loss, max drawdown, consistency rule, and minimum trading days — with a clear PASS/FAIL report.

GDS Renko Donchian Demo EA GDS Renko Donchian Demo EA

A small educational Expert Advisor showing how a classic Donchian breakout can be applied directly to completed Renko bricks.

Risk-Based Lot Size Calculator Risk-Based Lot Size Calculator

A simple MQL5 script that calculates the correct lot size for a trade based on your account risk percentage and stop loss distance in pips, so every trade risks a consistent, controlled amount of your balance.

MACD Signals MACD Signals

Indicator edition for new platform.