1

After mainly working with functional programming in the past - I wanted to start using OOP, classes more often, as I am planning to share and collaborate more with others.

Can you help me on the following problem:

#! /usr/bin/python3
# -*- coding: utf-8 -*-
from pandas_datareader import data as pdr
import quandl

class Stock_Analysis:
    def __init__(self, Stock_Ticker,Start_Date):    
        self.ticker = Stock_Ticker
        self.start_date = Start_Date
        try:
            self.stock_data = pdr.get_data_yahoo(self.ticker,self.start_date)
        except:
            print("Error with Yahoo - please enter Quandl Tickers")
            try:
                self.quandl_ticker = input()
                self.stock_data = quandl.get(self.quandl_ticker)
            except:
                "Failled"
    def Print_Data(self):
        print(self.stock_data)

apple = Stock_Analysis("AAPL","2015-01-01")
apple.Print_Data()

The Code is supposed to fetch data for a user defined stock and return the OHLC Time Series data when using the Print_Data() Method on the object. In case the data can not be taken from Yahoo, it uses Quandl as a data source. The ticker for Quandl is: "WIKI/AAPL"

When executing the Code:

apple = Stock_Analysis("AAPL","2015-01-01")
Error with Yahoo - please enter Quandl Tickers
"WIKI/AAPL"
apple.Print_Data()
Traceback (most recent call last):
  File "C:\Users\Julian\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-89-cff530c657a2>", line 1, in <module>
    apple.Print_Data()
  File "<ipython-input-86-1c393f01d103>", line 15, in Print_Data
    print(self.stock_data)
AttributeError: 'Stock_Analysis' object has no attribute 'stock_data'

I tried it with return to a variable and with print() but neither worked

Thank you very much for your help.

JsDotPy
  • 11
  • 2
  • [This link](https://stackoverflow.com/questions/44112403/yahoo-finance-api-url-not-working-python-fix-for-pandas-datareader) might help you with a fix for the get_data_yahoo part. – patrickjlong1 Oct 10 '17 at 19:42
  • Thank you so much - This helped a lot - I used it in another script of mine but forgot about it - added the line to handle this at first stage - That is a very helpful point. – JsDotPy Oct 11 '17 at 00:27

0 Answers0