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

I am trying to compute the percent change for each stock in a for loop. I want to find the 1-month return.
why does this not work?

for stock in context stock:
day30_hist=data.history(stock, "price", 90, "1d")
day30_perc_chg=day30_hist[stock].pct_change()[-1]
Thanks

3 responses

Generally use pipeline instead of 'data.history' to fetch daily data. It's much more efficient. (see https://www.quantopian.com/tutorials/pipeline)

Generally use built in array/dataset methods and not for loops in python. It's much more efficient and easier to read and debug. (the reason you are using Python and not visual basic)

Maybe the attached is something close to what you are trying to achieve?

Thanks Dan,

I was using http://www.the-lazy-trader.com/2015/01/etf-rotation-systems-to-beat-market-American-Equities.html
as a reference for the strategy. I wanted to take the top3 etfs scores each month.

I thought the weights on your portfolio were really interesting. Any reason to the large drop and increase in the backtest?

Thanks,
Quantdog

Ah, didn't realize that the weights could potentially be negative. When normalizing the weights, this is the original code

    weight = context.output.score/context.output.score.sum()

To account for negative weights, it should be this

    weight = context.output.score/context.output.score.abs().sum()

The big dip in the original algorithm was caused by the leverage going VERY high because of this oversight. Attached is the corrected version.