Skip to content

Instantly share code, notes, and snippets.

@joeykrug
Last active May 24, 2019 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeykrug/b311388b49ff29af27033f595628017e to your computer and use it in GitHub Desktop.
Save joeykrug/b311388b49ff29af27033f595628017e to your computer and use it in GitHub Desktop.
ryan algo mod w/ comments
def getLiquidityTokens(marketId, outcome, spreadPercent) {
// we adjust for fees by ask / (1 + totalFee%) and bid / (1 - totalFee%)
// also the share price should be converted to non display prices so a scalar from -100 to 200, -100 would be 0, 200 would be 300.
const orderBook = feeAdjust(sharePrice(select *
from orders where marketId and outcome));
// also add orders to the book for infinite liquidity (so maybe just whatever the biggest js value is) at the min and max, for bids we add it at the min, for asks we add it at the max
let liquidityTokens = 0 eth; // ie. proceeds from selling complete sets
let liquidityTokenCost = 0 eth; // ie. cost for complete sets
const sellIncrementQuantity = numCompleteSetsForNEthTokens(
SELL_INCREMENT_COST
);; // ie. number of complete sets bought by SELL_INCREMENT_COST tokens
let liquidityTokens = 0 eth;
do {
liquidityTokenCost += SELL_INCREMENT_COST;
// sell the complete sets into the book by selling into the bid and then buying it back via the ask and seeing how
// much money is left over. e.g. take .1 eth worth of complete sets which is y complete sets and try to sell y shares by
// filling the bid side, then try to buy back y shares by filling the asks. So an example is the user buys a complete set.
// Then they get rid of outcome x by selling it, which gives them say 198, then they buy it back, which costs them 202. Then they
// close their complete set out for 300. In the transaction they made 300 + 198 - 202 which is 296. If that was a range 300 eth
// market with a complete set cost of 300 eth, then if they had a sell increment cost of 300, the liquidityTokens would go up
// to 296 and the liquidityTokenCost would be 300. 296/300. Makes sense! If the sell increment cost was a more reasonable 0.1
// eth, then .1/300 = 0.0003333333333 it’d be 0.099999 + 0.065999 - 0.067333 or 0.098665. 0.098665 would be the liquidityTokens
// received divided by the cost of 0.099999 or 0.098665/0.099999. Note those two numbers have the same spread, just on a
// different size/scale, so we can see that the math works!
tmpProceeds = orderBook.takeCompleteSets(sellIncrementQuantity);
if (liquidityTokens / liquidityTokenCost < (1 - spreadPercent))
break;
liquidityTokens += tmpProceeds
} while )
return liquidityTokens;
}
if (binary or scalar market):
numOutcomes = 1
Liquidity = array(numOutcomes)
for outcome in marketId {
if (binary or scalar and outcome == no):
continue
Liquidity[outcome] = getLiquidityTokens(marketId, outcome,
spreadPercent)
}
Liquidity_Value = min(Liquidity)
If(best_display_ask - best_display_bid) / range < spreadPercent {
show market
}
else {
hide market
}
Sort visible markets by Liquidity_Value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment