orderflow-metrics 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. package/CHANGELOG.md +229 -0
  2. package/LICENSE +21 -0
  3. package/README.md +556 -0
  4. package/dist/bars.d.ts +67 -0
  5. package/dist/bars.d.ts.map +1 -0
  6. package/dist/bars.js +92 -0
  7. package/dist/bars.js.map +1 -0
  8. package/dist/classify.d.ts +25 -0
  9. package/dist/classify.d.ts.map +1 -0
  10. package/dist/classify.js +57 -0
  11. package/dist/classify.js.map +1 -0
  12. package/dist/covariance.d.ts +37 -0
  13. package/dist/covariance.d.ts.map +1 -0
  14. package/dist/covariance.js +72 -0
  15. package/dist/covariance.js.map +1 -0
  16. package/dist/efficiency.d.ts +24 -0
  17. package/dist/efficiency.d.ts.map +1 -0
  18. package/dist/efficiency.js +65 -0
  19. package/dist/efficiency.js.map +1 -0
  20. package/dist/entropy.d.ts +40 -0
  21. package/dist/entropy.d.ts.map +1 -0
  22. package/dist/entropy.js +73 -0
  23. package/dist/entropy.js.map +1 -0
  24. package/dist/execution.d.ts +40 -0
  25. package/dist/execution.d.ts.map +1 -0
  26. package/dist/execution.js +64 -0
  27. package/dist/execution.js.map +1 -0
  28. package/dist/fairvalue.d.ts +19 -0
  29. package/dist/fairvalue.d.ts.map +1 -0
  30. package/dist/fairvalue.js +27 -0
  31. package/dist/fairvalue.js.map +1 -0
  32. package/dist/hurst.d.ts +23 -0
  33. package/dist/hurst.d.ts.map +1 -0
  34. package/dist/hurst.js +71 -0
  35. package/dist/hurst.js.map +1 -0
  36. package/dist/imbalance.d.ts +12 -0
  37. package/dist/imbalance.d.ts.map +1 -0
  38. package/dist/imbalance.js +19 -0
  39. package/dist/imbalance.js.map +1 -0
  40. package/dist/impact.d.ts +59 -0
  41. package/dist/impact.d.ts.map +1 -0
  42. package/dist/impact.js +63 -0
  43. package/dist/impact.js.map +1 -0
  44. package/dist/index.d.ts +39 -0
  45. package/dist/index.d.ts.map +1 -0
  46. package/dist/index.js +25 -0
  47. package/dist/index.js.map +1 -0
  48. package/dist/jumps.d.ts +33 -0
  49. package/dist/jumps.d.ts.map +1 -0
  50. package/dist/jumps.js +63 -0
  51. package/dist/jumps.js.map +1 -0
  52. package/dist/liquidity.d.ts +18 -0
  53. package/dist/liquidity.d.ts.map +1 -0
  54. package/dist/liquidity.js +22 -0
  55. package/dist/liquidity.js.map +1 -0
  56. package/dist/moments.d.ts +20 -0
  57. package/dist/moments.d.ts.map +1 -0
  58. package/dist/moments.js +48 -0
  59. package/dist/moments.js.map +1 -0
  60. package/dist/ofi.d.ts +22 -0
  61. package/dist/ofi.d.ts.map +1 -0
  62. package/dist/ofi.js +25 -0
  63. package/dist/ofi.js.map +1 -0
  64. package/dist/online.d.ts +101 -0
  65. package/dist/online.d.ts.map +1 -0
  66. package/dist/online.js +205 -0
  67. package/dist/online.js.map +1 -0
  68. package/dist/orderbook.d.ts +30 -0
  69. package/dist/orderbook.d.ts.map +1 -0
  70. package/dist/orderbook.js +66 -0
  71. package/dist/orderbook.js.map +1 -0
  72. package/dist/rangevol.d.ts +40 -0
  73. package/dist/rangevol.d.ts.map +1 -0
  74. package/dist/rangevol.js +101 -0
  75. package/dist/rangevol.js.map +1 -0
  76. package/dist/scheduling.d.ts +19 -0
  77. package/dist/scheduling.d.ts.map +1 -0
  78. package/dist/scheduling.js +45 -0
  79. package/dist/scheduling.js.map +1 -0
  80. package/dist/semivar.d.ts +49 -0
  81. package/dist/semivar.d.ts.map +1 -0
  82. package/dist/semivar.js +59 -0
  83. package/dist/semivar.js.map +1 -0
  84. package/dist/shortfall.d.ts +42 -0
  85. package/dist/shortfall.d.ts.map +1 -0
  86. package/dist/shortfall.js +32 -0
  87. package/dist/shortfall.js.map +1 -0
  88. package/dist/simulate.d.ts +29 -0
  89. package/dist/simulate.d.ts.map +1 -0
  90. package/dist/simulate.js +36 -0
  91. package/dist/simulate.js.map +1 -0
  92. package/dist/spreadest.d.ts +38 -0
  93. package/dist/spreadest.d.ts.map +1 -0
  94. package/dist/spreadest.js +68 -0
  95. package/dist/spreadest.js.map +1 -0
  96. package/dist/types.d.ts +24 -0
  97. package/dist/types.d.ts.map +1 -0
  98. package/dist/types.js +9 -0
  99. package/dist/types.js.map +1 -0
  100. package/dist/volatility.d.ts +21 -0
  101. package/dist/volatility.d.ts.map +1 -0
  102. package/dist/volatility.js +33 -0
  103. package/dist/volatility.js.map +1 -0
  104. package/dist/vpin.d.ts +38 -0
  105. package/dist/vpin.d.ts.map +1 -0
  106. package/dist/vpin.js +81 -0
  107. package/dist/vpin.js.map +1 -0
  108. package/package.json +70 -0
  109. package/src/bars.ts +141 -0
  110. package/src/classify.ts +59 -0
  111. package/src/covariance.ts +72 -0
  112. package/src/efficiency.ts +64 -0
  113. package/src/entropy.ts +67 -0
  114. package/src/execution.ts +100 -0
  115. package/src/fairvalue.ts +33 -0
  116. package/src/hurst.ts +77 -0
  117. package/src/imbalance.ts +25 -0
  118. package/src/impact.ts +99 -0
  119. package/src/index.ts +87 -0
  120. package/src/jumps.ts +61 -0
  121. package/src/liquidity.ts +29 -0
  122. package/src/moments.ts +45 -0
  123. package/src/ofi.ts +46 -0
  124. package/src/online.ts +228 -0
  125. package/src/orderbook.ts +76 -0
  126. package/src/rangevol.ts +108 -0
  127. package/src/scheduling.ts +50 -0
  128. package/src/semivar.ts +67 -0
  129. package/src/shortfall.ts +69 -0
  130. package/src/simulate.ts +68 -0
  131. package/src/spreadest.ts +75 -0
  132. package/src/types.ts +26 -0
  133. package/src/volatility.ts +36 -0
  134. package/src/vpin.ts +98 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,229 @@
1
+ # Changelog
2
+
3
+ The format is based on [Keep a Changelog](https://keepachangelog.com/).
4
+ This project follows [Semantic Versioning](https://semver.org/); pre-1.0 the
5
+ public API may still change between minor versions.
6
+
7
+ ## [0.24.0] - 2026-08-25
8
+
9
+ ### Changed
10
+ - Packaging (no API changes): the npm package now ships **compiled ESM plus
11
+ type declarations** under `dist/` (built with TypeScript's
12
+ `rewriteRelativeImportExtensions`), so it imports cleanly in any Node ≥ 18
13
+ project without a type-stripping flag. `main`/`module`/`types`/`exports` point
14
+ at the compiled output; the TypeScript source under `src/` stays vendorable as
15
+ before. Every metric, signature and result is unchanged (129 TS tests still
16
+ green). (Python: unchanged at 0.12.0.)
17
+
18
+ ### Added
19
+ - npm publishing via GitHub Actions on `v*` tags (`npm-publish.yml`), with build
20
+ provenance — the TypeScript counterpart to the existing PyPI Trusted
21
+ Publishing workflow. First npm release of the package.
22
+
23
+ ## [0.23.0] - 2026-08-24
24
+
25
+ ### Added
26
+ - Realized covariance (`covariance`) — cross-asset co-movement from two aligned
27
+ return series: `realizedCovariance` (Σ xᵢyᵢ), `realizedCorrelation`
28
+ (Σxy / (√Σx²·√Σy²), in [−1, 1]) and `realizedBeta` (Σa·m / Σm², an asset's
29
+ sensitivity to a market). Model-free, high-frequency analogues of covariance /
30
+ correlation / beta. Test suite included. (Python: 0.12.0.)
31
+
32
+ ## [0.22.0] - 2026-08-22
33
+
34
+ ### Added
35
+ - Online / streaming estimators (`online`) — O(1)-per-update, constant-memory
36
+ stateful estimators for live pipelines, numerically stable (Welford / West,
37
+ not the naive Σx² form):
38
+ - `Welford` — running mean & variance over all data (sample and population).
39
+ - `Ewma` — exponentially weighted moving average of a level.
40
+ - `EwmaVariance` — RiskMetrics-style EWMA variance / volatility (λ decay).
41
+ - `RollingWindow` — mean & variance over a fixed trailing window, with O(1)
42
+ add/remove (West 1979).
43
+ Each is validated in tests to equal a batch recomputation at every step.
44
+ (Python: 0.11.0.)
45
+
46
+ ## [0.21.0] - 2026-08-21
47
+
48
+ ### Added
49
+ - Order-flow entropy (`entropy`): `shannonEntropy` (H = −Σpᵢ·log₂pᵢ, in bits,
50
+ of a count/probability vector), `normalizedEntropy` (H / log₂k, in [0,1]) and
51
+ `signEntropy` (the up/down balance of a return or signed-flow series, in [0,1]
52
+ bits). A predictability / informativeness measure — persistently low flow
53
+ entropy marks one-sided, potentially informed activity. Test suite included.
54
+ (Python: 0.10.0.)
55
+
56
+ ## [0.20.0] - 2026-08-20
57
+
58
+ ### Added
59
+ - Realized semivariance (`semivar`): `realizedSemivariance` (splits realized
60
+ variance into upside RS⁺ = Σrᵢ²·1{rᵢ>0} and downside RS⁻ = Σrᵢ²·1{rᵢ<0}, whose
61
+ sum is RV), `downsideVarianceRatio` (RS⁻ share of RV, in [0,1]) and
62
+ `signedJumpVariation` (RS⁺ − RS⁻, which keeps the *direction* of jump risk) —
63
+ Barndorff-Nielsen, Kinnebrock & Shephard (2010) and Patton & Shephard (2015).
64
+ Test suite included. (Python: 0.9.0.)
65
+
66
+ ## [0.19.0] - 2026-08-19
67
+
68
+ ### Added
69
+ - Jump detection via bipower variation (`jumps`): `bipowerVariation`
70
+ ((π/2)·Σ|rᵢ₋₁||rᵢ|, a jump-robust estimate of continuous variance),
71
+ `jumpVariation` (max(RV − BV, 0)) and `relativeJumpVariation` (jump share of
72
+ realized variance, in [0,1]) — Barndorff-Nielsen & Shephard (2004). Test suite
73
+ included. (Python: 0.8.0.)
74
+
75
+ ## [0.18.1] - 2026-08-18
76
+
77
+ ### Changed
78
+ - Docs only (no API changes): grouped **metric navigation** (a table of contents
79
+ by metric family) at the top of the README, and runnable, dependency-free
80
+ **quickstart examples** (TypeScript + Python) under `examples/`. Both tour the
81
+ library end to end on deterministic synthetic data and print identical output,
82
+ demonstrating TS/Python parity. (Python: 0.7.1.)
83
+
84
+ ## [0.18.0] - 2026-08-17
85
+
86
+ ### Added
87
+ - Realized higher moments (`moments`): `realizedSkewness` and `realizedKurtosis`
88
+ summarise the asymmetry and tail heaviness of an intraday return series
89
+ (Amaya, Christoffersen, Jacobs & Vasquez, 2015), scaled to be comparable
90
+ across sampling frequencies. Test suite included. (Python: 0.7.0.)
91
+
92
+ ## [0.17.0] - 2026-08-14
93
+
94
+ ### Added
95
+ - Hurst exponent (`hurst`): `hurstExponent` estimates long-memory from a return
96
+ series via rescaled-range (R/S) analysis — >0.5 persistent/trending, <0.5
97
+ mean-reverting, ~0.5 random walk. A companion to the market-efficiency
98
+ metrics (variance ratio, autocorrelation). Test suite included.
99
+ (Python: 0.6.0.)
100
+
101
+ ## [0.16.0] - 2026-08-14
102
+
103
+ ### Added
104
+ - Range-based volatility estimators (`rangevol`): `parkinsonVolatility`
105
+ (Parkinson 1980, high-low), `garmanKlassVolatility` (Garman-Klass 1980, OHLC),
106
+ `rogersSatchellVolatility` (Rogers-Satchell 1991, drift-independent), and
107
+ `yangZhangVolatility` (Yang-Zhang 2000, drift- and jump-robust). Each returns
108
+ per-bar volatility from OHLC candles. Test suite included. (Python: 0.5.0.)
109
+
110
+ ## [0.15.0] - 2026-08-13
111
+
112
+ ### Added
113
+ - OHLC bid-ask spread estimators (`spreadest`): `corwinSchultz` (Corwin &
114
+ Schultz, 2012 — the two-day high-low range estimator) and `abdiRanaldo`
115
+ (Abdi & Ranaldo, 2017 — close vs the high-low mid-range). Recover the
116
+ effective proportional spread from daily high/low/close with no tick data;
117
+ negative estimates are floored at 0. Test suite included. (Python: 0.4.0.)
118
+
119
+ ## [0.14.0] - 2026-08-12
120
+
121
+ ### Added
122
+ - Implementation shortfall & arrival slippage (`shortfall`):
123
+ `implementationShortfall` (Perold's decomposition into execution cost,
124
+ opportunity cost and fees) and `arrivalSlippageBps` (signed slippage vs the
125
+ arrival price, in basis points). Test suite included.
126
+
127
+ ## [0.13.0] - 2026-08-10
128
+
129
+ ### Added
130
+ - Market-impact models and trade markouts (`impact`): `squareRootImpact` (the
131
+ empirical Y·σ·√(Q/V) law), `linearPermanentImpact` and
132
+ `linearTemporaryImpact`, `almgrenChrissCost` (TWAP permanent/temporary cost
133
+ split), and `markout` / `averageMarkout` (post-trade adverse-selection drift,
134
+ signed by trade direction). Test suite included.
135
+
136
+ ## [0.12.0] - 2026-08-10
137
+
138
+ ### Added
139
+ - Information-driven bars (López de Prado, 2018): `tickBars`, `volumeBars` and
140
+ `dollarBars` aggregate a raw trade stream into OHLCV bars sampled on activity
141
+ rather than clock time — the natural upstream sampling layer for OFI,
142
+ imbalance, volatility and VPIN. Each `Bar` carries open/high/low/close,
143
+ volume, traded value, VWAP, tick count and signed buy/sell volume. Test
144
+ suite included.
145
+
146
+ ### Fixed
147
+ - `varianceRatio` now returns 1 (the documented degenerate value) when
148
+ `q === returns.length`. Previously that case produced a spurious 0 because
149
+ the variance of a single overlapping q-period return is zero.
150
+
151
+ ## [0.11.0] - 2026-08-05
152
+
153
+ ### Added
154
+ - Execution scheduling: `twap(totalSize, slices)` (even time-weighted slices,
155
+ exact sum) and `pov(totalSize, intervalVolumes, rate)` (percentage-of-volume
156
+ participation with shortfall handling). Test suite included.
157
+
158
+ ## [0.10.0] - 2026-08-05
159
+
160
+ ### Added
161
+ - `simulateMarketOrder(book, side, size)` — sweep an `OrderBook` with a market
162
+ order: volume-weighted fill price, slippage vs mid (bps), unfilled remainder
163
+ and per-level fills. Read-only. Test suite included.
164
+
165
+ ## [0.9.0] - 2026-08-04
166
+
167
+ ### Added
168
+ - `OrderBook` — limit order book reconstruction from incremental level
169
+ updates: `bestBid`/`bestAsk`, `mid`, `spread`, `depth(side, n)` and
170
+ top-n `imbalance`. Size-0 updates remove a level. Test suite included.
171
+
172
+ ## [0.8.0] - 2026-08-04
173
+
174
+ ### Added
175
+ - Market-efficiency diagnostics: `autocorrelation(returns, lag)` and
176
+ `varianceRatio(returns, q)` (Lo-MacKinlay: <1 mean-reverting, ~1 random
177
+ walk, >1 trending). Test suite included.
178
+
179
+ ## [0.7.0] - 2026-08-02
180
+
181
+ ### Added
182
+ - Realized volatility: `realizedVariance` (Σ rᵢ²), `realizedVolatility`
183
+ (√ of it) and `annualizedVolatility` (scaled by periods per year). Test
184
+ suite included.
185
+
186
+ ## [0.6.0] - 2026-08-01
187
+
188
+ ### Added
189
+ - `amihudIlliquidity` — Amihud (2002) illiquidity, the average of
190
+ |return| / volume across periods (price move per unit of traded volume).
191
+ Test suite included.
192
+
193
+ ## [0.5.0] - 2026-07-31
194
+
195
+ ### Added
196
+ - Trade-sign classification: `tickRule` and `leeReady` (Lee-Ready 1991) infer
197
+ buyer/seller-initiated trades from prints, so OFI / imbalance / VPIN inputs
198
+ can be signed. Test suite included.
199
+
200
+ ## [0.4.0] - 2026-07-30
201
+
202
+ ### Added
203
+ - Fair-value helpers: `weightedMid` (imbalance-weighted mid / simple
204
+ micro-price), `mid`, and `relativeSpreadBps` (quoted spread in basis
205
+ points). Test suite included.
206
+
207
+ ## [0.3.0] - 2026-07-29
208
+
209
+ ### Added
210
+ - Execution-cost & price-impact metrics: `effectiveSpread`,
211
+ `effectiveHalfSpread`, `realizedSpread`, `priceImpact`, `kyleLambda`
212
+ (price impact per unit signed flow) and `rollSpread` (Roll's 1984
213
+ autocovariance estimator). Test suite included.
214
+
215
+ ## [0.2.0] - 2026-07-28
216
+
217
+ ### Added
218
+ - VPIN (Volume-Synchronized Probability of Informed Trading): `vpin`,
219
+ `bucketByVolume`, `bvcBuyFraction`, `standardNormalCdf` — with Bulk Volume
220
+ Classification and equal-volume bucketing.
221
+ - Test suite for VPIN, BVC and volume bucketing.
222
+
223
+ ## [0.1.0] - 2026-07-27
224
+
225
+ ### Added
226
+ - Initial release.
227
+ - Order Flow Imbalance (Cont–Kukanov–Stoikov): `ofi`, `ofiSeries`,
228
+ `ofiContribution`.
229
+ - Top-of-book depth imbalance and trade imbalance.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 RATE LTD (TwoWayMind)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.