Skip to content

BINDI231/Currency

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Currency Arbitrage Detector

A professional-grade detection system for finding profitable currency exchange cycles using the Floyd-Warshall Algorithm with logarithmic transformation.

๐ŸŽฏ Project Overview

This project detects currency arbitrage opportunitiesโ€”cycles where you can exchange currencies and end up with more money than you started with. It uses pure algorithm implementation (no external packages) combined with a Django web interface.

Key Features

  • Pure Floyd-Warshall Implementation: O(nยณ) algorithm for finding shortest paths in currency graphs
  • Mathematical Elegance: Converts exchange rates to negative logarithms to detect negative cycles
  • Professional Django Setup: Full Django project with models, admin panel, and API endpoints
  • Real-time Dashboard: Beautiful UI showing detected arbitrage opportunities
  • Historical Data Support: Store and analyze detection results
  • API Endpoints: JSON endpoints for programmatic access

๐Ÿงฎ Algorithm Details

Mathematical Principle

A profitable arbitrage cycle satisfies: $$r_1 \times r_2 \times \cdots \times r_n > 1$$

Taking logarithm: $$\log(r_1) + \log(r_2) + \cdots + \log(r_n) > 0$$

By negating the weights: $$-\log(r_1) - \log(r_2) - \cdots - \log(r_n) < 0$$

This transforms the problem into finding negative cycles using the Floyd-Warshall algorithm.

Complexity

  • Time: O(nยณ) where n = number of currencies
  • Space: O(nยฒ) for distance and next-node matrices

Algorithm Steps

  1. Convert Rates to Weights: weight[iโ†’j] = -log(exchange_rate[iโ†’j])
  2. Build Matrices: Initialize distance and reconstruction matrices
  3. Run Floyd-Warshall: Find shortest paths between all currency pairs
  4. Detect Cycles: Check if dist[i][i] < 0 for any currency i
  5. Reconstruct Path: Use backtracking to find the actual cycle sequence
  6. Calculate Profit: Compute the percentage profit for the cycle

๐Ÿ“ Project Structure

currency-arbitrage-detector/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ manage.py
โ”‚   โ”œโ”€โ”€ currency_arbitrage/          # Django settings
โ”‚   โ”‚   โ”œโ”€โ”€ settings.py
โ”‚   โ”‚   โ”œโ”€โ”€ urls.py
โ”‚   โ”‚   โ”œโ”€โ”€ wsgi.py
โ”‚   โ”‚   โ””โ”€โ”€ asgi.py
โ”‚   โ”œโ”€โ”€ detector/                     # Main app
โ”‚   โ”‚   โ”œโ”€โ”€ models.py                # Database models
โ”‚   โ”‚   โ”œโ”€โ”€ views.py                 # Web views and API
โ”‚   โ”‚   โ”œโ”€โ”€ services.py              # Floyd-Warshall algorithm
โ”‚   โ”‚   โ”œโ”€โ”€ urls.py
โ”‚   โ”‚   โ”œโ”€โ”€ admin.py
โ”‚   โ”‚   โ””โ”€โ”€ templates/detector/
โ”‚   โ”‚       โ””โ”€โ”€ index.html           # Dashboard template
โ”‚   โ”œโ”€โ”€ static/
โ”‚   โ”‚   โ”œโ”€โ”€ css/style.css            # Professional styling
โ”‚   โ”‚   โ””โ”€โ”€ js/
โ”‚   โ””โ”€โ”€ templates/                    # Base templates
โ”œโ”€โ”€ data/
โ”‚   โ””โ”€โ”€ exchange_rates.csv           # Exchange rate data
โ””โ”€โ”€ requirements.txt

๐Ÿš€ Installation & Running

1. Install Dependencies

cd src
pip install -r ../requirements.txt

2. Set Up Database

python manage.py migrate
python manage.py createsuperuser  # Create admin account

3. Run Server

python manage.py runserver

Visit: http://localhost:8000/

4. Admin Panel

Access: http://localhost:8000/admin/

๐Ÿ“Š API Endpoints

Get Latest Detection

GET /api/latest/

Response:

{
  "has_arbitrage": true,
  "cycle": ["USD", "EUR", "INR", "USD"],
  "profit_percent": 2.45,
  "cycle_length": 4,
  "calculation_time_ms": 1.23,
  "detected_at": "2026-02-03T10:30:00Z"
}

Get Detection History

GET /api/history/

Get Exchange Rates

GET /api/rates/

๐ŸŒ Free Live Data (No API Key)

You can fetch free live FX rates and update the CSV using a builtโ€‘in management command. This keeps the algorithm from scratch while allowing live data ingestion.

cd src
python manage.py fetch_live_rates --base USD --symbols EUR,GBP,JPY,INR

What this does:

  • Calls a free public FX endpoint
  • Builds full crossโ€‘rates from the base rates
  • Updates data/exchange_rates.csv

Note: When all crossโ€‘rates are derived from a single base, realโ€‘world arbitrage is usually not present. The algorithm still runs and reports โ€œno arbitrageโ€ if none exists.

โฑ๏ธ Live Simulation from Historical Data

Simulate a โ€œlive feedโ€ using free historical timeโ€‘series data:

cd src
python manage.py simulate_live --start 2025-12-01 --end 2025-12-07 --base USD --symbols EUR,GBP,JPY,INR --write-csv --sleep 1

This runs the algorithm for each day in the range and (optionally) updates the CSV so the dashboard reflects each step.

๐Ÿ“ CSV Data Format

Add exchange rates to data/exchange_rates.csv:

base,quote,rate
USD,EUR,0.90
EUR,GBP,1.18
GBP,USD,1.27
USD,JPY,148.50
JPY,INR,0.55
INR,USD,0.013

๐Ÿ” Database Models

ExchangeRate

  • base_currency: Currency code (e.g., USD)
  • quote_currency: Currency code (e.g., EUR)
  • rate: Exchange rate value
  • timestamp: When the rate was recorded

ArbitrageDetection

  • has_arbitrage: Boolean flag
  • cycle: JSON list of currencies in cycle
  • profit_percent: Calculated profit percentage
  • cycle_length: Number of currencies in cycle
  • calculation_time_ms: Algorithm execution time

๐Ÿ’ก Example Usage

Python API Usage

from pathlib import Path
from detector.services import detect_arbitrage

csv_path = Path("data/exchange_rates.csv")
result, edges = detect_arbitrage(csv_path)

if result.has_arbitrage:
    print(f"Cycle: {' โ†’ '.join(result.cycle)}")
    print(f"Profit: {result.profit_percent:.2f}%")
    print(f"Time: {result.calculation_time_ms:.2f}ms")

๐ŸŽ“ Educational Value

This project is perfect for demonstrating:

  • Graph algorithms in Python
  • Dynamic programming concepts
  • Mathematical problem solving
  • Web application development with Django
  • Algorithm optimization and complexity analysis
  • Professional code documentation

๐Ÿ“‹ Features Summary

Feature Status
Floyd-Warshall Algorithm โœ… Implemented
Cycle Detection โœ… Implemented
Profit Calculation โœ… Implemented
Django Models โœ… Implemented
Admin Panel โœ… Implemented
REST API โœ… Implemented
Professional UI โœ… Implemented
Historical Data โœ… Implemented
Dark Mode Theme โœ… Implemented
Responsive Design โœ… Implemented

๐Ÿ›  Technical Stack

  • Backend: Django 5.0+
  • Algorithm: Pure Python (no external packages)
  • Frontend: HTML5, CSS3, JavaScript
  • Database: SQLite (default)
  • Server: Django development server / Gunicorn

โš ๏ธ Important Notes

  1. Real-World Limitations: This detector uses the latest rates provided. In production, you'd need real-time market data and account for:

    • Trading fees and commissions
    • Bid-ask spreads
    • Time delays in execution
    • Currency conversion minimums
  2. Numerical Precision: The algorithm uses float precision tolerance (1e-9) for stability

  3. Graph Size: Works efficiently for up to ~100 currencies; optimize for larger datasets

๐Ÿ“œ License

Educational project for DAA subject

๐Ÿ‘จโ€๐Ÿ’ป Author

Created as a comprehensive demonstration of algorithm implementation and web development


Note: This is an educational project. Always verify exchange rates with real-time market data before any actual trading.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages