Port the simulator to a Rust workspace and document each crate
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
use crate::{
|
||||
ledger,
|
||||
validation::{amount, identifier, phase},
|
||||
};
|
||||
use anyhow::{ensure, Result};
|
||||
use matching::{cross, Order, Phase, Side};
|
||||
use rusqlite::{params, Transaction};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
pub(crate) struct NewOrder {
|
||||
pub market: String,
|
||||
pub account: String,
|
||||
pub outcome: String,
|
||||
pub side: String,
|
||||
pub price: i64,
|
||||
pub qty: i64,
|
||||
pub tif: String,
|
||||
}
|
||||
|
||||
pub(crate) fn submit(tx: &Transaction, order: NewOrder) -> Result<Value> {
|
||||
let NewOrder {
|
||||
market,
|
||||
account,
|
||||
outcome,
|
||||
side,
|
||||
price,
|
||||
qty,
|
||||
tif,
|
||||
} = order;
|
||||
identifier(&account)?;
|
||||
amount(qty)?;
|
||||
let stage = phase(tx, &market, &["preopen", "continuous"])?;
|
||||
ensure!(
|
||||
matches!(outcome.as_str(), "YES" | "NO") && matches!(side.as_str(), "buy" | "sell"),
|
||||
"invalid instrument or side"
|
||||
);
|
||||
ensure!((0..=100).contains(&price), "invalid price");
|
||||
ensure!(
|
||||
tif == "GTC" || (tif == "IOC" && stage == "continuous"),
|
||||
"unsupported time in force"
|
||||
);
|
||||
if side == "buy" {
|
||||
ledger::cash(tx, &account, -price * qty)?;
|
||||
} else {
|
||||
ledger::position(tx, &account, &market, &outcome, -qty)?;
|
||||
}
|
||||
tx.execute("INSERT INTO orders(account,market,outcome,side,price,quantity,remaining,status) VALUES(?,?,?,?,?,?,?,'live')",params![account,market,outcome,side,price,qty,qty])?;
|
||||
let seq = tx.last_insert_rowid();
|
||||
if stage == "continuous" {
|
||||
match_book(tx, &market, &outcome, Phase::Continuous)?;
|
||||
}
|
||||
let (remaining, mut status): (i64, String) = tx.query_row(
|
||||
"SELECT remaining,status FROM orders WHERE seq=?",
|
||||
[seq],
|
||||
|r| Ok((r.get(0)?, r.get(1)?)),
|
||||
)?;
|
||||
if tif == "IOC" && remaining > 0 {
|
||||
release(tx, seq, &account)?;
|
||||
status = "cancelled".into();
|
||||
}
|
||||
Ok(json!({"order":seq,"filled":qty-remaining,"status":status}))
|
||||
}
|
||||
|
||||
pub(crate) fn match_book(
|
||||
tx: &Transaction,
|
||||
market: &str,
|
||||
outcome: &str,
|
||||
phase: Phase,
|
||||
) -> Result<()> {
|
||||
let mut stmt=tx.prepare("SELECT seq,side,price,remaining FROM orders WHERE market=? AND outcome=? AND status='live'")?;
|
||||
let orders = stmt
|
||||
.query_map(params![market, outcome], |r| {
|
||||
Ok(Order {
|
||||
seq: r.get(0)?,
|
||||
side: if r.get::<_, String>(1)? == "buy" {
|
||||
Side::Buy
|
||||
} else {
|
||||
Side::Sell
|
||||
},
|
||||
price: r.get(2)?,
|
||||
remaining: r.get(3)?,
|
||||
})
|
||||
})?
|
||||
.collect::<rusqlite::Result<Vec<_>>>()?;
|
||||
for f in cross(orders, phase) {
|
||||
let (buyer, limit): (String, i64) = tx.query_row(
|
||||
"SELECT account,price FROM orders WHERE seq=?",
|
||||
[f.bid],
|
||||
|r| Ok((r.get(0)?, r.get(1)?)),
|
||||
)?;
|
||||
let seller: String =
|
||||
tx.query_row("SELECT account FROM orders WHERE seq=?", [f.ask], |r| {
|
||||
r.get(0)
|
||||
})?;
|
||||
ledger::cash(tx, &seller, f.price * f.quantity)?;
|
||||
ledger::cash(tx, &buyer, (limit - f.price) * f.quantity)?;
|
||||
ledger::position(tx, &buyer, market, outcome, f.quantity)?;
|
||||
tx.execute(
|
||||
"INSERT INTO fills(bid,ask,price,quantity) VALUES(?,?,?,?)",
|
||||
params![f.bid, f.ask, f.price, f.quantity],
|
||||
)?;
|
||||
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(())
|
||||
}
|
||||
|
||||
pub(crate) fn release(tx: &Transaction, seq: i64, owner: &str) -> Result<i64> {
|
||||
let (market,outcome,side,price,qty):(String,String,String,i64,i64)=tx.query_row("SELECT market,outcome,side,price,remaining FROM orders WHERE seq=? AND account=? AND status='live'",params![seq,owner],|r|Ok((r.get(0)?,r.get(1)?,r.get(2)?,r.get(3)?,r.get(4)?)))?;
|
||||
if side == "buy" {
|
||||
ledger::cash(tx, owner, price * qty)?;
|
||||
} else {
|
||||
ledger::position(tx, owner, &market, &outcome, qty)?;
|
||||
}
|
||||
tx.execute("UPDATE orders SET status='cancelled' WHERE seq=?", [seq])?;
|
||||
Ok(qty)
|
||||
}
|
||||
Reference in New Issue
Block a user