Skip to content

Commit

Permalink
Fixed confusion in CentralizedMarket about bids/asks
Browse files Browse the repository at this point in the history
  • Loading branch information
poliwop committed Oct 18, 2023
1 parent f545a49 commit 2c5db06
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
44 changes: 22 additions & 22 deletions hydradx/model/amm/centralized_market.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,32 @@ def swap(
sell_tkns_remaining = sell_quantity
tkns_bought = 0

if tkn_sell == quote:
if tkn_sell == base:
for bid in self.order_book[(base, quote)].bids:
if sell_tkns_remaining <= 0:
break
if bid[0] * bid[1] >= sell_tkns_remaining:
if bid[1] >= sell_tkns_remaining:
# this bid can fill the entire remaining order
tkns_bought += sell_tkns_remaining / bid[0]
bid[1] -= sell_tkns_remaining / bid[0]
tkns_bought += bid[0] * sell_tkns_remaining
bid[1] -= sell_tkns_remaining
sell_tkns_remaining = 0
else:
# this bid can partially fill the order
tkns_bought += bid[1]
sell_tkns_remaining -= bid[0] * bid[1]
tkns_bought += bid[1] * bid[0]
sell_tkns_remaining -= bid[1]
bid[1] = 0

else:
for ask in self.order_book[(base, quote)].asks:
if sell_tkns_remaining <= 0:
break
if ask[1] >= sell_tkns_remaining:
tkns_bought += ask[0] * sell_tkns_remaining
ask[1] -= sell_tkns_remaining
if ask[0] * ask[1] >= sell_tkns_remaining:
tkns_bought += sell_tkns_remaining / ask[0]
ask[1] -= sell_tkns_remaining / ask[0]
sell_tkns_remaining = 0
else:
tkns_bought += ask[0] * ask[1]
sell_tkns_remaining -= ask[1]
tkns_bought += ask[1]
sell_tkns_remaining -= ask[1] * ask[0]
ask[1] = 0

agent.holdings[tkn_sell] -= sell_quantity - sell_tkns_remaining
Expand All @@ -111,30 +111,30 @@ def swap(
buy_tkns_remaining = buy_quantity
tkns_sold = 0

if tkn_buy == quote:
if tkn_buy == base:
for ask in self.order_book[(base, quote)].asks:
if buy_tkns_remaining <= 0:
break
if ask[0] * ask[1] >= buy_tkns_remaining:
tkns_sold += buy_tkns_remaining / ask[0]
ask[1] -= buy_tkns_remaining / ask[0]
if ask[1] >= buy_tkns_remaining:
tkns_sold += buy_tkns_remaining * ask[0]
ask[1] -= buy_tkns_remaining
buy_tkns_remaining = 0
else:
tkns_sold += ask[1]
buy_tkns_remaining -= ask[0] * ask[1]
tkns_sold += ask[0] * ask[1]
buy_tkns_remaining -= ask[1]
ask[1] = 0

else:
for bid in self.order_book[(base, quote)].bids:
if buy_tkns_remaining <= 0:
break
if bid[1] >= buy_tkns_remaining:
tkns_sold += bid[0] * buy_tkns_remaining
bid[1] -= buy_tkns_remaining
if bid[0] * bid[1] >= buy_tkns_remaining:
tkns_sold += buy_tkns_remaining / bid[0]
bid[1] -= buy_tkns_remaining / bid[0]
buy_tkns_remaining = 0
else:
tkns_sold += bid[0] * bid[1]
buy_tkns_remaining -= bid[1]
tkns_sold += bid[1]
buy_tkns_remaining -= bid[0] * bid[1]
bid[1] = 0

agent.holdings[tkn_buy] += buy_quantity - buy_tkns_remaining
Expand Down
32 changes: 16 additions & 16 deletions hydradx/tests/test_centralized_market.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ def test_sell_quote(quantity: float, trade_fee: float, order_book: OrderBook):
)

value_bought = sum(
[quantity * price for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].bids]
[quantity * price for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].asks]
) - sum(
[quantity * price for (price, quantity) in sell_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].bids]
[quantity * price for (price, quantity) in sell_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].asks]
)

quantity_sold = initial_state.agents['agent'].holdings[tkn_sell] - sell_state.agents['agent'].holdings[tkn_sell]
Expand All @@ -65,9 +65,9 @@ def test_sell_quote(quantity: float, trade_fee: float, order_book: OrderBook):
quantity_bought = sell_state.agents['agent'].holdings[tkn_buy] - initial_state.agents['agent'].holdings[tkn_buy]

value_sold = sum(
[quantity for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].bids]
[quantity for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].asks]
) - sum(
[quantity for (price, quantity) in sell_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].bids]
[quantity for (price, quantity) in sell_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].asks]
)

if value_sold * (1 - initial_state.pools['Kraken'].trade_fee) != pytest.approx(quantity_bought):
Expand Down Expand Up @@ -104,18 +104,18 @@ def test_sell_base(quantity: float, trade_fee: float, order_book: OrderBook):
quantity_sold = initial_state.agents['agent'].holdings[tkn_sell] - buy_state.agents['agent'].holdings[tkn_sell]

value_bought = sum(
[quantity for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].asks]
[quantity for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].bids]
) - sum(
[quantity for (price, quantity) in buy_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].asks]
[quantity for (price, quantity) in buy_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].bids]
)

if value_bought != pytest.approx(quantity_sold):
raise AssertionError('Central market sell trade failed to execute correctly.')

value_sold = sum(
[quantity * price for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].asks]
[quantity * price for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].bids]
) - sum(
[quantity * price for (price, quantity) in buy_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].asks]
[quantity * price for (price, quantity) in buy_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].bids]
)

quantity_bought = buy_state.agents['agent'].holdings[tkn_buy] - initial_state.agents['agent'].holdings[tkn_buy]
Expand Down Expand Up @@ -152,9 +152,9 @@ def test_buy_quote(quantity: float, trade_fee: float, order_book: OrderBook):
)

value_sold = sum(
[quantity * price for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].asks]
[quantity * price for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].bids]
) - sum(
[quantity * price for (price, quantity) in buy_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].asks]
[quantity * price for (price, quantity) in buy_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].bids]
)

quantity_bought = buy_state.agents['agent'].holdings[tkn_buy] - initial_state.agents['agent'].holdings[tkn_buy]
Expand All @@ -165,9 +165,9 @@ def test_buy_quote(quantity: float, trade_fee: float, order_book: OrderBook):
quantity_sold = initial_state.agents['agent'].holdings[tkn_sell] - buy_state.agents['agent'].holdings[tkn_sell]

value_bought = sum(
[quantity for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].asks]
[quantity for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].bids]
) - sum(
[quantity for (price, quantity) in buy_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].asks]
[quantity for (price, quantity) in buy_state.pools['Kraken'].order_book[(tkn_sell, tkn_buy)].bids]
)

if value_bought / (1 - initial_state.pools['Kraken'].trade_fee) != pytest.approx(quantity_sold):
Expand Down Expand Up @@ -203,18 +203,18 @@ def test_buy_base(quantity: float, order_book: OrderBook):
quantity_bought = sell_state.agents['agent'].holdings[tkn_buy] - initial_state.agents['agent'].holdings[tkn_buy]

value_sold = sum(
[quantity for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].bids]
[quantity for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].asks]
) - sum(
[quantity for (price, quantity) in sell_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].bids]
[quantity for (price, quantity) in sell_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].asks]
)

if value_sold != pytest.approx(quantity_bought):
raise AssertionError('Central market buy trade failed to execute correctly.')

value_bought = sum(
[quantity * price for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].bids]
[quantity * price for (price, quantity) in initial_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].asks]
) - sum(
[quantity * price for (price, quantity) in sell_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].bids]
[quantity * price for (price, quantity) in sell_state.pools['Kraken'].order_book[(tkn_buy, tkn_sell)].asks]
)

quantity_sold = initial_state.agents['agent'].holdings[tkn_sell] - sell_state.agents['agent'].holdings[tkn_sell]
Expand Down

0 comments on commit 2c5db06

Please sign in to comment.