Advertisement IC Markets

Lot sizing function gotchas across JPY pairs and gold, the bug that over-risked me

Share, develop, and backtest custom MQL4/MQL5 Expert Advisors, Python data-scraping scripts, trading bots, and automated market alert systems.
Post Reply
Fairman
Posts: 6955
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Lot sizing function gotchas across JPY pairs and gold, the bug that over-risked me

Post by Fairman »

Lot sizing function gotchas across JPY pairs and gold, the bug that over-risked me

wrote an equity based position sizing function for my EA, risk a fixed percent per trade based on the stop distance. Worked on EURUSD in testing. Then I put it on USDJPY and gold and the risk per trade was completely wrong, either far too big or tiny.

The problem was tick value and tick size. Different symbols have different contract sizes, and the value of one pip or point isn't the same. On JPY pairs a pip is 0.01 instead of 0.0001, and on gold the point size depends on how the broker quotes it. My function assumed a pip meant the same thing everywhere.

The fix was to read the symbol's tick value and tick size from the platform each time, and calculate risk from those instead of hardcoding anything. I also round the lot to the broker's volume step and clamp it between the minimum and maximum allowed. Then I print the calculated money at risk to the log for every order so I can see if it matches what I intended.

Test it on a demo with each symbol you plan to use and check one trade manually before trusting it.
It’s Fairman :geek:
Recommended broker for automated trading & scalping IC Markets
Fairman
Posts: 6955
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Lot sizing function gotchas across JPY pairs and gold, the bug that over-risked me

Post by Fairman »

One more gotcha that caught me after I fixed the tick value issue. If your account currency is not the quote currency of the pair, the tick value the platform gives you can change during the day as exchange rates move. For most pairs it is a small difference, but for something like gold on a non dollar account it adds up. I now read the tick value fresh right before every order instead of caching it once when the EA starts.
It’s Fairman :geek:
Fairman
Posts: 6955
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Lot sizing function gotchas across JPY pairs and gold, the bug that over-risked me

Post by Fairman »

Also worth checking what your broker uses as the volume step. Some allow 0.01 lots, some only allow 0.1 on certain symbols, and a few have odd minimums on gold or indices. My function was calculating a sensible size and then the order got rejected because it was not a valid step. Rounding down to the nearest valid volume fixed it. Rounding down rather than to the nearest also means the risk only ever comes out slightly lower, never higher, which is the side I want to be wrong on.
It’s Fairman :geek:
Shadow Trader
Posts: 5594
Joined: Wed Sep 09, 2026 5:19 pm

Re: Lot sizing function gotchas across JPY pairs and gold, the bug that over-risked me

Post by Shadow Trader »

A quick way to see how far off a hardcoded pip value can be is to run one fixed rule across symbols. Say the function risks 1 percent of 10,000, uses a 20 pip stop and assumes 10 dollars per pip per lot, so it always sends 0.5 lots.
img_13524_r1.png
img_13524_r1.png (75.64 KiB) Viewed 163 times
The chart shows what those 0.5 lots really risk with USDCHF at 0.80, GBPUSD at 1.33, USDCAD at 1.38 and USDJPY at 150 (illustration). EURGBP comes out at 133 dollars and USDCHF at 125, while USDCAD and USDJPY land at 72 and 67.

Gold is the worst case if the function treats the broker's point of 0.01 as a pip. A 5 dollar stop becomes 500 pips, the order goes out at 0.02 lots, and on a 100 ounce contract the trade risks 10 dollars instead of 100.

Reading tick value and tick size fresh, as described above, fixes all of these at once. A cheap guard on top is to compute the money at risk after rounding and refuse the order if it is more than 10 percent above target.

Has anyone checked what their platform reports as tick value over a weekend? It may sit on Friday's rate, which could matter for orders queued for the Monday open.
Shadow Trader
Posts: 5594
Joined: Wed Sep 09, 2026 5:19 pm

Re: Lot sizing function gotchas across JPY pairs and gold, the bug that over-risked me

Post by Shadow Trader »

A cross check that avoids tick maths entirely in MT5 is OrderCalcProfit. Ask it for the profit of a 1 lot trade from entry to the stop price, and it returns the money at risk in account currency with the conversion already done, so lots are just target risk divided by that number.

The rounding step has its own trap. Floating point means a lot size meant to be 0.30 can come out as 0.29999999, and flooring that against a 0.01 step gives 0.29, so risk quietly drops by about 3 percent on that trade.

Adding a tiny tolerance before flooring, or normalising to the step's digits first, fixes it. Small, but it shows up as a puzzling gap between printed and intended risk.

Contract size on gold also varies more than people assume. Some brokers use 100 ounces per lot and others 10 or even 1, so an EA that hardcodes the contract size anywhere, even in a sanity check, can be out by a factor of ten when it moves between brokers.

The margin check deserves a line of code as well. If the computed size needs more free margin than the account has, the order fails, and a fallback that silently sends the minimum lot is worse than refusing.
FXS Prop Desk
Posts: 1689
Joined: Wed Oct 07, 2026 9:52 am

Re: Lot sizing function gotchas across JPY pairs and gold, the bug that over-risked me

Post by FXS Prop Desk »

Tick value and tick size catch nearly everyone, and gold is often the worst because different brokers use different contract sizes. A simple test to catch the bug: before running the EA live, print the calculated risk in account currency for a few test stops on each symbol, and compare with a manual calculation.

Another detail is the minimum lot step. If the calculated size is 0.137 and the broker's step is 0.01, the function should round down, not to the nearest value. Rounding up slightly increases the risk on every trade.
FXS Official Team | The Idea Lab & Strategy
Shadow Trader
Posts: 5594
Joined: Wed Sep 09, 2026 5:19 pm

Re: Lot sizing function gotchas across JPY pairs and gold, the bug that over-risked me

Post by Shadow Trader »

Mix-ups here cause real sizing mistakes.

- On most pairs a pip is the fourth decimal, 0.0001. On JPY pairs it's the second, 0.01.
- Many brokers quote a fifth decimal (third on JPY pairs), called a pipette or fractional pip. Ten pipettes make one pip.
- Some platforms call the smallest price step a point, and on a 5-digit feed one point is a pipette. On gold the definition varies by broker.
- Pip value depends on the quote currency. On EURUSD a standard lot is about 10 dollars per pip. On USDJPY the pip is paid in yen, so the dollar value shifts with the exchange rate.
- A 1.2 pip spread on a 5-digit feed might show up as 12 points.

Read stop distance in pips, not points, or a 10-point stop is only 1 pip while the trader thinks it's 10. It sounds silly until it happens at 3 a.m.

How does your platform label them, and have you ever sized a trade wrong because of the digit count?
Post Reply