@fullstackcraftllc/floe 0.0.1 → 0.0.3
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.
- package/README.md +10 -210
- package/dist/adapters/index.js +64 -16
- package/dist/client/FloeClient.d.ts +411 -0
- package/dist/client/FloeClient.js +550 -0
- package/dist/client/brokers/SchwabClient.d.ts +2 -0
- package/dist/client/brokers/SchwabClient.js +6 -0
- package/dist/client/brokers/TradierClient.d.ts +393 -0
- package/dist/client/brokers/TradierClient.js +869 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +14 -1
- package/dist/types/index.d.ts +35 -0
- package/dist/utils/occ.d.ts +164 -0
- package/dist/utils/occ.js +203 -0
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -10,4 +10,9 @@ export { getIVSurfaces, getIVForStrike, } from './volatility';
|
|
|
10
10
|
export { smoothTotalVarianceSmile, } from './volatility/smoothing';
|
|
11
11
|
export { calculateGammaVannaCharmExposures, calculateSharesNeededToCover, } from './exposure';
|
|
12
12
|
export { cumulativeNormalDistribution, normalPDF, } from './utils/statistics';
|
|
13
|
+
export { buildOCCSymbol, parseOCCSymbol, generateStrikesAroundSpot, generateOCCSymbolsForStrikes, generateOCCSymbolsAroundSpot, } from './utils/occ';
|
|
14
|
+
export type { OCCSymbolParams, ParsedOCCSymbol, StrikeGenerationParams, } from './utils/occ';
|
|
15
|
+
export { FloeClient, Broker } from './client/FloeClient';
|
|
16
|
+
export { TradierClient } from './client/brokers/TradierClient';
|
|
17
|
+
export type { AggressorSide, IntradayTrade } from './client/brokers/TradierClient';
|
|
13
18
|
export { genericAdapter, schwabAdapter, ibkrAdapter, tdaAdapter, brokerAdapters, getAdapter, createOptionChain, } from './adapters';
|
package/dist/index.js
CHANGED
|
@@ -20,7 +20,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
20
20
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
21
21
|
};
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
-
exports.createOptionChain = exports.getAdapter = exports.brokerAdapters = exports.tdaAdapter = exports.ibkrAdapter = exports.schwabAdapter = exports.genericAdapter = exports.normalPDF = exports.cumulativeNormalDistribution = exports.calculateSharesNeededToCover = exports.calculateGammaVannaCharmExposures = exports.smoothTotalVarianceSmile = exports.getIVForStrike = exports.getIVSurfaces = exports.getTimeToExpirationInYears = exports.getMillisecondsToExpiration = exports.calculateImpliedVolatility = exports.calculateGreeks = exports.blackScholes = void 0;
|
|
23
|
+
exports.createOptionChain = exports.getAdapter = exports.brokerAdapters = exports.tdaAdapter = exports.ibkrAdapter = exports.schwabAdapter = exports.genericAdapter = exports.TradierClient = exports.Broker = exports.FloeClient = exports.generateOCCSymbolsAroundSpot = exports.generateOCCSymbolsForStrikes = exports.generateStrikesAroundSpot = exports.parseOCCSymbol = exports.buildOCCSymbol = exports.normalPDF = exports.cumulativeNormalDistribution = exports.calculateSharesNeededToCover = exports.calculateGammaVannaCharmExposures = exports.smoothTotalVarianceSmile = exports.getIVForStrike = exports.getIVSurfaces = exports.getTimeToExpirationInYears = exports.getMillisecondsToExpiration = exports.calculateImpliedVolatility = exports.calculateGreeks = exports.blackScholes = void 0;
|
|
24
24
|
// Core types
|
|
25
25
|
__exportStar(require("./types"), exports);
|
|
26
26
|
// Black-Scholes pricing and Greeks
|
|
@@ -45,6 +45,19 @@ Object.defineProperty(exports, "calculateSharesNeededToCover", { enumerable: tru
|
|
|
45
45
|
var statistics_1 = require("./utils/statistics");
|
|
46
46
|
Object.defineProperty(exports, "cumulativeNormalDistribution", { enumerable: true, get: function () { return statistics_1.cumulativeNormalDistribution; } });
|
|
47
47
|
Object.defineProperty(exports, "normalPDF", { enumerable: true, get: function () { return statistics_1.normalPDF; } });
|
|
48
|
+
// OCC symbol utilities
|
|
49
|
+
var occ_1 = require("./utils/occ");
|
|
50
|
+
Object.defineProperty(exports, "buildOCCSymbol", { enumerable: true, get: function () { return occ_1.buildOCCSymbol; } });
|
|
51
|
+
Object.defineProperty(exports, "parseOCCSymbol", { enumerable: true, get: function () { return occ_1.parseOCCSymbol; } });
|
|
52
|
+
Object.defineProperty(exports, "generateStrikesAroundSpot", { enumerable: true, get: function () { return occ_1.generateStrikesAroundSpot; } });
|
|
53
|
+
Object.defineProperty(exports, "generateOCCSymbolsForStrikes", { enumerable: true, get: function () { return occ_1.generateOCCSymbolsForStrikes; } });
|
|
54
|
+
Object.defineProperty(exports, "generateOCCSymbolsAroundSpot", { enumerable: true, get: function () { return occ_1.generateOCCSymbolsAroundSpot; } });
|
|
55
|
+
// Client
|
|
56
|
+
var FloeClient_1 = require("./client/FloeClient");
|
|
57
|
+
Object.defineProperty(exports, "FloeClient", { enumerable: true, get: function () { return FloeClient_1.FloeClient; } });
|
|
58
|
+
Object.defineProperty(exports, "Broker", { enumerable: true, get: function () { return FloeClient_1.Broker; } });
|
|
59
|
+
var TradierClient_1 = require("./client/brokers/TradierClient");
|
|
60
|
+
Object.defineProperty(exports, "TradierClient", { enumerable: true, get: function () { return TradierClient_1.TradierClient; } });
|
|
48
61
|
// Broker adapters
|
|
49
62
|
var adapters_1 = require("./adapters");
|
|
50
63
|
Object.defineProperty(exports, "genericAdapter", { enumerable: true, get: function () { return adapters_1.genericAdapter; } });
|
package/dist/types/index.d.ts
CHANGED
|
@@ -63,10 +63,37 @@ export interface Greeks {
|
|
|
63
63
|
/** Ultima: Rate of change of volga with respect to volatility */
|
|
64
64
|
ultima: number;
|
|
65
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Normalized ticker data structure (broker-agnostic)
|
|
68
|
+
*/
|
|
69
|
+
export interface NormalizedTicker {
|
|
70
|
+
/** Underlying symbol */
|
|
71
|
+
symbol: string;
|
|
72
|
+
/** Current spot price of the underlying (mid of bid/ask, or last trade) */
|
|
73
|
+
spot: number;
|
|
74
|
+
/** Current bid price */
|
|
75
|
+
bid: number;
|
|
76
|
+
/** Current bid size */
|
|
77
|
+
bidSize: number;
|
|
78
|
+
/** Current ask price */
|
|
79
|
+
ask: number;
|
|
80
|
+
/** Current ask size */
|
|
81
|
+
askSize: number;
|
|
82
|
+
/** Last traded price */
|
|
83
|
+
last: number;
|
|
84
|
+
/** Cumulative volume for the day */
|
|
85
|
+
volume: number;
|
|
86
|
+
/** Timestamp of the quote in milliseconds */
|
|
87
|
+
timestamp: number;
|
|
88
|
+
}
|
|
66
89
|
/**
|
|
67
90
|
* Normalized option data structure (broker-agnostic)
|
|
68
91
|
*/
|
|
69
92
|
export interface NormalizedOption {
|
|
93
|
+
/** OCC-formatted option symbol (e.g., 'AAPL 230120C00150000') */
|
|
94
|
+
occSymbol: string;
|
|
95
|
+
/** Underlying ticker symbol */
|
|
96
|
+
underlying: string;
|
|
70
97
|
/** Strike price */
|
|
71
98
|
strike: number;
|
|
72
99
|
/** Expiration date (ISO 8601) */
|
|
@@ -77,8 +104,12 @@ export interface NormalizedOption {
|
|
|
77
104
|
optionType: OptionType;
|
|
78
105
|
/** Current bid price */
|
|
79
106
|
bid: number;
|
|
107
|
+
/** Current bid size */
|
|
108
|
+
bidSize: number;
|
|
80
109
|
/** Current ask price */
|
|
81
110
|
ask: number;
|
|
111
|
+
/** Current ask size */
|
|
112
|
+
askSize: number;
|
|
82
113
|
/** Mark (mid) price */
|
|
83
114
|
mark: number;
|
|
84
115
|
/** Last traded price */
|
|
@@ -87,8 +118,12 @@ export interface NormalizedOption {
|
|
|
87
118
|
volume: number;
|
|
88
119
|
/** Open interest */
|
|
89
120
|
openInterest: number;
|
|
121
|
+
/** Live open interest - calculated intraday by comparing using open interest as t=0 and comparing trades to the current NBBO for that option */
|
|
122
|
+
liveOpenInterest?: number;
|
|
90
123
|
/** Implied volatility (as decimal) */
|
|
91
124
|
impliedVolatility: number;
|
|
125
|
+
/** Timestamp of the quote in milliseconds */
|
|
126
|
+
timestamp: number;
|
|
92
127
|
/** Pre-calculated Greeks (optional) */
|
|
93
128
|
greeks?: Greeks;
|
|
94
129
|
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OCC (Options Clearing Corporation) symbol utilities
|
|
3
|
+
*
|
|
4
|
+
* OCC symbols follow the format: ROOT + YYMMDD + C/P + STRIKE
|
|
5
|
+
* Example: AAPL230120C00150000 = AAPL $150 Call expiring Jan 20, 2023
|
|
6
|
+
*/
|
|
7
|
+
import { OptionType } from '../types';
|
|
8
|
+
/**
|
|
9
|
+
* Parameters for building an OCC option symbol
|
|
10
|
+
*/
|
|
11
|
+
export interface OCCSymbolParams {
|
|
12
|
+
/** Underlying ticker symbol (e.g., 'AAPL', 'QQQ') */
|
|
13
|
+
symbol: string;
|
|
14
|
+
/** Expiration date (Date object or ISO string) */
|
|
15
|
+
expiration: Date | string;
|
|
16
|
+
/** Option type: 'call' or 'put' */
|
|
17
|
+
optionType: OptionType;
|
|
18
|
+
/** Strike price in dollars (e.g., 150.50) */
|
|
19
|
+
strike: number;
|
|
20
|
+
/**
|
|
21
|
+
* If true, pads the symbol to 6 characters with spaces (standard OCC format).
|
|
22
|
+
* If false (default), uses compact format without spaces (common for APIs).
|
|
23
|
+
* @default false
|
|
24
|
+
*/
|
|
25
|
+
padded?: boolean;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Parsed components of an OCC option symbol
|
|
29
|
+
*/
|
|
30
|
+
export interface ParsedOCCSymbol {
|
|
31
|
+
/** Underlying ticker symbol */
|
|
32
|
+
symbol: string;
|
|
33
|
+
/** Expiration date */
|
|
34
|
+
expiration: Date;
|
|
35
|
+
/** Option type */
|
|
36
|
+
optionType: OptionType;
|
|
37
|
+
/** Strike price in dollars */
|
|
38
|
+
strike: number;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Parameters for generating strikes around a spot price
|
|
42
|
+
*/
|
|
43
|
+
export interface StrikeGenerationParams {
|
|
44
|
+
/** Current spot/underlying price */
|
|
45
|
+
spot: number;
|
|
46
|
+
/** Number of strikes above spot to include */
|
|
47
|
+
strikesAbove?: number;
|
|
48
|
+
/** Number of strikes below spot to include */
|
|
49
|
+
strikesBelow?: number;
|
|
50
|
+
/** Strike increment (e.g., 1 for $1 increments, 5 for $5) */
|
|
51
|
+
strikeIncrementInDollars?: number;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Builds an OCC-formatted option symbol.
|
|
55
|
+
*
|
|
56
|
+
* @param params - The option parameters
|
|
57
|
+
* @returns OCC-formatted symbol string
|
|
58
|
+
*
|
|
59
|
+
* @remarks
|
|
60
|
+
* OCC format: ROOT(6 chars, left-padded) + YYMMDD + C/P + STRIKE(8 digits, price × 1000)
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const symbol = buildOCCSymbol({
|
|
65
|
+
* symbol: 'AAPL',
|
|
66
|
+
* expiration: new Date('2023-01-20'),
|
|
67
|
+
* optionType: 'call',
|
|
68
|
+
* strike: 150
|
|
69
|
+
* });
|
|
70
|
+
* // Returns: 'AAPL230120C00150000' (compact format, default)
|
|
71
|
+
*
|
|
72
|
+
* // With padded format (standard OCC)
|
|
73
|
+
* const symbol2 = buildOCCSymbol({
|
|
74
|
+
* symbol: 'QQQ',
|
|
75
|
+
* expiration: '2024-03-15',
|
|
76
|
+
* optionType: 'put',
|
|
77
|
+
* strike: 425.50,
|
|
78
|
+
* padded: true
|
|
79
|
+
* });
|
|
80
|
+
* // Returns: 'QQQ 240315P00425500'
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export declare function buildOCCSymbol(params: OCCSymbolParams): string;
|
|
84
|
+
/**
|
|
85
|
+
* Parses an OCC-formatted option symbol into its components.
|
|
86
|
+
* Supports both compact format (e.g., 'AAPL230120C00150000') and
|
|
87
|
+
* padded format (e.g., 'AAPL 230120C00150000').
|
|
88
|
+
*
|
|
89
|
+
* @param occSymbol - The OCC symbol to parse
|
|
90
|
+
* @returns Parsed symbol components
|
|
91
|
+
* @throws {Error} If the symbol format is invalid
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* // Compact format
|
|
96
|
+
* const parsed = parseOCCSymbol('AAPL230120C00150000');
|
|
97
|
+
* // Returns: { symbol: 'AAPL', expiration: Date, optionType: 'call', strike: 150 }
|
|
98
|
+
*
|
|
99
|
+
* // Padded format (21 chars)
|
|
100
|
+
* const parsed2 = parseOCCSymbol('AAPL 230120C00150000');
|
|
101
|
+
* // Returns: { symbol: 'AAPL', expiration: Date, optionType: 'call', strike: 150 }
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
export declare function parseOCCSymbol(occSymbol: string): ParsedOCCSymbol;
|
|
105
|
+
/**
|
|
106
|
+
* Generates an array of strike prices centered around a spot price.
|
|
107
|
+
*
|
|
108
|
+
* @param params - Strike generation parameters
|
|
109
|
+
* @returns Array of strike prices, sorted ascending
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const strikes = generateStrikesAroundSpot({
|
|
114
|
+
* spot: 450.25,
|
|
115
|
+
* strikesAbove: 10,
|
|
116
|
+
* strikesBelow: 10,
|
|
117
|
+
* strikeIncrement: 5
|
|
118
|
+
* });
|
|
119
|
+
* // Returns: [405, 410, 415, 420, 425, 430, 435, 440, 445, 450, 455, 460, 465, 470, 475, 480, 485, 490, 495, 500]
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
export declare function generateStrikesAroundSpot(params: StrikeGenerationParams): number[];
|
|
123
|
+
/**
|
|
124
|
+
* Generates OCC symbols for calls and puts across multiple strikes.
|
|
125
|
+
*
|
|
126
|
+
* @param symbol - Underlying ticker symbol
|
|
127
|
+
* @param expiration - Option expiration date
|
|
128
|
+
* @param strikes - Array of strike prices
|
|
129
|
+
* @param includeTypes - Which option types to include (default: both)
|
|
130
|
+
* @returns Array of OCC symbol strings
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* const symbols = generateOCCSymbolsForStrikes(
|
|
135
|
+
* 'QQQ',
|
|
136
|
+
* new Date('2024-01-19'),
|
|
137
|
+
* [495, 500, 505],
|
|
138
|
+
* ['call', 'put']
|
|
139
|
+
* );
|
|
140
|
+
* // Returns 6 symbols: 3 calls + 3 puts
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
export declare function generateOCCSymbolsForStrikes(symbol: string, expiration: Date | string, strikes: number[], includeTypes?: OptionType[]): string[];
|
|
144
|
+
/**
|
|
145
|
+
* Convenience function to generate OCC symbols around current spot price.
|
|
146
|
+
*
|
|
147
|
+
* @param symbol - Underlying ticker symbol
|
|
148
|
+
* @param expiration - Option expiration date
|
|
149
|
+
* @param spot - Current spot price
|
|
150
|
+
* @param options - Strike generation options
|
|
151
|
+
* @returns Array of OCC symbol strings for calls and puts
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* ```typescript
|
|
155
|
+
* // Generate 20 calls + 20 puts around QQQ at $502.50
|
|
156
|
+
* const symbols = generateOCCSymbolsAroundSpot('QQQ', '2024-01-19', 502.50, {
|
|
157
|
+
* strikesAbove: 10,
|
|
158
|
+
* strikesBelow: 10,
|
|
159
|
+
* strikeIncrement: 5
|
|
160
|
+
* });
|
|
161
|
+
* // Returns 42 symbols (21 strikes × 2 types)
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
export declare function generateOCCSymbolsAroundSpot(symbol: string, expiration: Date | string, spot: number, options?: Omit<StrikeGenerationParams, 'spot'>): string[];
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* OCC (Options Clearing Corporation) symbol utilities
|
|
4
|
+
*
|
|
5
|
+
* OCC symbols follow the format: ROOT + YYMMDD + C/P + STRIKE
|
|
6
|
+
* Example: AAPL230120C00150000 = AAPL $150 Call expiring Jan 20, 2023
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.buildOCCSymbol = buildOCCSymbol;
|
|
10
|
+
exports.parseOCCSymbol = parseOCCSymbol;
|
|
11
|
+
exports.generateStrikesAroundSpot = generateStrikesAroundSpot;
|
|
12
|
+
exports.generateOCCSymbolsForStrikes = generateOCCSymbolsForStrikes;
|
|
13
|
+
exports.generateOCCSymbolsAroundSpot = generateOCCSymbolsAroundSpot;
|
|
14
|
+
/**
|
|
15
|
+
* Builds an OCC-formatted option symbol.
|
|
16
|
+
*
|
|
17
|
+
* @param params - The option parameters
|
|
18
|
+
* @returns OCC-formatted symbol string
|
|
19
|
+
*
|
|
20
|
+
* @remarks
|
|
21
|
+
* OCC format: ROOT(6 chars, left-padded) + YYMMDD + C/P + STRIKE(8 digits, price × 1000)
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const symbol = buildOCCSymbol({
|
|
26
|
+
* symbol: 'AAPL',
|
|
27
|
+
* expiration: new Date('2023-01-20'),
|
|
28
|
+
* optionType: 'call',
|
|
29
|
+
* strike: 150
|
|
30
|
+
* });
|
|
31
|
+
* // Returns: 'AAPL230120C00150000' (compact format, default)
|
|
32
|
+
*
|
|
33
|
+
* // With padded format (standard OCC)
|
|
34
|
+
* const symbol2 = buildOCCSymbol({
|
|
35
|
+
* symbol: 'QQQ',
|
|
36
|
+
* expiration: '2024-03-15',
|
|
37
|
+
* optionType: 'put',
|
|
38
|
+
* strike: 425.50,
|
|
39
|
+
* padded: true
|
|
40
|
+
* });
|
|
41
|
+
* // Returns: 'QQQ 240315P00425500'
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
function buildOCCSymbol(params) {
|
|
45
|
+
const { symbol, expiration, optionType, strike, padded = false } = params;
|
|
46
|
+
// Format symbol - either padded to 6 chars or compact
|
|
47
|
+
const formattedSymbol = padded
|
|
48
|
+
? symbol.toUpperCase().padEnd(6, ' ')
|
|
49
|
+
: symbol.toUpperCase();
|
|
50
|
+
// Format expiration date as YYMMDD
|
|
51
|
+
// Use UTC methods to avoid timezone shifts when parsing ISO date strings
|
|
52
|
+
const expirationDate = typeof expiration === 'string' ? new Date(expiration + 'T12:00:00') : expiration;
|
|
53
|
+
const year = expirationDate.getFullYear().toString().slice(-2);
|
|
54
|
+
const month = (expirationDate.getMonth() + 1).toString().padStart(2, '0');
|
|
55
|
+
const day = expirationDate.getDate().toString().padStart(2, '0');
|
|
56
|
+
const dateString = `${year}${month}${day}`;
|
|
57
|
+
// Option type indicator
|
|
58
|
+
const typeIndicator = optionType === 'call' ? 'C' : 'P';
|
|
59
|
+
// Strike price: multiply by 1000, pad to 8 digits
|
|
60
|
+
const strikeInt = Math.round(strike * 1000);
|
|
61
|
+
const strikeString = strikeInt.toString().padStart(8, '0');
|
|
62
|
+
return `${formattedSymbol}${dateString}${typeIndicator}${strikeString}`;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Parses an OCC-formatted option symbol into its components.
|
|
66
|
+
* Supports both compact format (e.g., 'AAPL230120C00150000') and
|
|
67
|
+
* padded format (e.g., 'AAPL 230120C00150000').
|
|
68
|
+
*
|
|
69
|
+
* @param occSymbol - The OCC symbol to parse
|
|
70
|
+
* @returns Parsed symbol components
|
|
71
|
+
* @throws {Error} If the symbol format is invalid
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* // Compact format
|
|
76
|
+
* const parsed = parseOCCSymbol('AAPL230120C00150000');
|
|
77
|
+
* // Returns: { symbol: 'AAPL', expiration: Date, optionType: 'call', strike: 150 }
|
|
78
|
+
*
|
|
79
|
+
* // Padded format (21 chars)
|
|
80
|
+
* const parsed2 = parseOCCSymbol('AAPL 230120C00150000');
|
|
81
|
+
* // Returns: { symbol: 'AAPL', expiration: Date, optionType: 'call', strike: 150 }
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
function parseOCCSymbol(occSymbol) {
|
|
85
|
+
// Find the option type indicator (C or P) which is always followed by 8 strike digits
|
|
86
|
+
// This works for both compact and padded formats
|
|
87
|
+
const typeMatch = occSymbol.match(/([CP])(\d{8})$/);
|
|
88
|
+
if (!typeMatch) {
|
|
89
|
+
throw new Error(`Invalid OCC symbol format: ${occSymbol}`);
|
|
90
|
+
}
|
|
91
|
+
const typeIndicator = typeMatch[1];
|
|
92
|
+
const strikeString = typeMatch[2];
|
|
93
|
+
// Everything before the type indicator should be: SYMBOL + YYMMDD
|
|
94
|
+
const prefix = occSymbol.slice(0, -9); // Remove C/P + 8 digits
|
|
95
|
+
// Last 6 characters of prefix are the date (YYMMDD)
|
|
96
|
+
if (prefix.length < 6) {
|
|
97
|
+
throw new Error(`Invalid OCC symbol format: ${occSymbol}`);
|
|
98
|
+
}
|
|
99
|
+
const dateString = prefix.slice(-6);
|
|
100
|
+
const symbol = prefix.slice(0, -6).trim();
|
|
101
|
+
if (symbol.length === 0) {
|
|
102
|
+
throw new Error(`Invalid OCC symbol: no ticker found in ${occSymbol}`);
|
|
103
|
+
}
|
|
104
|
+
// Parse date - use noon to avoid timezone edge cases
|
|
105
|
+
const year = 2000 + parseInt(dateString.slice(0, 2), 10);
|
|
106
|
+
const month = parseInt(dateString.slice(2, 4), 10) - 1; // 0-indexed
|
|
107
|
+
const day = parseInt(dateString.slice(4, 6), 10);
|
|
108
|
+
const expiration = new Date(year, month, day, 12, 0, 0);
|
|
109
|
+
// Validate date
|
|
110
|
+
if (isNaN(expiration.getTime())) {
|
|
111
|
+
throw new Error(`Invalid date in OCC symbol: ${dateString}`);
|
|
112
|
+
}
|
|
113
|
+
// Parse option type
|
|
114
|
+
const optionType = typeIndicator === 'C' ? 'call' : 'put';
|
|
115
|
+
// Parse strike (divide by 1000)
|
|
116
|
+
const strike = parseInt(strikeString, 10) / 1000;
|
|
117
|
+
return { symbol, expiration, optionType, strike };
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Generates an array of strike prices centered around a spot price.
|
|
121
|
+
*
|
|
122
|
+
* @param params - Strike generation parameters
|
|
123
|
+
* @returns Array of strike prices, sorted ascending
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* const strikes = generateStrikesAroundSpot({
|
|
128
|
+
* spot: 450.25,
|
|
129
|
+
* strikesAbove: 10,
|
|
130
|
+
* strikesBelow: 10,
|
|
131
|
+
* strikeIncrement: 5
|
|
132
|
+
* });
|
|
133
|
+
* // Returns: [405, 410, 415, 420, 425, 430, 435, 440, 445, 450, 455, 460, 465, 470, 475, 480, 485, 490, 495, 500]
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
function generateStrikesAroundSpot(params) {
|
|
137
|
+
const { spot, strikesAbove = 10, strikesBelow = 10, strikeIncrementInDollars: strikeIncrement = 1 } = params;
|
|
138
|
+
// Find the nearest strike at or below spot
|
|
139
|
+
const baseStrike = Math.floor(spot / strikeIncrement) * strikeIncrement;
|
|
140
|
+
const strikes = [];
|
|
141
|
+
// Generate strikes below (including base)
|
|
142
|
+
for (let i = strikesBelow; i >= 0; i--) {
|
|
143
|
+
strikes.push(baseStrike - i * strikeIncrement);
|
|
144
|
+
}
|
|
145
|
+
// Generate strikes above
|
|
146
|
+
for (let i = 1; i <= strikesAbove; i++) {
|
|
147
|
+
strikes.push(baseStrike + i * strikeIncrement);
|
|
148
|
+
}
|
|
149
|
+
return strikes;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Generates OCC symbols for calls and puts across multiple strikes.
|
|
153
|
+
*
|
|
154
|
+
* @param symbol - Underlying ticker symbol
|
|
155
|
+
* @param expiration - Option expiration date
|
|
156
|
+
* @param strikes - Array of strike prices
|
|
157
|
+
* @param includeTypes - Which option types to include (default: both)
|
|
158
|
+
* @returns Array of OCC symbol strings
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```typescript
|
|
162
|
+
* const symbols = generateOCCSymbolsForStrikes(
|
|
163
|
+
* 'QQQ',
|
|
164
|
+
* new Date('2024-01-19'),
|
|
165
|
+
* [495, 500, 505],
|
|
166
|
+
* ['call', 'put']
|
|
167
|
+
* );
|
|
168
|
+
* // Returns 6 symbols: 3 calls + 3 puts
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
function generateOCCSymbolsForStrikes(symbol, expiration, strikes, includeTypes = ['call', 'put']) {
|
|
172
|
+
const symbols = [];
|
|
173
|
+
for (const strike of strikes) {
|
|
174
|
+
for (const optionType of includeTypes) {
|
|
175
|
+
symbols.push(buildOCCSymbol({ symbol, expiration, optionType, strike }));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return symbols;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Convenience function to generate OCC symbols around current spot price.
|
|
182
|
+
*
|
|
183
|
+
* @param symbol - Underlying ticker symbol
|
|
184
|
+
* @param expiration - Option expiration date
|
|
185
|
+
* @param spot - Current spot price
|
|
186
|
+
* @param options - Strike generation options
|
|
187
|
+
* @returns Array of OCC symbol strings for calls and puts
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```typescript
|
|
191
|
+
* // Generate 20 calls + 20 puts around QQQ at $502.50
|
|
192
|
+
* const symbols = generateOCCSymbolsAroundSpot('QQQ', '2024-01-19', 502.50, {
|
|
193
|
+
* strikesAbove: 10,
|
|
194
|
+
* strikesBelow: 10,
|
|
195
|
+
* strikeIncrement: 5
|
|
196
|
+
* });
|
|
197
|
+
* // Returns 42 symbols (21 strikes × 2 types)
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
function generateOCCSymbolsAroundSpot(symbol, expiration, spot, options) {
|
|
201
|
+
const strikes = generateStrikesAroundSpot({ spot, ...options });
|
|
202
|
+
return generateOCCSymbolsForStrikes(symbol, expiration, strikes);
|
|
203
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fullstackcraftllc/floe",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "Production-ready options analytics toolkit. Normalize broker data structures and calculate Black-Scholes, Greeks, and
|
|
3
|
+
"version": "0.0.3",
|
|
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",
|
|
7
7
|
"scripts": {
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"@types/node": "^20.0.0",
|
|
44
44
|
"jest": "^29.5.0",
|
|
45
45
|
"ts-jest": "^29.1.0",
|
|
46
|
-
"typescript": "^5.
|
|
46
|
+
"typescript": "^5.9.3"
|
|
47
47
|
},
|
|
48
48
|
"files": [
|
|
49
49
|
"dist",
|