Apply per-market taker fees to cash budgets, fills and account activity
This commit is contained in:
+45
-13
@@ -25,10 +25,15 @@ pub(crate) struct ShareExecution {
|
||||
pub filled: i64,
|
||||
pub status: String,
|
||||
pub gross_cents: i64,
|
||||
pub fee_micros: i64,
|
||||
}
|
||||
impl ShareExecution {
|
||||
pub fn receipt(&self) -> Value {
|
||||
json!({"order":self.seq,"filled":self.filled,"status":self.status})
|
||||
let mut receipt = json!({"order":self.seq,"filled":self.filled,"status":self.status});
|
||||
if self.fee_micros > 0 {
|
||||
receipt["fee_micros"] = json!(self.fee_micros);
|
||||
}
|
||||
receipt
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,6 +99,7 @@ pub(crate) fn submit(tx: &Transaction, books: &Books, order: NewOrder) -> Result
|
||||
)?;
|
||||
}
|
||||
let mut gross = 0;
|
||||
let mut fee_micros = 0;
|
||||
if stage == "continuous" {
|
||||
let incoming = Order {
|
||||
seq,
|
||||
@@ -120,7 +126,7 @@ pub(crate) fn submit(tx: &Transaction, books: &Books, order: NewOrder) -> Result
|
||||
"FAK has no matching liquidity"
|
||||
);
|
||||
gross = gross_cents(&fills);
|
||||
settle(tx, &market, &outcome, fills, Some(Taker::Limit(seq)))?;
|
||||
fee_micros = settle(tx, &market, &outcome, fills, Some(Taker::Limit(seq)))?;
|
||||
}
|
||||
let (remaining, mut status): (i64, String) = tx.query_row(
|
||||
"SELECT remaining,status FROM orders WHERE seq=?",
|
||||
@@ -131,7 +137,9 @@ pub(crate) fn submit(tx: &Transaction, books: &Books, order: NewOrder) -> Result
|
||||
release(tx, seq, &account, CancelReason::Cancelled)?;
|
||||
status = "cancelled".into();
|
||||
}
|
||||
crate::fees::debit(tx, &account, fee_micros)?;
|
||||
Ok(ShareExecution {
|
||||
fee_micros,
|
||||
seq,
|
||||
filled: qty - remaining,
|
||||
status,
|
||||
@@ -175,16 +183,19 @@ pub(crate) fn market_buy(tx: &Transaction, books: &Books, order: BudgetBuy) -> R
|
||||
params![account, market, outcome, max_price],
|
||||
)?;
|
||||
let seq = tx.last_insert_rowid();
|
||||
let fills = books
|
||||
let mut fills = books
|
||||
.get(&market, &outcome)
|
||||
.map(|b| b.match_budget_buy(seq, max_price, amount_cents))
|
||||
.unwrap_or_default();
|
||||
let rate = crate::fees::rate(tx, &market)?;
|
||||
crate::fees::fit_budget(rate, &mut fills, amount_cents);
|
||||
ensure!(!fills.is_empty(), "no affordable liquidity");
|
||||
let quantity: i64 = fills.iter().map(|f| f.quantity).sum();
|
||||
let spent = gross_cents(&fills);
|
||||
let unspent = amount_cents - spent;
|
||||
let gross_remainder = amount_cents - spent;
|
||||
let unspent_micros = gross_remainder * 10000 - crate::fees::total(rate, &fills);
|
||||
ensure!(
|
||||
tif != TimeInForce::Fok || unspent == 0,
|
||||
tif != TimeInForce::Fok || unspent_micros == 0,
|
||||
"FOK cannot fill the entire cash amount"
|
||||
);
|
||||
tx.execute(
|
||||
@@ -195,17 +206,22 @@ pub(crate) fn market_buy(tx: &Transaction, books: &Books, order: BudgetBuy) -> R
|
||||
"INSERT INTO budget_buys(order_seq,amount_cents,spent_cents) VALUES(?,?,?)",
|
||||
params![seq, amount_cents, spent],
|
||||
)?;
|
||||
settle(tx, &market, &outcome, fills, Some(Taker::Budget(seq)))?;
|
||||
ledger::cash(tx, &account, unspent)?;
|
||||
let status = if unspent == 0 {
|
||||
let fee = settle(tx, &market, &outcome, fills, Some(Taker::Budget(seq)))?;
|
||||
ledger::cash(tx, &account, gross_remainder)?;
|
||||
crate::fees::debit(tx, &account, fee)?;
|
||||
let status = if unspent_micros == 0 {
|
||||
"filled"
|
||||
} else {
|
||||
tx.execute("UPDATE orders SET status='cancelled' WHERE seq=?", [seq])?;
|
||||
"cancelled"
|
||||
};
|
||||
Ok(
|
||||
json!({"order":seq,"status":status,"filled":quantity,"amount_cents":amount_cents,"spent_cents":spent,"unspent_cents":unspent}),
|
||||
)
|
||||
let mut receipt = json!({"order":seq,"status":status,"filled":quantity,"amount_cents":amount_cents,"spent_cents":spent,"unspent_cents":unspent_micros/10000});
|
||||
if fee > 0 {
|
||||
receipt["fee_micros"] = json!(fee);
|
||||
receipt["unspent_micros"] = json!(unspent_micros);
|
||||
receipt["total_spent_micros"] = json!(spent * 10000 + fee);
|
||||
}
|
||||
Ok(receipt)
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
@@ -271,7 +287,13 @@ pub(crate) fn settle(
|
||||
outcome: &str,
|
||||
fills: Vec<Fill>,
|
||||
taker: Option<Taker>,
|
||||
) -> Result<()> {
|
||||
) -> Result<i64> {
|
||||
let rate = if taker.is_some() && !fills.is_empty() {
|
||||
crate::fees::rate(tx, market)?
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let mut fees = 0;
|
||||
for f in fills {
|
||||
let first = book_leg(tx, market, outcome, &f, f.bid, taker)?;
|
||||
let second = book_leg(tx, market, outcome, &f, f.ask, taker)?;
|
||||
@@ -322,11 +344,21 @@ pub(crate) fn settle(
|
||||
"INSERT INTO execution_context(execution,taker_order) VALUES(?,?)",
|
||||
params![execution, taker.map(Taker::seq)],
|
||||
)?;
|
||||
if let Some(taker) = taker {
|
||||
let fee = crate::fees::micros(rate, f.price, f.quantity);
|
||||
if fee > 0 {
|
||||
tx.execute(
|
||||
"INSERT INTO execution_fees VALUES(?,?,?)",
|
||||
params![execution, taker.seq(), fee],
|
||||
)?;
|
||||
fees += fee;
|
||||
}
|
||||
}
|
||||
for seq in [f.bid, f.ask] {
|
||||
tx.execute("UPDATE orders SET remaining=remaining-?1,status=CASE WHEN remaining=?1 THEN 'filled' ELSE 'live' END WHERE seq=?2",params![f.quantity,seq])?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
Ok(fees)
|
||||
}
|
||||
|
||||
pub(crate) enum CancelReason {
|
||||
|
||||
Reference in New Issue
Block a user