Code Monkey home page Code Monkey logo

llamma-simulator's Introduction

Example of using the simulator to find optimal parameters

Installation

To make simulator work, one needs just Python, NumPy and Matplotlib installed

Downloading data

Use scripts located in data/fetch-*.py to download price data from Binance. For example, to download CRV/USDT price data:

cd data
python3 fetch-crvusd.py
mv crvusdt.json crvusdt-1m.json  # Format we used in scripts, change to any naming you wish
gzip crvusdt-1m.json

All price data files are assumed to be stored gzipped.

Finding optimal fee

Algorithm is pretty insensitive to AMM fees, however optimal value for almost all coins seems to be within 0.6%-0.9% with the exception of EUR (in examples).

Let's look at fee calculation in example_crv.

python3 1_simulate_fee.py

This script calculates losses depending on the AMM fee for a 4-band position with A=50 (you can change that).

This gives this graph:

CRV/USD fee optimization

The graph suggests that optimal fee is at the minimum of the graph and is equal to 0.6%.

Finding optimal A

Amplification (A) depends on asset volatility. Let's put the optimal fee we have found into the script 2_simulate_A.py and run it.

We obtain two graphs - losses and discount (sum of losses and the value of liquidity if price slowly goes down) for N = 4 bands:

CRV/USD A optimization

Value of A for the minimum of orange graph is the A we need to set for the market: A = 50.

Let's take the value of the blue graph at A = 50: it is 0.11 (when rounded up). It is the loss we MAY have in the worst case with N=4 which the position will lose if volatility is really bad.

This means, we set liquidation_discount = 0.11.

It's not great to be immediately liquidated once we get there, so let's add 3% margin to it. This gives: loan_discount = 0.14.

Minimum of the orange graph is 0.135..0.14. Let's add those 3% to it - this is 0.17. It means that: LTV_max = 1 - 0.17 = 0.83 - maximum achievable LTV for CRV.

Therefore, maximum possible leverage for CRV is 1 / (1 - 0.83) = 5.9.

Checking losses for different band numbers

User can adjust position losses by adjusting numbers of bands. Let's calculate losses in SL using recent prices for CRV depending on N using script 4_simulate_avg_loss_brief.py:

CRV/USD SL loss calculation

Graph calculates loss user would experience when borrowing maximum possible amount and waiting for 1 day (on average). For example, on this graph, at N=50, loss is 0.075% per day.

llamma-simulator's People

Contributors

michwill avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.