Financial data analysis is crucial for traders, investors, and analysts who need to make informed decisions based on historical market trends. Access to reliable and timely data is essential. Today, we’re excited to introduce OpenChart, a Python library designed to simplify the process of downloading intraday and end-of-day (EOD) historical data from the National Stock Exchange of India (NSE) and NSE Futures and Options (NFO) exchanges.
In this blog post, we’ll delve into the features of OpenChart, guide you through the installation process, and provide examples of how to use the library to fetch historical data for your favorite stocks and derivatives.
What is OpenChart?
OpenChart is an open-source Python library that allows users to:
- Download NSE and NFO master data
- Search for symbols in NSE and NFO exchanges
- Fetch historical data for equities and derivatives
- Support various timeframes:
1m
,3m
,5m
,10m
,15m
,30m
,1h
,1d
,1w
,1M
Key Features
- Ease of Use: Simple and intuitive API for fetching data.
- Flexibility: Supports multiple timeframes and exchanges.
- Open Source: Licensed under MIT License for community use and contributions.
- Up-to-Date Data: Fetches the most recent data available from NSE and NFO.
Installation
OpenChart is available on PyPI, and you can install it using pip
:
pip install openchart
Getting Started
Importing the Library and Initializing the NSEData Class
from openchart import NSEData
nse = NSEData()
Downloading Master Data
Before fetching historical data or searching for symbols, download the master data for NSE and NFO:
nse.download()
Fetching Historical Data
Important Note
Always specify start
and end
dates when fetching historical data. This ensures you get the data for the exact period you’re interested in.
Fetching Intraday Data
Fetch 5-minute interval data for RELIANCE from the last 30 days:
import datetime
end_date = datetime.datetime.now()
start_date = end_date - datetime.timedelta(days=30)
data = nse.historical(
symbol='RELIANCE',
exchange='NSE',
start=start_date,
end=end_date,
interval='5m'
)
print(data)
Output
Open High Low Close Volume
Timestamp
2024-09-20 13:14:59 2983.85 2985.60 2982.30 2984.65 81648
2024-09-20 13:19:59 2984.95 2987.00 2983.20 2985.75 93902
2024-09-20 13:24:59 2986.00 2987.75 2981.75 2985.30 118105
2024-09-20 13:29:59 2985.70 2990.00 2983.70 2986.40 161902
2024-09-20 13:34:59 2985.95 2987.15 2959.00 2960.95 383962
... ... ... ... ... ...
2024-10-18 15:09:59 2721.45 2721.50 2718.40 2719.40 210486
2024-10-18 15:14:59 2720.00 2720.90 2715.20 2716.10 178000
2024-10-18 15:19:59 2716.10 2716.10 2713.00 2714.70 175154
2024-10-18 15:24:59 2714.70 2718.85 2713.55 2718.85 175184
2024-10-18 15:29:59 2718.85 2719.10 2715.50 2717.00 194882
[480 rows x 5 columns]
Fetch historical data for a futures contract of 15min data, from 30 days ago until today:
import datetime
end_date = datetime.datetime.now()
start_date = end_date - datetime.timedelta(days=30)
data = nse.historical(
symbol='BANKNIFTY24OCTFUT',
exchange='NFO',
start=start_date,
end=end_date,
interval='15m'
)
print(data)
Output
Open High Low Close Volume
Timestamp
2024-09-20 13:14:58 53553.20 53562.60 53543.30 53544.95 1290
2024-09-20 13:29:59 53554.00 53572.00 53516.50 53532.95 32940
2024-09-20 13:44:58 53522.40 53535.40 53145.05 53238.85 266415
2024-09-20 13:59:59 53232.40 53428.00 53220.00 53331.70 137010
2024-09-20 14:14:58 53326.80 53376.05 53275.00 53335.05 54195
... ... ... ... ... ...
2024-10-18 14:29:59 52338.00 52400.00 52311.15 52360.75 71445
2024-10-18 14:44:58 52360.00 52415.50 52360.00 52390.00 68265
2024-10-18 14:59:59 52390.00 52390.00 52320.00 52348.60 49935
2024-10-18 15:14:58 52350.10 52355.00 52241.00 52275.00 146895
2024-10-18 15:29:58 52272.95 52360.00 52264.95 52360.00 183180
[480 rows x 5 columns]
Fetching End-of-Day Data
Fetch daily data for Nifty 50 for the last 3650 days:
import datetime
end_date = datetime.datetime.now()
start_date = end_date - datetime.timedelta(days=3650)
data = nse.historical(
symbol='Nifty 50',
exchange='NSE',
start=start_date,
end=end_date,
interval='1d'
)
print(data)
Output
Open High Low Close Volume
Timestamp
2014-10-27 00:00:00 8064.35 8064.40 7985.65 7991.70 133453808
2014-10-28 00:00:00 8002.40 8037.80 7995.05 8027.60 130545143
2014-10-29 00:00:00 8077.05 8097.95 8052.25 8090.45 157278723
2014-10-30 00:00:00 8085.20 8181.55 8085.20 8169.20 203111083
2014-10-31 00:00:00 8200.80 8330.75 8198.05 8322.20 215483674
... ... ... ... ... ...
2024-10-14 00:00:00 25023.45 25159.75 25017.50 25127.95 3009476572
2024-10-15 00:00:00 25186.30 25212.05 25008.15 25057.35 2875166269
2024-10-16 00:00:00 25008.55 25093.40 24908.45 24971.30 2883096337
2024-10-17 00:00:00 25027.40 25029.50 24728.90 24749.85 2886728701
2024-10-18 16:14:59 24664.95 24886.20 24567.65 24854.05 1
[2473 rows x 5 columns]
Supported Timeframes
You can check the supported timeframes using:
print(nse.timeframes())
Output
['1m', '3m', '5m', '10m', '15m', '30m', '1h', '1d', '1w', '1M']
Search for Symbols
NSE Exchange
Search for symbols like Nifty 50, TCS, RELIANCE in the NSE exchange.
symbols = nse.search('RELIANCE', exchange='NSE')
print(symbols)
NFO Exchange
Search for symbols like NIFTY24OCTFUT, BANKNIFTY24OCTFUT, NIFTY24N2124800CE, NIFTY24N2124800PE in the NFO exchange.
- NIFTY24N2124800CE corresponds to NIFTY 21 Nov 2024 CE 24800.00.
- NIFTY24N2124800PE corresponds to NIFTY 21 Nov 2024 PE 24800.00.
symbols = nse.search('24OCTFUT', exchange='NFO')
print(symbols)
Output
ScripCode Symbol Name Type
0 35006 BANKNIFTY24OCTFUT BANKNIFTY 30 Oct 2024 1
1 35007 NIFTYNXT5024OCTFUT NIFTYNXT50 25 Oct 2024 1
2 35012 FINNIFTY24OCTFUT FINNIFTY 29 Oct 2024 1
3 35239 MIDCPNIFTY24OCTFUT MIDCPNIFTY 28 Oct 2024 1
4 35382 NIFTY24OCTFUT NIFTY 31 Oct 2024 1
.. ... ... ... ...
179 48598 UPL24OCTFUT UPL 31 Oct 2024 3
180 48601 VEDL24OCTFUT VEDL 31 Oct 2024 3
181 48602 VOLTAS24OCTFUT VOLTAS 31 Oct 2024 3
182 48603 WIPRO24OCTFUT WIPRO 31 Oct 2024 3
183 48604 ZYDUSLIFE24OCTFUT ZYDUSLIFE 31 Oct 2024 3
[184 rows x 4 columns]
Exact Match Search
If you want to perform an exact match search, you can set exact_match=True
.
symbol_info = nse.search('BANKNIFTY24OCTFUT', exchange='NFO', exact_match=True)
print(symbol_info)
Contributing
OpenChart is an open-source project, and contributions are welcome! You can contribute by:
- Reporting bugs
- Submitting pull requests
- Suggesting new features
Visit the GitHub repository to get started.
OpenChart simplifies the process of fetching historical data from NSE and NFO, making it an invaluable tool for traders, analysts, and developers. With easy installation and a straightforward API, you can integrate it into your projects quickly.
Give OpenChart a try, and feel free to contribute to the project!