Track prediction-market parity and expose outcome display quotes
This commit is contained in:
@@ -141,6 +141,10 @@ impl Listing {
|
||||
#[derive(Deserialize)]
|
||||
#[serde(tag = "op", rename_all = "snake_case", deny_unknown_fields)]
|
||||
pub(crate) enum Query {
|
||||
Quote {
|
||||
market: String,
|
||||
outcome: String,
|
||||
},
|
||||
Catalog {
|
||||
event: String,
|
||||
},
|
||||
@@ -157,6 +161,34 @@ pub(crate) enum Query {
|
||||
impl Query {
|
||||
pub fn read(self, db: &Connection) -> Result<Value> {
|
||||
match self {
|
||||
Self::Quote { market, outcome } => {
|
||||
ensure!(matches!(outcome.as_str(), "YES" | "NO"), "invalid outcome");
|
||||
let phase: String =
|
||||
db.query_row("SELECT phase FROM markets WHERE id=?", [&market], |r| {
|
||||
r.get(0)
|
||||
})?;
|
||||
let (bid,ask):(Option<i64>,Option<i64>)=db.query_row(
|
||||
"SELECT MAX(CASE WHEN side='buy' THEN price END), MIN(CASE WHEN side='sell' THEN price END)
|
||||
FROM orders WHERE market=? AND outcome=? AND status='live'",
|
||||
params![market,outcome], |r|Ok((r.get(0)?,r.get(1)?)))?;
|
||||
// Fills also cover databases created before timestamped history was introduced.
|
||||
let last: Option<i64> = db.query_row(
|
||||
"SELECT (SELECT f.price FROM fills f JOIN orders o ON o.seq=f.bid
|
||||
WHERE o.market=? AND o.outcome=? ORDER BY f.seq DESC LIMIT 1)",
|
||||
params![market, outcome],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
let (display, source) = match (phase.as_str(), bid, ask, last) {
|
||||
("continuous", Some(b), Some(a), _) if (0..=10).contains(&(a - b)) => {
|
||||
(Some((a + b) * 5000), Some("midpoint"))
|
||||
}
|
||||
("continuous", _, _, Some(p)) => (Some(p * 10000), Some("last_trade")),
|
||||
_ => (None, None),
|
||||
};
|
||||
Ok(json!({"market":market,"outcome":outcome,"phase":phase,
|
||||
"best_bid_micros":bid.map(|p|p*10000),"best_ask_micros":ask.map(|p|p*10000),
|
||||
"last_trade_micros":last.map(|p|p*10000),"display_price_micros":display,"source":source}))
|
||||
}
|
||||
Self::Catalog { event } => {
|
||||
let detail = db.query_row(
|
||||
"SELECT title,sport,competition,home,away,scheduled_at_ms FROM events WHERE id=?",
|
||||
|
||||
@@ -30,7 +30,10 @@ impl Exchange {
|
||||
}
|
||||
|
||||
pub fn execute(&mut self, payload: Value) -> Result<Value> {
|
||||
if matches!(payload["op"].as_str(), Some("catalog" | "history")) {
|
||||
if matches!(
|
||||
payload["op"].as_str(),
|
||||
Some("catalog" | "history" | "quote")
|
||||
) {
|
||||
let query: catalog::Query = serde_json::from_value(payload)?;
|
||||
let tx = self.db.transaction()?;
|
||||
let value = query.read(&tx)?;
|
||||
|
||||
@@ -245,3 +245,27 @@ fn sports_catalog_and_trade_curve_survive_retries() {
|
||||
next["after"] = json!([p["at_ms"], p["fill"]]);
|
||||
assert_eq!(e.execute(next).unwrap()["points"], json!([]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn displayed_quote_uses_midpoint_through_ten_cents_then_last_fill() {
|
||||
let mut e = Exchange::open(":memory:").unwrap();
|
||||
setup(&mut e);
|
||||
let quote = json!({"op":"quote","market":"e","outcome":"YES"});
|
||||
e.execute(json!({"id":"open","op":"open","market":"e"}))
|
||||
.unwrap();
|
||||
assert!(e.execute(quote.clone()).unwrap()["display_price_micros"].is_null());
|
||||
e.execute(order("maker", "alice", "sell", 40, 1)).unwrap();
|
||||
e.execute(order("trade", "bob", "buy", 40, 1)).unwrap();
|
||||
e.execute(order("bid", "bob", "buy", 34, 1)).unwrap();
|
||||
let ask = e.execute(order("ask", "alice", "sell", 44, 1)).unwrap();
|
||||
assert_eq!(
|
||||
e.execute(quote.clone()).unwrap()["display_price_micros"],
|
||||
390000
|
||||
);
|
||||
e.execute(json!({"op":"cancel","id":"withdraw","account":"alice","order":ask["order"]}))
|
||||
.unwrap();
|
||||
e.execute(order("wider", "alice", "sell", 45, 1)).unwrap();
|
||||
let display = e.execute(quote).unwrap();
|
||||
assert_eq!(display["display_price_micros"], 400000);
|
||||
assert_eq!(display["source"], "last_trade");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user