Python Forum
Python help for yahoo finance!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python help for yahoo finance!
#1
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.
Reply
#2
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)
Reply
#3
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.
Reply
#4
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.
Reply
#5
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.
Reply
#6
(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.
Reply
#7
Thanks man!
Reply
#8
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  
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Heart Yahoo email "search and destroy"! Linh_lee 20 4,295 Aug-28-2022, 10:57 AM
Last Post: sandrahdes
  Finance: Black Scholes Model not working pwt 5 3,944 May-27-2020, 10:14 AM
Last Post: buran
  Real time pricing Yahoo finance nathanz 1 2,525 Aug-09-2018, 01:02 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020