Apply per-market taker fees to cash budgets, fills and account activity
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
use crate::ledger;
|
||||
use anyhow::{ensure, Result};
|
||||
use matching::Fill;
|
||||
use rusqlite::{params, Connection, Transaction};
|
||||
|
||||
pub(crate) fn rate(db: &Connection, market: &str) -> Result<i64> {
|
||||
Ok(db.query_row("SELECT COALESCE(f.rate_ppm,0) FROM markets m LEFT JOIN market_fee_rates f ON f.market=m.id WHERE m.id=?", [market], |r|r.get(0))?)
|
||||
}
|
||||
|
||||
// rate is parts per million; prices are cents. Round each fill to units of
|
||||
// ten micros, suppressing amounts below the published minimum.
|
||||
pub(crate) fn micros(rate: i64, price: i64, shares: i64) -> i64 {
|
||||
let numerator =
|
||||
i128::from(shares) * i128::from(rate) * i128::from(price) * i128::from(100 - price);
|
||||
if numerator < 100_000 {
|
||||
return 0;
|
||||
}
|
||||
(((numerator + 50_000) / 100_000) * 10) as i64
|
||||
}
|
||||
|
||||
pub(crate) fn total(rate: i64, fills: &[Fill]) -> i64 {
|
||||
fills
|
||||
.iter()
|
||||
.map(|f| micros(rate, f.price, f.quantity))
|
||||
.sum()
|
||||
}
|
||||
|
||||
// match_budget_buy supplies FIFO candidates under the gross cap. Stop at
|
||||
// the first partially affordable maker, including its rounded fee.
|
||||
pub(crate) fn fit_budget(rate: i64, fills: &mut Vec<Fill>, budget: i64) {
|
||||
if rate == 0 {
|
||||
return;
|
||||
}
|
||||
let mut remaining = budget * 10000;
|
||||
let mut count = 0;
|
||||
for f in fills.iter_mut() {
|
||||
let cost = |qty: i64| {
|
||||
i128::from(qty) * i128::from(f.price) * 10000 + i128::from(micros(rate, f.price, qty))
|
||||
};
|
||||
if cost(f.quantity) <= i128::from(remaining) {
|
||||
remaining -= cost(f.quantity) as i64;
|
||||
count += 1;
|
||||
continue;
|
||||
}
|
||||
let (mut lo, mut hi) = (0, f.quantity);
|
||||
while lo < hi {
|
||||
let mid = lo + (hi - lo + 1) / 2;
|
||||
if cost(mid) <= i128::from(remaining) {
|
||||
lo = mid;
|
||||
} else {
|
||||
hi = mid - 1;
|
||||
}
|
||||
}
|
||||
if lo > 0 {
|
||||
f.quantity = lo;
|
||||
count += 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
fills.truncate(count);
|
||||
}
|
||||
|
||||
pub(crate) fn debit(tx: &Transaction, account: &str, fee: i64) -> Result<()> {
|
||||
if fee == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
let old: i64 = tx.query_row(
|
||||
"SELECT COALESCE((SELECT micros FROM cash_remainders WHERE account=?),0)",
|
||||
[account],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
let delta = old - fee;
|
||||
ledger::cash(tx, account, delta.div_euclid(10000))?;
|
||||
tx.execute("INSERT INTO cash_remainders(account,micros) VALUES(?,?) ON CONFLICT(account) DO UPDATE SET micros=excluded.micros",params![account,delta.rem_euclid(10000)])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn configure(tx: &Transaction, market: &str, rate: i64) -> Result<()> {
|
||||
ensure!((0..=1_000_000).contains(&rate), "invalid taker fee rate");
|
||||
tx.execute(
|
||||
"INSERT INTO market_fee_rates VALUES(?,?)",
|
||||
params![market, rate],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::micros;
|
||||
|
||||
#[test]
|
||||
fn symmetric_taker_curve_matches_published_examples_and_minimum() {
|
||||
for (rate, price, size, expected) in [
|
||||
(50000, 50, 100, 1250000),
|
||||
(50000, 30, 100, 1050000),
|
||||
(10000, 50, 100, 250000),
|
||||
(10000, 30, 100, 210000),
|
||||
(0, 50, 100, 0),
|
||||
(1, 50, 39, 0),
|
||||
(1, 50, 40, 10),
|
||||
(1, 50, 60, 20),
|
||||
(50000, 0, 1, 0),
|
||||
] {
|
||||
assert_eq!(micros(rate, price, size), expected);
|
||||
assert_eq!(micros(rate, 100 - price, size), expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user