Skip to content

Instantly share code, notes, and snippets.

@nuevoalex
Last active July 10, 2019 21:43
Show Gist options
  • Save nuevoalex/7c9c9bc688aee05d11718f953cc0e228 to your computer and use it in GitHub Desktop.
Save nuevoalex/7c9c9bc688aee05d11718f953cc0e228 to your computer and use it in GitHub Desktop.
### LIQUIDITY SORT:
Get 0x bids/asks (remove all bids where gas fees are > max(5 cents, 1% of the trade's value), we should do this for all books on augur in v2)
Fetch all orders to start and store them somewhere that get_orders_down_to_price and get_orders_up_to_price can easily access
midpoint_liquidity = []
outcomes = 0
For outcome in market:
best_bid = get_best_bid(market)
best_ask = get_best_ask(market)
if(no_bid_exists): best_bid = minPrice
if(no_ask_exists): best_ask = maxPrice
best_bid = best_bid / (1 - totalFee %)
best_ask = best_ask / (1 + totalFee %)
midpoint_price = (best_bid + best_ask)/2
ask_price = midpoint_price + spread % * range/2
bid_price = midpoint_price - spread % * range/2
bid_quantities = 0
ask_quantities = 0
// for bids we get orders from the midpoint down to and inclusive of the bid price. For asks we get the orders from the midpoint *up to* inclusive of the ask price.
for order in get_orders_down_to_price(bids, bid_price): bid_quantities += order.quantity
for order in get_orders_up_to_price(asks, ask_price): ask_quantities += order.quantity
num_shares = min(ask_quantities, bid_quantities)
bid_value = 0
ask_value = 0
raw_bid_value = 0
raw_ask_value = 0
// the getters return a dictionary of orders w/ quantity and price
bid_quantity_gotten = 0
for order in get_orders_down_to_price(bids, bid_price):
bid_num_shares = num_shares
if(bid_num_shares == 0)
break
if(order.quantity + bid_quantity_gotten > bid_num_shares):
order.quantity = bid_num_shares - quantity_gotten
bid_value += order.quantity * order.price
raw_bid_value += order.quantity * (order.price - min)
bid_quantity_gotten += order.quantity
ask_quantity_gotten = 0
for order in get_orders_up_to_price(asks, ask_price):
ask_num_shares = num_shares
if(ask_num_shares == 0)
break
if(order.quantity + ask_quantity_gotten > num_shares):
order.quantity = num_shares - quantity_gotten
ask_value += order.quantity * order.price
raw_ask_value += order.quantity * (max - order.price)
ask_quantity_gotten += order.quantity
if(num_shares == 0):
Spread[outcomes] = 1
midpoint_liquidity = 0
else:
midpoint_liquidity[outcomes] = (raw_ask_value + raw_bid_value)
Spread[outcomes] = (ask_value - bid_value) / ((max - min)*num_shares)
outcomes += 1
// sum midpoint liquidity across all outcomes
Liquidity = sum(midpoint_liquidity)
Spread = min(Spread)
Let user choose a spread % like in dropdown now, and it'll sort markets by liquidity within that spread % threshold where a market has a Spread < spread % and then rank them by the Liquidity metric
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment