Expire GTD orders before matching, reads and recovery
This commit is contained in:
@@ -16,6 +16,7 @@ pub(crate) struct NewOrder {
|
||||
pub price: i64,
|
||||
pub qty: i64,
|
||||
pub tif: TimeInForce,
|
||||
pub expiration: Option<i64>,
|
||||
pub post_only: bool,
|
||||
}
|
||||
|
||||
@@ -28,6 +29,7 @@ pub(crate) fn submit(tx: &Transaction, books: &Books, order: NewOrder) -> Result
|
||||
price,
|
||||
qty,
|
||||
tif,
|
||||
expiration,
|
||||
post_only,
|
||||
} = order;
|
||||
identifier(&account)?;
|
||||
@@ -43,9 +45,25 @@ pub(crate) fn submit(tx: &Transaction, books: &Books, order: NewOrder) -> Result
|
||||
"unsupported time in force"
|
||||
);
|
||||
ensure!(
|
||||
!post_only || (stage == "continuous" && tif == TimeInForce::Gtc),
|
||||
"post-only requires continuous GTC"
|
||||
!post_only || (stage == "continuous" && matches!(tif, TimeInForce::Gtc | TimeInForce::Gtd)),
|
||||
"post-only requires continuous GTC or GTD"
|
||||
);
|
||||
let deadline = match tif {
|
||||
TimeInForce::Gtd => {
|
||||
let expiration =
|
||||
expiration.ok_or_else(|| anyhow::anyhow!("GTD requires expiration"))?;
|
||||
let now: i64 = tx.query_row("SELECT unixepoch()", [], |r| r.get(0))?;
|
||||
ensure!(
|
||||
expiration >= now + 180,
|
||||
"GTD expiration must be at least 180 seconds ahead"
|
||||
);
|
||||
Some(expiration)
|
||||
}
|
||||
_ => {
|
||||
ensure!(expiration.unwrap_or(0) == 0, "expiration requires GTD");
|
||||
None
|
||||
}
|
||||
};
|
||||
if side == "buy" {
|
||||
ledger::cash(tx, &account, -price * qty)?;
|
||||
} else {
|
||||
@@ -53,6 +71,12 @@ pub(crate) fn submit(tx: &Transaction, books: &Books, order: NewOrder) -> Result
|
||||
}
|
||||
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 let Some(deadline) = deadline {
|
||||
tx.execute(
|
||||
"INSERT INTO order_deadlines(seq,expiration) VALUES(?,?)",
|
||||
params![seq, deadline],
|
||||
)?;
|
||||
}
|
||||
if stage == "continuous" {
|
||||
let incoming = Order {
|
||||
seq,
|
||||
@@ -86,7 +110,7 @@ pub(crate) fn submit(tx: &Transaction, books: &Books, order: NewOrder) -> Result
|
||||
|r| Ok((r.get(0)?, r.get(1)?)),
|
||||
)?;
|
||||
if matches!(tif, TimeInForce::Ioc | TimeInForce::Fak) && remaining > 0 {
|
||||
release(tx, seq, &account)?;
|
||||
release(tx, seq, &account, CancelReason::Cancelled)?;
|
||||
status = "cancelled".into();
|
||||
}
|
||||
Ok(json!({"order":seq,"filled":qty-remaining,"status":status}))
|
||||
@@ -191,13 +215,30 @@ pub(crate) fn settle(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn release(tx: &Transaction, seq: i64, owner: &str) -> Result<i64> {
|
||||
pub(crate) enum CancelReason {
|
||||
Cancelled,
|
||||
Expired,
|
||||
}
|
||||
|
||||
pub(crate) fn release(
|
||||
tx: &Transaction,
|
||||
seq: i64,
|
||||
owner: &str,
|
||||
reason: CancelReason,
|
||||
) -> 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])?;
|
||||
let status = match reason {
|
||||
CancelReason::Cancelled => "cancelled",
|
||||
CancelReason::Expired => "expired",
|
||||
};
|
||||
tx.execute(
|
||||
"UPDATE orders SET status=? WHERE seq=?",
|
||||
params![status, seq],
|
||||
)?;
|
||||
Ok(qty)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user