Market data is the main resource for conducting quantitative analysis. Yahoo finance provides API for getting market data and is the best source of free data to start market analysis.
In this writing I explain how you can get daily, monthly and even intraday data using Yahoo API in Python.
Fortunately yfinance package in Python provided us a great function we can use to extract market data for each Symbol.
Here I show you how you can create working directory in Python and save the market data in it and make a function that get all SP500 data at the same time. It is pretty straightforward.
Warning: make sure you create an empty folder for your working directory and set it as working directory because for getting the SP500 data we first clean the working directory first and then add the new .csv data in the folder and I am sure you don’t want to delete the sensitive data accidently by executing this code.
|
import yfinance as yf
|
|
import os
|
|
import pandas as pd
|
|
WorkingDirectory='set your WorkingDirecotory Here'
|
|
os.chdir(WorkingDirectory)
|
|
# ___________________________ Single Symbol ______________________
|
|
def Ticker(symbol):
# You Probably just need the first line of this function to download the data the next lines
# is just preparing the downloaded data and the code for saving it as .csv file.
|
|
df = yf.download(symbol, period='5y').dropna()
|
|
df=df.reset_index()
|
|
del df['Adj Close']
|
|
string_date=[]
|
|
for date in df['Date']:
|
|
string_date.append((str(date)[:10]).replace('-',''))
|
|
df['Date']=string_date
|
|
df.to_csv(WorkingDirectory+'/'+symbol+'.csv', index=False)
|
|
# ___________________________ Market Data ______________________
|
|
# you can provide a list of Tickers to get their market data
|
|
def Market(symbols,remove=True):
|
|
# Warning if remove == True this part removes all files in your WorkingDirectory
|
|
if remove == True:
|
|
folder = WorkingDirectory
|
|
for the_file in os.listdir(folder):
|
|
file_path = os.path.join(folder, the_file)
|
|
try:
|
|
if os.path.isfile(file_path):
|
|
os.unlink(file_path)
|
|
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
for tiker in symbols:
|
|
Ticker(tiker)
|
|
# ___________________________ Intraday _____________________________________
|
|
|
|
def intraday(symbol,Interval='60m',period='3mo'):
|
|
df_1h = yf.download(symbol,period=period,interval =Interval)
|
|
|
|
# save data with Ticker name in the working directory
|
|
df_1h.to_csv(WorkingDirectory+'/'+symbol+Interval+'.csv', index=False)
|
|
|
|
|
|
|
|
# ____________________________ Done __________________________________
|
|
#_______________________________Execution code_________________________
|
|
Ticker('QBAK')
|
|
#_________________________________________________________________________
|
|
intraday('AET',Interval='60m',period='3mo')
|
|
#_________________________________________________________________________
|
|
# You can get data of the customized list of stocks here as .csv file
market=['^GSPC','SYY','PNR','MRK']
|
|
Market(market,remove=True)
|
|
#_______________________________________________________________________
|
|
|
|
market=['^GSPC','ALXN','PM','NBL']
|
|
Market(market,remove=True)
|
|
#_________________________________________________________________________
|
|
Market(SP500,remove=True)
|
|
|
|
Comments
Post a Comment