Skip to content

Commit

Permalink
Add min trade limit to pairwise optimizer
Browse files Browse the repository at this point in the history
  • Loading branch information
nagakingg committed Oct 30, 2023
1 parent 389b58c commit 5895bb2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
35 changes: 23 additions & 12 deletions curvesim/pipelines/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,16 @@ def post_trade_price_error(dx, coin_in, coin_out, price_target):
trades = []

for pair in prices:
i, j = pair

if pool.price(i, j) - prices[pair] > 0:
price = prices[pair]
coin_in, coin_out = i, j
elif pool.price(j, i) - 1 / prices[pair] > 0:
price = 1 / prices[pair]
coin_in, coin_out = j, i
else:
trades.append(ArbTrade(i, j, 0, prices[pair]))
coin_in, coin_out, price = _get_optimizer_args(pair, pool, prices[pair])

lower_bound = pool.get_min_trade_size(coin_in)
min_price_diff = post_trade_price_error(lower_bound, coin_in, coin_out, price)
if min_price_diff <= 0:
trades.append(ArbTrade(coin_in, coin_out, 0, price))
continue

high = pool.get_max_trade_size(coin_in, coin_out)
bounds = (0, high)
upper_bound = pool.get_max_trade_size(coin_in, coin_out)
bounds = (lower_bound, upper_bound)
try:
res = root_scalar(
post_trade_price_error,
Expand All @@ -94,3 +90,18 @@ def post_trade_price_error(dx, coin_in, coin_out, price_target):
trades.append(ArbTrade(coin_in, coin_out, size, price))

return trades


def _get_optimizer_args(pair, pool, market_price):
i, j = pair
price_error_i = pool.price(i, j) - market_price
price_error_j = pool.price(j, i) - 1 / market_price

if price_error_i >= price_error_j:
target_price = market_price
coin_in, coin_out = i, j
else:
target_price = 1 / market_price
coin_in, coin_out = j, i

return coin_in, coin_out, target_price
2 changes: 1 addition & 1 deletion curvesim/pipelines/vol_limited_arb/trader.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def _apply_volume_limits(arb_trades, limits, pool):
limited_amount_in = min(limits[pair], trade.amount_in)
lim_trade = trade.replace_amount_in(limited_amount_in)

if lim_trade.amount_in > pool.get_min_trade_size(lim_trade.coin_in):
if lim_trade.amount_in >= pool.get_min_trade_size(lim_trade.coin_in):
limited_arb_trades.append(lim_trade)
else:
excluded_trades.append(lim_trade)
Expand Down

0 comments on commit 5895bb2

Please sign in to comment.