galacticcouncil / HydraDX-simulations

Apache License 2.0
5 stars 10 forks source link

Omnipool optimal arbitrage size calculation #31

Closed inventandchill closed 2 years ago

inventandchill commented 2 years ago

https://github.com/galacticcouncil/HydraDX-simulations/blob/main/hydradx_update/derivations/Optimal_arbitrage.ipynb

inventandchill commented 2 years ago

Please check math behind omnipool optimal arbitrage size calculation

poliwop commented 2 years ago

@inventandchill you can type latex directly into python notebooks by switching a cell from "code" to "markdown", then wrapping math equations in "$" like $/phi_i^j$ for in-line equations or $$ \phi_i^j $$ for equation blocks

poliwop commented 2 years ago

I've reviewed the math, and everything looks good except I think for the fee calculation... we have two fees in the spec, an asset fee and a protocol fee, and I do not believe your equation handles them correctly. But everything else looks good.

I made a pull request for your review, once you confirm that the equations I put directly in the notebook match the equations in you screenshots, I will remove the screenshots and then push the update to main. I'd prefer that we put equations directly into python notebooks rather than in images, athough it's great that you're getting utility out of SMath.

Once we've gotten that done we can look at the fee formula.

inventandchill commented 2 years ago

@poliwop created new file with all changes and removed pictures https://github.com/galacticcouncil/HydraDX-simulations/blob/cg-optimal-arbitrage-math/hydradx_update/derivations/Optimal_arbitrage_v2.ipynb

inventandchill commented 2 years ago

@poliwop Math derivation and Smath model updated with adjustment for protocol and asset fee https://github.com/galacticcouncil/HydraDX-simulations/blob/cg-optimal-arbitrage-math/hydradx_update/derivations/Optimal_arbitrage.ipynb

poliwop commented 2 years ago

The formula for \Delta Q_i with fee does not look correct to me. If the trade is asset i in for asset j out, then the fees don't affect \Delta Q_i. If the trade is asset j in for asset i out, then \Delta Q_i should be off from the no-fee version by a factor of (1 - f_P). This formula doesn't match either of those...

poliwop commented 2 years ago

Ah I think I see a complication, when accounting for fees the spot prices change a bit and in particular do not match if you consider spot prices for a trade in each direction of an asset pair.

For example the spot price of trading asset i in for j out (spot price of asset i denominated in asset j) is Qi/Qj Rj/Ri (1 - f_A) (1 - f_P) while the spot price of trading asset j in for i out (also asset i denominated in asset j) is Qi/Qj Rj/Ri / [(1 - f_A) * (1 - f_P)]

So the analysis with fees may be a bit more complex

inventandchill commented 2 years ago

Just reading reply but cant undertand what is missed here. Could you describe it pls?

As i undertand we swap from delta_Ri to delta_Qi. delta_Qi x f_p go to treasury. Rest is go for swapping delta_Qj to delta_Rj. delta_Rj x f_a is go to protocol owned liqudity. Our goal is to make dollar price of Qi is equal to dollar price of Qj.

And everything is ok, checked it with numbers. In our case arbitrage agent add asset i and receive asset j from pool

poliwop commented 2 years ago

With fees, there is no longer just one spot price for dollar price of Q_i. There's a bid/ask spread. I think you would need to determine which direction the arbitrager is trading, and then use the appropriate spot price for the analysis?

inventandchill commented 2 years ago

Yes, looks like i missed it in description. Formula is ok when (Price of Qi in $ < Price of Qj in $). In opposit case it is also ok, but we need to assume that i is j and j is i