Arbitrage
Background
Arbitrage is taking advantage of the price difference of the same asset in two different markets (pools in our example). The arbitrage agent calculates the difference in price for an asset and buys from the pool where it's valued less and sells at a pool where it's valued more.
Arbitrage ensures price efficiency across markets, as traders continuously buy and sell to capitalize on price discrepancies.
There are several forms of arbitrage — including pure arbitrage, statistical arbitrage, and triangular arbitrage — each varying in complexity and methodology. The key to successful arbitrage is speed, as price differences are often minimal and can disappear quickly due to market adjustments.
Arbitrage Strategy
If the last trade was too recent or the earning amount is less than the minimum amount (not enough profit to cover the trading fees), the agent doesn't execute any trades. Otherwise, the agent executes a trade, buying the token from the pool where it's valued less and selling the token in the pool where it's valued more.
How To Run
Installation
Follow our Getting Started guide to install the dojo library and other required tools.
Then clone the dojo_examples
repository and go into the relevant directory.
Running
Download the dashboard to view the simulation results.
To view example simulation data, download arbitrage.db
from here and click 'Add A Simulation' on the dashboard.
To run the simulation yourself, use the following command.
This command will setup your local blockchain, contracts, accounts and agents. You can then access your results on your Dojo dashboard by connecting to a running simulation.
Step-By-Step Explanation
Initialization
We create a new class called ArbitragePolicy
which inherits from the BasePolicy
class. We set additional variables such as block_last_trade
and min_block_dist
which ensure that trades are not too recent as this would cause a higher price impact.
Signal Calculation
This method identifies potential arbitrage opportunities between two pools by first verifying that both pools trade the same tokens. It then retrieves the token prices from each pool and calculates the price ratio between them. To ensure an accurate assessment of profitability, the method adjusts the price ratio by factoring in the transaction fees of both pools, resulting in the net arbitrage signals.
Trade Execution
Afterwards, we return a UniswapV3Trade
object containing our order to buy/sell, specifying the pool, the quantity and the agent. If the last trade was too recent or no profit would be made, we return an empty list meaning no trade is being made.
In the run.py
file, we create two pools, a Uniswap environment and an agent to implement the arbitrage strategy.
Results
You can download the results to this example below.
We offer a dashboard desktop application for visualizing your simulation results. You can download the file for the desktop application here, or just open the results in our hosted dashboard.