Created
August 19, 2022 07:25
-
-
Save AkhilRD/174143a55a69ecdb7363ee01ac2168b7 to your computer and use it in GitHub Desktop.
Extracting multiple stock price data into a single excel sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import datetime as dt | |
import time | |
#tickers as on yahoo finance | |
tickers = ['BAJFINANCE.NS','CHOLAFIN.NS','CUB.BO','DCBBANK.NS','DHANBANK.BO','EDELWEISS.NS','HDFCBANK.NS','ICICIBANK.NS', | |
'IDFC.NS','KOTAKBANK.BO','L&TFH.NS','MANAPPURAM.BO','MUTHOOTFIN.BO','RELCAPITAL.BO','SBIN.BO','KTKBANK.BO', | |
'KARURVYSYA.NS','SOUTHBANK.BO','YESBANK.BO','MOTILALOFS.NS'] | |
#interval: 1d or 1mo | |
interval = '1d' | |
period1 = int(time.mktime(dt.datetime(2013,6,30,23,59).timetuple())) #start date | |
period2 = int(time.mktime(dt.datetime(2019,4,1,23,59).timetuple())) #end date | |
writer = pd.ExcelWriter('prices.xlsx',engine='openpyxl') | |
for ticker in tickers: | |
query = f'https://query1.finance.yahoo.com/v7/finance/download/{ticker}?period1={period1}&period2={period2}&interval={interval}&events=history&includeAdjustedClose=true' | |
df = pd.read_csv(query) | |
df.to_excel(writer,sheet_name = ticker,index = False) | |
writer.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment