@fullstackcraftllc/floe 0.0.14 → 0.0.15

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.
@@ -0,0 +1,26 @@
1
+ import { PriceObservation, RealizedVolatilityResult } from './types';
2
+ export type { PriceObservation, RealizedVolatilityResult } from './types';
3
+ /**
4
+ * Compute annualized realized volatility from price observations.
5
+ *
6
+ * Uses the standard quadratic variation estimator:
7
+ *
8
+ * QV = Σᵢ (ln(Pᵢ / Pᵢ₋₁))²
9
+ *
10
+ * Annualized as:
11
+ *
12
+ * σ_realized = √(QV × (N_year / elapsed_time))
13
+ *
14
+ * This function is stateless and tick-based: pass all observed prices
15
+ * and it computes from the full set. No windowing, no sampling — the
16
+ * consumer decides what observations to include. As the session
17
+ * progresses and more ticks arrive, the estimate naturally converges.
18
+ *
19
+ * Observations are sorted by timestamp internally, so order does not
20
+ * matter. Duplicate timestamps are preserved (both contribute).
21
+ * Zero or negative prices are filtered out.
22
+ *
23
+ * @param observations - Array of { price, timestamp } observations
24
+ * @returns Realized volatility result with annualized RV
25
+ */
26
+ export declare function computeRealizedVolatility(observations: PriceObservation[]): RealizedVolatilityResult;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.computeRealizedVolatility = computeRealizedVolatility;
4
+ const types_1 = require("../types");
5
+ /**
6
+ * Compute annualized realized volatility from price observations.
7
+ *
8
+ * Uses the standard quadratic variation estimator:
9
+ *
10
+ * QV = Σᵢ (ln(Pᵢ / Pᵢ₋₁))²
11
+ *
12
+ * Annualized as:
13
+ *
14
+ * σ_realized = √(QV × (N_year / elapsed_time))
15
+ *
16
+ * This function is stateless and tick-based: pass all observed prices
17
+ * and it computes from the full set. No windowing, no sampling — the
18
+ * consumer decides what observations to include. As the session
19
+ * progresses and more ticks arrive, the estimate naturally converges.
20
+ *
21
+ * Observations are sorted by timestamp internally, so order does not
22
+ * matter. Duplicate timestamps are preserved (both contribute).
23
+ * Zero or negative prices are filtered out.
24
+ *
25
+ * @param observations - Array of { price, timestamp } observations
26
+ * @returns Realized volatility result with annualized RV
27
+ */
28
+ function computeRealizedVolatility(observations) {
29
+ // Filter invalid observations and sort by timestamp
30
+ const valid = observations
31
+ .filter(o => o.price > 0 && isFinite(o.price) && isFinite(o.timestamp))
32
+ .sort((a, b) => a.timestamp - b.timestamp);
33
+ if (valid.length < 2) {
34
+ return emptyResult(valid);
35
+ }
36
+ // Compute sum of squared log returns
37
+ let quadraticVariation = 0;
38
+ let numReturns = 0;
39
+ for (let i = 1; i < valid.length; i++) {
40
+ const logReturn = Math.log(valid[i].price / valid[i - 1].price);
41
+ quadraticVariation += logReturn * logReturn;
42
+ numReturns++;
43
+ }
44
+ // Elapsed time
45
+ const firstTs = valid[0].timestamp;
46
+ const lastTs = valid[valid.length - 1].timestamp;
47
+ const elapsedMs = lastTs - firstTs;
48
+ const elapsedMinutes = elapsedMs / 60000;
49
+ const elapsedYears = elapsedMs / types_1.MILLISECONDS_PER_YEAR;
50
+ // Guard against zero elapsed time
51
+ if (elapsedYears <= 0) {
52
+ return emptyResult(valid);
53
+ }
54
+ // Annualize: σ² = QV × (year / elapsed)
55
+ const annualizedVariance = quadraticVariation / elapsedYears;
56
+ const realizedVolatility = Math.sqrt(annualizedVariance);
57
+ return {
58
+ realizedVolatility,
59
+ annualizedVariance,
60
+ quadraticVariation,
61
+ numObservations: valid.length,
62
+ numReturns,
63
+ elapsedMinutes,
64
+ elapsedYears,
65
+ firstObservation: firstTs,
66
+ lastObservation: lastTs,
67
+ };
68
+ }
69
+ function emptyResult(valid) {
70
+ return {
71
+ realizedVolatility: 0,
72
+ annualizedVariance: 0,
73
+ quadraticVariation: 0,
74
+ numObservations: valid.length,
75
+ numReturns: 0,
76
+ elapsedMinutes: 0,
77
+ elapsedYears: 0,
78
+ firstObservation: valid.length > 0 ? valid[0].timestamp : 0,
79
+ lastObservation: valid.length > 0 ? valid[valid.length - 1].timestamp : 0,
80
+ };
81
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * A single price observation for realized volatility computation.
3
+ */
4
+ export interface PriceObservation {
5
+ /** Price of the underlying (mid, last, or whatever the consumer provides) */
6
+ price: number;
7
+ /** Timestamp in milliseconds */
8
+ timestamp: number;
9
+ }
10
+ /**
11
+ * Result of computing realized volatility from price observations.
12
+ */
13
+ export interface RealizedVolatilityResult {
14
+ /** Annualized realized volatility as decimal (e.g. 0.18 = 18%) */
15
+ realizedVolatility: number;
16
+ /** Annualized realized variance */
17
+ annualizedVariance: number;
18
+ /** Raw quadratic variation: Σ (ln(Pᵢ/Pᵢ₋₁))² */
19
+ quadraticVariation: number;
20
+ /** Number of price observations used */
21
+ numObservations: number;
22
+ /** Number of returns computed (observations - 1) */
23
+ numReturns: number;
24
+ /** Elapsed time in minutes from first to last observation */
25
+ elapsedMinutes: number;
26
+ /** Elapsed time in years (for annualization reference) */
27
+ elapsedYears: number;
28
+ /** Timestamp of first observation */
29
+ firstObservation: number;
30
+ /** Timestamp of last observation */
31
+ lastObservation: number;
32
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fullstackcraftllc/floe",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "description": "Production-ready options analytics toolkit. Normalize broker data structures and calculate Black-Scholes, Greeks, and exposures with a clean, type-safe API. Built for trading platforms and fintech applications.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",