-1

I am currently creating a program where I want to fetch stock data from yahoo finance using the yahoo_finance module. However, I want to fetch data for 4 stocks using what i assume would be a loop. Here's the basic structure I thought of using:

from yahoo_finance import Share
ticker_symbols = ["YHOO", "GOOG", "AAPL"]

i = 0
while i < 4:
    company = Share(str(i))
    print (company.get_open())
    i += 1

The main problem I need assistance with is how I would construct a loop that iterates over all the ticker_symbols. As you can tell from my "try" above I am completely clueless, since I am new to python. A secondary problem I have is how I would fetch data from 30 days ago up to current date using the module. Maybe I should have resorted to web scraping but it seems so much more difficult.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
ghostfkrcb
  • 11
  • 3

2 Answers2

0

to loop over a list you can just do :

for symbol in ticker_symbols :
    company = Share(symbol)

That's basic python ! I will advise you to follow a small tutorial to learn the python basics.

You can get historical daily data using Share(symbol).get_historical('aDate'). Here you can find all the available methods for the package : https://pypi.python.org/pypi/yahoo-finance

good luck with that

Mohamed AL ANI
  • 2,012
  • 1
  • 12
  • 29
0

You need to iterate over the ticker_symbols list and simply ditch the while loop:

from yahoo_finance import Share
ticker_symbols = ["YHOO", "GOOG", "AAPL"]

for i in ticker_symbols:
    company = Share(i)
    print (company.get_open())
Alexander Ejbekov
  • 5,594
  • 1
  • 26
  • 26
  • Makes a lot of sense, read up on python today and realised the answer might have been somewhat obvious. If you don't mind me asking, do you know if there is a way to calculate beta value using the yahoo finance module or get 30 days high/low? – ghostfkrcb Nov 22 '16 at 22:34
  • I am not familiar with the yahoo finance module and API to be honest. I'm not sure if it returns such data or you have to compile the results yourself, in which case I'd recommend having a look at pandas, numpy and scipy - they have a ton of utilities to help you deal with number crunching. – Alexander Ejbekov Nov 23 '16 at 18:33