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.
README.md: This instruction file.integrator.cpp: The core C++ simulation code that implements the iterative numerical solution.iteration_solver: The compiled executable ofintegrator.cpp(this is generated byrun_integrator.sh).extract_trade_region.py: A Python script designed to parsetails_ratio_*.txtfiles and display a formatted summary of the "tails ratio."run_integrator.sh: A Bash script that automates the compilation ofintegrator.cppand manages running simulations across a defined parameter space.
- C++ Compiler: A C++ compiler with C++20 support (e.g., g++).
- OpenMP: Required for parallel execution features within the
integrator.cppcode. - Boost Libraries: The Boost Math library is used, specifically for
gauss_kronrodquadrature. Development headers are necessary (e.g.,libboost-math-devon Debian/Ubuntu systems or equivalent for other systems like macOS with Homebrew). - Bash: For executing the
run_integrator.shscript. bccommand-line utility: Used by therun_integrator.shscript for arbitrary precision arithmetic when setting parameters.- Python 3: For running the
extract_trade_region.pyanalysis script. - Standard Linux/macOS command-line utilities (e.g.,
rm,mkdir,mv,tr,sed,find,ls).
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.
To execute the simulation suite, run the Bash script:
bash ./run_integrator.shThis 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.
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.pyThe 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.
- 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.shscript is responsible for setting these for each simulation run. - The
run_integrator.shscript 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.
python3 generate_plots.pyYou 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.shbash real_world_integrator.sh --no-qpython3 compare_models.pyThe convergence of the stationary density can be observed using the following two commands:
bash density_iteration.shpython3 plot_density_iteration.pyYou 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