Serve the CLOB price history protocol with minute-based fidelity
This commit is contained in:
@@ -209,3 +209,16 @@ sequenceDiagram
|
||||
```
|
||||
|
||||
[可重放请求](../../docs/examples/clob-http.http)同时展示相同盘口下的买价、卖价与中点。
|
||||
|
||||
### CLOB 客户端历史曲线
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant C as CLOB 客户端
|
||||
participant H as market_http.rs
|
||||
participant E as prices_history/legacy.rs
|
||||
C->>H: GET /prices-history,market 与 interval 或 startTs、endTs
|
||||
H->>E: clob_price_history,fidelity 以分钟计
|
||||
E-->>H: 按观测时刻排序的价格点
|
||||
H-->>C: history 数组,每点包含 t 和 p
|
||||
```
|
||||
|
||||
@@ -284,6 +284,23 @@ fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
|
||||
allow = Some("GET");
|
||||
Err((405, "Method not allowed"))
|
||||
}
|
||||
(&Method::Get, "/prices-history") => {
|
||||
let result = serde_urlencoded::from_str::<exchange::ClobHistoryQuery>(params)
|
||||
.map_err(|_| HistoryError::Invalid("invalid query parameters".into()))
|
||||
.and_then(|query| {
|
||||
engine
|
||||
.lock()
|
||||
.map_err(|_| HistoryError::Unavailable)?
|
||||
.clob_price_history(query)
|
||||
});
|
||||
match result {
|
||||
Ok(history) => Ok(history),
|
||||
Err(HistoryError::Invalid(message)) => {
|
||||
return send(req, 400, json!({"error":message}), None)
|
||||
}
|
||||
Err(HistoryError::Unavailable) => Err((500, "Internal server error")),
|
||||
}
|
||||
}
|
||||
(&Method::Get, "/price") => serde_urlencoded::from_str::<WirePrice>(params)
|
||||
.map_err(|_| (400, "Invalid token id"))
|
||||
.and_then(|p| execution_prices(engine, vec![p], true)),
|
||||
@@ -361,7 +378,7 @@ fn respond(mut req: Request, engine: &Mutex<Exchange>) -> std::io::Result<()> {
|
||||
}
|
||||
(
|
||||
_,
|
||||
"/price" | "/midpoint" | "/spread" | "/book" | "/last-trade-price"
|
||||
"/price" | "/midpoint" | "/spread" | "/book" | "/last-trade-price" | "/prices-history"
|
||||
| "/v2/prices-history",
|
||||
) => {
|
||||
allow = Some("GET");
|
||||
@@ -488,6 +505,12 @@ mod tests {
|
||||
let price_batch =
|
||||
json!({"eb:YES:mid-example":{"BUY":0.09,"SELL":0.08},"eb:NO:mid-example":{"BUY":0.92}});
|
||||
let cases = [
|
||||
("GET","/prices-history?market=eb:YES:s&interval=1h&fidelity=10",String::new(),200,Some(json!({"history":[]}))),
|
||||
("GET","/prices-history?market=eb:NO:s&startTs=1000&endTs=2000",String::new(),200,Some(json!({"history":[]}))),
|
||||
("GET","/prices-history?market=eb:YES:s&startTs=1000",String::new(),400,None),
|
||||
("GET","/prices-history?market=eb:YES:s&interval=1d&fidelity=0",String::new(),400,None),
|
||||
("GET","/prices-history?market=eb:YES:s&startTs=1&endTs=2&interval=max",String::new(),400,None),
|
||||
("POST","/prices-history","{}".into(),405,None),
|
||||
("GET","/price?token_id=eb:YES:mid-example&side=BUY",String::new(),200,Some(json!({"price":"0.09"}))),
|
||||
("GET","/price?token_id=eb:YES:mid-example&side=SELL",String::new(),200,Some(json!({"price":"0.08"}))),
|
||||
("GET","/price?token_id=eb:YES:one&side=SELL",String::new(),200,Some(json!({"price":"0.20"}))),
|
||||
|
||||
Reference in New Issue
Block a user