Downloading NSE stock data from internet
I was recently trying to find any good way to start trading (most probably algo trading) and was looking for some kind of basic reference. Then I found some video online but were not ready reference as these were for mostly US stock. But definitely these gave some idea how to start. I did know basic of Python.
The task is to get End of Day (EOD) stock data for NSE 500.
- First get the list of NSE 500 from NSE website
- We source the stock data from Yahoo Finance, so the Symbol name shall be updated as per Yahoo Finance nomenclature (e.g. SBIN in NSE is SBIN.NS in yahoo finance)
- Now define time frame i.e. Start and End dates for which data needs to be calculated
- Define a function that take input from the NSE 500 list and save data to CSV in same directory where is .py file is stored.
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web#define date from and to
start = dt.datetime(2015,4,1)
end = dt.datetime(2021,5,4)#function for importing data from yahoo finance and saving to csv
def getdata(ticker):
dataread = web.DataReader(ticker,’yahoo’,start,end)
dataread.to_csv(“E:/nse/”+ticker+”.csv”)df = pd.read_csv(“E:/nse/nifty500.csv”)
# for nse data yahoo has .NS in ticker name end
df[‘Symbol’] = df[‘Symbol’]+ ‘.NS’
# create csv
df[‘Symbol’].apply(getdata)
Thanks!!