Skip to content

Instantly share code, notes, and snippets.

@joeykrug
Last active August 1, 2019 18:19
Show Gist options
  • Save joeykrug/f61596d7b51cf66252c01b599bc3f0f6 to your computer and use it in GitHub Desktop.
Save joeykrug/f61596d7b51cf66252c01b599bc3f0f6 to your computer and use it in GitHub Desktop.
Liquidity ranking
### INVALID FILTER:
Get 0x bids (remove all bids where gas fees are > 1% of the trade's value, we should do this for all books on augur in v2)
valid_revenue = best_bid_quantity*range*(1-reporter_fee-creator_fee)*e^(-.15*time_til_market_finalizes_in_years) - gasToDoATrade - gasToClaimWinnings
valid_cost = best_bid_quantity * (max - best_bid)
valid_profit = valid_revenue - valid_cost
if valid_profit >= 0:
show_market
else:
invalid
### HORIZONTAL_LIQUIDITY:
Get 0x bids/asks (remove all bids where gas fees are > 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
Skip invalid outcome
midpoint_liquidity = []
For outcome in market:
best_bid = get_best_bid(market, outcome)
best_ask = get_best_ask(market, outcome)
if(no_bid_exists or no_ask_exists):
midpoint_liquidity = 0
continue
best_bid = best_bid / (1 - reporter_fee-creator_fee)
best_ask = best_ask / (1 + reporter_fee+creator_fee)
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, outcome, bid_price): bid_quantities += order.quantity
for order in get_orders_up_to_price(asks, outcome, ask_price): ask_quantities += order.quantity
num_shares = min(ask_quantities, bid_quantities)
raw_bid_value = 0
raw_ask_value = 0
// the getters return a dictionary of orders w/ quantity and price
bid_quantity_gotten = 0
ask_quantity_gotten = 0
if(num_shares == 0):
midpoint_liquidity = 0
else:
for order in get_orders_down_to_price(bids, outcome, bid_price):
if(order.quantity + bid_quantity_gotten > num_shares):
order.quantity = num_shares - bid_quantity_gotten
if(bid_quantity_gotten >= num_shares):
break
raw_bid_value += order.quantity * (order.price - min)
bid_quantity_gotten += order.quantity
for order in get_orders_up_to_price(asks, outcome, ask_price):
if(order.quantity + ask_quantity_gotten > num_shares):
order.quantity = num_shares - ask_quantity_gotten
if(ask_quantity_gotten >= num_shares):
break
raw_ask_value += order.quantity * (max - order.price)
ask_quantity_gotten += order.quantity
midpoint_liquidity[outcome].bids = raw_bid_value
midpoint_liquidity[outcome].asks = raw_ask_value
// sum midpoint liquidity across all outcomes
horizontal_liquidity = sum(midpoint_liquidity bids and asks)
### VERTICAL LIQUIDITY:
Get 0x bids/asks (remove all bids where gas fees are > 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
Skip invalid outcome
bid_quantities = []
bid_price = []
bid_sum = 0
ask_quantities = []
ask_price = []
ask_sum = 0
if(binary or scalar):
vertical_liquidity.left = 0
vertical_liquidity.right = 0
else:
// for bids
For outcome in market:
best_bid = get_best_bid(market, outcome)
if(no_best_bid):
vertical_liquidity.left = 0
bid_sum = 0
break
else:
bid_price[outcome] = best_bid / (1 - reporter_fee-creator_fee)
bid_sum += bid_price[outcome] - min
excess_spread = bid_sum - range * (1 - spread %)
// if liquidity > the spread % even at best bids or there is no best bid
if(excess_spread <= 0 or bid_sum == 0):
vertical_liquidity.left = 0
else:
bid_quantities = []
for outcome in market:
bid_price[outcome] = bid_price[outcome] - excess_spread/num_outcomes_excluding_skipped
for order in get_orders_down_to_price(bids, outcome, bid_price[outcome]): bid_quantities[outcome] += order.quantity
num_shares = min(bid_quantities)
for outcome in market:
raw_bid_value = 0
bid_quantity_gotten = 0
for order in get_orders_down_to_price(bids, outcome, bid_price[outcome]):
if(order.quantity + bid_quantity_gotten > num_shares):
order.quantity = num_shares - bid_quantity_gotten
if(bid_quantity_gotten >= num_shares):
break
raw_bid_value += order.quantity * (order.price - min)
bid_quantity_gotten += order.quantity
vertical_liquidity[outcome].left = raw_bid_value
// for asks
For outcome in market:
best_ask = get_best_ask(market, outcome)
if(no_best_ask):
vertical_liquidity.right = 0
ask_sum = 0
break
else:
ask_price[outcome] = best_ask / (1 + reporter_fee-creator_fee)
// uses min b/c we want prices that add up to range to calculate a spread, so selling at 33 should be 33*3 ~= 100, not 66*3
ask_sum += ask_price[outcome] - min
excess_spread = range * (spread % + 1) - ask_sum
// if liquidity > the spread % even at best asks or there is no best ask
if(excess_spread <= 0 or ask_sum == 0):
vertical_liquidity.right = 0
else:
ask_quantities = []
for outcome in market:
ask_price[outcome] = ask_price[outcome] + excess_spread/num_outcomes_excluding_invalid
for order in get_orders_up_to_price(asks, outcome, ask_price[outcome]): ask_quantities[outcome] += order.quantity
num_shares = min(ask_quantities)
for outcome in market:
raw_ask_value = 0
ask_quantity_gotten = 0
for order in get_orders_up_to_price(asks, outcome, ask_price[outcome]):
if(order.quantity + ask_quantity_gotten > num_shares):
order.quantity = num_shares - ask_quantity_gotten
if(ask_quantity_gotten >= num_shares):
break
raw_ask_value += order.quantity * (max - order.price)
ask_quantity_gotten += order.quantity
vertical_liquidity[outcome].right = raw_ask_value
// sum midpoint liquidity across all outcomes
vertical_liquidity.left = sum(vertical_liquidity.left for all outcomes)
vertical_liquidity.right = sum(vertical_liquidity.right for all outcomes)
left_overlap = 0
right_overlap = 0
for outcome in market:
left_overlap += min(vertical_liquidity[outcome].left, midpoint_liquidity[outcome].bids)
right_overlap += min(vertical_liquidity[outcome].right, midpoint_liquidity[outcome].asks)
### LIQUIDITY_SORT:
Liquidity = vertical_liquidity.left + horizontal_liquidity + vertical_liquidity.right - left_overlap - right_overlap
Let user choose a spread % like in dropdown now, and show markets ranked by Liquidity, only showing ones w/ Liquidity > 0 (those are markets within that spread % threshold)
// rerun this hourly
// will need to store in local storage or w/e
if(time.now() - last_updated >= 1 hr ago):
hourly_rankings[current hour day year] = do_liquidity_calcs()
last_updated = time.now()
ranking = sum(hourly_rankings.trailing24hr.only_nonempty) / len(hourly_rankings.trailing24hr.only_nonempty)
Note: for markets that don't appear in the sort, put them below these markets and still display them, ranking those by OI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment