Preview market sweeps and execute protected share sales
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
use crate::{
|
||||
assets,
|
||||
book::gross_cents,
|
||||
command::TimeInForce,
|
||||
resident::Books,
|
||||
validation::{amount, phase},
|
||||
};
|
||||
use anyhow::{ensure, Result};
|
||||
use matching::{Order, Side};
|
||||
use rusqlite::Transaction;
|
||||
use serde::Deserialize;
|
||||
use serde_json::{json, Value};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(tag = "side", rename_all = "lowercase", deny_unknown_fields)]
|
||||
pub(crate) enum Request {
|
||||
Buy {
|
||||
market: String,
|
||||
outcome: String,
|
||||
amount_cents: i64,
|
||||
max_price: Option<i64>,
|
||||
tif: Option<TimeInForce>,
|
||||
},
|
||||
Sell {
|
||||
market: String,
|
||||
outcome: String,
|
||||
qty: i64,
|
||||
min_price: Option<i64>,
|
||||
tif: Option<TimeInForce>,
|
||||
},
|
||||
}
|
||||
|
||||
impl Request {
|
||||
pub(crate) fn read(self, tx: &Transaction, books: &mut Books) -> Result<Value> {
|
||||
let (market, outcome, side, requested, limit, tif) = match self {
|
||||
Self::Buy {
|
||||
market,
|
||||
outcome,
|
||||
amount_cents,
|
||||
max_price,
|
||||
tif,
|
||||
} => (
|
||||
market,
|
||||
outcome,
|
||||
Side::Buy,
|
||||
amount_cents,
|
||||
max_price.unwrap_or(100),
|
||||
tif,
|
||||
),
|
||||
Self::Sell {
|
||||
market,
|
||||
outcome,
|
||||
qty,
|
||||
min_price,
|
||||
tif,
|
||||
} => (
|
||||
market,
|
||||
outcome,
|
||||
Side::Sell,
|
||||
qty,
|
||||
min_price.unwrap_or(0),
|
||||
tif,
|
||||
),
|
||||
};
|
||||
let tif = tif.unwrap_or(TimeInForce::Fak);
|
||||
amount(requested)?;
|
||||
ensure!(
|
||||
matches!(tif, TimeInForce::Fak | TimeInForce::Fok),
|
||||
"estimate requires FAK or FOK"
|
||||
);
|
||||
ensure!(
|
||||
matches!(outcome.as_str(), "YES" | "NO") && (0..=100).contains(&limit),
|
||||
"invalid outcome or price"
|
||||
);
|
||||
phase(tx, &market, &["continuous"])?;
|
||||
let changes = books.changes(tx)?;
|
||||
books.apply(changes);
|
||||
// The hypothetical incoming order follows all resting orders in time priority.
|
||||
let fills = books
|
||||
.get(&market, &outcome)
|
||||
.map(|b| match side {
|
||||
Side::Buy => b.match_budget_buy(i64::MAX, limit, requested),
|
||||
Side::Sell => b.match_incoming(&Order {
|
||||
seq: i64::MAX,
|
||||
side,
|
||||
price: limit,
|
||||
remaining: requested,
|
||||
}),
|
||||
})
|
||||
.unwrap_or_default();
|
||||
let shares: i64 = fills.iter().map(|f| f.quantity).sum();
|
||||
let gross = gross_cents(&fills);
|
||||
let remainder = requested
|
||||
- match side {
|
||||
Side::Buy => gross,
|
||||
Side::Sell => shares,
|
||||
};
|
||||
let mut result = json!({"asset_id":assets::id(&market,&outcome),"filled_shares":shares,
|
||||
"gross_cents":gross,"best_fill_price":fills.first().map(|f|f.price),
|
||||
"worst_fill_price":fills.last().map(|f|f.price),
|
||||
"average_price_micros":(gross*10000).checked_div(shares),
|
||||
"fully_fillable":remainder==0,"executable":shares>0 && (tif==TimeInForce::Fak || remainder==0)});
|
||||
let key = match side {
|
||||
Side::Buy => "unspent_cents",
|
||||
Side::Sell => "unsold_shares",
|
||||
};
|
||||
result[key] = json!(remainder);
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user