Repository navigation
Expand file tree
/
Copy pathtwap.ts
More file actions
359 lines (308 loc) · 12 KB
/
Copy pathtwap.ts
File metadata and controls
359 lines (308 loc) · 12 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
import { pgPool } from '../db'
import { activeNetwork, type NetworkName } from '../config'
import type { DecimalFormat } from '../utils/decimals'
// ─── TWAP — Time-Weighted Average Price ───────────────────────────────────────
// Computed by averaging the midpoints of successive price points weighted by
// the time interval between them. This gives more weight to prices that prevailed
// for longer, reducing the impact of brief manipulation spikes.
export interface TWAPResult {
twap: number | string
startTime: Date
endTime: Date
sampleCount: number
outlierRejected: number
filterMethod: string
network: NetworkName
}
export interface VWAPResult {
vwap: number | string
startTime: Date
endTime: Date
sampleCount: number
volumeTotal: number | string
outlierRejected: number
filterMethod: string
network: NetworkName
}
function median(arr: number[]): number {
const sorted = [...arr].sort((a, b) => a - b)
const mid = Math.floor(sorted.length / 2)
return sorted.length % 2 !== 0 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2
}
// ─── TWAP Computation ─────────────────────────────────────────────────────────
/**
* Compute Time-Weighted Average Price over a configurable window.
* Uses IQR outlier rejection by default.
*
* TWAP = Σ(price_i * Δt_i) / Σ(Δt_i) where Δt_i is the time to the next sample.
*
* Price points are fetched from the price_points table at a regular sample
* interval. If no trades occur within a sub-interval, the last known price
* is carried forward (prevents stale gap manipulation).
*/
export async function computeTWAP(
pairKey: string,
windowMinutes: number,
options?: {
sampleIntervalSeconds?: number
outlierMethod?: 'iqr' | 'modified_zscore'
network?: NetworkName
decimalFormat?: DecimalFormat
}
): Promise<TWAPResult> {
const sampleIntervalSeconds = options?.sampleIntervalSeconds ?? 60
const outlierMethod = options?.outlierMethod ?? 'iqr'
const network = options?.network ?? activeNetwork
const isString = options?.decimalFormat === 'string'
// Fetch price points within the window for the specific network, ordered by observed_at
const result = await pgPool.query(
`SELECT price::numeric as price, timestamp, COALESCE(observed_at, timestamp) as observed_at
FROM price_points
WHERE pair_key = $1
AND network = $3
AND COALESCE(observed_at, timestamp) > NOW() - ($2 || ' minutes')::interval
ORDER BY COALESCE(observed_at, timestamp) ASC`,
[pairKey, windowMinutes, network]
)
const rows = result.rows
if (rows.length === 0) {
return {
twap: isString ? '0' : 0,
startTime: new Date(Date.now() - windowMinutes * 60_000),
endTime: new Date(),
sampleCount: 0,
outlierRejected: 0,
filterMethod: outlierMethod,
network,
}
}
// Extract raw prices for outlier rejection
const rawPrices = rows.map((r: any) => parseFloat(r.price))
// Reject outliers by original index so timestamps and prices stay paired.
const { rejectedIndices: statisticalOutliers } = outlierMethod === 'iqr'
? rejectOutliersWithIndices(rawPrices)
: rejectOutliersModifiedZScoreWithIndices(rawPrices)
const rejectedIndices = new Set(statisticalOutliers)
rawPrices.forEach((price, i) => {
if (!Number.isFinite(price)) rejectedIndices.add(i)
})
const rejected = rejectedIndices.size
const validPoints = rows
.map((r: any, i: number) => ({
ts: new Date(r.observed_at ?? r.timestamp).getTime(),
price: rawPrices[i],
rejected: rejectedIndices.has(i),
}))
.filter((point: any) => !point.rejected)
.map(({ ts, price }: any) => ({ ts, price }))
.sort((a: any, b: any) => a.ts - b.ts)
const firstPointTime = rows[0].observed_at ?? rows[0].timestamp
const lastPointTime = rows[rows.length - 1].observed_at ?? rows[rows.length - 1].timestamp
if (validPoints.length === 0) {
return {
startTime: firstPointTime,
endTime: lastPointTime,
twap: isString ? '0' : 0,
sampleCount: rawPrices.length,
outlierRejected: rejected,
filterMethod: outlierMethod,
network,
}
}
// Build equi-spaced time grid and interpolate price at each sample point
const startTime = new Date(firstPointTime).getTime()
const endTime = new Date(lastPointTime).getTime()
const totalDurationMs = endTime - startTime
if (totalDurationMs <= 0) {
// Single data point — use its price
return {
startTime: firstPointTime,
endTime: lastPointTime,
twap: isString ? String(rows[0].price) : validPoints[0].price,
sampleCount: rawPrices.length,
outlierRejected: rejected,
filterMethod: outlierMethod,
network,
}
}
const sampleIntervalMs = sampleIntervalSeconds * 1000
const sampleCount = Math.max(1, Math.floor(totalDurationMs / sampleIntervalMs))
const interpolatedPrices: { price: number; weight: number }[] = []
for (let i = 0; i <= sampleCount; i++) {
const sampleTs = startTime + i * sampleIntervalMs
if (sampleTs > endTime) break
// Find the closest price at or before this sample time (last observation carry forward)
let interpolatedPrice = validPoints[0].price
for (let j = 0; j < validPoints.length; j++) {
if (validPoints[j].ts <= sampleTs) {
interpolatedPrice = validPoints[j].price
} else {
break
}
}
// Weight: time to next sample or to end
const nextSampleTs = Math.min(startTime + (i + 1) * sampleIntervalMs, endTime)
const weight = (nextSampleTs - sampleTs) / totalDurationMs
interpolatedPrices.push({ price: interpolatedPrice, weight })
}
// Compute TWAP as weighted average
const twap = interpolatedPrices.reduce((sum, p) => sum + p.price * p.weight, 0)
return {
twap: isString ? String(twap) : twap,
startTime: new Date(startTime),
endTime: new Date(endTime),
sampleCount: rawPrices.length,
outlierRejected: rejected,
filterMethod: outlierMethod,
network,
}
}
// ─── VWAP Computation ─────────────────────────────────────────────────────────
/**
* Compute Volume-Weighted Average Price over a configurable window.
* Uses IQR outlier rejection by default.
*
* VWAP = Σ(price_i * volume_i) / Σ(volume_i)
*
* Volume is base_volume. Only trades within the window contribute —
* no interpolation needed since each trade is naturally volume-weighted.
*/
export async function computeVWAP(
pairKey: string,
windowMinutes: number,
options?: {
source?: 'SDEX' | 'AMM'
outlierMethod?: 'iqr' | 'modified_zscore'
network?: NetworkName
decimalFormat?: DecimalFormat
}
): Promise<VWAPResult> {
const outlierMethod = options?.outlierMethod ?? 'iqr'
const network = options?.network ?? activeNetwork
const isString = options?.decimalFormat === 'string'
let query: string
let params: any[]
if (options?.source) {
query = `SELECT price::numeric as price, base_volume::numeric as volume, COALESCE(observed_at, timestamp) as observed_at, timestamp
FROM price_points
WHERE pair_key = $1
AND network = $4
AND source = $3
AND COALESCE(observed_at, timestamp) > NOW() - ($2 || ' minutes')::interval
ORDER BY COALESCE(observed_at, timestamp) ASC`
params = [pairKey, windowMinutes, options.source, network]
} else {
query = `SELECT price::numeric as price, base_volume::numeric as volume, COALESCE(observed_at, timestamp) as observed_at, timestamp
FROM price_points
WHERE pair_key = $1
AND network = $3
AND COALESCE(observed_at, timestamp) > NOW() - ($2 || ' minutes')::interval
ORDER BY COALESCE(observed_at, timestamp) ASC`
params = [pairKey, windowMinutes, network]
}
const result = await pgPool.query(query, params)
const rows = result.rows
if (rows.length === 0) {
return {
vwap: isString ? '0' : 0,
startTime: new Date(Date.now() - windowMinutes * 60_000),
endTime: new Date(),
sampleCount: 0,
volumeTotal: isString ? '0' : 0,
outlierRejected: 0,
filterMethod: outlierMethod,
network,
}
}
// For VWAP, outlier rejection considers the price dimension only
// (volume dimension is not subject to manipulation in the same way)
const rawPrices = rows.map((r: any) => parseFloat(r.price))
// Reject outliers on prices
const { rejectedIndices } = outlierMethod === 'iqr'
? rejectOutliersWithIndices(rawPrices)
: rejectOutliersModifiedZScoreWithIndices(rawPrices)
// Filter out outlier rows
const validRows = rows.filter((_: any, i: number) => !rejectedIndices.has(i))
const firstPointTime = rows[0].observed_at ?? rows[0].timestamp
const lastPointTime = rows[rows.length - 1].observed_at ?? rows[rows.length - 1].timestamp
if (validRows.length === 0) {
return {
startTime: firstPointTime,
endTime: lastPointTime,
vwap: isString ? '0' : 0,
sampleCount: rawPrices.length,
volumeTotal: isString ? '0' : 0,
outlierRejected: rejectedIndices.size,
filterMethod: outlierMethod,
network,
}
}
if (validRows.length === 1) {
const singleVol = parseFloat(validRows[0].volume)
return {
vwap: isString ? String(validRows[0].price) : parseFloat(validRows[0].price),
startTime: firstPointTime,
endTime: lastPointTime,
sampleCount: rawPrices.length,
volumeTotal: isString ? String(validRows[0].volume) : singleVol,
outlierRejected: rejectedIndices.size,
filterMethod: outlierMethod,
network,
}
}
let totalVolume = 0
let weightedPriceSum = 0
for (const row of validRows) {
const price = parseFloat(row.price)
const volume = parseFloat(row.volume)
weightedPriceSum += price * volume
totalVolume += volume
}
const vwap = totalVolume > 0 ? weightedPriceSum / totalVolume : 0
return {
startTime: firstPointTime,
endTime: lastPointTime,
vwap: isString ? String(vwap) : vwap,
sampleCount: rawPrices.length,
volumeTotal: isString ? String(totalVolume) : totalVolume,
outlierRejected: rejectedIndices.size,
filterMethod: outlierMethod,
network,
}
}
// ─── Helper: Outlier rejection returning rejected indices ─────────────────────
export const MIN_OUTLIER_SAMPLES = 4
export const IQR_MULTIPLIER = 1.5
export const MODIFIED_Z_SCORE_THRESHOLD = 3.5
export const MODIFIED_Z_SCORE_FACTOR = 0.6745
function rejectOutliersWithIndices(prices: number[]): { rejectedIndices: Set<number> } {
if (prices.length < MIN_OUTLIER_SAMPLES) return { rejectedIndices: new Set() }
const sorted = [...prices].sort((a, b) => a - b)
const n = sorted.length
const lowerHalf = sorted.slice(0, Math.floor(n / 2))
const upperHalf = sorted.slice(Math.ceil(n / 2))
const q1 = median(lowerHalf)
const q3 = median(upperHalf)
const iqr = q3 - q1
const lowerBound = q1 - IQR_MULTIPLIER * iqr
const upperBound = q3 + IQR_MULTIPLIER * iqr
const rejectedIndices = new Set<number>()
prices.forEach((p, i) => {
if (p < lowerBound || p > upperBound) rejectedIndices.add(i)
})
return { rejectedIndices }
}
function rejectOutliersModifiedZScoreWithIndices(prices: number[]): { rejectedIndices: Set<number> } {
if (prices.length < MIN_OUTLIER_SAMPLES) return { rejectedIndices: new Set() }
const sorted = [...prices].sort((a, b) => a - b)
const med = median(sorted)
const mad = median(sorted.map(v => Math.abs(v - med)))
if (mad === 0) return { rejectedIndices: new Set() }
const threshold = MODIFIED_Z_SCORE_THRESHOLD
const rejectedIndices = new Set<number>()
prices.forEach((p, i) => {
const zScore = MODIFIED_Z_SCORE_FACTOR * (p - med) / mad
if (Math.abs(zScore) > threshold) rejectedIndices.add(i)
})
return { rejectedIndices }
}