-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added price slippage to impact calculation to sort trades for processing
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from hydradx.model.amm.omnipool_amm import OmnipoolState | ||
|
||
|
||
def calculate_price_slippage_to_impact(omnipool: OmnipoolState, intent: dict) -> float: | ||
tkn_buy, tkn_sell = intent['tkn_buy'], intent['tkn_sell'] | ||
sell_price = omnipool.price(omnipool, tkn_sell, tkn_buy) | ||
buy_liquidity, sell_liquidity = omnipool.liquidity[tkn_buy], omnipool.liquidity[tkn_sell] | ||
buy_lrna, sell_lrna = omnipool.lrna[tkn_buy], omnipool.lrna[tkn_sell] | ||
|
||
if 'sell_quantity' in intent: | ||
if sell_lrna <= buy_lrna: | ||
impact = intent['sell_quantity'] / sell_liquidity | ||
else: | ||
impact = intent['sell_quantity'] * sell_price / buy_liquidity | ||
limit_price = intent['buy_limit'] / intent['sell_quantity'] | ||
else: | ||
if sell_lrna <= buy_lrna: | ||
impact = intent['buy_quantity'] / sell_price / sell_liquidity | ||
else: | ||
impact = intent['buy_quantity'] / buy_liquidity | ||
limit_price = intent['buy_quantity'] / intent['sell_limit'] | ||
slippage = (sell_price - limit_price) / sell_price | ||
|
||
return slippage / impact | ||
|
||
|
||
def _calculate_price_slippage_to_impact_sign_tuple(omnipool: OmnipoolState, intent: dict) -> tuple: | ||
x = calculate_price_slippage_to_impact(omnipool, intent) | ||
return (1, x) if x >= 0 else (-1, -x) | ||
|
||
|
||
def order_initial_intents(intents: list): | ||
# intents = [ | ||
# {'agent': agent_alice, 'buy_quantity': 10000, 'sell_limit': 81000, 'tkn_buy': 'DOT', 'tkn_sell': 'USDT'}, | ||
# {'agent': agent_bob, 'sell_quantity': 10000, 'buy_limit': 75500, 'tkn_buy': 'USDT', 'tkn_sell': 'DOT'} | ||
# ] | ||
|
||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import pytest | ||
from hypothesis import given, strategies as st, settings | ||
from mpmath import mp, mpf | ||
|
||
from hydradx.model.amm.agents import Agent | ||
from hydradx.model.amm.omnipool_amm import OmnipoolState | ||
from hydradx.model.amm.omnix_solver import calculate_price_slippage_to_impact | ||
|
||
|
||
def test_calculate_price_slippage_to_impact(): | ||
omnipool = OmnipoolState( | ||
tokens={ | ||
"DOT": {'liquidity': 10000000 / 7.5, 'LRNA': 10000000}, | ||
"USDT": {'liquidity': 10000000, 'LRNA': 10000000}, | ||
"HDX": {'liquidity': 100000000, 'LRNA': 1000000} | ||
}, | ||
preferred_stablecoin='USDT', | ||
asset_fee=0.0, | ||
lrna_fee=0.0 | ||
) | ||
|
||
intent = {'agent': Agent(), 'buy_quantity': 10000, 'sell_limit': 81000, 'tkn_buy': 'DOT', 'tkn_sell': 'USDT'} | ||
ratio = calculate_price_slippage_to_impact(omnipool, intent) | ||
if ratio != pytest.approx(9.87654321): | ||
raise | ||
|
||
intent = {'agent': Agent(), 'buy_quantity': 10000, 'sell_limit': 74000, 'tkn_buy': 'DOT', 'tkn_sell': 'USDT'} | ||
ratio = calculate_price_slippage_to_impact(omnipool, intent) | ||
if ratio != pytest.approx(-1.801801802): | ||
raise | ||
|
||
intent = {'agent': Agent(), 'sell_quantity': 10000, 'buy_limit': 74000, 'tkn_buy': 'USDT', 'tkn_sell': 'DOT'} | ||
ratio = calculate_price_slippage_to_impact(omnipool, intent) | ||
if ratio != pytest.approx(1.777777777777): | ||
raise | ||
|
||
intent = {'agent': Agent(), 'sell_quantity': 10000, 'buy_limit': 81000, 'tkn_buy': 'USDT', 'tkn_sell': 'DOT'} | ||
ratio = calculate_price_slippage_to_impact(omnipool, intent) | ||
if ratio != pytest.approx(-10.6666666666666): | ||
raise | ||
|
||
# doubling liquidity of DOT should not change any results | ||
omnipool = OmnipoolState( | ||
tokens={ | ||
"DOT": {'liquidity': 2 * 10000000 / 7.5, 'LRNA': 2 * 10000000}, | ||
"USDT": {'liquidity': 10000000, 'LRNA': 10000000}, | ||
"HDX": {'liquidity': 100000000, 'LRNA': 1000000} | ||
}, | ||
preferred_stablecoin='USDT', | ||
asset_fee=0.0, | ||
lrna_fee=0.0 | ||
) | ||
|
||
intent = {'agent': Agent(), 'buy_quantity': 10000, 'sell_limit': 81000, 'tkn_buy': 'DOT', 'tkn_sell': 'USDT'} | ||
ratio = calculate_price_slippage_to_impact(omnipool, intent) | ||
if ratio != pytest.approx(9.87654321): | ||
raise | ||
|
||
intent = {'agent': Agent(), 'buy_quantity': 10000, 'sell_limit': 74000, 'tkn_buy': 'DOT', 'tkn_sell': 'USDT'} | ||
ratio = calculate_price_slippage_to_impact(omnipool, intent) | ||
if ratio != pytest.approx(-1.801801802): | ||
raise | ||
|
||
intent = {'agent': Agent(), 'sell_quantity': 10000, 'buy_limit': 74000, 'tkn_buy': 'USDT', 'tkn_sell': 'DOT'} | ||
ratio = calculate_price_slippage_to_impact(omnipool, intent) | ||
if ratio != pytest.approx(1.777777777777): | ||
raise | ||
|
||
intent = {'agent': Agent(), 'sell_quantity': 10000, 'buy_limit': 81000, 'tkn_buy': 'USDT', 'tkn_sell': 'DOT'} | ||
ratio = calculate_price_slippage_to_impact(omnipool, intent) | ||
if ratio != pytest.approx(-10.6666666666666): | ||
raise |