Sort public positions by entry price and profit metrics

This commit is contained in:
2026-12-05 04:58:22 +08:00
parent 76f721786c
commit 460bedac26
6 changed files with 166 additions and 39 deletions
+1 -1
View File
@@ -1456,6 +1456,6 @@ sequenceDiagram
H->>P: 钱包或市场、持仓状态和游标
P->>D: 清理到期任务并读取同一快照中的仓位与市场
D-->>P: 持仓、配对份额、裁决、目录及活动时间
P->>P: 按排序键保留一页、签署下一页游标
P->>P: 按有效排序键保留一页、签署下一页游标
P-->>H: data 和 pagination
```
+60 -14
View File
@@ -11,9 +11,10 @@ pub(super) fn round_money(micros: i128) -> i128 {
}
#[derive(Default)]
struct Cost {
pub(super) struct Cost {
held_lots: i128,
bought_lots: i128,
lifetime_principal: i128,
principal: i128,
entry_fees: i128,
realized: i128,
@@ -29,6 +30,7 @@ impl Cost {
self.held_lots += qty;
self.bought_lots += qty;
self.principal += amount;
self.lifetime_principal += amount;
self.entry_fees += fee;
self.realized -= fee;
}
@@ -45,16 +47,32 @@ impl Cost {
Some(())
}
fn fields(&self, current_value: Option<i128>) -> Value {
fn avg_price(&self) -> Option<i128> {
let (basis, lots) = if self.held_lots > 0 {
(self.principal, self.held_lots)
} else {
(self.lifetime_principal, self.bought_lots)
};
(lots > 0).then(|| {
let scale = i128::from(crate::lots::PER_SHARE);
round_money((basis * scale + lots / 2) / lots)
})
}
fn realized_pnl(&self) -> i128 {
round_money(self.realized)
}
fn unrealized_pnl(&self, value: Option<i128>) -> Option<i128> {
value.map(|v| round_money(v) - round_money(self.principal))
}
pub(super) fn fields(&self, current_value: Option<i128>) -> Value {
let basis = round_money(self.principal);
let realized = round_money(self.realized);
let unrealized = current_value.map(|v| round_money(v) - basis);
let avg = (self.held_lots > 0).then(|| {
let scale = i128::from(lots::PER_SHARE);
round_money((self.principal * scale + self.held_lots / 2) / self.held_lots)
});
let realized = self.realized_pnl();
let unrealized = self.unrealized_pnl(current_value);
json!({
"avg_price": avg.map(|v| decimal(v,DOLLAR)),
"avg_price": self.avg_price().map(|v| decimal(v,DOLLAR)),
"total_size": decimal(self.bought_lots,lots::PER_SHARE),
"entry_cost_usdc":decimal(basis,DOLLAR),
"entry_fees_usdc":decimal(self.entry_fees,DOLLAR),
@@ -66,6 +84,35 @@ impl Cost {
}
}
#[derive(Clone, Copy)]
pub(super) enum Metric {
Average,
Realized,
Unrealized,
Total,
}
impl Metric {
pub(super) fn parse(sort: &str) -> Option<Self> {
match sort {
"AVG_PRICE" => Some(Self::Average),
"REALIZED_PNL" => Some(Self::Realized),
"UNREALIZED_PNL" => Some(Self::Unrealized),
"TOTAL_PNL" => Some(Self::Total),
_ => None,
}
}
pub(super) fn rank(self, cost: &Cost, value: Option<i128>) -> Option<i128> {
match self {
Self::Average => cost.avg_price(),
Self::Realized => Some(cost.realized_pnl()),
Self::Unrealized => cost.unrealized_pnl(value),
Self::Total => cost.unrealized_pnl(value).map(|p| p + cost.realized_pnl()),
}
}
}
/// Reconstruct basis only when the recorded fills explain the actual holding.
pub(super) fn read(
db: &Connection,
@@ -73,8 +120,7 @@ pub(super) fn read(
market: &str,
outcome: &str,
current_size: i64,
current_value: Option<i128>,
) -> rusqlite::Result<Value> {
) -> rusqlite::Result<Option<Cost>> {
let mut stmt = db.prepare_cached(include_str!("economics.sql"))?;
let mut rows = stmt.query(params![account, market, outcome])?;
let mut cost = Cost::default();
@@ -84,15 +130,15 @@ pub(super) fn read(
.fill(&side, row.get(2)?, row.get(1)?, row.get(3)?)
.is_none()
{
return Ok(Value::Null);
return Ok(None);
}
}
if cost.bought_lots == 0
|| cost.held_lots * i128::from(lots::MICROS) != i128::from(current_size)
{
return Ok(Value::Null);
return Ok(None);
}
Ok(cost.fields(current_value))
Ok(Some(cost))
}
#[cfg(test)]
+62 -23
View File
@@ -214,24 +214,45 @@ impl Exchange {
if status == "CLOSED" && query.include_archived.is_some() {
return Err(invalid("include_archived is unavailable for CLOSED"));
}
let sort = query
let mut sort = query
.sort_by
.or_else(|| prior.map(|s| s.sort.clone()))
.unwrap_or_else(|| {
if status == "MERGEABLE" {
"TOKENS".into()
} else {
"CURRENT_VALUE".into()
match status.as_str() {
"MERGEABLE" => "TOKENS",
"CLOSED" if user.is_some() => "REALIZED_PNL",
_ => "CURRENT_VALUE",
}
.into()
});
if user.is_some()
&& matches!(status.as_str(), "CLOSED" | "REDEEMABLE_LOST")
&& sort == "AVG_PRICE"
{
return Err(invalid(
"sortBy=AVG_PRICE is not supported on a user's CLOSED or REDEEMABLE_LOST positions",
));
}
if user.is_some()
&& status == "CLOSED"
&& matches!(
sort.as_str(),
"TOKENS" | "CURRENT_VALUE" | "PRICE" | "UNREALIZED_PNL"
)
{
sort = "REALIZED_PNL".into();
}
let metric = super::economics::Metric::parse(&sort);
let direction = query
.sort_direction
.or_else(|| prior.map(|s| s.direction.clone()))
.unwrap_or_else(|| "DESC".into());
if !matches!(
sort.as_str(),
"CURRENT_VALUE" | "PRICE" | "TOKENS" | "TIMESTAMP"
) || !matches!(direction.as_str(), "ASC" | "DESC")
if (metric.is_none()
&& !matches!(
sort.as_str(),
"CURRENT_VALUE" | "PRICE" | "TOKENS" | "TIMESTAMP"
))
|| !matches!(direction.as_str(), "ASC" | "DESC")
{
return Err(invalid("invalid or unsupported position sort"));
}
@@ -284,16 +305,27 @@ impl Exchange {
}
let last_event_at: Option<i64> = row.get(13)?;
let first_entry_at: Option<i64> = row.get(14)?;
let rank = match selection.sort.as_str() {
"TIMESTAMP" => last_event_at,
"TOKENS" => Some(size),
"PRICE" => Some(mark.unwrap_or(0)),
_ => Some(
value
.unwrap_or(0)
.try_into()
.map_err(|_| MarketDataError::Unavailable)?,
),
let (rank, cost) = if let Some(metric) = metric {
let cost = super::economics::read(&tx, &account, &market, &outcome, size)?;
let rank = cost.as_ref().and_then(|c| metric.rank(c, value));
let rank = rank
.map(i64::try_from)
.transpose()
.map_err(|_| MarketDataError::Unavailable)?;
(rank, cost)
} else {
let rank = match selection.sort.as_str() {
"TIMESTAMP" => last_event_at,
"TOKENS" => Some(size),
"PRICE" => Some(mark.unwrap_or(0)),
_ => Some(
value
.unwrap_or(0)
.try_into()
.map_err(|_| MarketDataError::Unavailable)?,
),
};
(rank, None)
};
let rank =
rank.map_or(
@@ -340,7 +372,7 @@ impl Exchange {
"outcome": row.get::<_, String>(if yes {10} else {11})?,
"opposite_outcome": row.get::<_, String>(if yes {11} else {10})?,
"outcome_index": if yes {0} else {1}
}), size, value));
}), size, value, cost));
if page.len() > limit as usize + 1 {
page.pop_last();
}
@@ -361,10 +393,17 @@ impl Exchange {
None
};
let mut data = Vec::with_capacity(page.len());
for ((_, _, account, market, outcome), (mut row, size, value)) in page {
let fields = super::economics::read(&tx, &account, &market, &outcome, size, value)?;
for ((_, _, account, market, outcome), (mut row, size, value, cost)) in page {
let cost = if metric.is_some() {
cost
} else {
super::economics::read(&tx, &account, &market, &outcome, size)?
};
let row_fields = row.as_object_mut().expect("position is an object");
if let Value::Object(fields) = fields {
if let Some(cost) = cost {
let Value::Object(fields) = cost.fields(value) else {
unreachable!("cost fields form an object")
};
row_fields.extend(fields);
} else {
for key in [
+27
View File
@@ -4308,6 +4308,23 @@ fn public_positions_reconstructs_trade_basis_after_a_partial_sale() {
] {
e.execute(cmd).unwrap();
}
for command in [
json!({"op":"list","id":"other-list","market":"other","rules":"other result"}),
json!({"op":"split","id":"other-split","market":"other","account":"alice","qty":5}),
json!({"op":"open","id":"other-open","market":"other"}),
] {
e.execute(command).unwrap();
}
for (id, account, side, price, qty) in [
("other-ask", "alice", "sell", 60, 5),
("other-buy", "trader", "buy", 60, 5),
("other-exit", "trader", "sell", 40, 5),
("other-take", "bob", "buy", 40, 5),
] {
let mut command = order(id, account, side, price, qty);
command["market"] = json!("other");
e.execute(command).unwrap();
}
let read = |user| {
e.public_positions(serde_json::from_value(json!({"user":user})).unwrap())
.unwrap()
@@ -4320,5 +4337,15 @@ fn public_positions_reconstructs_trade_basis_after_a_partial_sale() {
assert_eq!(row["realized_pnl"], 1.2);
assert_eq!(row["unrealized_pnl"], 2.8);
assert_eq!(row["total_pnl"], 4);
let sorted = |params| e.public_positions(serde_json::from_value(params).unwrap());
let closed = sorted(json!({"user":"trader","status":"CLOSED","sort_by":"PRICE"})).unwrap();
assert_eq!(closed["data"][0]["realized_pnl"], -1);
assert_eq!(closed["data"][0]["avg_price"], 0.6);
assert!(sorted(json!({"user":"trader","status":"CLOSED","sort_by":"AVG_PRICE"})).is_err());
let market = sorted(json!({"condition":"e","sort_by":"AVG_PRICE","limit":1})).unwrap();
assert_eq!(market["data"][0]["proxy_wallet"], "bob");
let cursor = market["pagination"]["next_cursor"].as_str().unwrap();
let next = sorted(json!({"condition":"e","cursor":cursor})).unwrap();
assert_eq!(next["data"][0]["proxy_wallet"], "trader");
assert!(read("alice")["data"][0]["entry_cost_usdc"].is_null());
}
@@ -0,0 +1,12 @@
{
"AVG_PRICE": {"url":"https://data-api.polymarket.com/v2/positions?user=0xbc79e27f2c94124aeefb08d74b4e4fd3ac834e4d&limit=7&sort_by=AVG_PRICE","status":200,"date":"Fri, 04 Dec 2026 20:56:44 GMT","body":{"data":[{"token_id":"60488319664307907808238843684363128940030194181923783216789526080413999461490","status":"REDEEMABLE","avg_price":0.8899,"current_price":0.0,"current_value":0.0,"realized_pnl":-2.354,"unrealized_pnl":-10.0025,"total_pnl":-12.3565,"current_size":11.239},{"token_id":"98073668715220604074158446612974804298018754563117252335606186300079136804004","status":"REDEEMABLE","avg_price":0.8399,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.04,"unrealized_pnl":-4.9999,"total_pnl":-5.0399,"current_size":5.9523},{"token_id":"47077648217971757308720798158325917219531658951054235799631815254380459178462","status":"REDEEMABLE","avg_price":0.8299,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.0175,"unrealized_pnl":-2.0499,"total_pnl":-2.0674,"current_size":2.4698},{"token_id":"70856206809794487052236221713547822530745876986932371021154800731989645923129","status":"REDEEMABLE","avg_price":0.792,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.052,"unrealized_pnl":-4.9999,"total_pnl":-5.0519,"current_size":6.3125},{"token_id":"108440305255097343955145320114677386272816360228724390087556347183639101640370","status":"REDEEMABLE","avg_price":0.7899,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.0525,"unrealized_pnl":-4.9999,"total_pnl":-5.0524,"current_size":6.3291},{"token_id":"63234107892075973252894556579763050283535055859503091414443970074757771467750","status":"REDEEMABLE","avg_price":0.7899,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.0436,"unrealized_pnl":-4.1499,"total_pnl":-4.1935,"current_size":5.2531},{"token_id":"74071744813515333849424646391280658530446865984842804791705239676919358527019","status":"REDEEMABLE","avg_price":0.7898,"current_price":0.0,"current_value":0.0,"realized_pnl":2.7632,"unrealized_pnl":-10.0053,"total_pnl":-7.2421,"current_size":12.6673}],"pagination":{"limit":7,"offset":0,"has_more":true}}},
"REALIZED_PNL": {"url":"https://data-api.polymarket.com/v2/positions?user=0xbc79e27f2c94124aeefb08d74b4e4fd3ac834e4d&limit=7&sort_by=REALIZED_PNL","status":200,"date":"Fri, 04 Dec 2026 20:56:43 GMT","body":{"data":[{"token_id":"85950283731933994682282061396049926478670817100428423549552644750092884541497","status":"REDEEMABLE","avg_price":0.3021,"current_price":0.0,"current_value":0.0,"realized_pnl":9.075,"unrealized_pnl":-5.0007,"total_pnl":4.0743,"current_size":16.552},{"token_id":"74071744813515333849424646391280658530446865984842804791705239676919358527019","status":"REDEEMABLE","avg_price":0.7898,"current_price":0.0,"current_value":0.0,"realized_pnl":2.7632,"unrealized_pnl":-10.0053,"total_pnl":-7.2421,"current_size":12.6673},{"token_id":"63086340633867374166296916716767345159121388286341018941116828465599669741959","status":"REDEEMABLE","avg_price":0.5897,"current_price":0.0,"current_value":0.0,"realized_pnl":1.1111,"unrealized_pnl":-3.0007,"total_pnl":-1.8896,"current_size":5.0882},{"token_id":"548462943067324124336689254204000269948663684506167200680384187254947446784","status":"OPEN","avg_price":0.5799,"current_price":0.0005,"current_value":0.0043,"realized_pnl":0.0,"unrealized_pnl":-4.9957,"total_pnl":-4.9957,"current_size":8.6206},{"token_id":"874402096953853186496076533822258356735156175384049251327656694874617413633","status":"OPEN","avg_price":0.4599,"current_price":0.0005,"current_value":0.0054,"realized_pnl":0.0,"unrealized_pnl":-4.9946,"total_pnl":-4.9946,"current_size":10.8695},{"token_id":"49458764017597659514573234731940998982339359646087074270291874830372572595732","status":"REDEEMABLE","avg_price":0.0199,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.01,"unrealized_pnl":-0.2039,"total_pnl":-0.2139,"current_size":10.2},{"token_id":"2618591573832738162913679271927267207972968872239887962293396886053858554649","status":"REDEEMABLE","avg_price":0.7299,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.0149,"unrealized_pnl":-1.0999,"total_pnl":-1.1148,"current_size":1.5068}],"pagination":{"limit":7,"offset":0,"has_more":true}}},
"UNREALIZED_PNL": {"url":"https://data-api.polymarket.com/v2/positions?user=0xbc79e27f2c94124aeefb08d74b4e4fd3ac834e4d&limit=7&sort_by=UNREALIZED_PNL","status":200,"date":"Fri, 04 Dec 2026 20:56:43 GMT","body":{"data":[{"token_id":"49932611791842248277648635261891267637049151266001723816279042019553905945029","status":"OPEN","avg_price":0.0767,"current_price":0.0845,"current_value":1.5914,"realized_pnl":-2.7801,"unrealized_pnl":0.1462,"total_pnl":-2.6339,"current_size":18.8333},{"token_id":"49458764017597659514573234731940998982339359646087074270291874830372572595732","status":"REDEEMABLE","avg_price":0.0199,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.01,"unrealized_pnl":-0.2039,"total_pnl":-0.2139,"current_size":10.2},{"token_id":"2618591573832738162913679271927267207972968872239887962293396886053858554649","status":"REDEEMABLE","avg_price":0.7299,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.0149,"unrealized_pnl":-1.0999,"total_pnl":-1.1148,"current_size":1.5068},{"token_id":"54065802348264340781180366286468940160774623409772296945681445781417976930148","status":"REDEEMABLE","avg_price":0.4199,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.0412,"unrealized_pnl":-1.4199,"total_pnl":-1.4611,"current_size":3.3809},{"token_id":"41330338653304219709555029188568269032082472789433655579873196372491063819991","status":"REDEEMABLE","avg_price":0.5299,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.0452,"unrealized_pnl":-1.9199,"total_pnl":-1.9651,"current_size":3.6226},{"token_id":"110628429202387177247865174708140645662223783382160894070566377074936174195944","status":"REDEEMABLE","avg_price":0.01,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.099,"unrealized_pnl":-2.0,"total_pnl":-2.099,"current_size":200.0},{"token_id":"17081582571570121060773872712824914681371082713110525237465736969237633904493","status":"REDEEMABLE","avg_price":0.01,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.099,"unrealized_pnl":-2.0,"total_pnl":-2.099,"current_size":200.0}],"pagination":{"limit":7,"offset":0,"has_more":true}}},
"TOTAL_PNL": {"url":"https://data-api.polymarket.com/v2/positions?user=0xbc79e27f2c94124aeefb08d74b4e4fd3ac834e4d&limit=7&sort_by=TOTAL_PNL","status":200,"date":"Fri, 04 Dec 2026 20:56:43 GMT","body":{"data":[{"token_id":"85950283731933994682282061396049926478670817100428423549552644750092884541497","status":"REDEEMABLE","avg_price":0.3021,"current_price":0.0,"current_value":0.0,"realized_pnl":9.075,"unrealized_pnl":-5.0007,"total_pnl":4.0743,"current_size":16.552},{"token_id":"49458764017597659514573234731940998982339359646087074270291874830372572595732","status":"REDEEMABLE","avg_price":0.0199,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.01,"unrealized_pnl":-0.2039,"total_pnl":-0.2139,"current_size":10.2},{"token_id":"2618591573832738162913679271927267207972968872239887962293396886053858554649","status":"REDEEMABLE","avg_price":0.7299,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.0149,"unrealized_pnl":-1.0999,"total_pnl":-1.1148,"current_size":1.5068},{"token_id":"54065802348264340781180366286468940160774623409772296945681445781417976930148","status":"REDEEMABLE","avg_price":0.4199,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.0412,"unrealized_pnl":-1.4199,"total_pnl":-1.4611,"current_size":3.3809},{"token_id":"63086340633867374166296916716767345159121388286341018941116828465599669741959","status":"REDEEMABLE","avg_price":0.5897,"current_price":0.0,"current_value":0.0,"realized_pnl":1.1111,"unrealized_pnl":-3.0007,"total_pnl":-1.8896,"current_size":5.0882},{"token_id":"41330338653304219709555029188568269032082472789433655579873196372491063819991","status":"REDEEMABLE","avg_price":0.5299,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.0452,"unrealized_pnl":-1.9199,"total_pnl":-1.9651,"current_size":3.6226},{"token_id":"47077648217971757308720798158325917219531658951054235799631815254380459178462","status":"REDEEMABLE","avg_price":0.8299,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.0175,"unrealized_pnl":-2.0499,"total_pnl":-2.0674,"current_size":2.4698}],"pagination":{"limit":7,"offset":0,"has_more":true}}},
"closed_default": {"url":"https://data-api.polymarket.com/v2/positions?user=0xbc79e27f2c94124aeefb08d74b4e4fd3ac834e4d&limit=7&status=CLOSED","status":200,"date":"Fri, 04 Dec 2026 20:56:44 GMT","body":{"data":[{"token_id":"64620987201699714450904116765860966371545498125118736828680924169103742549403","status":"CLOSED","avg_price":0.2563,"current_price":1.0,"current_value":0.0,"realized_pnl":140.2894,"unrealized_pnl":0.0,"total_pnl":140.2894,"current_size":0.0},{"token_id":"75330054133399519852942415648680462506229215995803834711708101333223557605575","status":"CLOSED","avg_price":0.1397,"current_price":1.0,"current_value":0.0,"realized_pnl":109.1872,"unrealized_pnl":0.0,"total_pnl":109.1872,"current_size":0.0},{"token_id":"72888742932028991999466800216526896647951096092660478043802178498251332433741","status":"CLOSED","avg_price":0.487,"current_price":1.0,"current_value":0.0,"realized_pnl":90.3027,"unrealized_pnl":0.0,"total_pnl":90.3027,"current_size":0.0},{"token_id":"86843952992160665672559618028215743820638930427178683578703356678040675334819","status":"CLOSED","avg_price":0.4409,"current_price":1.0,"current_value":0.0,"realized_pnl":76.3429,"unrealized_pnl":0.0,"total_pnl":76.3429,"current_size":0.0},{"token_id":"55777510390318665492467086761401190607530819299865696880260617846434456780822","status":"CLOSED","avg_price":0.3715,"current_price":1.0,"current_value":0.0088,"realized_pnl":69.8901,"unrealized_pnl":0.0,"total_pnl":69.8901,"current_size":0.0088},{"token_id":"44996766623670111163185478315686536955427082108927675029181126638545537017422","status":"CLOSED","avg_price":0.1583,"current_price":1.0,"current_value":0.0,"realized_pnl":66.0998,"unrealized_pnl":0.0,"total_pnl":66.0998,"current_size":0.0},{"token_id":"651293246995138689769147052202072185438268329505895120511651147642598064129","status":"CLOSED","avg_price":0.1734,"current_price":1.0,"current_value":0.0,"realized_pnl":59.5228,"unrealized_pnl":0.0,"total_pnl":59.5228,"current_size":0.0}],"pagination":{"limit":7,"offset":0,"has_more":true}}},
"closed_price": {"url":"https://data-api.polymarket.com/v2/positions?user=0xbc79e27f2c94124aeefb08d74b4e4fd3ac834e4d&limit=7&status=CLOSED&sort_by=PRICE","status":200,"date":"Fri, 04 Dec 2026 20:56:44 GMT","body":{"data":[{"token_id":"64620987201699714450904116765860966371545498125118736828680924169103742549403","status":"CLOSED","avg_price":0.2563,"current_price":1.0,"current_value":0.0,"realized_pnl":140.2894,"unrealized_pnl":0.0,"total_pnl":140.2894,"current_size":0.0},{"token_id":"75330054133399519852942415648680462506229215995803834711708101333223557605575","status":"CLOSED","avg_price":0.1397,"current_price":1.0,"current_value":0.0,"realized_pnl":109.1872,"unrealized_pnl":0.0,"total_pnl":109.1872,"current_size":0.0},{"token_id":"72888742932028991999466800216526896647951096092660478043802178498251332433741","status":"CLOSED","avg_price":0.487,"current_price":1.0,"current_value":0.0,"realized_pnl":90.3027,"unrealized_pnl":0.0,"total_pnl":90.3027,"current_size":0.0},{"token_id":"86843952992160665672559618028215743820638930427178683578703356678040675334819","status":"CLOSED","avg_price":0.4409,"current_price":1.0,"current_value":0.0,"realized_pnl":76.3429,"unrealized_pnl":0.0,"total_pnl":76.3429,"current_size":0.0},{"token_id":"55777510390318665492467086761401190607530819299865696880260617846434456780822","status":"CLOSED","avg_price":0.3715,"current_price":1.0,"current_value":0.0088,"realized_pnl":69.8901,"unrealized_pnl":0.0,"total_pnl":69.8901,"current_size":0.0088},{"token_id":"44996766623670111163185478315686536955427082108927675029181126638545537017422","status":"CLOSED","avg_price":0.1583,"current_price":1.0,"current_value":0.0,"realized_pnl":66.0998,"unrealized_pnl":0.0,"total_pnl":66.0998,"current_size":0.0},{"token_id":"651293246995138689769147052202072185438268329505895120511651147642598064129","status":"CLOSED","avg_price":0.1734,"current_price":1.0,"current_value":0.0,"realized_pnl":59.5228,"unrealized_pnl":0.0,"total_pnl":59.5228,"current_size":0.0}],"pagination":{"limit":7,"offset":0,"has_more":true}}},
"closed_value": {"url":"https://data-api.polymarket.com/v2/positions?user=0xbc79e27f2c94124aeefb08d74b4e4fd3ac834e4d&limit=7&status=CLOSED&sort_by=CURRENT_VALUE","status":200,"date":"Fri, 04 Dec 2026 20:56:44 GMT","body":{"data":[{"token_id":"64620987201699714450904116765860966371545498125118736828680924169103742549403","status":"CLOSED","avg_price":0.2563,"current_price":1.0,"current_value":0.0,"realized_pnl":140.2894,"unrealized_pnl":0.0,"total_pnl":140.2894,"current_size":0.0},{"token_id":"75330054133399519852942415648680462506229215995803834711708101333223557605575","status":"CLOSED","avg_price":0.1397,"current_price":1.0,"current_value":0.0,"realized_pnl":109.1872,"unrealized_pnl":0.0,"total_pnl":109.1872,"current_size":0.0},{"token_id":"72888742932028991999466800216526896647951096092660478043802178498251332433741","status":"CLOSED","avg_price":0.487,"current_price":1.0,"current_value":0.0,"realized_pnl":90.3027,"unrealized_pnl":0.0,"total_pnl":90.3027,"current_size":0.0},{"token_id":"86843952992160665672559618028215743820638930427178683578703356678040675334819","status":"CLOSED","avg_price":0.4409,"current_price":1.0,"current_value":0.0,"realized_pnl":76.3429,"unrealized_pnl":0.0,"total_pnl":76.3429,"current_size":0.0},{"token_id":"55777510390318665492467086761401190607530819299865696880260617846434456780822","status":"CLOSED","avg_price":0.3715,"current_price":1.0,"current_value":0.0088,"realized_pnl":69.8901,"unrealized_pnl":0.0,"total_pnl":69.8901,"current_size":0.0088},{"token_id":"44996766623670111163185478315686536955427082108927675029181126638545537017422","status":"CLOSED","avg_price":0.1583,"current_price":1.0,"current_value":0.0,"realized_pnl":66.0998,"unrealized_pnl":0.0,"total_pnl":66.0998,"current_size":0.0},{"token_id":"651293246995138689769147052202072185438268329505895120511651147642598064129","status":"CLOSED","avg_price":0.1734,"current_price":1.0,"current_value":0.0,"realized_pnl":59.5228,"unrealized_pnl":0.0,"total_pnl":59.5228,"current_size":0.0}],"pagination":{"limit":7,"offset":0,"has_more":true}}},
"closed_avg": {"url":"https://data-api.polymarket.com/v2/positions?user=0xbc79e27f2c94124aeefb08d74b4e4fd3ac834e4d&limit=7&status=CLOSED&sort_by=AVG_PRICE","status":400,"date":"Fri, 04 Dec 2026 20:56:44 GMT","body":{"error":"sortBy=AVG_PRICE is not supported on a user's CLOSED or REDEEMABLE_LOST positions","code":"invalid_request","parameter":"sort_by","retryable":false}},
"lost_avg": {"url":"https://data-api.polymarket.com/v2/positions?user=0xbc79e27f2c94124aeefb08d74b4e4fd3ac834e4d&limit=7&status=REDEEMABLE_LOST&sort_by=AVG_PRICE","status":400,"date":"Fri, 04 Dec 2026 20:56:44 GMT","body":{"error":"sortBy=AVG_PRICE is not supported on a user's CLOSED or REDEEMABLE_LOST positions","code":"invalid_request","parameter":"sort_by","retryable":false}},
"realized_asc": {"url":"https://data-api.polymarket.com/v2/positions?user=0xbc79e27f2c94124aeefb08d74b4e4fd3ac834e4d&limit=7&sort_by=REALIZED_PNL&sort_direction=ASC","status":200,"date":"Fri, 04 Dec 2026 20:56:45 GMT","body":{"data":[{"token_id":"49932611791842248277648635261891267637049151266001723816279042019553905945029","status":"OPEN","avg_price":0.0767,"current_price":0.0845,"current_value":1.5914,"realized_pnl":-2.7801,"unrealized_pnl":0.1462,"total_pnl":-2.6339,"current_size":18.8333},{"token_id":"1188548547445269520707219060348990378779441344231931183199848184706112898196","status":"REDEEMABLE","avg_price":0.4867,"current_price":0.0,"current_value":0.0,"realized_pnl":-2.5321,"unrealized_pnl":-4.2565,"total_pnl":-6.7886,"current_size":8.745},{"token_id":"60488319664307907808238843684363128940030194181923783216789526080413999461490","status":"REDEEMABLE","avg_price":0.8899,"current_price":0.0,"current_value":0.0,"realized_pnl":-2.354,"unrealized_pnl":-10.0025,"total_pnl":-12.3565,"current_size":11.239},{"token_id":"892410193561107172609378383902568085199971906757190112078441271767169236993","status":"REDEEMABLE","avg_price":0.2521,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.5598,"unrealized_pnl":-15.0,"total_pnl":-15.5598,"current_size":59.4884},{"token_id":"95229129827670087220727100480106541228024373756540169057303133111627214417806","status":"REDEEMABLE","avg_price":0.4514,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.5483,"unrealized_pnl":-19.9999,"total_pnl":-20.5482,"current_size":44.3002},{"token_id":"93415544680886777702827441791006010294818009048547752679075414774317684068568","status":"REDEEMABLE","avg_price":0.4265,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.5375,"unrealized_pnl":-18.9999,"total_pnl":-19.5374,"current_size":44.5414},{"token_id":"62981699707887980466392119482265940477346988633684735288536223089899860523618","status":"REDEEMABLE","avg_price":0.7096,"current_price":0.0,"current_value":0.0,"realized_pnl":-0.43,"unrealized_pnl":-29.9998,"total_pnl":-30.4298,"current_size":42.2733}],"pagination":{"limit":7,"offset":0,"has_more":true}}}
}
+4 -1
View File
@@ -481,4 +481,7 @@ SDK 使用 `GET /neg-risk?token_id=…`,也可以从已缓存的 CLOB 市场
持仓活动时间按结果资产从成交、拆分、合并、互斥转换及赎回推导。start/end 按 Unix 秒包含两端,非正数表示无界。有界查询排除没有可信时间的行。可用 TIMESTAMP 排序,翻页时重发窗口。本地 first_entry_at 是最早有记录的取得时间。历史全额赎回缺少分侧数据,转换费用金库的入账时间及在平仓后重新入场的时间口径仍待核验。新赎回按实际烧掉的结果记录时间。裁决、挂单或撤单本身不增加持仓活动。[生产时间窗口样本](examples/public-position-time.json)
公开持仓的可重建成交仓位返回平均买入价、累计买入份额、剩余入场本金和费用、以及已实现和未实现盈亏。部分卖出按加权成本摊销本金和入场费用。费用从真实账本读取,发生时计入已实现盈亏。四位小数的本金、估值和盈亏与六位费用分开舍入。只有成交流水能解释当前持有量时才提供成本字段。涉及拆分、合并、赎回或互斥转换时现阶段返回 null。尚未核验外场买卖费用的逐笔归因、平仓再买后的成本口径或盈亏排序。[生产持仓和对应成交](examples/public-position-economics.json) · [官方持仓字段](https://data-api.polymarket.com/v2/openapi.json)
公开持仓的可重建成交仓位返回平均买入价、累计买入份额、剩余入场本金和费用、以及已实现和未实现盈亏。部分卖出按加权成本摊销本金和入场费用。费用从真实账本读取,发生时计入已实现盈亏。四位小数的本金、估值和盈亏与六位费用分开舍入。只有成交流水能解释当前持有量时才提供成本字段。涉及拆分、合并、赎回或互斥转换时现阶段返回 null。尚未核验外场买卖费用的逐笔归因、平仓再买后的成本口径。[生产持仓和对应成交](examples/public-position-economics.json) · [官方持仓字段](https://data-api.polymarket.com/v2/openapi.json)
持仓页可按 AVG_PRICE、REALIZED_PNL、UNREALIZED_PNL 和 TOTAL_PNL 排序,已知值按返回精度比较,缺少可重建成本的仓位排列在末尾。钱包 CLOSED 默认按已实现盈亏排序,给它 TOKENS、CURRENT_VALUE、PRICE 或 UNREALIZED_PNL 时也采用已实现盈亏。钱包 CLOSED 和 REDEEMABLE_LOST 拒绝 AVG_PRICE。游标绑定实际采用的排序。旧 CLOSED 游标使用被替代的排序键时要从第一页重新获取。生产未核验缺成本行的相对位置。[生产排序样本](examples/public-position-economics-sorts.json)