xtraderClient/src/api/priceHistory.ts
2026-03-15 21:06:37 +08:00

98 lines
2.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 价格历史接口GET /pmPriceHistory/getPmPriceHistoryPublic
* 用于 TradeDetail.vue 折线图数据
*/
import { buildQuery, get } from './request'
import type { ApiResponse } from './types'
import type { PageResult } from './types'
/** 单条价格历史(与 doc.json definitions["polymarket.PmPriceHistory"] 对齐) */
export interface PmPriceHistoryItem {
ID?: number
createdAt?: string
fidelity?: number
interval?: string
/** 市场标识 */
market?: string
/** 价格(通常 01 小数,前端展示可乘 100 为百分比) */
price?: number
/** 时间戳Unix 秒) */
time?: number
updatedAt?: string
}
/** GET /pmPriceHistory/getPmPriceHistoryPublic 请求参数 */
export interface GetPmPriceHistoryPublicParams {
/** 市场标识(必填,用于筛选) */
market: string
page?: number
pageSize?: number
/** 数据间隔 */
interval?: string
/** 时间筛选 */
time?: number
/** 创建时间范围 */
createdAtRange?: string[]
fidelity?: number
keyword?: string
order?: string
sort?: string
price?: number
}
/** 响应 data 为 PageResult<PmPriceHistoryItem> */
export interface PmPriceHistoryPublicResponse {
code: number
data?: PageResult<PmPriceHistoryItem>
msg: string
}
/**
* 获取价格历史(公开接口,无需鉴权)
* GET /pmPriceHistory/getPmPriceHistoryPublic
*/
export async function getPmPriceHistoryPublic(
params: GetPmPriceHistoryPublicParams,
config?: { headers?: Record<string, string> },
): Promise<PmPriceHistoryPublicResponse> {
const { market, page = 1, pageSize = 500, interval, time, createdAtRange, fidelity, keyword, order, sort, price } = params
const query = buildQuery({
market,
page,
pageSize,
interval,
time,
createdAtRange,
fidelity,
keyword,
order,
sort,
price,
})
return get<PmPriceHistoryPublicResponse>('/pmPriceHistory/getPmPriceHistoryPublic', query, config)
}
/** 图表单点格式 [timestamp_ms, value_0_100] */
export type ChartDataPoint = [number, number]
/**
* 将接口返回的 list 转为 ECharts 折线图数据
* - time 转为毫秒时间戳
* - price 若 ≤1 视为小数概率,乘 100否则视为 0100
*/
export function priceHistoryToChartData(list: PmPriceHistoryItem[]): ChartDataPoint[] {
if (!list?.length) return []
const out: ChartDataPoint[] = []
for (const item of list) {
const t = item.time
const p = item.price
if (t == null || p == null || !Number.isFinite(Number(t))) continue
const tsMs = Number(t) < 1e12 ? Number(t) * 1000 : Number(t)
const value = Number(p) <= 1 ? Number(p) * 100 : Number(p)
out.push([tsMs, value])
}
out.sort((a, b) => a[0] - b[0])
return out
}