Match public midpoint and spread across all four book shapes
This commit is contained in:
@@ -60,6 +60,13 @@ impl Quote {
|
||||
display_price(self.bid, self.ask, self.last, phase)
|
||||
}
|
||||
|
||||
fn public_sides(&self) -> (i64, i64) {
|
||||
(
|
||||
self.bid.unwrap_or(0),
|
||||
self.ask.unwrap_or(crate::tick_size::PAR),
|
||||
)
|
||||
}
|
||||
|
||||
fn spread_micros(&self) -> Option<i64> {
|
||||
Some((self.ask? - self.bid?) * 100)
|
||||
}
|
||||
@@ -279,9 +286,10 @@ impl crate::Exchange {
|
||||
let mut result = json!({});
|
||||
for (request, quote) in requests.iter().zip(quote_rows(&tx, requests)?) {
|
||||
if let Some(quote) = quote {
|
||||
let (bid, ask) = quote.public_sides();
|
||||
let ticks = match request.side {
|
||||
PriceSide::Buy => quote.bid.unwrap_or(0),
|
||||
PriceSide::Sell => quote.ask.unwrap_or(crate::tick_size::PAR),
|
||||
PriceSide::Buy => bid,
|
||||
PriceSide::Sell => ask,
|
||||
};
|
||||
let text = format!("{}.{:04}", ticks / 10000, ticks % 10000);
|
||||
result[request.token_id.as_str()][request.label()] =
|
||||
@@ -296,20 +304,20 @@ impl crate::Exchange {
|
||||
&self,
|
||||
tokens: &[String],
|
||||
) -> Result<BTreeMap<String, Option<String>>, crate::MarketDataError> {
|
||||
self.quote_values(tokens, Quote::spread_micros)
|
||||
self.quote_values(tokens, |bid, ask| (ask - bid) * 100)
|
||||
}
|
||||
|
||||
pub fn midpoints(
|
||||
&self,
|
||||
tokens: &[String],
|
||||
) -> Result<BTreeMap<String, Option<String>>, crate::MarketDataError> {
|
||||
self.quote_values(tokens, Quote::midpoint_micros)
|
||||
self.quote_values(tokens, |bid, ask| (ask + bid) * 50)
|
||||
}
|
||||
|
||||
fn quote_values(
|
||||
&self,
|
||||
tokens: &[String],
|
||||
measure: fn(&Quote) -> Option<i64>,
|
||||
measure: fn(i64, i64) -> i64,
|
||||
) -> Result<BTreeMap<String, Option<String>>, crate::MarketDataError> {
|
||||
if tokens.len() > 500 {
|
||||
return Err(crate::MarketDataError::Invalid("too many tokens".into()));
|
||||
@@ -321,7 +329,9 @@ impl crate::Exchange {
|
||||
.map_err(|_| crate::MarketDataError::Invalid("invalid token id".into()))?;
|
||||
let input =
|
||||
serde_json::to_string(&assets).map_err(|_| crate::MarketDataError::Unavailable)?;
|
||||
let mut stmt = self.db.prepare_cached(
|
||||
let tx = crate::expiry::transaction(&self.db, rusqlite::TransactionBehavior::Deferred)
|
||||
.map_err(|_| crate::MarketDataError::Unavailable)?;
|
||||
let mut stmt = tx.prepare_cached(
|
||||
"SELECT COALESCE(q.seq,0),q.yes_bid,q.yes_ask,q.at_ms,j.key FROM json_each(?1) j
|
||||
JOIN markets m ON m.id=json_extract(j.value,'$[1]')
|
||||
LEFT JOIN quote_events q ON q.seq=(SELECT MAX(seq) FROM quote_events WHERE market=m.id)"
|
||||
@@ -331,11 +341,15 @@ impl crate::Exchange {
|
||||
while let Some(row) = rows.next()? {
|
||||
let index: usize = row.get(4)?;
|
||||
let quote = Quote::read(row, assets[index].0)?;
|
||||
let value = measure("e).map(|micros| {
|
||||
crate::inventory::decimal(micros.into(), crate::inventory::UNIT).to_string()
|
||||
});
|
||||
result.insert(tokens[index].clone(), value);
|
||||
let (bid, ask) = quote.public_sides();
|
||||
let micros = measure(bid, ask);
|
||||
let value =
|
||||
crate::inventory::decimal(micros.into(), crate::inventory::UNIT).to_string();
|
||||
result.insert(tokens[index].clone(), Some(value));
|
||||
}
|
||||
drop(rows);
|
||||
drop(stmt);
|
||||
tx.commit()?;
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user