Search code examples
pythonpython-3.xclassyahoo-finance

Printing the result of my python class & function to show yahoo finance data as rows


I am trying to get stock prices from Yahoo-finance and print results as "," separated (e.g. AAPL, 163.05). I have created a class and defined some functions but I am not getting the desired results. Can you tell me what I am doing wrong?

from yahoo_finance import Share

class YahooFinance():
    def Prices(self, symbol):
        price = Share(symbol).get_price()
        #print(price)
    def Change(self, symbol):
        change = Share(symbol).get_change()
        #print(change)
    def pClose(self, symbol):
        pclose = Share(symbol).get_prev_close()
        #print(pclose)
    def tDateTime(self, symbol):
        tdatetime = Share(symbol).get_trade_datetime()
        #print(tdatetime)

>>> print(YahooFinance().Prices('AAPL'))
None
>>> Share('AAPL').get_price()
'163.05'

Solution

  • You're not returning anything in your methods. So rewrite your method as:

    def Prices(self, symbol):
            return Share(symbol).get_price()
    
    YahooFinance().Prices('AAPL')
    '163.05'