Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Custom Factors? S&P 500?

I am new to using Python and really only have a little programming experience (with VB). I really appreciate the example code as that got me a lot further than I would be otherwise. My goal at the moment with this algorithm is to find the best and worst performers over the previous year in the S&P 500. I have the following questions:

  • Is there a better way to write the custom factor for percent change? Maybe one that allows the window to be changed.

  • When I ran the backtest there appeared to be some inconsistencies on some of the stocks and using close price vs. adjusted close price. For example, in the list of worst performers, ITUB showed a 1-year change of -94%. I added the current and past close prices so I could see what was going on. When I compared it with yahoo finance adjusted close price I noticed that it didn't follow the adjusted close price, but did follow the close price. Most of the others did follow the adjusted close price. Is this a problem with the data? Is there a better parameter to use than close?

  • Is there a better way to get the S&P 500? Currently the screen of top 500 by market cap gives me some non-US equities. Maybe there is a way to screen by US equities?

I really appreciate any help. Once I get this working I will look at different ways of adjusting the portfolio.

2 responses

In my view there is no magic in any index such as the S&P 500 and unless there is some column within Morning Star which tells you on a day by day basis whether a stock is in or out of a given index the the answer to your last question is "no". Not unless you want to create your own list of in and out dates from a source such as Reuters which is tedious but perfectly possible.

More importantly perhaps you should focus on why having the constituents of the S & P 500 is important to you. It is of no importance to me although there are certain stocks I do like to filter out such as geared ETFS. It is possible to filter out the latter here on Q I think although I have yet to confirm that.

Otherwise I am as happy to trade the top 3000 by market cap as I am the 'real" constituents of the Russell, the S & P or whatever.

I actually welcome the diversification of non US shares with their associated currency risks / rewards.

Perhaps ask yourself "why should it be be necessary for me to trade the exact constituents of" .....relevant index.

As to adjusted/ unadjusted stock prices I spent years trading futures..... Which make stocks look like simplicity itself from this point of view.

There are some very good guides on providers adjust for splits / dividends etc and there are definitely big differences between providers. Decimal point precision can also make a huge difference in the deep past with a much split stock like aapl.

There is no short cut. You need to become an expert on back adjusting techniques. It is a tedious and laborious experience but an essential education. Eventually it all becomes plain.