The stockHistory
function fetches historical stock data from NSE India for a given stock symbol and time period. The data can include various metrics such as closing prices, opening prices, high, low, and volume.
declare const stockHistory: ({ symbol, startTimestamp, endTimestamp, resolution, onlyClose }: StockHistoryOptions) => Promise<ResponseMapperResult>;
- Fetch historical stock data for a given symbol.
- Customize the resolution of the data (e.g., minute, hour, day, week, month).
- Retrieve only the closing prices if needed.
- Flexible time range selection.
To use the Stock History Fetcher, simply install it via npm:
npm i @imdr/nse-stock-history
type ResponseMapperResult = {
t: number[];
c: number[];
o?: number[];
h?: number[];
l?: number[];
v?: number[];
};
- t: Array of timestamps.
- c: Array of closing prices.
- o (optional): Array of opening prices.
- h (optional): Array of high prices.
- l (optional): Array of low prices.
- v (optional): Array of trading volumes.
type Resolution = 1 | 3 | 5 | 10 | 15 | 30 | 60 | "4h" | "1d" | "1w" | "1m";
- 1, 3, 5, 10, 15, 30, 60: Minute resolutions.
- "4h": Four-hour resolution.
- "1d": Daily resolution.
- "1w": Weekly resolution.
- "1m": Monthly resolution.
type StockHistoryOptions = {
symbol: string;
resolution: Resolution;
startTimestamp: number;
endTimestamp: number;
onlyClose?: boolean;
};
- symbol: The stock symbol to fetch data for.
- resolution: The desired resolution of the data.
- startTimestamp: The start of the time range (timestamp example: 1609459200000 ).
- endTimestamp: The end of the time range (timestamp example: 1612137600000).
- onlyClose (optional): If true, only the closing prices will be fetched.
Here is an example of how to use the stockHistory function:
import stockHistory from 'your-library-path';
const options = {
symbol: 'RELIANCE',
resolution: '1d',
startTimestamp: 1609459200000, // January 1, 2021
endTimestamp: 1612137600000, // January 31, 2021
onlyClose: false,
};
stockHistory(options)
.then(data => {
console.log('Historical Stock Data:', data);
})
.catch(error => {
console.error('Error fetching stock history:', error);
});
- Ensure that the symbol provided is a valid NSE India stock symbol.
- The startTimestamp should be less than the endTimestamp.
- The resolution should be one of the allowed values.
This project is licensed under the MIT License.
If you have any questions or need further assistance, please contact the project maintainer at [dineshrawataias@gmail.com].