def test_empty_metadata(self): empty_metadata = pd.DataFrame() act = metadata_utils.list_all_futures_contracts( metadata=empty_metadata, fut_code='AZ') exp = [] self.assertListEqual(act, exp)
def test(self): fut_code = self.metadata.loc[0,'contract'][:-5] act = metadata_utils.list_all_futures_contracts( metadata=self.metadata, fut_code=fut_code) exp = filter(lambda x : x[:-5] == fut_code, self.metadata['contract']) self.assertListEqual(act, exp)
def test_no_matches(self): # Won't find any matches for this futures code fut_code = 'ZZ' act = metadata_utils.list_all_futures_contracts( metadata=self.metadata, fut_code=fut_code) exp = [] self.assertListEqual(act, exp)
def get_futures_code_dataset(metadata, exchange, fut_code, start=None, end=None, auth_token=None): ''' Synopsis -------- Fetches datasets for each of the contracts associated with the given futures code (e.g., the futures contracts that share the code 'SP' on CME include SPZ2015, SPH2016, etc). Datasets will be aggregated into a single Pandas DataFrame with a column identifying the contract from which each observation was sourced. Parameters ---------- metadata: Pandas DataFrame. Describes futures contract datasets on Quandl. Must have the following columns * contract exchange: Name of futures exchange. String. fut_code: Prefix for futures contract. String. start: Optional start date for dataset. Ignores all data timestamped before this date. Example: 2015-01-01. String. end: Optional end date for dataset. Ignores all data timestamped after this date. Example: 2016-01-01. String. Cannot be before the start date. auth_token: Quandl auth token. String. Returns ------- Pandas DataFrame ''' contracts = metadata_utils.list_all_futures_contracts(metadata, fut_code) if start: # To reduce the number of Quandl API calls, we remove all contracts # whose expiration date fell in a year before the provided start date # This removes most but not all expired contracts filter_rule = lambda contract : not _contract_year_is_before_year_beg( contract=contract, date=start) contracts = filter(filter_rule, contracts) datasets = [get_futures_contract_dataset( exchange=exchange, contract=contract, start=start, end=end, auth_token=auth_token) for contract in contracts] # Make a copy of each dataframe because we need to mutate them datasets = map(pd.DataFrame.copy, datasets) for contract, dataset in zip(contracts, datasets): dataset['Contract'] = contract aggregate_dataset = reduce(pd.DataFrame.append, datasets) return aggregate_dataset