Enforce FOK, FAK and post-only constraints before applying fills
This commit is contained in:
+34
-12
@@ -1,4 +1,4 @@
|
||||
use crate::resident::Books;
|
||||
use crate::{command::TimeInForce, resident::Books};
|
||||
use crate::{
|
||||
ledger,
|
||||
validation::{amount, identifier, phase},
|
||||
@@ -15,7 +15,8 @@ pub(crate) struct NewOrder {
|
||||
pub side: String,
|
||||
pub price: i64,
|
||||
pub qty: i64,
|
||||
pub tif: String,
|
||||
pub tif: TimeInForce,
|
||||
pub post_only: bool,
|
||||
}
|
||||
|
||||
pub(crate) fn submit(tx: &Transaction, books: &Books, order: NewOrder) -> Result<Value> {
|
||||
@@ -27,6 +28,7 @@ pub(crate) fn submit(tx: &Transaction, books: &Books, order: NewOrder) -> Result
|
||||
price,
|
||||
qty,
|
||||
tif,
|
||||
post_only,
|
||||
} = order;
|
||||
identifier(&account)?;
|
||||
amount(qty)?;
|
||||
@@ -37,9 +39,13 @@ pub(crate) fn submit(tx: &Transaction, books: &Books, order: NewOrder) -> Result
|
||||
);
|
||||
ensure!((0..=100).contains(&price), "invalid price");
|
||||
ensure!(
|
||||
tif == "GTC" || (tif == "IOC" && stage == "continuous"),
|
||||
tif == TimeInForce::Gtc || stage == "continuous",
|
||||
"unsupported time in force"
|
||||
);
|
||||
ensure!(
|
||||
!post_only || (stage == "continuous" && tif == TimeInForce::Gtc),
|
||||
"post-only requires continuous GTC"
|
||||
);
|
||||
if side == "buy" {
|
||||
ledger::cash(tx, &account, -price * qty)?;
|
||||
} else {
|
||||
@@ -48,22 +54,38 @@ 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 stage == "continuous" {
|
||||
if let Some(book) = books.get(&market, &outcome) {
|
||||
let incoming = Order {
|
||||
seq,
|
||||
side: if side == "buy" { Side::Buy } else { Side::Sell },
|
||||
price,
|
||||
remaining: qty,
|
||||
};
|
||||
settle(tx, &market, &outcome, book.match_incoming(&incoming))?;
|
||||
let incoming = Order {
|
||||
seq,
|
||||
side: if side == "buy" { Side::Buy } else { Side::Sell },
|
||||
price,
|
||||
remaining: qty,
|
||||
};
|
||||
let fills = books
|
||||
.get(&market, &outcome)
|
||||
.map(|book| book.match_incoming(&incoming))
|
||||
.unwrap_or_default();
|
||||
ensure!(
|
||||
!post_only || fills.is_empty(),
|
||||
"post-only order would take liquidity"
|
||||
);
|
||||
if tif == TimeInForce::Fok {
|
||||
ensure!(
|
||||
fills.iter().map(|fill| fill.quantity).sum::<i64>() == qty,
|
||||
"FOK cannot fill the entire order"
|
||||
);
|
||||
}
|
||||
ensure!(
|
||||
tif != TimeInForce::Fak || !fills.is_empty(),
|
||||
"FAK has no matching liquidity"
|
||||
);
|
||||
settle(tx, &market, &outcome, fills)?;
|
||||
}
|
||||
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 {
|
||||
if matches!(tif, TimeInForce::Ioc | TimeInForce::Fak) && remaining > 0 {
|
||||
release(tx, seq, &account)?;
|
||||
status = "cancelled".into();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user