Hi,
I'm just beginning to familiarize myself with the Quantopian API and am trying to implement a simple cap-weighted index (i.e. a quasi S&P500 index). At some point in my algorithm I need to sum the constituent market caps and divide to get my weightings. I'm trying to figure out where is the appropriate place to do that and how; whether I should calculate the weights in initialize() via a factor, or whether I should calculate the weights in a scheduled rebalance(). I apologize if this has already been answered, but the pipeline API seems fairly new and of the examples I've found so far, all have used equal weightings.
If I've setup my pipeline in initialize() like so, screening for the top 500, is the pipeline data actually "screened" at this point?
def initialize(context):
pipe = Pipeline()
attach_pipeline(pipe, name = 'pipeMktCap')
# add market cap custom factor
mktCap = MarketCap()
pipe.add(mktCap, 'mktCap')
# filter for the 500 largest by market cap
pipe.set_screen(mktCap.top(500))
Could/should I just do something like this to get the sum and then add a factor with the weights?
totalMktCap = pipe.sum('mktSum')
mktWgt = pipe['mktCap'] / totalMktCap
pipe.add(mktWgt, 'mktWgt')
Or would I need to apply a "mask" to the pipe.sum method? I don't really understand what the pipe is, i.e. what it contains, when still in initialize(). Once you've accessed the pipeline in some other function like before_trading_start() via pipeline_output() it's a Pandas data frame.
Alternatively, I could calculate the weights in a scheduled rebalance function which seems like the more appropriate place to do this. If so, where do I need to access the pipeline via pipeline_output? For example, do I need to do it in before_trading_start() or could I just put this in my rebalance(). As an aside, could I output from the pipeline in both places and should I always get the some output?
I've obviously got a lot of questions regarding details of the pipeline, but to begin, I'm just trying to figure out the appropriate method of calculating these weights.
Thanks.