Right now, I want to download historical prices for some tickers but I don’t need the full download, only the date and close price.
Currently I am doing this way :
with open("tickers_list.txt") as fp:
Lines = fp.readlines()
for line in Lines: #for each line = ticker
line = line.strip() #remove "/n" in the line
data = yf.download(line,period = '10y', interval='1d')
del data["Open"] #remove useless column
del data["High"] #remove useless column
del data["Low"] #remove useless column
del data["Adj Close"] #remove useless column
del data["Volume"] #remove useless column
data.to_csv('download/'+line+'.csv')
# close the file
fp.close()
As you can see, I download the whole “package” then delete the useless columns. The future problem with this way is : if I need to download data for 10’000 companies, I will lose quite sometimes in the process.
Normal download
yf.download(line,period = '10y', interval='1d') Date Open High Low Close Adj Close Volume 2012-04-23 409.200012 410.899994 398.700012 402.399994 331.590271 203708 2012-04-24 408.600006 412.299988 406.700012 408.500000 336.616821 204588
what I am looking for
yf.download(line,period = '10y', interval='1d', [Date], [Close]) Date Close 2012-04-23 402.399994 2012-04-24 408.500000
Advertisement
Answer
df = yf.Ticker("IBM").history(start="2017-01-01", end="2017-04-30", frequency='1dy')['Close']
Returns only one price
Output:
Date
2017-01-03 125.567116
2017-01-04 127.121788
2017-01-05 126.701187
2017-01-06 127.324532
2017-01-09 125.912628
...
2017-04-24 121.685005
2017-04-25 121.412483
2017-04-26 121.162666
2017-04-27 121.359474
2017-04-28 121.336777
Also, if you request all the data, you can try to delete the unnecessary immediately
data = yf.download("AAPL", start="2017-01-01", end="2017-04-30", auto_adjust=True)
data = data[['Close']]
print(data)