-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathopenapi.yaml
More file actions
564 lines (527 loc) · 18.9 KB
/
Copy pathopenapi.yaml
File metadata and controls
564 lines (527 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
openapi: '3.0.3'
info:
title: NeuroWealth Backend API
version: '1.0.0'
description: |
NeuroWealth autonomous DeFi portfolio manager backend.
Authentication: All protected endpoints require a `Bearer <JWT>` token in the
`Authorization` header, obtained from `POST /api/auth/login`.
servers:
- url: /api/v1
description: Versioned API base (alias of /api for current release)
- url: /api
description: API base
tags:
- name: Analytics
description: Portfolio performance and risk analytics
- name: Auth
description: Authentication and session management
- name: Portfolio
description: User portfolio management
# ─── Reusable components ──────────────────────────────────────────────────────
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
schemas:
# ── Shared primitives ────────────────────────────────────────────────────
Period:
type: string
enum: ['7d', '30d', '90d']
default: '30d'
description: Analysis time window. Maximum available history is 90 days.
InsufficientHistoryFlag:
type: boolean
description: |
True when the available snapshot history is shorter than the requested
window. Rows/responses with this flag set are excluded from leaderboard
rankings. Never silently serves a truncated window under the requested label.
NullableDecimal:
type: number
format: double
nullable: true
# ── Risk metrics ─────────────────────────────────────────────────────────
RiskMetrics:
type: object
description: |
Computed risk/performance statistics for a portfolio-value series.
**Null contract**: any metric that is not computable (insufficient samples,
zero variance, degenerate series) is returned as `null` — never as `0`,
`Infinity`, or `NaN`.
**VaR / CVaR estimators**:
- `varHistorical*` / `cvarHistorical*` — empirical (plain-historical),
sorts the observed return distribution. Preferred for DeFi portfolios.
- `varParametric*` — Gaussian assumption (mean + σ). Underestimates tail
risk for fat-tailed distributions; provided for comparison only.
All loss magnitudes are **positive numbers** (0.05 = 5% potential loss).
properties:
sampleCount:
type: integer
description: Number of period-return observations used in all computations.
annualisedVolatility:
allOf:
- $ref: '#/components/schemas/NullableDecimal'
description: |
Sample standard deviation of period returns × √(periodsPerYear).
null if fewer than 2 return observations.
sortinoRatio:
allOf:
- $ref: '#/components/schemas/NullableDecimal'
description: |
(Annualised mean return − MAR) / annualised downside deviation.
null if fewer than 2 observations or downside deviation is 0 (no losses).
downsideDeviation:
allOf:
- $ref: '#/components/schemas/NullableDecimal'
description: RMS of returns below MAR, annualised. null if no returns.
maxDrawdown:
allOf:
- $ref: '#/components/schemas/NullableDecimal'
description: |
Maximum peak-to-trough decline as a positive fraction (0.15 = 15%).
null if fewer than 2 value points.
maxDrawdownDuration:
nullable: true
type: integer
description: |
Number of observation steps from peak to trough in the max drawdown
episode. null if no drawdown occurred.
varHistorical95:
allOf:
- $ref: '#/components/schemas/NullableDecimal'
description: Historical VaR at 95% confidence (positive = potential loss).
varHistorical99:
allOf:
- $ref: '#/components/schemas/NullableDecimal'
description: Historical VaR at 99% confidence.
varParametric95:
allOf:
- $ref: '#/components/schemas/NullableDecimal'
description: Parametric (Gaussian) VaR at 95% confidence.
varParametric99:
allOf:
- $ref: '#/components/schemas/NullableDecimal'
description: Parametric (Gaussian) VaR at 99% confidence.
cvarHistorical95:
allOf:
- $ref: '#/components/schemas/NullableDecimal'
description: Historical CVaR (Expected Shortfall) at 95%.
cvarHistorical99:
allOf:
- $ref: '#/components/schemas/NullableDecimal'
description: Historical CVaR at 99%.
beta:
allOf:
- $ref: '#/components/schemas/NullableDecimal'
description: |
Beta vs an exogenous benchmark series. null when no benchmark is
provided (benchmark index ingestion is deferred).
periodsPerYear:
type: number
description: |
Inferred periods-per-year used for annualisation, derived from the
median inter-observation spacing. Robust to snapshot gaps.
required:
- sampleCount
- periodsPerYear
PortfolioRiskResponse:
type: object
properties:
userId:
type: string
format: uuid
requestedWindow:
$ref: '#/components/schemas/Period'
actualWindowDays:
type: integer
description: Days of data actually used (may be shorter than requested).
insufficientHistory:
$ref: '#/components/schemas/InsufficientHistoryFlag'
dataFrom:
nullable: true
type: string
format: date-time
description: ISO timestamp of the earliest snapshot included.
dataTo:
nullable: true
type: string
format: date-time
description: ISO timestamp of the latest snapshot included.
computedAt:
type: string
format: date-time
description: When these figures were computed (staleness signal).
source:
type: string
enum: ['precomputed', 'live']
description: Whether the response was served from the precomputed cache.
metrics:
allOf:
- $ref: '#/components/schemas/RiskMetrics'
- nullable: true
type: object
description: null when sampleCount is 0 (entirely un-funded history).
required:
- userId
- requestedWindow
- actualWindowDays
- insufficientHistory
- computedAt
- source
RollingVolPoint:
type: object
properties:
timestampMs:
type: integer
format: int64
description: Epoch milliseconds of the last return in this rolling window.
volatility:
nullable: true
type: number
format: double
description: Annualised volatility over the window. null if insufficient data.
required:
- timestampMs
- volatility
DrawdownPoint:
type: object
properties:
timestampMs:
type: integer
format: int64
drawdown:
type: number
format: double
description: Drawdown from the running peak as a positive fraction.
required:
- timestampMs
- drawdown
TimeseriesResponse:
type: object
properties:
userId:
type: string
format: uuid
requestedWindow:
$ref: '#/components/schemas/Period'
insufficientHistory:
$ref: '#/components/schemas/InsufficientHistoryFlag'
rollingVolatility:
type: array
items:
$ref: '#/components/schemas/RollingVolPoint'
drawdown:
type: array
items:
$ref: '#/components/schemas/DrawdownPoint'
computedAt:
type: string
format: date-time
required:
- userId
- requestedWindow
- insufficientHistory
- rollingVolatility
- drawdown
- computedAt
ValidationError:
type: object
properties:
error:
type: string
example: Validation error
details:
type: object
required:
- error
UnauthorizedError:
type: object
properties:
error:
type: string
example: Unauthorized
required:
- error
responses:
Unauthorized:
description: Missing or invalid JWT.
content:
application/json:
schema:
$ref: '#/components/schemas/UnauthorizedError'
# ─── Paths ────────────────────────────────────────────────────────────────────
paths:
# ── Risk endpoints ───────────────────────────────────────────────────────────
/analytics/risk:
get:
operationId: getPortfolioRisk
summary: Portfolio risk metrics
description: |
Returns VaR (95%/99%, historical and parametric), CVaR (95%/99%,
historical), Sortino ratio, downside deviation, max drawdown (+duration),
and annualised volatility for the authenticated user's portfolio.
**Ownership**: `userId` is taken from the JWT payload — you cannot read
another user's risk data via this endpoint.
**Retention boundary**: YieldSnapshot rows are hard-deleted after 90 days
by the snapshotter. Windows longer than the available history are
relabelled with `insufficientHistory: true` and `actualWindowDays` set to
the honest available span — never silently truncated.
**Caching**: If a precomputed row < 1 hour old exists, it is returned
directly (`source: "precomputed"`). Otherwise a live compute is performed
(`source: "live"`).
**Null contract**: metrics that cannot be computed return `null`, never
`0`, `Infinity`, or `NaN`.
tags: [Analytics]
security:
- bearerAuth: []
parameters:
- name: window
in: query
required: false
schema:
$ref: '#/components/schemas/Period'
description: Analysis time window.
responses:
'200':
description: Risk metrics for the authenticated user.
content:
application/json:
schema:
$ref: '#/components/schemas/PortfolioRiskResponse'
examples:
sufficient_history:
summary: User with 30d of data
value:
userId: "3f8e1c2a-4b5d-6e7f-8a9b-0c1d2e3f4a5b"
requestedWindow: "30d"
actualWindowDays: 30
insufficientHistory: false
dataFrom: "2026-07-21T00:00:00.000Z"
dataTo: "2026-08-20T00:00:00.000Z"
computedAt: "2026-08-20T12:00:00.000Z"
source: "precomputed"
metrics:
sampleCount: 720
annualisedVolatility: 0.12
sortinoRatio: 1.85
downsideDeviation: 0.045
maxDrawdown: 0.08
maxDrawdownDuration: 14
varHistorical95: 0.031
varHistorical99: 0.065
varParametric95: 0.028
varParametric99: 0.044
cvarHistorical95: 0.052
cvarHistorical99: 0.078
beta: null
periodsPerYear: 8760
insufficient_history:
summary: New user with < 7d data requesting 30d
value:
userId: "3f8e1c2a-4b5d-6e7f-8a9b-0c1d2e3f4a5b"
requestedWindow: "30d"
actualWindowDays: 3
insufficientHistory: true
dataFrom: "2026-08-17T00:00:00.000Z"
dataTo: "2026-08-20T00:00:00.000Z"
computedAt: "2026-08-20T12:00:00.000Z"
source: "live"
metrics: null
'400':
description: Invalid query parameters.
content:
application/json:
schema:
$ref: '#/components/schemas/ValidationError'
'401':
description: Missing or invalid JWT.
content:
application/json:
schema:
$ref: '#/components/schemas/UnauthorizedError'
'500':
description: Internal server error.
/analytics/risk/timeseries:
get:
operationId: getPortfolioRiskTimeseries
summary: Portfolio risk timeseries (rolling volatility + drawdown)
description: |
Returns graph-ready rolling annualised volatility and running-peak
drawdown series for the authenticated user's portfolio.
**Ownership**: `userId` is taken from the JWT payload.
**Null in rolling volatility**: points with fewer observations than
`rollingWindow` have `volatility: null`.
tags: [Analytics]
security:
- bearerAuth: []
parameters:
- name: window
in: query
required: false
schema:
$ref: '#/components/schemas/Period'
- name: rollingWindow
in: query
required: false
schema:
type: integer
minimum: 2
maximum: 30
default: 7
description: Number of observations per rolling volatility window.
responses:
'200':
description: Timeseries data for the authenticated user.
content:
application/json:
schema:
$ref: '#/components/schemas/TimeseriesResponse'
'400':
description: Invalid query parameters.
content:
application/json:
schema:
$ref: '#/components/schemas/ValidationError'
'401':
description: Missing or invalid JWT.
content:
application/json:
schema:
$ref: '#/components/schemas/UnauthorizedError'
'500':
description: Internal server error.
# ── Existing analytics endpoints ─────────────────────────────────────────────
/analytics/apy-history:
get:
operationId: getApyHistory
summary: APY snapshot history
description: |
Returns APY snapshots over time for the authenticated user's positions.
Graph-ready (sorted ascending by date).
tags: [Analytics]
security:
- bearerAuth: []
parameters:
- name: period
in: query
required: false
schema:
$ref: '#/components/schemas/Period'
responses:
'200':
description: APY history points.
content:
application/json:
schema:
type: object
properties:
userId:
type: string
format: uuid
period:
$ref: '#/components/schemas/Period'
points:
type: array
items:
type: object
properties:
date:
type: string
format: date
apy:
type: number
positionId:
type: string
'401':
$ref: '#/components/responses/Unauthorized'
/analytics/user-yield:
get:
operationId: getUserYield
summary: User yield summary
description: Returns cumulative and period yield earned by the authenticated user.
tags: [Analytics]
security:
- bearerAuth: []
parameters:
- name: period
in: query
required: false
schema:
$ref: '#/components/schemas/Period'
responses:
'200':
description: Yield summary.
content:
application/json:
schema:
type: object
properties:
userId:
type: string
format: uuid
period:
$ref: '#/components/schemas/Period'
totalYield:
type: number
periodYield:
type: number
averageApy:
type: number
points:
type: array
items:
type: object
'401':
$ref: '#/components/responses/Unauthorized'
/analytics/protocol-performance:
get:
operationId: getProtocolPerformance
summary: Protocol APY history
description: Returns historical APY rates per protocol. Public endpoint (no auth required).
tags: [Analytics]
security: []
parameters:
- name: period
in: query
required: false
schema:
$ref: '#/components/schemas/Period'
responses:
'200':
description: Protocol APY history grouped by protocol.
content:
application/json:
schema:
type: object
properties:
period:
$ref: '#/components/schemas/Period'
protocols:
type: array
items:
type: object
# ── Health ────────────────────────────────────────────────────────────────────
/health/live:
get:
operationId: getLiveness
summary: Liveness probe
description: Always returns 200 once the process is running.
tags: [Health]
security: []
responses:
'200':
description: Process alive.
/health/ready:
get:
operationId: getReadiness
summary: Readiness probe
description: Returns 200 only when all background services are healthy.
tags: [Health]
security: []
responses:
'200':
description: All services ready.
'503':
description: One or more services not ready.