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 :
JavaScript
x
15
15
1
with open("tickers_list.txt") as fp:
2
Lines = fp.readlines()
3
for line in Lines: #for each line = ticker
4
line = line.strip() #remove "/n" in the line
5
data = yf.download(line,period = '10y', interval='1d')
6
del data["Open"] #remove useless column
7
del data["High"] #remove useless column
8
del data["Low"] #remove useless column
9
del data["Adj Close"] #remove useless column
10
del data["Volume"] #remove useless column
11
data.to_csv('download/'+line+'.csv')
12
13
# close the file
14
fp.close()
15
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
JavaScript
1
5
1
yf.download(line,period = '10y', interval='1d')
2
Date Open High Low Close Adj Close Volume
3
2012-04-23 409.200012 410.899994 398.700012 402.399994 331.590271 203708
4
2012-04-24 408.600006 412.299988 406.700012 408.500000 336.616821 204588
5
what I am looking for
JavaScript
1
5
1
yf.download(line,period = '10y', interval='1d', [Date], [Close])
2
Date Close
3
2012-04-23 402.399994
4
2012-04-24 408.500000
5
Advertisement
Answer
JavaScript
1
2
1
df = yf.Ticker("IBM").history(start="2017-01-01", end="2017-04-30", frequency='1dy')['Close']
2
Returns only one price
Output:
JavaScript
1
13
13
1
Date
2
2017-01-03 125.567116
3
2017-01-04 127.121788
4
2017-01-05 126.701187
5
2017-01-06 127.324532
6
2017-01-09 125.912628
7
8
2017-04-24 121.685005
9
2017-04-25 121.412483
10
2017-04-26 121.162666
11
2017-04-27 121.359474
12
2017-04-28 121.336777
13
Also, if you request all the data, you can try to delete the unnecessary immediately
JavaScript
1
4
1
data = yf.download("AAPL", start="2017-01-01", end="2017-04-30", auto_adjust=True)
2
data = data[['Close']]
3
print(data)
4