Skip to content

Commit

Permalink
Added stock price API
Browse files Browse the repository at this point in the history
  • Loading branch information
lprokein committed Dec 21, 2021
1 parent d887988 commit 6eacc98
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 1 deletion.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ You can also search for tickers of particular dataset
const tickers = await client.getTickerList("tesla", "job_listings");
```

### Stock price API

You can get stock price for specific ticker.

```js
const priceData = await client.getStockPrice("nasdaq:aapl");
```

You can also get price for crypto coins.

```js
const priceData = await client.getStockPrice("blockchain:eos");
```

You can specify history range.

```js
const priceData = await client.getStockPrice("nasdaq:aapl", {
startDate: "2021-11-01",
endDate: "2021-12-31",
});
```

### Query

Initialize `Query` with client object or API credentials.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@thinknum/client-js",
"version": "0.1.0",
"version": "0.1.1",
"description": "An API client for Thinknum Alternative Data written in Typescript",
"main": "dist/index.js",
"module": "dist/index.m.js",
Expand Down
45 changes: 45 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {version} from "../package.json";
import {
IDatasetListResponse,
IDatasetMetadataResponse,
IStockOverlayFetchResponse,
ITickersResponse,
} from "./dataTypes/response";
import {Paths} from "./paths";
import {validateRangeDateFormat} from "./utils";

export interface IClientCredentials {
clientId: string;
Expand Down Expand Up @@ -151,4 +153,47 @@ export class Client {
return data.queries[searchQuery];
});
}

/* Stock API
-------------------------------------------------------------------------*/

public async getStockPrice(ticker: string, range?: {startDate: string; endDate: string}) {
if (!ticker || ticker.trim().length === 0) {
return Promise.reject(new Error("Missing or invalid ticker"));
}

if (range) {
const hasStartDate = range.startDate && range.startDate.length > 0;
const hasEndDate = range.endDate && range.endDate.length > 0;

if (!hasStartDate || !hasEndDate) {
return Promise.reject(
new Error("Both startDate and endDate are required when specifying range."),
);
}

if (!validateRangeDateFormat(range.startDate)) {
return Promise.reject(new Error("Invalid startDate format. Please use YYYY-MM-DD format."));
}

if (!validateRangeDateFormat(range.endDate)) {
return Promise.reject(new Error("Invalid endDate format. Please use YYYY-MM-DD format."));
}
}

const data: {start_date?: string; end_date?: string} = {};
if (range?.startDate) {
data.start_date = range.startDate;
}
if (range?.endDate) {
data.end_date = range.endDate;
}

return this.requestData<IStockOverlayFetchResponse>(Paths.stock(ticker.trim()), {
method: "POST",
body: JSON.stringify(data),
}).then((data) => {
return data.results;
});
}
}
6 changes: 6 additions & 0 deletions src/dataTypes/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {IQuerySort, QueryFormat} from "./common";
import {ICompanyWithEntities} from "./datasetCompaniesEntities";
import {QueryOption, QueryRow} from "./datasetQueries";
import {DatasetField, IDatasetPreset, IFunctionsMetadata} from "./datasets";
import {IStockOverlayData} from "./stock";

export interface IDatasetListResponse {
datasets: IDataset[];
Expand Down Expand Up @@ -63,3 +64,8 @@ export interface ITableFetchQueryResponse extends IFetchQueryResponse {
group_fields: DatasetField[];
sort_fields: IQuerySort[];
}

export interface IStockOverlayFetchResponse {
count: number;
results: IStockOverlayData;
}
6 changes: 6 additions & 0 deletions src/dataTypes/stock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface IStockOverlayDataChunk {
date: string;
price: number;
}

export type IStockOverlayData = Array<IStockOverlayDataChunk>;
4 changes: 4 additions & 0 deletions src/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export class Paths {
return this.BASE_URL + `/datasets/${datasetId}/tickers/`;
}
static tickers = this.BASE_URL + "/tickers/";
static stock(ticker: string) {
return this.BASE_URL + "/companies/stock/" + ticker;
}

static query(datasetId: string) {
return this.BASE_URL + `/datasets/${datasetId}/query/`;
}
Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

const dashFormatRegex = /(\d{4})-(\d{2})-(\d{2})$/;

export function validateRangeDateFormat(date: string) {
return dashFormatRegex.test(date);
}

0 comments on commit 6eacc98

Please sign in to comment.