def lookup_future_chain(self, root_symbol, as_of_date, knowledge_date): """ Return the futures chain for a given root symbol. Parameters ---------- root_symbol : str Root symbol of the desired future. as_of_date : pd.Timestamp Date at which the chain determination is rooted. I.e. the existing contract that expires first after (or on) this date is the primary contract, etc. knowledge_date : pd.Timestamp Date for determining which contracts exist for inclusion in this chain. Contracts exist only if they have a start_date on or before this date. Returns ------- [Future] """ try: return [ c for c in self.future_chains_cache[root_symbol] if c.notice_date and (as_of_date < c.notice_date) and c.start_date and (c.start_date <= knowledge_date) ] except KeyError: raise RootSymbolNotFound(root_symbol=root_symbol)
def lookup_future_chain(self, root_symbol, as_of_date, knowledge_date): """ Return the futures chain for a given root symbol. Parameters ---------- root_symbol : str Root symbol of the desired future. as_of_date : pd.Timestamp Date at which the chain determination is rooted. I.e. the existing contract whose notice date is first after this date is the primary contract, etc. knowledge_date : pd.Timestamp Date for determining which contracts exist for inclusion in this chain. Contracts exist only if they have a start_date on or before this date. Returns ------- list A list of Future objects, the chain for the given parameters. Raises ------ RootSymbolNotFound Raised when a future chain could not be found for the given root symbol. """ c = self.conn.cursor() t = { 'root_symbol': root_symbol, 'as_of_date': as_of_date.value, 'knowledge_date': knowledge_date.value } c.execute( """ select sid from futures where root_symbol=:root_symbol and :as_of_date < notice_date and start_date <= :knowledge_date order by notice_date asc """, t) sids = [r[0] for r in c.fetchall()] if not sids: # Check if root symbol exists. c.execute( """ select count(sid) from futures where root_symbol=:root_symbol """, t) count = c.fetchone()[0] if count == 0: raise RootSymbolNotFound(root_symbol=root_symbol) else: # If symbol exists, return empty future chain. return [] return [self._retrieve_futures_contract(sid) for sid in sids]
def lookup_future_chain(self, root_symbol, as_of_date): """ Return the futures chain for a given root symbol. Parameters ---------- root_symbol : str Root symbol of the desired future. as_of_date : pd.Timestamp or pd.NaT Date at which the chain determination is rooted. I.e. the existing contract whose notice date/expiration date is first after this date is the primary contract, etc. If NaT is given, the chain is unbounded, and all contracts for this root symbol are returned. Returns ------- list A list of Future objects, the chain for the given parameters. Raises ------ RootSymbolNotFound Raised when a future chain could not be found for the given root symbol. """ fc_cols = self.futures_contracts.c if as_of_date is pd.NaT: # If the as_of_date is NaT, get all contracts for this # root symbol. sids = list(map( itemgetter('sid'), sa.select((fc_cols.sid,)).where( (fc_cols.root_symbol == root_symbol), ).order_by( fc_cols.notice_date.asc(), ).execute().fetchall())) else: sids = self._get_future_sids_for_root_symbol( root_symbol, as_of_date.value ) if not sids: # Check if root symbol exists. count = sa.select((sa.func.count(fc_cols.sid),)).where( fc_cols.root_symbol == root_symbol, ).scalar() if count == 0: raise RootSymbolNotFound(root_symbol=root_symbol) contracts = self.retrieve_futures_contracts(sids) return [contracts[sid] for sid in sids]
def lookup_future_chain(self, root_symbol, as_of_date, knowledge_date): """ Return the futures chain for a given root symbol. Parameters ---------- root_symbol : str Root symbol of the desired future. as_of_date : pd.Timestamp or pd.NaT Date at which the chain determination is rooted. I.e. the existing contract whose notice date is first after this date is the primary contract, etc. If NaT is given, the chain is unbounded, and all contracts for this root symbol are returned. knowledge_date : pd.Timestamp or pd.NaT Date for determining which contracts exist for inclusion in this chain. Contracts exist only if they have a start_date on or before this date. If NaT is given and as_of_date is is not NaT, the value of as_of_date is used for knowledge_date. Returns ------- list A list of Future objects, the chain for the given parameters. Raises ------ RootSymbolNotFound Raised when a future chain could not be found for the given root symbol. """ fc_cols = self.futures_contracts.c if as_of_date is pd.NaT: # If the as_of_date is NaT, get all contracts for this # root symbol. sids = list(map( itemgetter('sid'), sa.select((fc_cols.sid,)).where( (fc_cols.root_symbol == root_symbol), ).order_by( fc_cols.notice_date.asc(), ).execute().fetchall())) else: as_of_date = as_of_date.value if knowledge_date is pd.NaT: # If knowledge_date is NaT, default to using as_of_date knowledge_date = as_of_date else: knowledge_date = knowledge_date.value sids = list(map( itemgetter('sid'), sa.select((fc_cols.sid,)).where( (fc_cols.root_symbol == root_symbol) & (fc_cols.notice_date > as_of_date) & (fc_cols.start_date <= knowledge_date), ).order_by( fc_cols.notice_date.asc(), ).execute().fetchall() )) if not sids: # Check if root symbol exists. count = sa.select((sa.func.count(fc_cols.sid),)).where( fc_cols.root_symbol == root_symbol, ).scalar() if count == 0: raise RootSymbolNotFound(root_symbol=root_symbol) return list(map(self._retrieve_futures_contract, sids))
def lookup_future_chain(self, root_symbol, as_of_date): """ Return the futures chain for a given root symbol. Parameters ---------- root_symbol : str Root symbol of the desired future. as_of_date : pd.Timestamp or pd.NaT Date at which the chain determination is rooted. I.e. the existing contract whose notice date/expiration date is first after this date is the primary contract, etc. If NaT is given, the chain is unbounded, and all contracts for this root symbol are returned. Returns ------- list A list of Future objects, the chain for the given parameters. Raises ------ RootSymbolNotFound Raised when a future chain could not be found for the given root symbol. """ fc_cols = self.futures_contracts.c if as_of_date is pd.NaT: # If the as_of_date is NaT, get all contracts for this # root symbol. sids = list(map( itemgetter('sid'), sa.select((fc_cols.sid,)).where( (fc_cols.root_symbol == root_symbol), ).order_by( fc_cols.notice_date.asc(), ).execute().fetchall())) else: as_of_date = as_of_date.value sids = list(map( itemgetter('sid'), sa.select((fc_cols.sid,)).where( (fc_cols.root_symbol == root_symbol) & # Filter to contracts that are still valid. If both # exist, use the one that comes first in time (i.e. # the lower value). If either notice_date or # expiration_date is NaT, use the other. If both are # NaT, the contract cannot be included in any chain. sa.case( [ ( fc_cols.notice_date == pd.NaT.value, fc_cols.expiration_date >= as_of_date ), ( fc_cols.expiration_date == pd.NaT.value, fc_cols.notice_date >= as_of_date ) ], else_=( sa.func.min( fc_cols.notice_date, fc_cols.expiration_date ) >= as_of_date ) ) ).order_by( # If both dates exist sort using minimum of # expiration_date and notice_date # else if one is NaT use the other. sa.case( [ ( fc_cols.expiration_date == pd.NaT.value, fc_cols.notice_date ), ( fc_cols.notice_date == pd.NaT.value, fc_cols.expiration_date ) ], else_=( sa.func.min( fc_cols.notice_date, fc_cols.expiration_date ) ) ).asc() ).execute().fetchall() )) if not sids: # Check if root symbol exists. count = sa.select((sa.func.count(fc_cols.sid),)).where( fc_cols.root_symbol == root_symbol, ).scalar() if count == 0: raise RootSymbolNotFound(root_symbol=root_symbol) contracts = self.retrieve_futures_contracts(sids) return [contracts[sid] for sid in sids]