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.