This project implements a real-time cryptocurrency arbitrage detection and execution system. It monitors multiple cryptocurrency exchanges, identifies profitable arbitrage opportunities, and simulates their execution. The system uses websocket connections to receive live market data and employs graph theory algorithms to detect arbitrage cycles.
- Real-time market data collection via WebSocket
- Arbitrage opportunity detection using the Bellman-Ford algorithm
- Simulated arbitrage execution
- Live dashboard for monitoring arbitrage opportunities and portfolio value
We use a modified version of the Bellman-Ford algorithm to detect negative cycles in a graph representation of exchange rates.
-
Graph Representation: Each cryptocurrency is a node, and exchange rates are weighted edges.
-
Edge Weight Calculation: For an exchange rate
r
from currency A to B, the edge weightw
is: w = -log(r) This transformation allows us to use addition instead of multiplication when calculating arbitrage profits. -
Bellman-Ford Algorithm: We run V-1 iterations (where V is the number of vertices) of the following relaxation step:
for each edge (u, v) with weight w:
if distance[u] + w < distance[v]:
distance[v] = distance[v] + w
If after these iterations we can still relax an edge, we have found a negative cycle, which represents an arbitrage opportunity.
- Profit Calculation: For a cycle
c
of currencies[c₁, c₂, ..., cₙ, c₁]
, the profitp
is:
p = (r₁₂ * r₂₃ * ... * rₙ₁) - 1
where rᵢⱼ
is the exchange rate from currency i
to j
.
The simulation assumes instant execution and no transaction fees. For each arbitrage opportunity:
- Calculate the profit percentage
p
. - Update the simulated balance
B
: B_new = B * (1 + p)
crypto_arbitrage/
│
├── app/
│ ├── init.py
│ ├── main.py
│ ├── config.py
│ │
│ ├── api/
│ │ ├── init.py
│ │ └── binance_api.py
│ │
│ ├── models/
│ │ ├── init.py
│ │ └── exchange_graph.py
│ │
│ ├── services/
│ │ ├── init.py
│ │ ├── arbitrage.py
│ │ └── websocket_handler.py
│ │
│ └── static/
│ ├── css/
│ │ └── styles.css
│ └── js/
│ └── dashboard.js
│
├── templates/
│ └── index.html
│
├── tests/
│ └── ...
│
├── .env
├── .gitignore
├── README.md
└── requirements.txt
- Clone the repository
- Install dependencies:
pip install -r requirements.txt
- Set up your
.env
file with necessary configurations - Run the application:
python app/main.py
Key configuration options in .env
:
INITIAL_BALANCE
: Starting balance for simulationMAX_ARBITRAGE_PROFIT
: Maximum allowed profit for an arbitrage opportunityARBITRAGE_CHECK_INTERVAL
: Time between arbitrage checksBINANCE_API_KEY
andBINANCE_API_SECRET
: Your Binance API credentials
The dashboard displays:
- Current simulated balance
- Portfolio value over time
- Top 5 current arbitrage opportunities