We can fetch the NSE live data using API from the excel itself, built python exe based application which is responsible to fetch live market data from NSE India such as LiveMarket, PreMarket, NseHolidays, NseFNO, Symboldata, and so on... Here, we automate excel itself for live data extraction using xlwings. Here demonstrated with python tk interface and built exe based application NSEApplication.exe.
Currenly script/ application collect NSE data as Live Market Data, Pre Market Data, Symbol List, FNO Matket Data and Market Holiday Data from excel itself in opened excel file.
-
Pre-market -->
['NIFTY 50': 'NIFTY', 'NIFTY BANK': 'BANKNIFTY', 'EMERGE': 'SME', 'SECURITIES IN F&O': 'FO', 'OTHERS': 'OTHERS', 'ALL': 'ALL']
-
Live Market -->
'Broad Market Indices': ['NIFTY 50', 'NIFTY NEXT 50', 'NIFTY MIDCAP 50', 'NIFTY MIDCAP 100', 'NIFTY MIDCAP 150', 'NIFTY SMALLCAP 50', 'NIFTY SMALLCAP 100', 'NIFTY SMALLCAP 250', 'NIFTY MIDSMALLCAP 400', 'NIFTY 100', 'NIFTY 200', 'NIFTY500 MULTICAP 50:25:25', 'NIFTY LARGEMIDCAP 250'], 'Sectorial Indices': ['NIFTY AUTO','NIFTY BANK', 'NIFTY ENERGY', 'NIFTY FINANCIAL SERVICES', 'NIFTY FINANCIAL SERVICES 25/50', 'NIFTY FMCG', 'NIFTY IT', 'NIFTY MEDIA', 'NIFTY METAL', 'NIFTY PHARMA', 'NIFTY PSU BANK', 'NIFTY REALTY', 'NIFTY PRIVATE BANK'], 'Others': ['Securities in F&O', 'Permitted to Trade'], 'Strategy Indices': ['NIFTY DIVIDEND OPPORTUNITIES 50', 'NIFTY50 VALUE 20', 'NIFTY100 QUALITY 30', 'NIFTY50 EQUAL WEIGHT', 'NIFTY100 EQUAL WEIGHT', 'NIFTY100 LOW VOLATILITY 30', 'NIFTY ALPHA 50', 'NIFTY200 QUALITY 30', 'NIFTY ALPHA LOW-VOLATILITY 30', 'NIFTY200 MOMENTUM 30'], 'Thematic Indices': ['NIFTY COMMODITIES', 'NIFTY INDIA CONSUMPTION', 'NIFTY CPSE', 'NIFTY INFRASTRUCTURE', 'NIFTY MNC', 'NIFTY GROWTH SECTORS 15', 'NIFTY PSE', 'NIFTY SERVICES SECTOR', 'NIFTY100 LIQUID 15', 'NIFTY MIDCAP LIQUID 15']}
-
Market Holidays -->
['Trading', 'Clearing']
- Below is the demonstrated sample at my local environments/ system with platform as Visual Studio Code (V1.61.2) on OS Windows 10.
Demo_Excel_NSE_data.mp4
- Run python code app.py and excel will open with naming conventition as NSE_data_DDMMYYYY. (Pre-requesite microsoft excel is installed on OS system)
- You are able to write Keys as mentioned in excel first sheet. If Key name is matched, then only you are able to fetch the data, else no data is captured in real time from NSE website https://www.nseindia.com/
-
Create virtual environment
python -m venv VIRTUAL_ENV_NAME
and activate it.\VIRTUAL_ENV_NAME\Scripts\activate
. -
Install necessary library for this project from the file
requirements.txt
or manually install bypip
.pip install -r requirements.txt
To create project library requirements, use below command,
pip freeze > requirements.txt
-
In code, write your browser user-agent in headers before proceeding the script.
import requests def __init__(self): self.headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36'} self.session = requests.Session() self.session.get('http://nseindia.com', headers=self.headers)
How to capture the user-agent and API from NSE? Refers below steps/ tutorial.
- Open NSE website https://www.nseindia.com/ and inspect (Right click -> Inspect).
- Go to Network and select www.nseindia.com under Name.
- Go to Headers and copy the user-agent value from bottom.
-
Now run python file.
python app.py
-
Read more about xlwings package and usages Excel automation
- Need to install library pyinstaller
pip install pyinstaller
. - Run below command (VS Code terminal) for python file to exe conversion. (Recommended: merge api.py code to app.py / make single python file)
-i icon , -n application name , -w windows based , --log DEBUG logging if error occures , app.py python file
pyinstaller -i .\icon.ico -n NseAppication -w -F --log DEBUG .\app.py
- For py to exe conversion, refers pyinstaller documentation
. # Root directory
βββ app.py # Python code
βββ api.py # Python code
βββ icon.ico # Icon file
βββ build # build folder generated due to py-to-exe conversion
βββ dist # dist folder generated due to py-to-exe conversion
β βββ NSE_data_0102021.xlsx # Outout excel working live file created after launching exe application
β βββ NSEApplication.exe # Output exe application created after py-to-exe conversion (Not uploaded exe file due to application size )
βββ NseApplicaiton.spec # Specification file generated due to py-to-exe conversion
βββ requirements.txt # Project requirements library with versions
βββ README.md # Project README file
βββ LICENSE # Project LICENSE file
You say freak, I say unique. Don't wait for an opportunity, create it.
Letβs connect, share the ideas and feel free to ping me...