FX ARBITRAGE

A loop of rates that multiplies past 1 is a negative cycle

L216 MINALGORITHMS
PREDICT

The pricing feed's GBP/NGN mid is stuck at ₦1,968 while USD/NGN (₦1,531.2516) and GBP/USD (1.27) keep updating; their cross puts GBP/NGN at ₦1,944.69. Owoleaf takes a 20 bp spread off every leg and checks every pair: rate(X→Y) × rate(Y→X) must be at most 1. What does the check report?

An exchange rate is an edge: one unit of X buys rate(X→Y) units of Y. A loop of conversions multiplies its rates. If the product is above 1, money appears from nowhere. A real market closes that gap in moments; a quote that stays open is a bug in your prices.

Take −ln(rate) as each edge’s weight. The product above 1 becomes a weight sum below 0, so arbitrage is exactly a negative cycle. Bellman-Ford relaxes every edge V−1 times; if an edge still relaxes after that, a negative cycle exists, and the predecessor pointers lead you onto it.

▶ SIMULATION
QUOTES (spread 50 bp)
NGN→USD 0.00064980
NGN→GBP 0.00051157
NGN→EUR 0.00060166
USD→NGN 1523.60
USD→GBP 0.78346
GBP→NGN 1935.28
GBP→USD 1.26
EUR→NGN 1645.48
EUR legs not shown; all 12 edges are relaxed
BELLMAN-FORD
not run
CYCLE
—
LIVE
not published
dist (sum of −ln rate), pred
NGN: — · pred —
USD: — · pred —
GBP: — · pred —
EUR: — · pred —
pair check—
last stepnothing has run yet
✓ no loop over the published quotes returns more than it started with
SCHEDULE
01[FEED]FEED: GBP/NGN mid stuck at ₦1,968 (fair cross ₦1,944.69); spread 20 bp per leg
02[CHECK]CHECK: every pair: rate(X→Y) × rate(Y→X) ≤ 1?
03[FEED]FEED: publish the quotes to the app
04[CUSTOMER]CUSTOMER: Oyin converts ₦1,000,000 around the best loop the app quotes
05[FEED]FEED: report
Start of pair check only scenario. 5 steps.
// Pair check: catches a two-leg round trip only.
for (x, y) in pairs:
  if rate[x][y] * rate[y][x] > 1: withhold(x, y)
publish(rates)
// A stale GBP/NGN mid keeps GBP→NGN and NGN→GBP consistent with
// each other, so every pair passes. The free money is a three-leg loop.

Step through FIX: BELLMAN-FORD and watch the dist column. Rates above 1 have negative weights, so the first round already lowers NGN, USD and EUR. Each later round lowers NGN by another 0.0059, the weight of one lap of the cycle, and after three rounds NGN→USD still relaxes. The trace names NGN → USD → GBP → NGN at 1.00593 a loop, GBP/NGN is withheld and the feed owner is paged. PAIR CHECK ONLY publishes the same feed, and Oyin turns ₦1,000,000 into ₦1,005,926.95 in one loop. FRESH FEED finds no cycle: the best loop loses the spread.

BREAK IT NOT YET

This feed has only the pair check. Move the stale GBP/NGN mid away from the fair cross of ₦1,944.69, and move the spread, until the app publishes quotes that let a customer loop money for a profit. Watch the pair check row: it never objects.

GOAL: the pair check passes and a published loop returns more than ₦1,000,000

The DIJKSTRA lesson used the same V-th pass to refuse a graph with a negative cycle. Here the cycle is the answer you want, so you keep the predecessors and read it off. Two details matter. Every currency starts at distance 0, which is a virtual source with a zero edge to each; a run from NGN alone misses a loop between two currencies NGN cannot reach. And compare with a small tolerance, because −ln of rates that agree to the last digit still sums to a few units of float noise, not to exactly 0.

FIX IT NOT YET

Rewrite findArbitrage(rates) to return a loop whose rates multiply to more than 1, as a list of currencies that starts and ends with the same one (['NGN', 'USD', 'GBP', 'NGN']), or null when no loop pays. rates[x][y] is units of y for one x; a missing entry means no quote. The grader tries the lesson feed, a fresh feed, a four-leg loop, and a loop among currencies NGN cannot reach.

Where you run the check matters as much as how. Run it on the rates you will quote, spread and fees included, because those are the rates a customer can loop. A cycle on the feed’s mid rates that the spread happens to cover is still a stale price: alert on it, even if no customer can profit yet. When a loop does pay, every conversion in it is a real pair of ledger entries, so the loss is already in the books, as PAYMENT LEDGER would record it. Stopping the quote is the only fix that does not involve clawing money back.

Mental model:

A loop of rates is a loop of edges, and −ln turns “multiplies past 1” into “sums below 0”. Bellman-Ford’s extra pass finds the negative cycle, and the predecessors name it. In a pricing system that cycle is never an opportunity: it is a feed defect, a stale or crossed rate, and the check belongs in front of the publish step, on the rates customers actually get.

IN PRODUCTION

FX quoting in wallets and remittance apps, multi-currency cards, treasury desks that build cross rates from a few base pairs, crypto exchanges with many pairs, and any pricing feed where one stale source can sit next to fresh ones.

IN INTERVIEWS

“Given a table of exchange rates, is there an arbitrage?” Map rates to edges with weight −ln(rate), run Bellman-Ford from a virtual source, use the V-th pass to detect a negative cycle, and walk predecessors to print it: O(V·E). Then say what it means in production: a stale or crossed quote, checked on the spread-adjusted rates before publishing, with the pair withheld and the feed owner alerted.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.