/
options.ts
117 lines (105 loc) · 2.77 KB
/
options.ts
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
import type {
ModuleOptions,
ModuleOptionsWithValidateTrue,
ModuleOptionsWithValidateFalse,
ModuleThis,
} from "../lib/moduleCommon.js";
import { Quote } from "./quote.js";
export interface OptionsResult {
[key: string]: any;
underlyingSymbol: string;
expirationDates: Date[];
strikes: number[];
hasMiniOptions: boolean;
quote: Quote;
options: Option[];
}
export interface Option {
[key: string]: any;
expirationDate: Date;
hasMiniOptions: boolean;
calls: CallOrPut[];
puts: CallOrPut[];
}
export interface CallOrPut {
[key: string]: any;
contractSymbol: string;
strike: number;
currency?: string;
lastPrice: number;
change: number;
percentChange?: number;
volume?: number;
openInterest?: number;
bid?: number;
ask?: number;
contractSize: "REGULAR";
expiration: Date;
lastTradeDate: Date;
impliedVolatility: number;
inTheMoney: boolean;
}
export interface OptionsOptions {
formatted?: boolean;
lang?: string;
region?: string;
date?: Date | number | string;
}
const queryOptionsDefaults: OptionsOptions = {
formatted: false,
lang: "en-US",
region: "US",
};
export default function options(
this: ModuleThis,
symbol: string,
queryOptionsOverrides: OptionsOptions,
moduleOptions?: ModuleOptionsWithValidateTrue
): Promise<OptionsResult>;
export default function options(
this: ModuleThis,
symbol: string,
queryOptionsOverrides: OptionsOptions,
moduleOptions?: ModuleOptionsWithValidateFalse
): Promise<any>;
export default function options(
this: ModuleThis,
symbol: string,
queryOptionsOverrides: OptionsOptions,
moduleOptions?: ModuleOptions
): Promise<any> {
return this._moduleExec({
moduleName: "options",
query: {
assertSymbol: symbol,
url: "https://${YF_QUERY_HOST}/v7/finance/options/" + symbol,
needsCrumb: true,
schemaKey: "#/definitions/OptionsOptions",
defaults: queryOptionsDefaults,
overrides: queryOptionsOverrides,
transformWith(queryOptions: OptionsOptions) {
const date = queryOptions.date;
if (date) {
// yfDate will convert valid number/string to Date.
if (date instanceof Date) {
// now we convert back to unix epoch in seconds for query
queryOptions.date = Math.floor(date.getTime() / 1000);
} else {
// yfDate didn't recognize it as a date.
throw new Error("Unsupported date type: " + date);
}
}
return queryOptions;
},
},
result: {
schemaKey: "#/definitions/OptionsResult",
transformWith(result: any) {
if (!result.optionChain)
throw new Error("Unexpected result: " + JSON.stringify(result));
return result.optionChain.result[0];
},
},
moduleOptions,
});
}