def _create_monthly_ts(self): return TimeSeries.create(name=u'Test', date_frame=u'Monthly', is_snapshot=False, dates=[ datetime(2017, 1, 31), datetime(2017, 2, 28), datetime(2017, 3, 31), datetime(2017, 4, 30), datetime(2017, 5, 31), datetime(2017, 6, 30), datetime(2017, 7, 31), datetime(2017, 8, 31), datetime(2017, 9, 30), datetime(2017, 10, 31), datetime(2017, 11, 30), datetime(2017, 12, 31), ], values=[ 100.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0, 900.0, 1000.0, 1100.0, 1200.0, ])
def get(self, stock_code, metric_name): """Get metric values by metric names. Args: metric_name: A string representing metric name. Returns: A map of TimeSeries. Keys are date frames and values are corresponding TimeSeries. """ output = {} literal_metric_name = self.metric_names.get(metric_name) statement_ids = self._get_statement_ids_containing( stock_code, literal_metric_name) for statement_id in statement_ids: date_frame = self.financial_statement_store.get_date_frame( statement_id) is_snapshot = self.financial_statement_store.get_is_snapshot( statement_id) results = self._get_by_statement_id(stock_code, literal_metric_name, statement_id) dates = [entry.statement_date for entry in results] values = [entry.metric_value for entry in results] output[date_frame] = TimeSeries.create(name=metric_name, date_frame=date_frame, is_snapshot=is_snapshot, dates=dates, values=values) return output
def _create_other_ts(self): return TimeSeries.create(name=u'Other', date_frame=u'Quarterly', is_snapshot=False, dates=[ datetime(2016, 3, 31), datetime(2016, 6, 30), datetime(2016, 9, 30) ], values=[400.0, 500.0, 600.0])
def _create_one_ts(self): return TimeSeries.create(name=u'One', date_frame=u'Quarterly', is_snapshot=False, dates=[ datetime(2016, 3, 31), datetime(2016, 6, 30), datetime(2016, 9, 30) ], values=[100.0, 200.0, 300.0])
def test_annualize(self): one = TimeSeries.create(name=u'Test', date_frame=u'Yearly', is_snapshot=False, dates=[ datetime(2013, 12, 31), datetime(2014, 12, 31), datetime(2015, 12, 31) ], values=[100.0, 200.0, 300.0]) other = TimeSeries.create(name=u'Test', date_frame=u'Quarterly', is_snapshot=False, dates=[ datetime(2016, 3, 31), datetime(2016, 6, 30), datetime(2016, 9, 30) ], values=[400.0, 500.0, 600.0]) actual = one.annualize(other).get() expected = { 'name': u'Test', 'date_frame': u'Yearly', 'is_snapshot': False, 'date': [ datetime(2013, 12, 31), datetime(2014, 12, 31), datetime(2015, 12, 31), datetime(2016, 12, 31) ], 'value': [100.0, 200.0, 300.0, 2000.0], } self.assertEqual(actual, expected)
def test_create(self): ts = TimeSeries.create( name=u'Test', date_frame=u'Quarterly', is_snapshot=False, dates=[datetime(2016, 6, 30), datetime(2016, 3, 31)], values=[100, 200]) expected = { 'name': u'Test', 'date_frame': u'Quarterly', 'is_snapshot': False, 'date': [datetime(2016, 3, 31), datetime(2016, 6, 30)], 'value': [200, 100], } self.assertEqual(ts.get(), expected)
def test_accumulate_annually(self): ts = TimeSeries.create( name=u'Test', date_frame=u'Quarterly', is_snapshot=False, dates=[ datetime(2015, 3, 31), datetime(2015, 6, 30), datetime(2015, 9, 30), datetime(2015, 12, 31), datetime(2016, 3, 31), datetime(2016, 6, 30), datetime(2016, 9, 30), datetime(2016, 12, 31) ], values=[100.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0]) actual = ts.accumulate_annually().get() expected = { 'name': u'Test', 'date_frame': u'Quarterly', 'is_snapshot': False, 'date': [ datetime(2015, 3, 31), datetime(2015, 6, 30), datetime(2015, 9, 30), datetime(2015, 12, 31), datetime(2016, 3, 31), datetime(2016, 6, 30), datetime(2016, 9, 30), datetime(2016, 12, 31) ], 'value': [ 100.0, 100.0 + 200.0, 100.0 + 200.0 + 300.0, 100.0 + 200.0 + 300.0 + 400.0, 500.0, 500.0 + 600.0, 500.0 + 600.0 + 700.0, 500.0 + 600.0 + 700.0 + 800.0 ], } self.assertEqual(actual, expected)
def test_yoy(self): ts = TimeSeries.create( name=u'Test', date_frame=u'Quarterly', is_snapshot=False, dates=[ datetime(2015, 3, 31), datetime(2015, 6, 30), datetime(2015, 9, 30), datetime(2015, 12, 31), datetime(2016, 3, 31), datetime(2016, 6, 30), datetime(2016, 9, 30), datetime(2016, 12, 31) ], values=[100.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0]) actual = ts.yoy().get() expected = { 'name': u'Test', 'date_frame': u'Quarterly', 'is_snapshot': False, 'date': [ datetime(2016, 3, 31), datetime(2016, 6, 30), datetime(2016, 9, 30), datetime(2016, 12, 31) ], 'value': [ (500.0 - 100.0) / 100.0, (600.0 - 200.0) / 200.0, (700.0 - 300.0) / 300.0, (800.0 - 400.0) / 400.0, ], } self.assertEqual(actual, expected)
def test_copy(self): original = TimeSeries.create( name=u'Test', date_frame=u'Quarterly', is_snapshot=False, dates=[datetime(2016, 3, 31), datetime(2016, 6, 30)], values=[100.0, 200.0]) original_expected = { 'name': u'Test', 'date_frame': u'Quarterly', 'is_snapshot': False, 'date': [datetime(2016, 3, 31), datetime(2016, 6, 30)], 'value': [100.0, 200.0], } # Make a copy. copied = original.copy() # Original time series won't be changed even we modify copied one. copied.name = u'Copied' copied.date_frame = u'Yearly' copied.is_snapshot = True copied_expected = { 'name': u'Copied', 'date_frame': u'Yearly', 'is_snapshot': True, 'date': [datetime(2016, 3, 31), datetime(2016, 6, 30)], 'value': [100.0, 200.0], } self.assertEqual(original.get(), original_expected) self.assertEqual(copied.get(), copied_expected)
class MockFinancialStatementEntryStore(object): mock_data = { '2317': { # CapitalIncreaseHistoryService 'CapitalIncreaseByCash': { u'Yearly': TimeSeries.create(name=u'CapitalIncreaseByCash', date_frame=u'Yearly', is_snapshot=False, dates=[ datetime(2005, 12, 31), datetime(2006, 12, 31), datetime(2007, 12, 31) ], values=[26.44, 27.01, 27.01]), }, 'CapitalIncreaseByEarnings': { u'Yearly': TimeSeries.create(name=u'CapitalIncreaseByEarnings', date_frame=u'Yearly', is_snapshot=False, dates=[ datetime(2005, 12, 31), datetime(2006, 12, 31), datetime(2007, 12, 31) ], values=[346.52, 435.51, 547.78]), }, 'CapitalIncreaseBySurplus': { u'Yearly': TimeSeries.create(name=u'CapitalIncreaseBySurplus', date_frame=u'Yearly', is_snapshot=False, dates=[ datetime(2005, 12, 31), datetime(2006, 12, 31), datetime(2007, 12, 31) ], values=[36.01, 53.83, 53.83]), }, # DuPontService 'NetProfit': { u'Yearly': TimeSeries.create(name=u'NetProfit', date_frame=u'Yearly', is_snapshot=False, dates=[ datetime(2014, 12, 31), datetime(2015, 12, 31), datetime(2016, 12, 31) ], values=[132482, 150201, 151357]), u'Quarterly': TimeSeries.create(name=u'NetProfit', date_frame=u'Quarterly', is_snapshot=False, dates=[ datetime(2017, 3, 31), datetime(2017, 6, 30), datetime(2017, 9, 30) ], values=[29207, 14919, 19665]), }, 'Assets': { u'Yearly': TimeSeries.create(name=u'Assets', date_frame=u'Yearly', is_snapshot=True, dates=[ datetime(2014, 12, 31), datetime(2015, 12, 31), datetime(2016, 12, 31) ], values=[2312461, 2462715, 2308300]), u'Quarterly': TimeSeries.create(name=u'Assets', date_frame=u'Quarterly', is_snapshot=True, dates=[ datetime(2017, 3, 31), datetime(2017, 6, 30), datetime(2017, 9, 30) ], values=[2332342, 2457578, 2762655]), }, 'Equity': { u'Yearly': TimeSeries.create(name=u'Equity', date_frame=u'Yearly', is_snapshot=True, dates=[ datetime(2014, 12, 31), datetime(2015, 12, 31), datetime(2016, 12, 31) ], values=[984677, 1060391, 1133789]), u'Quarterly': TimeSeries.create(name=u'Equity', date_frame=u'Quarterly', is_snapshot=True, dates=[ datetime(2017, 3, 31), datetime(2017, 6, 30), datetime(2017, 9, 30) ], values=[1183505, 1132860, 1156638]), }, 'Sales': { u'Yearly': TimeSeries.create(name=u'Sales', date_frame=u'Yearly', is_snapshot=False, dates=[ datetime(2014, 12, 31), datetime(2015, 12, 31), datetime(2016, 12, 31) ], values=[4213172, 4482146, 4358733]), u'Quarterly': TimeSeries.create(name=u'Sales', date_frame=u'Quarterly', is_snapshot=False, dates=[ datetime(2017, 3, 31), datetime(2017, 6, 30), datetime(2017, 9, 30) ], values=[975044, 922412, 1078892]), } } } def get(self, stock_code, metric_name): if stock_code in self.mock_data and metric_name in self.mock_data[ stock_code]: return self.mock_data[stock_code][metric_name] raise ValueError(u'Could not get mock data: stock_code={stock_code} metric_name={metric_name}' \ .format(stock_code=stock_code, metric_name=metric_name))