Posts: 4
Threads: 1
Joined: Apr 2021
Apr-03-2021, 05:12 PM
(This post was last modified: Apr-04-2021, 04:56 AM by buran.)
Hi,
I'm new to this forum & my python skills aren't that much good.
I was facing a problem related to the code I thought I could get some help here!
This is the code!
import yfinance as yf
import pandas as PD
data = yf.download("ATVI", period="max", group_by='tickers')
df = PD.DataFrame(data)
with open('D:ATVI.csv', 'a+') as f:
df.to_csv(f, header=False)
print(df) Now this works just fine when I wanna get one ticker historical quotes in csv format
But if I want multiple tickers which I get by adding this!
data = yf.download("ATVI AAPL BA", period="max", group_by='tickers')
I get AAPL & BA quotes as well But the problem is I wanna save all tickers individually like AAPL.csv BA.csv now I don't wanna write each time different code for different tickers I wanna save files with one code now how would I do that if someone could help me out here I would be really thankful!
buran write Apr-04-2021, 04:56 AM:Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Posts: 7,320
Threads: 123
Joined: Sep 2016
Use Code Tags
Can try something this.
import yfinance as yf
import pandas as PD
def yf_func(tickers):
data = yf.download(tickers, period="max", group_by="tickers")
df = PD.DataFrame(data)
return df.to_csv(index=False)
def save_data(lst_tic, lst_csv):
data = list(zip(lst_tic, lst_csv))
for item in data:
with open(f"/content/sample_data/{item[0]}.csv", "w") as f:
f.write(item[1])
if __name__ == "__main__":
lst_tic = ["ATVI", "AAPL", "BA"]
lst_csv = []
for tic in lst_tic:
lst_csv.append(yf_func(tic))
save_data(lst_tic, lst_csv)
Posts: 4
Threads: 1
Joined: Apr 2021
Apr-04-2021, 03:52 AM
(This post was last modified: Apr-04-2021, 04:56 AM by buran.)
HI,
Thanks for your reply I tried but it is not working!
It's showing an error!
Error: File "<string>", line 7
data = yf.download(tickers, period="max", group_by="tickers")
^
SyntaxError: invalid character in identifier
[Program finished]
If you could suggest more,
Thanks.
buran write Apr-04-2021, 04:56 AM:Please, use proper tags when post code, traceback, output, etc. This time I have added error tags for you.
See BBcode help for more info.
Posts: 7,320
Threads: 123
Joined: Sep 2016
Here is code in Notebook tested and it work.
Try copy code from there,error should also be on line 5 not 7.
That error means that there can be trouble with data variable.
Means that you have some character(non-printing garbage characters) in variable name, function, etc. that's not a letter, number, or underscore.
Also try delete that line and write it yourself.
Posts: 4
Threads: 1
Joined: Apr 2021
HI,
My apologies it was my mistake,
Anyway I can't thank you enough for the code, one thing though when I use your code the csv file doesn't have date column the code I mentioned earlier also downloads that now how could I get date column if you could suggest anything more!
I'm sorry for asking again but you are the only one that replied and I don't have much skills in python so I'm asking!
Thanks.
Posts: 7,320
Threads: 123
Joined: Sep 2016
(Apr-04-2021, 10:24 AM)Zshanar Wrote: one thing though when I use your code the csv file doesn't have date column Change line 7 to:
return df.to_csv() And other way to first do df = df.reset_index() then can have df.to_csv(index=False)
The result should ne the same.
Posts: 4
Threads: 1
Joined: Apr 2021
Posts: 9
Threads: 0
Joined: Jul 2022
Hi Zshanar,
See the code below.
import yfinance as yf
tickers=['ATVI','AAPL','BA']
df = yf.download(tickers, period='max')
df.head()
Out[838]:
Adj Close Close High \
AAPL ATVI BA AAPL ATVI BA AAPL ATVI BA
Date
1962-01-02 NaN NaN 0.190931 NaN NaN 0.823045 NaN NaN 0.837449
1962-01-03 NaN NaN 0.194750 NaN NaN 0.839506 NaN NaN 0.851852
1962-01-04 NaN NaN 0.192840 NaN NaN 0.831276 NaN NaN 0.853909
1962-01-05 NaN NaN 0.189022 NaN NaN 0.814815 NaN NaN 0.835391
1962-01-08 NaN NaN 0.189499 NaN NaN 0.816872 NaN NaN 0.829218
Low Open Volume
AAPL ATVI BA AAPL ATVI BA AAPL ATVI BA
Date
1962-01-02 NaN NaN 0.823045 NaN NaN 0.837449 NaN NaN 352350
1962-01-03 NaN NaN 0.835391 NaN NaN 0.835391 NaN NaN 710775
1962-01-04 NaN NaN 0.831276 NaN NaN 0.839506 NaN NaN 911250
1962-01-05 NaN NaN 0.792181 NaN NaN 0.831276 NaN NaN 880875
1962-01-08 NaN NaN 0.804527 NaN NaN 0.814815 NaN NaN 473850
|