Program to Find Out Currency Arbitrage in Python

Currency arbitrage is a strategy where we exploit differences in exchange rates to make a profit. Given an N x N table of currency exchange rates, we need to check whether there's a sequence of trades that can turn some amount A of any currency into an amount greater than A of the same currency.

1 CAD 0.65 EUR 0.7865 USD 1.00672 CAD × 0.65 × 1.21 × 1.28

The value at entry [i, j] represents the amount of currency j we can buy with one unit of currency i. In the example above with USD (0), CAD (1), and EUR (2), we can create an arbitrage opportunity ?

Understanding the Problem

The currency exchange matrix looks like this ?

USD CAD EUR
USD 1 1.28 0.82
CAD 0.78 1 0.65
EUR 1.21 1.55 1

Algorithm Approach

We use the Floyd-Warshall algorithm with logarithmic transformation. Since multiplication becomes addition in log space, we can detect negative cycles (arbitrage opportunities) by checking if any diagonal element becomes negative after applying the algorithm ?

Steps

  • Transform exchange rates using negative logarithm: -log(rate)

  • Apply Floyd-Warshall to find shortest paths

  • Check if any diagonal element is negative (indicates arbitrage)

Implementation

import math

class CurrencyArbitrage:
    def detect_arbitrage(self, exchange_matrix):
        # Create a copy to avoid modifying original matrix
        matrix = [row[:] for row in exchange_matrix]
        
        # Convert exchange rates to negative log values
        for i in range(len(matrix)):
            for j in range(len(matrix[0])):
                matrix[i][j] = -math.log(matrix[i][j], 2)
        
        n = len(matrix)
        
        # Apply Floyd-Warshall algorithm
        for k in range(n):
            for i in range(n):
                for j in range(n):
                    matrix[i][j] = min(matrix[i][j], matrix[i][k] + matrix[k][j])
        
        # Check for negative diagonal elements
        return any(matrix[i][i] < 0 for i in range(n))

# Example usage
arbitrage_detector = CurrencyArbitrage()

exchange_rates = [
    [1, 1.28, 0.82],
    [0.78, 1, 0.65],
    [1.21, 1.55, 1]
]

result = arbitrage_detector.detect_arbitrage(exchange_rates)
print(f"Arbitrage opportunity exists: {result}")

# Let's trace the arbitrage path manually
print("\nTracing arbitrage opportunity:")
print("1 CAD ? 0.65 EUR ? 0.7865 USD ? 1.00672 CAD")
print(f"Starting with 1 CAD, ending with {0.65 * 1.21 * 1.28:.5f} CAD")
print(f"Profit: {0.65 * 1.21 * 1.28 - 1:.5f} CAD")
Arbitrage opportunity exists: True

Tracing arbitrage opportunity:
1 CAD ? 0.65 EUR ? 0.7865 USD ? 1.00672 CAD
Starting with 1 CAD, ending with 1.00672 CAD
Profit: 0.00672 CAD

How the Algorithm Works

The algorithm transforms the problem from finding profitable cycles in exchange rates to finding negative cycles in a graph. Here's why it works ?

import math

# Example: Why negative log transformation works
print("Original rates and their negative log values:")
rates = [1.28, 0.65, 1.21]
for rate in rates:
    log_val = -math.log(rate, 2)
    print(f"Rate: {rate:4.2f} ? -log?({rate:4.2f}) = {log_val:6.3f}")

print(f"\nProduct of rates: {1.28 * 0.65 * 1.21:.5f}")
print(f"Sum of -log values: {sum(-math.log(rate, 2) for rate in rates):.5f}")
print("If product > 1, sum of -log values < 0 (negative cycle)")
Original rates and their negative log values:
Rate: 1.28 ? -log?(1.28) = -0.356
Rate: 0.65 ? -log?(0.65) =  0.620
Rate: 1.21 ? -log?(1.21) = -0.274

Product of rates: 1.00672
Sum of -log values: -0.010
If product > 1, sum of -log values < 0 (negative cycle)

Testing with No Arbitrage Case

# Example with no arbitrage opportunity
no_arbitrage_matrix = [
    [1, 0.5, 2],
    [2, 1, 4],
    [0.5, 0.25, 1]
]

arbitrage_detector = CurrencyArbitrage()
result = arbitrage_detector.detect_arbitrage(no_arbitrage_matrix)
print(f"Arbitrage opportunity exists: {result}")
Arbitrage opportunity exists: False

Conclusion

Currency arbitrage detection uses the Floyd-Warshall algorithm with logarithmic transformation to convert multiplication of exchange rates into addition. This allows us to detect profitable trading cycles by finding negative diagonal elements in the transformed matrix, indicating arbitrage opportunities.

---
Updated on: 2026-03-25T13:39:49+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements