Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Indexing Mistake

I'm very new to Quantopian and was just trying to make an Small-Cap Index using equal weighting and a short on the SPY. Somehow I ended up making purchases of 200MM the first day (in a 1MM portfolio). At the end I somehow have 4,000,000% return over 2 years. I think I did something wrong...

2 responses

I'm fairly new to this myself, and some things will need a better eye. However here is what I discovered:

  • SPY is being shorted every month
  • Weights = 0.5/len(context.top_500) is "broken" I'm not sure if it's a value or type conversion issue.
  • Weights is ordering 0.5 (50%) for every security
  • for sec in data is your universe, not your new_secs. (I think you are buying into existing positions)
  • Dat inconsistent comment nightmare (ಠ_ಠ)

I've cleaned up the comments, made some changes to weighting and ordering in the example below.

I have no idea why SPY is not shorting every month. From my experience you should be shorting 500k every month.

edit Also, you were trying to trade stocks not in data, so I added if sec in data:

I was trying to re-balance existing positions every month to still make up an equal portion of the portfolio (that's why I used order_target_percent and used data, because i wanted both to buy new positions and re-balance existing ones). The same goes for the SPY (I wanted to ensure that it was being balanced to the same percentage of my portfolio value every period).

I don't know why the weights ended up as 0.5, (I think because these odd objects used to hold equities and such sometimes return length of 1 unless you do [sec for sec in X] which is quite new to me) since context.top_500 should have held some sort of list of 500 or so stocks.

The comments were nightmarish (I'd never do that sort of thing in finalized code) but since I'm still learning the language, I was putting incomplete docstrings in the code, as well as commenting out lines of code that I replaced but thought I might need later (if the replacement code worked worse), and I was using triple quotes to write personal notes to myself, i.e. for the difference between data, get_open_orders(), context.portfolio.positions, and context.the_secs. I still don't really know what the difference between all of these are, in terms of the data they hold, the form they hold it in (or how to comfortably retrieve it) since most of them (with my log.info() functions) seemed to start at around 500, and continuously go upwards as the code ran, which led me to believe that none of my stocks were ever sold.

Anyhow, thank you so much for your help!