def load_fundamental_data(data_type, symbol, columns=None): """ Returns a DataFrame object containing <data_type> fundamental data of <symbol> with <columns>. data_type: name of *_updated_at fields in Symbol model without _updated_at symbol: e.g. 'C6L.SI' columns: a sequence of column names as defined in fa.database.*_numerical_columns modules. Default: None - include all. """ if columns is None: model_name = data_type.replace("_", "") columns = get_numerical_column_names(model_name) columns = ["Date"] + list(columns) field_names = [to_pythonic_name(c) for c in columns] records = list(get_fundamentals(data_type, symbol, field_names)) return pd.DataFrame.from_records(records, columns=columns, index="Date")
def test_get_fundamentals(self): symbols = [ {"symbol": "C6L.SI"}, {"symbol": "ABC.SI"}, ] prices = [ {"symbol_obj": "C6L.SI", "date": datetime(2012, 12, 21), "open": 0, "close": 0, "high": 0, "low": 0, "volume": 7850, "adj_close": 99.9}, {"symbol_obj": "ABC.SI", "date": datetime(2012, 12, 21), "open": 0, "close": 0, "high": 0, "low": 0, "volume": 4430, "adj_close": 54.4}, {"symbol_obj": "C6L.SI", "date": datetime(2012, 12, 20), "open": 0, "close": 0, "high": 0, "low": 0, "volume": 6860, "adj_close": 100.4}, {"symbol_obj": "C6L.SI", "date": datetime(2012, 12, 22), "open": 0, "close": 0, "high": 0, "low": 0, "volume": 3870, "adj_close": 32.6}, ] with db.transaction(): Symbol.insert_many(symbols).execute() Price.insert_many(prices).execute() result = list(query.get_fundamentals("price", "C6L.SI", ("date", "volume", "adj_close")).dicts()) self.assertEqual(result, [ (datetime(2012, 12, 20), 6860, 100.4), (datetime(2012, 12, 21), 7850, 99.9), (datetime(2012, 12, 22), 3870, 32.6), ])