diff --git a/schema.json b/schema.json index 115a3009..c2abb6ac 100644 --- a/schema.json +++ b/schema.json @@ -690,13 +690,13 @@ }, "additionalProperties": false }, - "HistoricalResult": { + "HistoricalHistoryResult": { "type": "array", "items": { - "$ref": "#/definitions/HistoricalRow" + "$ref": "#/definitions/HistoricalRowHistory" } }, - "HistoricalRow": { + "HistoricalRowHistory": { "type": "object", "properties": { "date": { @@ -730,6 +730,63 @@ "volume" ] }, + "HistoricalDividendsResult": { + "type": "array", + "items": { + "$ref": "#/definitions/HistoricalRowDividend" + } + }, + "HistoricalRowDividend": { + "type": "object", + "properties": { + "date": { + "yahooFinanceType": "date" + }, + "dividends": { + "yahooFinanceType": "number" + } + }, + "required": [ + "date", + "dividends" + ], + "additionalProperties": false + }, + "HistoricalStockSplitsResult": { + "type": "array", + "items": { + "$ref": "#/definitions/HistoricalRowStockSplit" + } + }, + "HistoricalRowStockSplit": { + "type": "object", + "properties": { + "date": { + "yahooFinanceType": "date" + }, + "stockSplits": { + "type": "string" + } + }, + "required": [ + "date", + "stockSplits" + ], + "additionalProperties": false + }, + "HistoricalResult": { + "anyOf": [ + { + "$ref": "#/definitions/HistoricalHistoryResult" + }, + { + "$ref": "#/definitions/HistoricalDividendsResult" + }, + { + "$ref": "#/definitions/HistoricalStockSplitsResult" + } + ] + }, "HistoricalOptions": { "type": "object", "properties": { @@ -779,6 +836,159 @@ ], "additionalProperties": false }, + "HistoricalOptionsEventsHistory": { + "type": "object", + "properties": { + "period1": { + "anyOf": [ + { + "yahooFinanceType": "date" + }, + { + "type": "string" + }, + { + "yahooFinanceType": "number" + } + ] + }, + "period2": { + "anyOf": [ + { + "yahooFinanceType": "date" + }, + { + "type": "string" + }, + { + "yahooFinanceType": "number" + } + ] + }, + "interval": { + "type": "string", + "enum": [ + "1d", + "1wk", + "1mo" + ] + }, + "events": { + "type": "string", + "const": "history" + }, + "includeAdjustedClose": { + "type": "boolean" + } + }, + "required": [ + "events", + "period1" + ], + "additionalProperties": false + }, + "HistoricalOptionsEventsDividends": { + "type": "object", + "properties": { + "period1": { + "anyOf": [ + { + "yahooFinanceType": "date" + }, + { + "type": "string" + }, + { + "yahooFinanceType": "number" + } + ] + }, + "period2": { + "anyOf": [ + { + "yahooFinanceType": "date" + }, + { + "type": "string" + }, + { + "yahooFinanceType": "number" + } + ] + }, + "interval": { + "type": "string", + "enum": [ + "1d", + "1wk", + "1mo" + ] + }, + "events": { + "type": "string", + "const": "dividends" + }, + "includeAdjustedClose": { + "type": "boolean" + } + }, + "required": [ + "events", + "period1" + ], + "additionalProperties": false + }, + "HistoricalOptionsEventsSplit": { + "type": "object", + "properties": { + "period1": { + "anyOf": [ + { + "yahooFinanceType": "date" + }, + { + "type": "string" + }, + { + "yahooFinanceType": "number" + } + ] + }, + "period2": { + "anyOf": [ + { + "yahooFinanceType": "date" + }, + { + "type": "string" + }, + { + "yahooFinanceType": "number" + } + ] + }, + "interval": { + "type": "string", + "enum": [ + "1d", + "1wk", + "1mo" + ] + }, + "events": { + "type": "string", + "const": "split" + }, + "includeAdjustedClose": { + "type": "boolean" + } + }, + "required": [ + "events", + "period1" + ], + "additionalProperties": false + }, "NamedParameters": { "type": "object", "properties": { diff --git a/src/modules/historical.spec.ts b/src/modules/historical.spec.ts index d16ed352..4f1e3622 100644 --- a/src/modules/historical.spec.ts +++ b/src/modules/historical.spec.ts @@ -42,6 +42,30 @@ describe("historical", () => { ).rejects.toThrow(/invalid date provided/); }); + it("dividends pass validation (#557)", async () => { + await yf.historical( + "MSFT", + { + period1: "2021-02-01", + period2: "2022-01-31", + events: "dividends", + }, + { devel: "historical-MSFT-dividends-2021-02-01-to-2022-01-31.csv" } + ); + }); + + it("splits pass validation (#557)", async () => { + await yf.historical( + "NVDA", + { + period1: "2021-02-01", + period2: "2022-01-31", + events: "split", + }, + { devel: "historical-NVDA-split-2021-02-01-to-2022-01-31.csv" } + ); + }); + describe("transformWith", () => { const yf = { _moduleExec: jest.fn(), historical }; // @ts-ignore: TODO diff --git a/src/modules/historical.ts b/src/modules/historical.ts index 3c04dd47..90a0f62e 100644 --- a/src/modules/historical.ts +++ b/src/modules/historical.ts @@ -5,9 +5,15 @@ import type { ModuleThis, } from "../lib/moduleCommon.js"; -export type HistoricalResult = Array; - -export interface HistoricalRow { +export type HistoricalHistoryResult = Array; +export type HistoricalDividendsResult = Array; +export type HistoricalStockSplitsResult = Array; +export type HistoricalResult = + | HistoricalHistoryResult + | HistoricalDividendsResult + | HistoricalStockSplitsResult; + +export interface HistoricalRowHistory { [key: string]: any; date: Date; open: number; @@ -18,6 +24,16 @@ export interface HistoricalRow { volume: number; } +export interface HistoricalRowDividend { + date: Date; + dividends: number; +} + +export interface HistoricalRowStockSplit { + date: Date; + stockSplits: string; +} + export interface HistoricalOptions { period1: Date | string | number; period2?: Date | string | number; @@ -26,6 +42,18 @@ export interface HistoricalOptions { includeAdjustedClose?: boolean; // true, } +export interface HistoricalOptionsEventsHistory extends HistoricalOptions { + events: "history"; +} + +export interface HistoricalOptionsEventsDividends extends HistoricalOptions { + events: "dividends"; +} + +export interface HistoricalOptionsEventsSplit extends HistoricalOptions { + events: "split"; +} + const queryOptionsDefaults: Omit = { interval: "1d", events: "history", @@ -35,9 +63,23 @@ const queryOptionsDefaults: Omit = { export default function historical( this: ModuleThis, symbol: string, - queryOptionsOverrides: HistoricalOptions, + queryOptionsOverrides: HistoricalOptionsEventsHistory, + moduleOptions?: ModuleOptionsWithValidateTrue +): Promise; + +export default function historical( + this: ModuleThis, + symbol: string, + queryOptionsOverrides: HistoricalOptionsEventsDividends, moduleOptions?: ModuleOptionsWithValidateTrue -): Promise; +): Promise; + +export default function historical( + this: ModuleThis, + symbol: string, + queryOptionsOverrides: HistoricalOptionsEventsSplit, + moduleOptions?: ModuleOptionsWithValidateTrue +): Promise; export default function historical( this: ModuleThis, @@ -52,6 +94,18 @@ export default function historical( queryOptionsOverrides: HistoricalOptions, moduleOptions?: ModuleOptions ): Promise { + let schemaKey; + if ( + !queryOptionsOverrides.events || + queryOptionsOverrides.events === "history" + ) + schemaKey = "#/definitions/HistoricalHistoryResult"; + else if (queryOptionsOverrides.events === "dividends") + schemaKey = "#/definitions/HistoricalDividendsResult"; + else if (queryOptionsOverrides.events === "split") + schemaKey = "#/definitions/HistoricalStockSplitsResult"; + else throw new Error("No such event type:" + queryOptionsOverrides.events); + return this._moduleExec({ moduleName: "historical", @@ -98,7 +152,7 @@ export default function historical( }, result: { - schemaKey: "#/definitions/HistoricalResult", + schemaKey, transformWith(result: any) { const filteredResults = []; const fieldCount = Object.keys(result[0]).length; diff --git a/tests/http/historical-MSFT-dividends-2021-02-01-to-2022-01-31.csv b/tests/http/historical-MSFT-dividends-2021-02-01-to-2022-01-31.csv new file mode 100644 index 00000000..6e9ed14a --- /dev/null +++ b/tests/http/historical-MSFT-dividends-2021-02-01-to-2022-01-31.csv @@ -0,0 +1,73 @@ +{ + "request": { + "url": "https://query2.finance.yahoo.com/v7/finance/download/MSFT?interval=1d&events=dividends&includeAdjustedClose=true&period1=1612137600&period2=1643587200" + }, + "response": { + "ok": true, + "status": 200, + "statusText": "OK", + "headers": { + "content-disposition": [ + "attachment; filename=MSFT.csv" + ], + "content-type": [ + "text/csv;charset=utf-8" + ], + "cache-control": [ + "private, max-age=10, stale-while-revalidate=20" + ], + "vary": [ + "Origin" + ], + "y-rid": [ + "65hfmd1hn48n1" + ], + "x-yahoo-request-id": [ + "65hfmd1hn48n1" + ], + "x-request-id": [ + "c8c8c432-8315-45cb-8e03-b5122e6365ea" + ], + "content-length": [ + "94" + ], + "x-envoy-upstream-service-time": [ + "24" + ], + "date": [ + "Mon, 14 Nov 2022 11:13:37 GMT" + ], + "server": [ + "ATS" + ], + "x-envoy-decorator-operation": [ + "finance-chart-api--mtls-production-ir2.finance-k8s.svc.yahoo.local:4080/*" + ], + "age": [ + "0" + ], + "strict-transport-security": [ + "max-age=15552000" + ], + "referrer-policy": [ + "no-referrer-when-downgrade" + ], + "x-frame-options": [ + "SAMEORIGIN" + ], + "connection": [ + "close" + ], + "expect-ct": [ + "max-age=31536000, report-uri=\"http://csp.yahoo.com/beacon/csp?src=yahoocom-expect-ct-report-only\"" + ], + "x-xss-protection": [ + "1; mode=block" + ], + "x-content-type-options": [ + "nosniff" + ] + }, + "body": "Date,Dividends\n2021-02-17,0.560000\n2021-05-19,0.560000\n2021-08-18,0.560000\n2021-11-17,0.620000" + } +} \ No newline at end of file diff --git a/tests/http/historical-NVDA-split-2021-02-01-to-2022-01-31.csv b/tests/http/historical-NVDA-split-2021-02-01-to-2022-01-31.csv new file mode 100644 index 00000000..4b5af585 --- /dev/null +++ b/tests/http/historical-NVDA-split-2021-02-01-to-2022-01-31.csv @@ -0,0 +1,73 @@ +{ + "request": { + "url": "https://query2.finance.yahoo.com/v7/finance/download/NVDA?interval=1d&events=split&includeAdjustedClose=true&period1=1612137600&period2=1643587200" + }, + "response": { + "ok": true, + "status": 200, + "statusText": "OK", + "headers": { + "content-disposition": [ + "attachment; filename=NVDA.csv" + ], + "content-type": [ + "text/csv;charset=utf-8" + ], + "cache-control": [ + "private, max-age=10, stale-while-revalidate=20" + ], + "vary": [ + "Origin" + ], + "y-rid": [ + "7vtkm61hn48n2" + ], + "x-yahoo-request-id": [ + "7vtkm61hn48n2" + ], + "x-request-id": [ + "4f1506fe-d8a9-4919-91f5-35f53ec1690d" + ], + "content-length": [ + "32" + ], + "x-envoy-upstream-service-time": [ + "14" + ], + "date": [ + "Mon, 14 Nov 2022 11:13:37 GMT" + ], + "server": [ + "ATS" + ], + "x-envoy-decorator-operation": [ + "finance-chart-api--mtls-production-ir2.finance-k8s.svc.yahoo.local:4080/*" + ], + "age": [ + "1" + ], + "strict-transport-security": [ + "max-age=15552000" + ], + "referrer-policy": [ + "no-referrer-when-downgrade" + ], + "x-frame-options": [ + "SAMEORIGIN" + ], + "connection": [ + "close" + ], + "expect-ct": [ + "max-age=31536000, report-uri=\"http://csp.yahoo.com/beacon/csp?src=yahoocom-expect-ct-report-only\"" + ], + "x-xss-protection": [ + "1; mode=block" + ], + "x-content-type-options": [ + "nosniff" + ] + }, + "body": "Date,Stock Splits\n2021-07-20,4:1" + } +} \ No newline at end of file