mirror of
https://ghfast.top/https://github.com/aeroxw/easy-tdx.git
synced 2026-09-12 20:24:16 +08:00
拼音搜索的底层依赖(首次需爬沪深 A 股 5000 条全名单,几十次 TDX 协议 往返,慢机器几十秒到超时)太重,反复优化(按需加载/遮罩/单飞/预热) 都无法兼顾'不阻塞核心行情请求'与'首次可用'。用户决定放弃此功能, 回到简单稳定的 6 位代码输入。 回退 e2bf29e..2523605 共 6 个 commit 的全部改动: - 删除 StockSearchInput / AppInitOverlay / useStockSearch - 移除 pypinyin 依赖、/security/search-index 端点、lifespan 预热 - SymbolPicker / StocksPicker 恢复为纯 6 位代码输入 - README / CHANGELOG 同步回退 代码状态等同 v1.18.1(一键寻优多进程并发)发布后的干净基线
333 lines
9.0 KiB
TypeScript
333 lines
9.0 KiB
TypeScript
// 后端 API 的 TypeScript 类型镜像。
|
||
// 与 src/easy_tdx/web/backtest_schemas.py 及 backtest router 的响应保持一致。
|
||
// 后端是唯一事实源;这里只做类型契约。
|
||
|
||
// ── 策略 schema(GET /api/v1/backtest/strategies) ───────────────────────────
|
||
|
||
export type ParamType = 'int' | 'float' | 'bool' | 'str'
|
||
|
||
export interface ParamSchema {
|
||
name: string
|
||
type: ParamType
|
||
default: number | string | boolean
|
||
label: string
|
||
min_value?: number
|
||
max_value?: number
|
||
choices?: string[]
|
||
description?: string
|
||
}
|
||
|
||
export interface StrategySchema {
|
||
name: string
|
||
label: string
|
||
description: string
|
||
params: ParamSchema[]
|
||
preset_grid?: Record<string, Array<number | string>>
|
||
}
|
||
|
||
export interface StrategiesResponse {
|
||
strategies: StrategySchema[]
|
||
count: number
|
||
}
|
||
|
||
// ── OHLCV 行情(GET /api/v1/bars) ────────────────────────────────────────────
|
||
|
||
export interface Bar {
|
||
datetime: string
|
||
open: number
|
||
high: number
|
||
low: number
|
||
close: number
|
||
vol: number
|
||
amount: number
|
||
}
|
||
|
||
export interface DataFrameResponse {
|
||
data: Record<string, unknown>[]
|
||
count: number
|
||
}
|
||
|
||
// ── 回测请求(POST /api/v1/backtest/run) ─────────────────────────────────────
|
||
|
||
export type ExecutionMode = 'next_open' | 'next_close'
|
||
export type Category = 'DAY' | 'WEEK' | 'MONTH' | 'MIN_5' | 'MIN_15' | 'MIN_30' | 'MIN_60'
|
||
|
||
export interface BacktestRequest {
|
||
strategy: string
|
||
params?: Record<string, number | string | boolean>
|
||
cash?: number
|
||
commission?: number
|
||
min_commission?: number
|
||
stamp_tax?: number
|
||
slippage?: number
|
||
execution?: ExecutionMode
|
||
ohlcv?: Bar[]
|
||
symbol?: string
|
||
category?: Category
|
||
count?: number
|
||
}
|
||
|
||
// ── 回测结果 ──────────────────────────────────────────────────────────────────
|
||
|
||
export interface Performance {
|
||
total_return: number
|
||
annual_return: number
|
||
max_drawdown: number
|
||
max_dd_duration: number
|
||
sharpe: number
|
||
sortino: number
|
||
calmar: number
|
||
total_trades: number
|
||
win_trades: number
|
||
lose_trades: number
|
||
rejected_trades: number
|
||
win_rate: number
|
||
profit_factor: number
|
||
avg_win: number
|
||
avg_loss: number
|
||
max_win: number
|
||
max_loss: number
|
||
avg_holding_days: number
|
||
volatility: number
|
||
}
|
||
|
||
export interface EquityPoint {
|
||
datetime: string
|
||
cash: number
|
||
position_value: number
|
||
total: number
|
||
drawdown: number
|
||
drawdown_pct: number
|
||
}
|
||
|
||
export interface Trade {
|
||
datetime: string
|
||
direction: 'BUY' | 'SELL'
|
||
size: number
|
||
price: number
|
||
commission: number
|
||
slippage: number
|
||
pnl: number
|
||
rejected: boolean
|
||
}
|
||
|
||
export interface BacktestResult {
|
||
performance: Performance
|
||
equity_curve: EquityPoint[]
|
||
trades: Trade[]
|
||
positions: Record<string, unknown>[]
|
||
config: Record<string, unknown>
|
||
}
|
||
|
||
// ── 后台任务(POST /api/v1/backtest/run/async + GET /tasks/{id}) ─────────────
|
||
|
||
export interface TaskSubmitResponse {
|
||
task_id: string
|
||
status: 'pending' | 'running'
|
||
}
|
||
|
||
export type TaskStatus = 'pending' | 'running' | 'done' | 'failed'
|
||
|
||
export interface TaskState {
|
||
task_id: string
|
||
status: TaskStatus
|
||
result: BacktestResult | PortfolioResult | OptimizeResult | OptimizeAllResult | null
|
||
error: string | null
|
||
description: string
|
||
elapsed: number
|
||
}
|
||
|
||
// ── 任务摘要(Phase 5 对比页) ────────────────────────────────────────────────
|
||
|
||
export interface TaskSummary {
|
||
task_id: string
|
||
status: TaskStatus
|
||
description: string
|
||
created_at: number
|
||
elapsed: number
|
||
}
|
||
|
||
export interface TaskListResponse {
|
||
tasks: TaskSummary[]
|
||
count: number
|
||
}
|
||
|
||
// ── 组合回测(Phase 3) ───────────────────────────────────────────────────────
|
||
|
||
export interface PortfolioBacktestRequest {
|
||
strategy: string
|
||
params?: Record<string, number | string | boolean>
|
||
cash?: number
|
||
commission?: number
|
||
slippage?: number
|
||
execution?: ExecutionMode
|
||
stocks: string[]
|
||
category?: Category
|
||
start_date?: string
|
||
end_date?: string
|
||
}
|
||
|
||
export interface PortfolioResult {
|
||
total_performance: {
|
||
total_return: number
|
||
annual_return: number
|
||
total_stocks: number
|
||
total_cash: number
|
||
}
|
||
individual_results: Record<string, BacktestResult>
|
||
equity_allocation: Record<string, number>
|
||
combined_equity: EquityPoint[]
|
||
}
|
||
|
||
// ── 参数网格寻优(Phase 4) ──────────────────────────────────────────────────
|
||
|
||
export interface OptimizeBacktestRequest {
|
||
strategy: string
|
||
cash?: number
|
||
commission?: number
|
||
slippage?: number
|
||
execution?: ExecutionMode
|
||
param_grid: Record<string, Array<number | string>>
|
||
ohlcv?: Bar[]
|
||
symbol?: string
|
||
category?: Category
|
||
count?: number
|
||
start_date?: string
|
||
end_date?: string
|
||
}
|
||
|
||
export interface GridPointResult {
|
||
params: Record<string, number | string>
|
||
total_return: number | null
|
||
sharpe: number | null
|
||
max_drawdown: number | null
|
||
total_trades: number
|
||
win_rate: number | null
|
||
profit_factor: number | null
|
||
}
|
||
|
||
export interface OptimizeHeatmap {
|
||
x_name: string
|
||
y_name: string
|
||
x: Array<number | string>
|
||
y: Array<number | string>
|
||
data: Array<[number, number, number | null]>
|
||
}
|
||
|
||
export interface OptimizeResult {
|
||
strategy: string
|
||
param_names: string[]
|
||
results: GridPointResult[]
|
||
best: GridPointResult | null
|
||
heatmap: OptimizeHeatmap | null
|
||
}
|
||
|
||
// ── 一键寻优所有策略(Phase 6) ──────────────────────────────────────────────
|
||
|
||
export interface OptimizeAllBacktestRequest {
|
||
cash?: number
|
||
commission?: number
|
||
slippage?: number
|
||
execution?: ExecutionMode
|
||
workers?: number
|
||
ohlcv?: Bar[]
|
||
symbol?: string
|
||
category?: Category
|
||
count?: number
|
||
start_date?: string
|
||
end_date?: string
|
||
}
|
||
|
||
export interface OptimizeAllRankEntry {
|
||
strategy: string
|
||
strategy_label: string
|
||
params: Record<string, number | string>
|
||
total_return: number | null
|
||
sharpe: number | null
|
||
max_drawdown: number | null
|
||
total_trades: number
|
||
win_rate: number | null
|
||
profit_factor: number | null
|
||
grid_points: number
|
||
}
|
||
|
||
export interface OptimizeAllResult {
|
||
ranking: OptimizeAllRankEntry[]
|
||
best: OptimizeAllRankEntry | null
|
||
per_strategy: Record<string, OptimizeAllRankEntry>
|
||
total_grid_points: number
|
||
}
|
||
|
||
// ── 错误响应(后端 ApiErrorResponse) ─────────────────────────────────────────
|
||
|
||
export interface ApiError {
|
||
error: string
|
||
detail: string
|
||
}
|
||
|
||
// ── 策略库(已保存策略,GET/POST/DELETE /api/v1/strategies) ─────────────────
|
||
|
||
/** 新建一条已保存策略的请求体(前端在回测结果区点「保存」时提交)。 */
|
||
export interface SavedStrategyCreate {
|
||
name: string
|
||
kind: 'single' | 'portfolio' | 'multi'
|
||
strategy: string
|
||
strategy_label?: string
|
||
params?: Record<string, number | string | boolean>
|
||
/** 标的上下文:single 存 symbol/category/start_date/end_date;portfolio 存 stocks;multi 存 items + cash/execution */
|
||
context?: Record<string, unknown>
|
||
/** 资金与成本配置(cash/commission/...) */
|
||
trade_config?: Record<string, unknown>
|
||
/** 保存时的成绩快照(total_return/sharpe/...) */
|
||
snapshot?: Record<string, unknown>
|
||
tags?: string[]
|
||
notes?: string
|
||
}
|
||
|
||
/** 一条已保存策略(响应模型,含 id 与时间戳)。 */
|
||
export interface SavedStrategy {
|
||
id: string
|
||
name: string
|
||
kind: 'single' | 'portfolio' | 'multi'
|
||
strategy: string
|
||
strategy_label: string
|
||
params: Record<string, number | string | boolean>
|
||
context: Record<string, unknown>
|
||
trade_config: Record<string, unknown>
|
||
snapshot: Record<string, unknown>
|
||
tags: string[]
|
||
notes: string
|
||
created_at: string
|
||
updated_at: string
|
||
app_version: string
|
||
}
|
||
|
||
export interface SavedStrategyListResponse {
|
||
strategies: SavedStrategy[]
|
||
count: number
|
||
}
|
||
|
||
// ── 多策略组合回测(资金分仓,POST /api/v1/backtest/multi-strategy/run/async) ──
|
||
|
||
/** 多策略组合的单个策略槽位(一个策略 + 参数 + 它要跑的原标的 + 日期)。 */
|
||
export interface MultiStrategyItem {
|
||
strategy: string
|
||
strategy_label?: string
|
||
params?: Record<string, number | string | boolean>
|
||
symbol: string
|
||
category?: Category
|
||
start_date?: string
|
||
end_date?: string
|
||
}
|
||
|
||
/** 多策略组合回测请求(各策略各拿 1/N 资金,结果结构同 PortfolioResult)。 */
|
||
export interface MultiStrategyBacktestRequest {
|
||
items: MultiStrategyItem[]
|
||
cash?: number
|
||
commission?: number
|
||
min_commission?: number
|
||
stamp_tax?: number
|
||
slippage?: number
|
||
execution?: ExecutionMode
|
||
}
|