def test_get_data_yahoo_actions(self): start = datetime(1990, 1, 1) end = datetime(2018, 4, 5) actions = web.get_data_yahoo_actions("AAPL", start, end, adjust_dividends=False) assert sum(actions["action"] == "DIVIDEND") == 47 assert sum(actions["action"] == "SPLIT") == 3 assert actions.loc["2005-02-28", "action"][0] == "SPLIT" assert actions.loc["2005-02-28", "value"][0] == 1 / 2.0 assert actions.loc["1995-11-21", "action"][0] == "DIVIDEND" assert round(actions.loc["1995-11-21", "value"][0], 3) == 0.120 actions = web.get_data_yahoo_actions("AAPL", start, end, adjust_dividends=True) assert actions.loc["1995-11-21", "action"][0] == "DIVIDEND" assert round(actions.loc["1995-11-21", "value"][0], 4) == 0.0043
def test_get_data_yahoo_actions(self): start = datetime(1990, 1, 1) end = datetime(2018, 4, 5) actions = web.get_data_yahoo_actions('AAPL', start, end, adjust_dividends=False) assert sum(actions['action'] == 'DIVIDEND') == 47 assert sum(actions['action'] == 'SPLIT') == 3 assert actions.loc['2005-02-28', 'action'][0] == 'SPLIT' assert actions.loc['2005-02-28', 'value'][0] == 1 / 2.0 assert actions.loc['1995-11-21', 'action'][0] == 'DIVIDEND' assert round(actions.loc['1995-11-21', 'value'][0], 3) == 0.120 actions = web.get_data_yahoo_actions('AAPL', start, end, adjust_dividends=True) assert actions.loc['1995-11-21', 'action'][0] == 'DIVIDEND' assert round(actions.loc['1995-11-21', 'value'][0], 4) == 0.0043
def dividends(self): actions = pdr.get_data_yahoo_actions(self.ticker, session=self._session, start=START_DATE) dividends = actions[actions['action'] == 'DIVIDEND']['value'] dividends.name = 'Dividends' return dividends
def test_yahoo_DataReader(self): start = datetime(2010, 1, 1) end = datetime(2015, 5, 9) result = web.DataReader('AAPL', 'yahoo-actions', start, end) exp_idx = pd.DatetimeIndex(['2015-05-07', '2015-02-05', '2014-11-06', '2014-08-07', '2014-06-09', '2014-05-08', '2014-02-06', '2013-11-06', '2013-08-08', '2013-05-09', '2013-02-07', '2012-11-07', '2012-08-09']) exp = pd.DataFrame({'action': ['DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'SPLIT', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND'], 'value': [0.52, 0.47, 0.47, 0.47, 0.14285714, 3.29, 3.05, 3.05, 3.05, 3.05, 2.65, 2.65, 2.65]}, index=exp_idx) exp.index.name = 'Date' tm.assert_frame_equal(result.reindex_like(exp).round(2), exp.round(2)) result = web.get_data_yahoo_actions('AAPL', start, end, adjust_dividends=True) exp = pd.DataFrame({'action': ['DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'SPLIT', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND'], 'value': [0.52, 0.47, 0.47, 0.47, 0.14285714, 0.47, 0.43571, 0.43571, 0.43571, 0.43571, 0.37857, 0.37857, 0.37857]}, index=exp_idx) exp.index.name = 'Date' tm.assert_frame_equal(result.reindex_like(exp).round(5), exp.round(5)) # test cases with "1/0" split ratio in actions - # no split, just chnage symbol from POT to NTR start = datetime(2017, 12, 30) end = datetime(2018, 12, 30) result = web.DataReader('NTR', 'yahoo-actions', start, end) exp_idx = pd.DatetimeIndex(['2018-12-28', '2018-09-27', '2018-06-28', '2018-03-28', '2018-01-02']) exp = pd.DataFrame({'action': ['DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'SPLIT'], 'value': [0.43, 0.40, 0.40, 0.40, 1.00]}, index=exp_idx) exp.index.name = 'Date' tm.assert_frame_equal(result.reindex_like(exp).round(2), exp.round(2))
def test_get_data_yahoo_actions(self): start = datetime(1990, 1, 1) end = datetime(2000, 4, 5) actions = web.get_data_yahoo_actions('BHP.AX', start, end) self.assertEqual(sum(actions['action'] == 'DIVIDEND'), 20) self.assertEqual(sum(actions['action'] == 'SPLIT'), 1) self.assertEqual(actions.ix['1995-05-11']['action'][0], 'SPLIT') self.assertEqual(actions.ix['1995-05-11']['value'][0], 1/1.1) self.assertEqual(actions.ix['1993-05-10']['action'][0], 'DIVIDEND') self.assertEqual(actions.ix['1993-05-10']['value'][0], 0.3)
def test_get_data_yahoo_actions(self): start = datetime(1990, 1, 1) end = datetime(2000, 4, 5) actions = web.get_data_yahoo_actions('BHP.AX', start, end) self.assertEqual(sum(actions['action'] == 'DIVIDEND'), 20) self.assertEqual(sum(actions['action'] == 'SPLIT'), 1) self.assertEqual(actions.ix['1995-05-11']['action'][0], 'SPLIT') self.assertEqual(actions.ix['1995-05-11']['value'][0], 1 / 1.1) self.assertEqual(actions.ix['1993-05-10']['action'][0], 'DIVIDEND') self.assertEqual(actions.ix['1993-05-10']['value'][0], 0.3)
def test_get_data_yahoo_actions(self): start = datetime(1990, 1, 1) end = datetime(2000, 4, 5) actions = web.get_data_yahoo_actions('BHP.AX', start, end) assert sum(actions['action'] == 'DIVIDEND') == 20 assert sum(actions['action'] == 'SPLIT') == 1 assert actions.loc['1995-05-11', 'action'][0] == 'SPLIT' assert actions.loc['1995-05-11', 'value'][0] == 1 / 1.1 assert actions.loc['1993-05-10', 'action'][0] == 'DIVIDEND' assert actions.loc['1993-05-10', 'value'][0] == 0.3
def test_yahoo_DataReader(self): start = datetime(2010, 1, 1) end = datetime(2015, 5, 9) result = web.DataReader('AAPL', 'yahoo-actions', start, end) exp_idx = pd.DatetimeIndex([ '2015-05-07', '2015-02-05', '2014-11-06', '2014-08-07', '2014-06-09', '2014-05-08', '2014-02-06', '2013-11-06', '2013-08-08', '2013-05-09', '2013-02-07', '2012-11-07', '2012-08-09' ]) exp = pd.DataFrame( { 'action': [ 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'SPLIT', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND' ], 'value': [ 0.52, 0.47, 0.47, 0.47, 0.14285714, 3.29, 3.05, 3.05, 3.05, 3.05, 2.65, 2.65, 2.65 ] }, index=exp_idx) exp.index.name = 'Date' tm.assert_frame_equal(result.reindex_like(exp).round(5), exp.round(5)) result = web.get_data_yahoo_actions('AAPL', start, end, adjust_dividends=True) exp = pd.DataFrame( { 'action': [ 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'SPLIT', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND', 'DIVIDEND' ], 'value': [ 0.52, 0.47, 0.47, 0.47, 0.14285714, 0.47, 0.43571, 0.43571, 0.43571, 0.43571, 0.37857, 0.37857, 0.37857 ] }, index=exp_idx) exp.index.name = 'Date' tm.assert_frame_equal(result.reindex_like(exp).round(5), exp.round(5))
def actions(ticker, components): # Splits are quoted as decimals - convert to fraction ('7 for 1' vs .142857) def split_ratio(dec): frac = Fraction.from_float(dec).limit_denominator(10) num, denom = frac.numerator, frac.denominator return "{} for {}".format(denom, num) try: actions = data.get_data_yahoo_actions(ticker) except Exception: return {"message": Response.data_notfound(ticker)} if len(actions)==0: return {"message": Response.no_actions(ticker)} actions.ix[actions.action=="SPLIT", 'value'] = actions.value.map(lambda x: split_ratio(x)) actions['action'] = actions.action.map(lambda x: x.lower()) actions.index = actions.index.map(lambda x: datetime.date(x.year, x.month, x.day).strftime('%Y-%m-%d')) actions.iloc[::-1] # Build message from DataFrame message = Response.list_actions(ticker) for date, action, value in zip(actions.index, actions.action, actions.value): message += ("\n{} - {} `{}`".format(date, action, value)) return {"message": message}
def test_yahoo_DataReader(self): start = datetime(2010, 1, 1) end = datetime(2015, 5, 9) # yahoo will adjust for dividends by default result = web.DataReader("AAPL", "yahoo-actions", start, end) exp_idx = pd.DatetimeIndex([ "2015-05-07", "2015-02-05", "2014-11-06", "2014-08-07", "2014-06-09", "2014-05-08", "2014-02-06", "2013-11-06", "2013-08-08", "2013-05-09", "2013-02-07", "2012-11-07", "2012-08-09", ]) exp = pd.DataFrame( { "action": [ "DIVIDEND", "DIVIDEND", "DIVIDEND", "DIVIDEND", "SPLIT", "DIVIDEND", "DIVIDEND", "DIVIDEND", "DIVIDEND", "DIVIDEND", "DIVIDEND", "DIVIDEND", "DIVIDEND", ], "value": [ 0.52, 0.47, 0.47, 0.47, 0.14285714, 0.47, 0.43571, 0.43571, 0.43571, 0.43571, 0.37857, 0.37857, 0.37857, ], }, index=exp_idx, ) exp.index.name = "Date" tm.assert_frame_equal(result.reindex_like(exp).round(2), exp.round(2)) # where dividends are not adjusted for splits result = web.get_data_yahoo_actions("AAPL", start, end, adjust_dividends=False) exp = pd.DataFrame( { "action": [ "DIVIDEND", "DIVIDEND", "DIVIDEND", "DIVIDEND", "SPLIT", "DIVIDEND", "DIVIDEND", "DIVIDEND", "DIVIDEND", "DIVIDEND", "DIVIDEND", "DIVIDEND", "DIVIDEND", ], "value": [ 0.52, 0.47, 0.47, 0.47, 0.14285714, 3.29, 3.05, 3.05, 3.05, 3.05, 2.65, 2.65, 2.65, ], }, index=exp_idx, ) exp.index.name = "Date" tm.assert_frame_equal(result.reindex_like(exp).round(4), exp.round(4)) # test cases with "1/0" split ratio in actions - # no split, just chnage symbol from POT to NTR start = datetime(2017, 12, 30) end = datetime(2018, 12, 30) result = web.DataReader("NTR", "yahoo-actions", start, end) exp_idx = pd.DatetimeIndex([ "2018-12-28", "2018-09-27", "2018-06-28", "2018-03-28", "2018-01-02" ]) exp = pd.DataFrame( { "action": ["DIVIDEND", "DIVIDEND", "DIVIDEND", "DIVIDEND", "SPLIT"], "value": [0.43, 0.40, 0.40, 0.40, 1.00], }, index=exp_idx, ) exp.index.name = "Date" tm.assert_frame_equal(result.reindex_like(exp).round(2), exp.round(2))
def test_get_data_yahoo_actions_invalid_symbol(self): start = datetime(1990, 1, 1) end = datetime(2000, 4, 5) with pytest.raises(IOError): web.get_data_yahoo_actions("UNKNOWN TICKER", start, end)
import pandas as pd from pandas_datareader import data as pdr from datetime import date import datetime import yfinance as yf import sys yf.pdr_override() import pandas.testing # Getting Data ticker = sys.argv[1] today = date.today() today = today + datetime.timedelta(days=1) start_date="2010-01-01" data_stock = pdr.get_data_yahoo_actions(ticker, start=start_date, end=today) # Transforming Data data_stock = data_stock[::-1].reset_index() # Save Data data_stock.to_csv("./stonk_data.csv")