Skip to content

Commit

Permalink
Fix validation for historical events {dividends,split} (fixes #557)
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed Nov 14, 2022
1 parent 78b166e commit 1586496
Show file tree
Hide file tree
Showing 5 changed files with 443 additions and 9 deletions.
216 changes: 213 additions & 3 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -690,13 +690,13 @@
},
"additionalProperties": false
},
"HistoricalResult": {
"HistoricalHistoryResult": {
"type": "array",
"items": {
"$ref": "#/definitions/HistoricalRow"
"$ref": "#/definitions/HistoricalRowHistory"
}
},
"HistoricalRow": {
"HistoricalRowHistory": {
"type": "object",
"properties": {
"date": {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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<typeof historical>": {
"type": "object",
"properties": {
Expand Down
24 changes: 24 additions & 0 deletions src/modules/historical.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 60 additions & 6 deletions src/modules/historical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ import type {
ModuleThis,
} from "../lib/moduleCommon.js";

export type HistoricalResult = Array<HistoricalRow>;

export interface HistoricalRow {
export type HistoricalHistoryResult = Array<HistoricalRowHistory>;
export type HistoricalDividendsResult = Array<HistoricalRowDividend>;
export type HistoricalStockSplitsResult = Array<HistoricalRowStockSplit>;
export type HistoricalResult =
| HistoricalHistoryResult
| HistoricalDividendsResult
| HistoricalStockSplitsResult;

export interface HistoricalRowHistory {
[key: string]: any;
date: Date;
open: number;
Expand All @@ -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;
Expand All @@ -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<HistoricalOptions, "period1"> = {
interval: "1d",
events: "history",
Expand All @@ -35,9 +63,23 @@ const queryOptionsDefaults: Omit<HistoricalOptions, "period1"> = {
export default function historical(
this: ModuleThis,
symbol: string,
queryOptionsOverrides: HistoricalOptions,
queryOptionsOverrides: HistoricalOptionsEventsHistory,
moduleOptions?: ModuleOptionsWithValidateTrue
): Promise<HistoricalHistoryResult>;

export default function historical(
this: ModuleThis,
symbol: string,
queryOptionsOverrides: HistoricalOptionsEventsDividends,
moduleOptions?: ModuleOptionsWithValidateTrue
): Promise<HistoricalResult>;
): Promise<HistoricalDividendsResult>;

export default function historical(
this: ModuleThis,
symbol: string,
queryOptionsOverrides: HistoricalOptionsEventsSplit,
moduleOptions?: ModuleOptionsWithValidateTrue
): Promise<HistoricalStockSplitsResult>;

export default function historical(
this: ModuleThis,
Expand All @@ -52,6 +94,18 @@ export default function historical(
queryOptionsOverrides: HistoricalOptions,
moduleOptions?: ModuleOptions
): Promise<any> {
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",

Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 1586496

Please sign in to comment.