Reconstruct public CLOB chart samples from historical books
This commit is contained in:
@@ -221,7 +221,7 @@ sequenceDiagram
|
||||
participant E as prices_history/legacy.rs
|
||||
C->>H: GET /prices-history,market 与 interval 或 startTs、endTs
|
||||
H->>E: clob_price_history,fidelity 以分钟计
|
||||
E-->>H: 按观测时刻排序的价格点
|
||||
E-->>H: 返回窗口内盘口中点样本,末尾追加最近可用点
|
||||
H-->>C: history 数组,每点包含 t 和 p
|
||||
```
|
||||
|
||||
|
||||
@@ -733,11 +733,13 @@ mod tests {
|
||||
("GET","/market-clarifications?offset=1",String::new(),200,Some(json!([]))),
|
||||
("GET","/market-clarifications?limit=101",String::new(),400,None),
|
||||
("POST","/market-clarifications","{}".into(),405,None),
|
||||
("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),
|
||||
("GET","/prices-history?market=eb:YES:missing&interval=1h&fidelity=10",String::new(),200,Some(json!({"history":[]}))),
|
||||
("GET","/prices-history?market=eb:NO:missing&startTs=1000&endTs=2000",String::new(),200,Some(json!({"history":[]}))),
|
||||
("GET","/prices-history?market=eb:YES:missing&startTs=1000",String::new(),200,Some(json!({"history":[]}))),
|
||||
("GET","/prices-history?market=eb:YES:missing&interval=1d&fidelity=0",String::new(),200,Some(json!({"history":[]}))),
|
||||
("GET","/prices-history?market=eb:YES:missing&startTs=1&endTs=2&interval=max",String::new(),200,Some(json!({"history":[]}))),
|
||||
("GET","/prices-history?market=eb:YES:s&interval=1w&fidelity=4",String::new(),400,Some(json!({"error":"invalid filters: minimum 'fidelity' for '1w' range is 5"}))),
|
||||
("GET","/prices-history?market=eb:YES:s&startTs=100&endTs=1296101",String::new(),400,Some(json!({"error":"invalid filters: 'startTs' and 'endTs' interval is too long"}))),
|
||||
("POST","/prices-history","{}".into(),405,None),
|
||||
("GET","/price?token_id=eb:YES:mid-example&side=BUY",String::new(),200,Some(json!({"price":"0.08"}))),
|
||||
("GET","/price?token_id=eb:YES:mid-example&side=SELL",String::new(),200,Some(json!({"price":"0.09"}))),
|
||||
|
||||
@@ -1125,14 +1125,16 @@ sequenceDiagram
|
||||
Q-->>H: 按资产和方向分组的可用价格
|
||||
```
|
||||
|
||||
### CLOB 历史交易价格
|
||||
### CLOB 历史价格
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
H["cli/market_http.rs"] --> L["prices_history/legacy.rs"]
|
||||
L --> A["assets.rs"]
|
||||
L --> Q["prices_history/legacy.sql"]
|
||||
Q --> T["price_migration.sql 中的 outcome_trade_prices"]
|
||||
Q --> T["quote_schema.sql 中的 quote_events"]
|
||||
Q --> O["prices_history/chart_schema.sql 中的采样起点和时间索引"]
|
||||
API["lib.rs"] --> O
|
||||
Q --> S["schema.sql 中的 price_settlements"]
|
||||
```
|
||||
|
||||
@@ -1142,8 +1144,8 @@ sequenceDiagram
|
||||
participant E as Exchange
|
||||
participant D as SQLite
|
||||
C->>E: clob_price_history
|
||||
E->>D: 读事务中确认资产并取得当前时钟
|
||||
E->>D: 按查询窗口和分钟粒度选每个桶内最后一个成交或兑付
|
||||
E->>D: 清理到期委托并取得读快照与数据库时钟
|
||||
E->>D: 按 UTC 分钟网格重放各采样点当时的盘口,再读取最近可用点
|
||||
D-->>E: 观测时间和定点价格
|
||||
E-->>C: t 为 Unix 秒,p 为十进制数
|
||||
```
|
||||
|
||||
@@ -120,6 +120,13 @@ impl Exchange {
|
||||
if !quotes_ready {
|
||||
db.execute_batch(include_str!("quote_schema.sql"))?;
|
||||
}
|
||||
let history_ready: bool = db.query_row(
|
||||
"SELECT EXISTS(SELECT 1 FROM sqlite_schema WHERE type='trigger' AND name='chart_market_listed')",
|
||||
[], |r| r.get(0),
|
||||
)?;
|
||||
if !history_ready {
|
||||
db.execute_batch(include_str!("prices_history/chart_schema.sql"))?;
|
||||
}
|
||||
let marks_ready: bool = db.query_row(
|
||||
"SELECT EXISTS(SELECT 1 FROM sqlite_schema WHERE type='table' AND name='last_asset_trades')",
|
||||
[], |row| row.get(0),
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
BEGIN IMMEDIATE;
|
||||
CREATE INDEX IF NOT EXISTS quote_market_time ON quote_events(market,at_ms,seq);
|
||||
CREATE TABLE IF NOT EXISTS chart_origins (
|
||||
market TEXT PRIMARY KEY REFERENCES markets,
|
||||
at_ms INTEGER NOT NULL
|
||||
);
|
||||
CREATE TRIGGER IF NOT EXISTS chart_market_listed AFTER INSERT ON markets BEGIN
|
||||
INSERT INTO chart_origins VALUES(NEW.id,CAST(unixepoch('subsec')*1000 AS INTEGER));
|
||||
END;
|
||||
-- Existing books have reliable history from their first recorded quote. For
|
||||
-- markets without one, recording starts at the migration's observation time.
|
||||
INSERT OR IGNORE INTO chart_origins
|
||||
SELECT id,COALESCE((SELECT MIN(at_ms) FROM quote_events WHERE market=m.id),
|
||||
CAST(unixepoch('subsec')*1000 AS INTEGER)) FROM markets m;
|
||||
COMMIT;
|
||||
@@ -15,31 +15,60 @@ pub struct ClobHistoryQuery {
|
||||
}
|
||||
|
||||
impl ClobHistoryQuery {
|
||||
fn bounds(&self, now: i64) -> Result<(i64, i64), HistoryError> {
|
||||
let invalid = || HistoryError::Invalid("invalid price history window".into());
|
||||
match (self.interval.as_deref(), self.start, self.end) {
|
||||
(Some(interval), None, None) => {
|
||||
let span = match interval {
|
||||
"1h" => 3600,
|
||||
"6h" => 6 * 3600,
|
||||
"1d" => super::DAY,
|
||||
"1w" => 7 * super::DAY,
|
||||
"1m" => 30 * super::DAY,
|
||||
"max" | "all" => return Ok((0, now)),
|
||||
_ => return Err(invalid()),
|
||||
};
|
||||
Ok(((now - span * 1000).max(0), now))
|
||||
fn window(&self, now: i64) -> Result<(i64, i64, i64), HistoryError> {
|
||||
let invalid = |reason: &str| HistoryError::Invalid(format!("invalid filters: {reason}"));
|
||||
for n in [self.start, self.end].into_iter().flatten() {
|
||||
if !n.is_finite() || !(0.0..=f64::from(u32::MAX)).contains(&n) {
|
||||
return Err(invalid("invalid timestamp"));
|
||||
}
|
||||
(None, Some(start), Some(end))
|
||||
if start.is_finite()
|
||||
&& start >= 0.0
|
||||
&& start < end
|
||||
&& end <= f64::from(u32::MAX) =>
|
||||
{
|
||||
Ok((boundary_ms(start, f64::ceil), boundary_ms(end, f64::floor)))
|
||||
}
|
||||
_ => Err(invalid()),
|
||||
}
|
||||
let start = self.start.unwrap_or(0.0);
|
||||
let end = self.end.unwrap_or(0.0);
|
||||
if end > 0.0 {
|
||||
if start > end {
|
||||
return Err(invalid(
|
||||
"invalid time component, 'startTs' can't be older than 'endTs'",
|
||||
));
|
||||
}
|
||||
if end - start > (15 * super::DAY) as f64 {
|
||||
return Err(invalid("'startTs' and 'endTs' interval is too long"));
|
||||
}
|
||||
}
|
||||
let minutes = self.fidelity.unwrap_or(1).max(1);
|
||||
let width = minutes.min(i64::from(u32::MAX)) * 60_000;
|
||||
if let Some(interval) = self.interval.as_deref() {
|
||||
let (span, minimum) = match interval {
|
||||
"1h" => (3600, 1),
|
||||
"6h" => (21600, 1),
|
||||
"1d" => (super::DAY, 1),
|
||||
"1w" => (7 * super::DAY, 5),
|
||||
"1m" => (30 * super::DAY, 10),
|
||||
"max" | "all" => return Ok((0, now, width)),
|
||||
_ => return Err(invalid(
|
||||
"the 'interval' value is unknown. Known values: '1m', '1w', '1d', '6h', '1h'",
|
||||
)),
|
||||
};
|
||||
if minutes < minimum {
|
||||
return Err(invalid(&format!(
|
||||
"minimum 'fidelity' for '{interval}' range is {minimum}"
|
||||
)));
|
||||
}
|
||||
return Ok(((now - span * 1000).max(0), now, width));
|
||||
}
|
||||
if start == 0.0 && end == 0.0 {
|
||||
return Err(invalid(
|
||||
"the time component is mandatory, please use 'startTs' and 'endTs' or 'interval'",
|
||||
));
|
||||
}
|
||||
Ok((
|
||||
boundary_ms(start, f64::ceil),
|
||||
if end == 0.0 {
|
||||
now
|
||||
} else {
|
||||
boundary_ms(end, f64::floor)
|
||||
},
|
||||
width,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,24 +88,17 @@ impl Exchange {
|
||||
pub fn clob_price_history(&self, query: ClobHistoryQuery) -> Result<Value, HistoryError> {
|
||||
let (outcome, market) = assets::parse_id(&query.market)
|
||||
.map_err(|_| HistoryError::Invalid("invalid market asset id".into()))?;
|
||||
let width = query
|
||||
.fidelity
|
||||
.unwrap_or(1)
|
||||
.checked_mul(60)
|
||||
.filter(|n| *n > 0)
|
||||
.ok_or_else(|| HistoryError::Invalid("invalid fidelity".into()))?;
|
||||
let tx = self.db.unchecked_transaction()?;
|
||||
let (listed, now): (bool, i64) = tx.query_row(
|
||||
"SELECT EXISTS(SELECT 1 FROM markets WHERE id=?1), CAST(unixepoch('subsec') * 1000 AS INTEGER)",
|
||||
[market], |r| Ok((r.get(0)?, r.get(1)?))
|
||||
let tx = crate::expiry::transaction(&self.db, rusqlite::TransactionBehavior::Deferred)
|
||||
.map_err(|_| HistoryError::Unavailable)?;
|
||||
let now: i64 = tx.query_row(
|
||||
"SELECT CAST(unixepoch('subsec') * 1000 AS INTEGER)",
|
||||
[],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
if !listed {
|
||||
return Err(HistoryError::Invalid("unknown market asset id".into()));
|
||||
}
|
||||
let (start, end) = query.bounds(now)?;
|
||||
let (start, end, width) = query.window(now)?;
|
||||
let points = {
|
||||
let mut stmt = tx.prepare_cached(include_str!("legacy.sql"))?;
|
||||
let rows = stmt.query_map(params![market, outcome, start, end, width], |r| {
|
||||
let rows = stmt.query_map(params![market, outcome, start, end, width, now], |r| {
|
||||
let timestamp: i64 = r.get(0)?;
|
||||
let micros: i64 = r.get(1)?;
|
||||
Ok(json!({"t":timestamp,"p":inventory::decimal(micros.into(), inventory::UNIT)}))
|
||||
|
||||
@@ -1,13 +1,34 @@
|
||||
-- Return the last observed price in each UTC fidelity window at its actual time.
|
||||
WITH observations(ms, micros, sequence, payout) AS (
|
||||
SELECT at_ms, price_micros, fill, 0 FROM outcome_trade_prices
|
||||
WHERE market=?1 AND outcome=?2 AND at_ms BETWEEN ?3 AND ?4
|
||||
-- Reconstruct public midpoint observations from committed book changes.
|
||||
-- The local sampler uses a UTC minute grid. The venue's collector has clock jitter.
|
||||
WITH RECURSIVE horizon AS (
|
||||
SELECT (SELECT at_ms FROM chart_origins WHERE market=?1) AS first_ms,
|
||||
(SELECT at_ms FROM price_settlements WHERE market=?1) AS resolved_ms,
|
||||
(SELECT winner FROM price_settlements WHERE market=?1) AS winner,
|
||||
(SELECT phase FROM markets WHERE id=?1) AS phase
|
||||
), bounds AS (
|
||||
SELECT first_ms, resolved_ms, winner,
|
||||
CASE WHEN phase='resolved' THEN COALESCE(resolved_ms,
|
||||
(SELECT MAX(at_ms) FROM quote_events WHERE market=?1))
|
||||
ELSE ?6/60000*60000 END AS last_ms
|
||||
FROM horizon
|
||||
), grid(ms) AS (
|
||||
SELECT ((MAX(?3,first_ms)+?5-1)/?5)*?5 FROM bounds
|
||||
WHERE ((MAX(?3,first_ms)+?5-1)/?5)*?5 <= MIN(?4,last_ms)
|
||||
UNION ALL
|
||||
SELECT at_ms, CASE WHEN winner='VOID' THEN 500000 WHEN winner=?2 THEN 1000000 ELSE 0 END, id, 1
|
||||
FROM price_settlements WHERE market=?1 AND at_ms BETWEEN ?3 AND ?4
|
||||
), ranked AS (
|
||||
SELECT ms, micros, ROW_NUMBER() OVER (
|
||||
PARTITION BY ms/1000/?5 ORDER BY ms DESC, payout DESC, sequence DESC
|
||||
) AS position FROM observations
|
||||
SELECT ms+?5 FROM grid,bounds WHERE ms+?5 <= MIN(?4,last_ms)
|
||||
), points(ms,tail) AS (
|
||||
SELECT ms,0 FROM grid
|
||||
UNION ALL
|
||||
SELECT last_ms,1 FROM bounds WHERE resolved_ms IS NOT NULL OR first_ms<=last_ms
|
||||
)
|
||||
SELECT ms/1000, micros FROM ranked WHERE position=1 ORDER BY ms
|
||||
SELECT points.ms/1000,
|
||||
CASE WHEN points.ms=b.resolved_ms THEN
|
||||
CASE WHEN b.winner='VOID' THEN 500000 WHEN b.winner=?2 THEN 1000000 ELSE 0 END
|
||||
WHEN ?2='YES' THEN (COALESCE(q.yes_bid,0)+COALESCE(q.yes_ask,10000))*50
|
||||
ELSE 1000000-(COALESCE(q.yes_bid,0)+COALESCE(q.yes_ask,10000))*50 END
|
||||
FROM points CROSS JOIN bounds b
|
||||
LEFT JOIN quote_events q ON q.seq=(
|
||||
SELECT seq FROM quote_events WHERE market=?1 AND at_ms<=points.ms
|
||||
ORDER BY at_ms DESC,seq DESC LIMIT 1
|
||||
)
|
||||
ORDER BY points.ms,points.tail
|
||||
|
||||
@@ -1512,6 +1512,64 @@ fn wallet_activity_paginates_fills_and_records_each_automatic_redemption_once()
|
||||
assert_eq!(deposit["activity"][0]["cash_delta_cents"], 2000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_history_samples_the_book_without_fills_and_appends_the_latest_mark() {
|
||||
let dir = tempdir().unwrap();
|
||||
let path = dir.path().join("chart.db");
|
||||
let mut exchange = Exchange::open(&path).unwrap();
|
||||
for line in include_str!("../../../docs/examples/midpoint.jsonl").lines() {
|
||||
exchange
|
||||
.execute(serde_json::from_str(line).unwrap())
|
||||
.unwrap();
|
||||
}
|
||||
let db = rusqlite::Connection::open(&path).unwrap();
|
||||
db.execute(
|
||||
"UPDATE quote_events SET at_ms=CASE WHEN yes_ask IS NULL THEN 119000 ELSE 180001 END",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
db.execute(
|
||||
"UPDATE chart_origins SET at_ms=119000 WHERE market='mid-example'",
|
||||
[],
|
||||
)
|
||||
.unwrap();
|
||||
let read = |e: &Exchange, outcome: &str, start: f64, end: f64| {
|
||||
e.clob_price_history(serde_json::from_value(json!({
|
||||
"market":format!("eb:{outcome}:mid-example"), "startTs":start, "endTs":end, "fidelity":1
|
||||
})).unwrap()).unwrap()["history"].as_array().unwrap().clone()
|
||||
};
|
||||
let points = read(&exchange, "YES", 120.0, 240.0);
|
||||
assert_eq!(points.len(), 4);
|
||||
assert_eq!(
|
||||
&points[..3],
|
||||
&[
|
||||
json!({"t":120,"p":0.54}),
|
||||
json!({"t":180,"p":0.54}),
|
||||
json!({"t":240,"p":0.085})
|
||||
]
|
||||
);
|
||||
assert_eq!(points[3]["p"], 0.085);
|
||||
assert!(points[3]["t"].as_i64().unwrap() > 240);
|
||||
exchange
|
||||
.execute(json!({"op":"close","id":"close","market":"mid-example"}))
|
||||
.unwrap();
|
||||
exchange
|
||||
.execute(json!({"op":"finalize","id":"verdict","market":"mid-example","winner":"NO"}))
|
||||
.unwrap();
|
||||
db.execute("UPDATE price_settlements SET at_ms=273000", [])
|
||||
.unwrap();
|
||||
drop(exchange);
|
||||
let reopened = Exchange::open(path).unwrap();
|
||||
assert_eq!(
|
||||
read(&reopened, "NO", 180.001, 240.0),
|
||||
vec![json!({"t":240,"p":0.915}), json!({"t":273,"p":1})]
|
||||
);
|
||||
assert_eq!(
|
||||
read(&reopened, "YES", 5.0, 5.0),
|
||||
vec![json!({"t":273,"p":0})]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prices_history_keeps_the_terminal_tick_and_pins_pages_before_settlement() {
|
||||
let dir = tempdir().unwrap();
|
||||
@@ -1543,25 +1601,6 @@ fn prices_history_keeps_the_terminal_tick_and_pins_pages_before_settlement() {
|
||||
let wire = |engine: &Exchange, params: Value| {
|
||||
engine.public_price_history(serde_json::from_value(params).unwrap())
|
||||
};
|
||||
let clob = |engine: &Exchange, start: f64, end: f64, minutes: i64| {
|
||||
engine
|
||||
.clob_price_history(
|
||||
serde_json::from_value(json!({
|
||||
"market":"eb:YES:e","startTs":start,"endTs":end,"fidelity":minutes
|
||||
}))
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap()
|
||||
};
|
||||
assert_eq!(
|
||||
clob(&e, 120.0, 245.0, 1),
|
||||
json!({"history":[{"t":149,"p":0.47},{"t":245,"p":0.55}]})
|
||||
);
|
||||
assert_eq!(clob(&e, 149.001, 244.999, 1), json!({"history":[]}));
|
||||
assert_eq!(
|
||||
clob(&e, 120.0, 245.0, 10),
|
||||
json!({"history":[{"t":245,"p":0.55}]})
|
||||
);
|
||||
let first_wire = wire(
|
||||
&e,
|
||||
json!({"tokenId":"eb:YES:e","start":120,"end":290,"bucketSeconds":60,"limit":1}),
|
||||
@@ -1593,10 +1632,6 @@ fn prices_history_keeps_the_terminal_tick_and_pins_pages_before_settlement() {
|
||||
.unwrap();
|
||||
drop(e);
|
||||
let mut e = Exchange::open(&path).unwrap();
|
||||
assert_eq!(
|
||||
clob(&e, 120.0, 290.0, 1),
|
||||
json!({"history":[{"t":149,"p":0.47},{"t":273,"p":0.5}]})
|
||||
);
|
||||
let second_wire = wire(
|
||||
&e,
|
||||
json!({"token_id":"eb:YES:e","start":120,"end":290,"cursor":saved,"limit":99}),
|
||||
|
||||
@@ -76,7 +76,7 @@ Content-Type: application/json
|
||||
### CLOB SDK time series: asset ID, trailing hour, ten-minute fidelity
|
||||
GET http://127.0.0.1:9002/prices-history?market=eb%3AYES%3Ademo&interval=1h&fidelity=10
|
||||
|
||||
### Explicit Unix-second window (the seeded exchange regression has fills at 121, 149 and 245)
|
||||
### Explicit Unix-second window plus the latest available mark
|
||||
GET http://127.0.0.1:9002/prices-history?market=eb%3AYES%3Ae&startTs=120&endTs=245&fidelity=1
|
||||
|
||||
### After replaying fees.jsonl, both outcome tokens report base_fee 30.
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"requests": {
|
||||
"1m_minimum": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=123&interval=1m&fidelity=10","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:29:31 GMT","body":{"history":[]}},
|
||||
"1w_minimum": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=123&interval=1w&fidelity=5","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:29:31 GMT","body":{"history":[]}},
|
||||
"absolute": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=35090254409399344650860070211816464534198960801264823587796928404808383727584&startTs=1796284795&endTs=1796285695","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:17:56 GMT","body":{"history":[{"t":1796284816,"p":0.0375},{"t":1796284885,"p":0.0375},{"t":1796284970,"p":0.0375},{"t":1796284998,"p":0.0375},{"t":1796285059,"p":0.0375},{"t":1796285115,"p":0.0375},{"t":1796285179,"p":0.0375},{"t":1796285311,"p":0.0375},{"t":1796285336,"p":0.0375},{"t":1796285365,"p":0.0375},{"t":1796285418,"p":0.0375},{"t":1796285474,"p":0.0375},{"t":1796285540,"p":0.0375},{"t":1796285600,"p":0.0375},{"t":1796285654,"p":0.0375},{"t":1796285835,"p":0.0375}]}},
|
||||
"five": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=35090254409399344650860070211816464534198960801264823587796928404808383727584&startTs=1796284795&endTs=1796285695&fidelity=5","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:17:56 GMT","body":{"history":[{"t":1796284816,"p":0.0375},{"t":1796285115,"p":0.0375},{"t":1796285418,"p":0.0375},{"t":1796285835,"p":0.0375}]}},
|
||||
"fractional": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=35090254409399344650860070211816464534198960801264823587796928404808383727584&startTs=1796284795.5&endTs=1796285695.5","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:17:56 GMT","body":{"history":[{"t":1796284816,"p":0.0375},{"t":1796284885,"p":0.0375},{"t":1796284970,"p":0.0375},{"t":1796284998,"p":0.0375},{"t":1796285059,"p":0.0375},{"t":1796285115,"p":0.0375},{"t":1796285179,"p":0.0375},{"t":1796285311,"p":0.0375},{"t":1796285336,"p":0.0375},{"t":1796285365,"p":0.0375},{"t":1796285418,"p":0.0375},{"t":1796285474,"p":0.0375},{"t":1796285540,"p":0.0375},{"t":1796285600,"p":0.0375},{"t":1796285654,"p":0.0375},{"t":1796285835,"p":0.0375}]}},
|
||||
"unknown": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=123&startTs=1796284795&endTs=1796285695","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:17:56 GMT","body":{"history":[]}},
|
||||
"no_trade": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=715035038973852440637533813088906197843594055254726475255007478722039119872&startTs=1796284795&endTs=1796285695","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:17:56 GMT","body":{"history":[{"t":1796285173,"p":0.5},{"t":1796285310,"p":0.5},{"t":1796285334,"p":0.5},{"t":1796285364,"p":0.5},{"t":1796285414,"p":0.5},{"t":1796285473,"p":0.5},{"t":1796285539,"p":0.5},{"t":1796285599,"p":0.5},{"t":1796285653,"p":0.5},{"t":1796285833,"p":0.5}]}},
|
||||
"start": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=35090254409399344650860070211816464534198960801264823587796928404808383727584&startTs=1796284795","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:17:57 GMT","body":{"history":[{"t":1796284816,"p":0.0375},{"t":1796284885,"p":0.0375},{"t":1796284970,"p":0.0375},{"t":1796284998,"p":0.0375},{"t":1796285059,"p":0.0375},{"t":1796285115,"p":0.0375},{"t":1796285179,"p":0.0375},{"t":1796285311,"p":0.0375},{"t":1796285336,"p":0.0375},{"t":1796285365,"p":0.0375},{"t":1796285418,"p":0.0375},{"t":1796285474,"p":0.0375},{"t":1796285540,"p":0.0375},{"t":1796285600,"p":0.0375},{"t":1796285654,"p":0.0375},{"t":1796285714,"p":0.0375},{"t":1796285780,"p":0.0375},{"t":1796285835,"p":0.0375},{"t":1796285835,"p":0.0375}]}},
|
||||
"end": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=35090254409399344650860070211816464534198960801264823587796928404808383727584&endTs=1796285695","payload":null,"status":400,"date":"Thu, 03 Dec 2026 08:17:57 GMT","body":{"error":"invalid filters: 'startTs' and 'endTs' interval is too long"}},
|
||||
"zero": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=35090254409399344650860070211816464534198960801264823587796928404808383727584&startTs=1796285695&endTs=1796285695","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:17:57 GMT","body":{"history":[{"t":1796285835,"p":0.0375}]}},
|
||||
"reverse": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=35090254409399344650860070211816464534198960801264823587796928404808383727584&startTs=1796285695&endTs=1796284795","payload":null,"status":400,"date":"Thu, 03 Dec 2026 08:17:57 GMT","body":{"error":"invalid filters: invalid time component, 'startTs' can't be older than 'endTs'"}},
|
||||
"zero_fidelity": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=35090254409399344650860070211816464534198960801264823587796928404808383727584&startTs=1796284795&endTs=1796285695&fidelity=0","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:17:57 GMT","body":{"history":[{"t":1796284816,"p":0.0375},{"t":1796284885,"p":0.0375},{"t":1796284970,"p":0.0375},{"t":1796284998,"p":0.0375},{"t":1796285059,"p":0.0375},{"t":1796285115,"p":0.0375},{"t":1796285179,"p":0.0375},{"t":1796285311,"p":0.0375},{"t":1796285336,"p":0.0375},{"t":1796285365,"p":0.0375},{"t":1796285418,"p":0.0375},{"t":1796285474,"p":0.0375},{"t":1796285540,"p":0.0375},{"t":1796285600,"p":0.0375},{"t":1796285654,"p":0.0375},{"t":1796285835,"p":0.0375}]}},
|
||||
"week": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=35090254409399344650860070211816464534198960801264823587796928404808383727584&interval=1w&fidelity=1","payload":null,"status":400,"date":"Thu, 03 Dec 2026 08:17:57 GMT","body":{"error":"invalid filters: minimum 'fidelity' for '1w' range is 5"}},
|
||||
"month": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=35090254409399344650860070211816464534198960801264823587796928404808383727584&interval=1m&fidelity=1","payload":null,"status":400,"date":"Thu, 03 Dec 2026 08:17:58 GMT","body":{"error":"invalid filters: minimum 'fidelity' for '1m' range is 10"}},
|
||||
"old": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=35090254409399344650860070211816464534198960801264823587796928404808383727584&startTs=120&endTs=245","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:18:57 GMT","body":{"history":[{"t":1796285889,"p":0.0375}]}},
|
||||
"historic": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=35090254409399344650860070211816464534198960801264823587796928404808383727584&startTs=1796199536&endTs=1796199896","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:18:57 GMT","body":{"history":[{"t":1796199553,"p":0.048},{"t":1796199610,"p":0.048},{"t":1796199681,"p":0.048},{"t":1796199739,"p":0.0475},{"t":1796199793,"p":0.0475},{"t":1796199853,"p":0.0475},{"t":1796285889,"p":0.0375}]}},
|
||||
"future": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=35090254409399344650860070211816464534198960801264823587796928404808383727584&startTs=1796289536&endTs=1796289836","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:18:58 GMT","body":{"history":[{"t":1796285889,"p":0.0375}]}},
|
||||
"none": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=35090254409399344650860070211816464534198960801264823587796928404808383727584","payload":null,"status":400,"date":"Thu, 03 Dec 2026 08:18:57 GMT","body":{"error":"invalid filters: the time component is mandatory, please use 'startTs' and 'endTs' or 'interval'"}},
|
||||
"15_days": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=35090254409399344650860070211816464534198960801264823587796928404808383727584&startTs=100&endTs=1296100&fidelity=60","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:21:43 GMT","body":{"history":[{"t":1796286074,"p":0.0375}]}},
|
||||
"15_days_plus_one_second": {"method":"GET","url":"https://clob.polymarket.com/prices-history?market=35090254409399344650860070211816464534198960801264823587796928404808383727584&startTs=100&endTs=1296101","payload":null,"status":400,"date":"Thu, 03 Dec 2026 08:22:50 GMT","body":{"error":"invalid filters: 'startTs' and 'endTs' interval is too long"}}
|
||||
},
|
||||
"mark_comparisons": [
|
||||
{"asset":"35090254409399344650860070211816464534198960801264823587796928404808383727584","book_summary":{"url":"https://clob.polymarket.com/book?token_id=35090254409399344650860070211816464534198960801264823587796928404808383727584","date":"Thu, 03 Dec 2026 08:19:58 GMT","hash":"18790d79470c806dbd57ac2d12a172beef1a68ca","bid_levels":37,"ask_levels":108},"midpoint":{"method":"GET","url":"https://clob.polymarket.com/midpoint?token_id=35090254409399344650860070211816464534198960801264823587796928404808383727584","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:19:58 GMT","body":{"mid":"0.0375"}},"last_trade":{"method":"GET","url":"https://clob.polymarket.com/last-trade-price?token_id=35090254409399344650860070211816464534198960801264823587796928404808383727584","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:19:58 GMT","body":{"price":"0.038","side":"BUY"}},"history":{"method":"GET","url":"https://clob.polymarket.com/prices-history?market=35090254409399344650860070211816464534198960801264823587796928404808383727584&startTs=120&endTs=245","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:19:58 GMT","body":{"history":[{"t":1796285889,"p":0.0375}]}}},
|
||||
{"asset":"77367448845023382525249480243608609306658725883285195065573683809293972009019","book_summary":{"url":"https://clob.polymarket.com/book?token_id=77367448845023382525249480243608609306658725883285195065573683809293972009019","date":"Thu, 03 Dec 2026 08:19:58 GMT","hash":"43069f07a7d5f8f3627731b94a4b35e0e46a4464","bid_levels":0,"ask_levels":82},"midpoint":{"method":"GET","url":"https://clob.polymarket.com/midpoint?token_id=77367448845023382525249480243608609306658725883285195065573683809293972009019","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:19:59 GMT","body":{"mid":"0.0005"}},"last_trade":{"method":"GET","url":"https://clob.polymarket.com/last-trade-price?token_id=77367448845023382525249480243608609306658725883285195065573683809293972009019","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:19:59 GMT","body":{"price":"0.001","side":"BUY"}},"history":{"method":"GET","url":"https://clob.polymarket.com/prices-history?market=77367448845023382525249480243608609306658725883285195065573683809293972009019&startTs=120&endTs=245","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:19:59 GMT","body":{"history":[{"t":1796285949,"p":0.0005}]}}},
|
||||
{"asset":"715035038973852440637533813088906197843594055254726475255007478722039119872","book_summary":{"url":"https://clob.polymarket.com/book?token_id=715035038973852440637533813088906197843594055254726475255007478722039119872","date":"Thu, 03 Dec 2026 08:19:58 GMT","hash":"3a202ae8ae0415885d7d45328f5d6bde8e17edbb","bid_levels":46,"ask_levels":46},"midpoint":{"method":"GET","url":"https://clob.polymarket.com/midpoint?token_id=715035038973852440637533813088906197843594055254726475255007478722039119872","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:19:58 GMT","body":{"mid":"0.5"}},"last_trade":{"method":"GET","url":"https://clob.polymarket.com/last-trade-price?token_id=715035038973852440637533813088906197843594055254726475255007478722039119872","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:19:58 GMT","body":{"price":"0.5","side":""}},"history":{"method":"GET","url":"https://clob.polymarket.com/prices-history?market=715035038973852440637533813088906197843594055254726475255007478722039119872&startTs=120&endTs=245","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:19:58 GMT","body":{"history":[{"t":1796285948,"p":0.5}]}}},
|
||||
{"asset":"677710575005589820071645776051748097729466874238555432645399459881587572736","book_summary":{"url":"https://clob.polymarket.com/book?token_id=677710575005589820071645776051748097729466874238555432645399459881587572736","date":"Thu, 03 Dec 2026 08:19:58 GMT","hash":"aaa70230b6d3bd49cb50319ce5da108f18cd23ce","bid_levels":2,"ask_levels":0},"midpoint":{"method":"GET","url":"https://clob.polymarket.com/midpoint?token_id=677710575005589820071645776051748097729466874238555432645399459881587572736","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:19:58 GMT","body":{"mid":"0.615"}},"last_trade":{"method":"GET","url":"https://clob.polymarket.com/last-trade-price?token_id=677710575005589820071645776051748097729466874238555432645399459881587572736","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:19:58 GMT","body":{"price":"0.5","side":""}},"history":{"method":"GET","url":"https://clob.polymarket.com/prices-history?market=677710575005589820071645776051748097729466874238555432645399459881587572736&startTs=120&endTs=245","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:19:58 GMT","body":{"history":[]}}},
|
||||
{"asset":"786881517248772499964745717487087687070393027034702462509471699350367240192","book_summary":{"url":"https://clob.polymarket.com/book?token_id=786881517248772499964745717487087687070393027034702462509471699350367240192","date":"Thu, 03 Dec 2026 08:19:59 GMT","hash":"84ebc93977b5b7c9e4297d9b790974db61738b63","bid_levels":2,"ask_levels":0},"midpoint":{"method":"GET","url":"https://clob.polymarket.com/midpoint?token_id=786881517248772499964745717487087687070393027034702462509471699350367240192","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:19:59 GMT","body":{"mid":"0.615"}},"last_trade":{"method":"GET","url":"https://clob.polymarket.com/last-trade-price?token_id=786881517248772499964745717487087687070393027034702462509471699350367240192","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:19:59 GMT","body":{"price":"0.5","side":""}},"history":{"method":"GET","url":"https://clob.polymarket.com/prices-history?market=786881517248772499964745717487087687070393027034702462509471699350367240192&startTs=120&endTs=245","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:19:59 GMT","body":{"history":[]}}},
|
||||
{"asset":"487977102041405871565562587068462258079490970359951828915739115972976443392","book_summary":{"url":"https://clob.polymarket.com/book?token_id=487977102041405871565562587068462258079490970359951828915739115972976443392","date":"Thu, 03 Dec 2026 08:19:59 GMT","hash":"b8d5e4dd0b2edf8b698bd5157eeaefcbc3e6daf6","bid_levels":2,"ask_levels":0},"midpoint":{"method":"GET","url":"https://clob.polymarket.com/midpoint?token_id=487977102041405871565562587068462258079490970359951828915739115972976443392","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:19:59 GMT","body":{"mid":"0.615"}},"last_trade":{"method":"GET","url":"https://clob.polymarket.com/last-trade-price?token_id=487977102041405871565562587068462258079490970359951828915739115972976443392","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:19:59 GMT","body":{"price":"0.5","side":""}},"history":{"method":"GET","url":"https://clob.polymarket.com/prices-history?market=487977102041405871565562587068462258079490970359951828915739115972976443392&startTs=120&endTs=245","payload":null,"status":200,"date":"Thu, 03 Dec 2026 08:19:59 GMT","body":{"history":[]}}}
|
||||
]
|
||||
}
|
||||
@@ -374,9 +374,11 @@ Polymarket 的单资产最近成交接口规定,无成交时返回价格字符
|
||||
|
||||
### CLOB 历史价格曲线
|
||||
|
||||
旧版 CLOB 客户端通过 `GET /prices-history` 发送结果资产 ID(参数名 market)。它可以传 interval,或传 startTs 和 endTs。时间戳以 Unix 秒计,fidelity 以分钟计,省略时本地采用 1 分钟。返回对象的 history 数组按时间升序排列,t 为秒,p 为 JSON 数字。[CLOB OpenAPI](https://docs.polymarket.com/api-reference/markets/get-prices-history) · [SDK 请求和响应测试](https://github.com/Polymarket/rs-clob-client-v2/blob/main/tests/clob.rs#L219-L294) · [HTTP 样例](examples/clob-http.http)
|
||||
旧版 CLOB 客户端用 market 指定结果资产。单侧盘口的生产曲线末点为 0.0005,同批中点为 0.0005、最近成交为 0.001。一个近端成交记录为空的资产也有连续的 0.5 历史点。取早于上市的绝对区间时,有历史的资产仍返回最近可用点;新资产在中点已经可查时也可能返回空曲线。[生产请求和对照样本](examples/public-clob-history.json) · [CLOB OpenAPI](https://docs.polymarket.com/api-reference/markets/get-prices-history)
|
||||
|
||||
本地以每个 UTC 采样窗口内最后一笔真实成交或模拟兑付为观测,保留其发生时刻。区间包含两端,只挂单的市场返回空数组。以第 121、149、245 秒的三个成交为例,1 分钟粒度返回第 149、245 秒两点,10 分钟粒度仅返回第 245 秒。市场作废时以实际裁决时刻记录 0.5。同毫秒的成交按执行序号排序,兑付优先。这个数据源、采样时刻和端点包含规则尚待生产逐点核对。
|
||||
本地用已经提交的盘口变更重建历史中点。到每个 UTC 采样边界,取该时刻之前(含该时刻)最近一次盘口;缺买侧用 0,缺卖侧用 1。没有挂单变化时延续上次观测。窗口之外也追加最近一分钟的可用点;若与窗口最后一点重合,可出现重复时间戳。模拟裁决后在裁决时刻以兑付值收尾。已裁决的旧盘若缺少裁决时间,只使用最后有时间记录的报价。升级旧库时从第一条可信报价开始,没有报价记录的资产从迁移时刻开始。生产采集存在秒级抖动,新盘首个采样的延迟、长期历史保留和裁决后取点尚未逐点验证。V2 的成交与兑付历史保留其独立数据源。
|
||||
|
||||
fidelity 以分钟为单位;省略或不大于零时按 1 分钟处理。相对窗口 1w 至少需要 5 分钟、1m 至少需要 10 分钟。显式起止时间最多相隔 15 天,多一秒即拒绝。单给 startTs 时读到当前;两个时间相等时仍可返回末点。合法但未上市的本地资产返回空数组。[HTTP 样例](examples/clob-http.http)
|
||||
|
||||
官方 SDK 仓库有用户在同一已裁决资产上记录了两种结果:max 窗口按 60 分钟查询返回空,按 720 分钟查询有历史;后来该用户用显式起止时间取得更细数据。本地可返回已保存的全部历史。这项生产覆盖差异仍然存在。[原报告及后续复查](https://github.com/Polymarket/py-clob-client/issues/216)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user