Skip to content

mlvl36667/amm_arbitrage

Repository files navigation

Numerical solver to obtain the stationary density of the mispricing process

This repository provides C++ code, a Bash runner script, and a Python analysis script for numerically solving aspects arbitrage opportunities in on-chain AMMs on Ethereum 2.0.

The C++ code (integrator.cpp) iteratively calculates a probability density function according to the model. The Bash script (run_integrator.sh) automates the compilation and execution of these simulations over a range of parameters. The Python script (extract_trade_region.py) then parses specific output files to summarise key metrics.

Repository Contents

  • README.md: This instruction file.
  • integrator.cpp: The core C++ simulation code that implements the iterative numerical solution.
  • iteration_solver: The compiled executable of integrator.cpp (this is generated by run_integrator.sh).
  • extract_trade_region.py: A Python script designed to parse tails_ratio_*.txt files and display a formatted summary of the "tails ratio."
  • run_integrator.sh: A Bash script that automates the compilation of integrator.cpp and manages running simulations across a defined parameter space.

Prerequisites

  • C++ Compiler: A C++ compiler with C++20 support (e.g., g++).
  • OpenMP: Required for parallel execution features within the integrator.cpp code.
  • Boost Libraries: The Boost Math library is used, specifically for gauss_kronrod quadrature. Development headers are necessary (e.g., libboost-math-dev on Debian/Ubuntu systems or equivalent for other systems like macOS with Homebrew).
  • Bash: For executing the run_integrator.sh script.
  • bc command-line utility: Used by the run_integrator.sh script for arbitrary precision arithmetic when setting parameters.
  • Python 3: For running the extract_trade_region.py analysis script.
  • Standard Linux/macOS command-line utilities (e.g., rm, mkdir, mv, tr, sed, find, ls).

Usage Instructions

1. Setup and Compilation

The run_integrator.sh script handles the compilation of the C++ code. The number of OpenMP threads can be set using OMP_THREADS in run_integrator.sh.

2. Running Simulations

To execute the simulation suite, run the Bash script:

bash ./run_integrator.sh

This script will perform the following actions:

  • Clean up output files and directories from previous runs (be cautious as it uses rm -rf).
  • Compile integrator.cpp using g++ (with OpenMP and Boost flags) to create the iteration_solver executable.
  • Iterate through a predefined set of parameters (Sigma, Gamma, Q, P values, which are defined within the script) to run multiple instances of the iteration_solver.
  • Environment variables are set for each run to pass parameters to the C++ programme.
  • Store detailed logs from each simulation run in the simulation_results/ directory.
  • Store full CSV outputs (containing the iterated functions f0, f1, ...) from each run in the simulation_results/ directory.
  • Collect summary tails_ratio_.txt and extra_calc_.txt files (generated by iteration_solver) into the tails_ratios/ directory.

Parameters such as OMP_THREADS, FIXED_SIGMA, GAMMA_BP_VALUES, Q_VALUES, and P_EXPRESSIONS can be modified directly within the run_integrator.sh script to explore different scenarios.

3. Analysing Results

After the run_integrator.sh script has completed its runs, the tails_ratios/ directory will contain the summary output files. The extract_trade_region.py script can then be used to parse these files. It prints a formatted summary to the console.

Run the Python analysis script using:

python3 extract_trade_region.py

Output Description

The outputs of the execution are as follows:

  • iteration_solver: The compiled C++ executable programme.
  • simulation_results/ Directory: log_*.txt: Contains the standard output and error streams (log messages, warnings, etc.) from each individual run of iteration_solver. The filename reflects the parameters used.
  • f_out_*.csv: A CSV file for each parameter combination, containing the x-grid values and the corresponding values of the iterated density functions (f0, f1, ..., fN).
  • tails_ratios/ Directory: tails_ratio_*.txt: Text files, each containing a single floating-point number representing the calculated "tails ratio percentage" for a specific parameter set, as output by iteration_solver.
  • extra_calc_*.txt: Text files containing additional calculated terms (like c1, c2) for specific parameter sets, as output by iteration_solver.
  • Console Output (from extract_trade_region.py): A table printed to the standard output, summarising Gamma (in basis points), the raw P value string, 1/P (formatted as a time duration), and the Tails Ratio (%) for the filtered set of results.

Important Notes

  • iteration_solver reads several key parameters (Q_FOR_H, P_PROBABILITY, GAMMA_PLUS, GAMMA_MINUS, CONST_SIGMA, CONST_MU) from environment variables. The run_integrator.sh script is responsible for setting these for each simulation run.
  • The run_integrator.sh script calculates MU based on SIGMA using MU = SIGMA^2 / 2.
  • The tails_ratios/ directory is created by run_integrator.sh to store the .txt files generated by iteration_solver.

Generate plots

python3 generate_plots.py

Compare models

You need a november_rates_1sec.csv file with the exchange rates. Then the following script will fit a GBM model with jumps. Turn off the jumps with --no-q.

bash real_world_integrator.sh
bash real_world_integrator.sh --no-q
python3  compare_models.py

Observe convergence

The convergence of the stationary density can be observed using the following two commands:

bash density_iteration.sh
python3 plot_density_iteration.py

Get arbitrage profits

You have to run the integrator with bash run_integrator.sh to first get the iterated densities, then you can run the following script to extract arbitrage profits. You have to set NIT and NPT accordingly so that get_arb_values.py finds the necessary files for its calculations.

python3 get_arb_values.py

About

This repository contains the necessary codes to calculate arbitrage opportunities in on-chain AMMs

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors