Fundamentals.exchange_id in pipeline is often different from the security object exchange attribute.
Fundamentals.exchange_id is None for 30% of them here.
For one thing it means for example one can't rely on Fundamentals to filter for NASDAQ only because that would miss 588 of them this day.
'''
2019-04-18 05:45
- - - - pipeline - - - -
Stocks 8708
Exchng Count
NYS 2907
NAS 2838
None 2655
ASE 299
ARCX 8
BATS 1
- - - - from symbols - - - -
Stocks 8708
Exchange Count
NYSE 4981
NASDAQ 3427
BATS 300
- - - - pairings - - - -
Pairing Count
NYS NYSE 2906
NAS NASDAQ 2838
None NYSE 1768
None NASDAQ 588
None BATS 299
ASE NYSE 299
ARCX NYSE 8
BATS BATS 1
NYS NASDAQ 1
'''
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data import Fundamentals
from quantopian.pipeline.filters import Q500US, Q1500US, Q3000US, QTradableStocksUS
def initialize(context):
'''
ALL
QTU
Q3000
Q1500
Q500
'''
# The one to use this time, copy/paste:
context.collection = 'ALL'
attach_pipeline(make_pipeline(context), 'pipeline')
def make_pipeline(c):
if c.collection == 'ALL' : limit = None
elif c.collection == 'QTU' : limit = QTradableStocksUS()
elif c.collection == 'Q3000': limit = Q3000US()
elif c.collection == 'Q1500': limit = Q1500US()
elif c.collection == 'Q500' : limit = Q500US()
return Pipeline(
screen = limit,
columns = {
'exchange': Fundamentals.exchange_id.latest
}
)
def before_trading_start(context, data):
out = pipeline_output('pipeline')
log.info(' - - - - pipeline - - - - \nStocks {}\nExchng Count\nNone {}\n{}'.format(
len(out),
len(out[out.exchange == None]),
out.exchange.value_counts(),
))
log.info(' - - - - from symbols - - - - ')
count = 0
exchngs = {}
pairs = {}
for s in out.index:
count += 1
if str(s.exchange) in exchngs:
exchngs[str(s.exchange)] += 1
# example
if 0 and str(s.exchange) and out.exchange[s] not in str(s.exchange):
print('{} pipe {} sid.exchange {}'.format(
s.symbol.rjust(6),
out.exchange[s],
str(s.exchange),
))
else:
exchngs[str(s.exchange)] = 1
pairstring = str(out.exchange[s]).rjust(7) + ' ' + str(s.exchange).rjust(7)
if pairstring in pairs:
pairs[pairstring] += 1
else:
pairs[pairstring] = 1
log.info('Stocks {}'.format(count))
log.info('Exchange Count')
for e in exchngs:
log.info('{} {}'.format(e.rjust(8), str(exchngs[e]).rjust(6)))
content = ''
for p in sorted((value,key) for (key,value) in pairs.items()):
content += p[1] + ' ' + str(p[0]).rjust(5) + '\n'
log.info(' - - - - pairings - - - - \n Pairing Count\n{}'.format(content))