Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Cross Moving average of the protfolio returns

Maybe its a simple answer .... I want to rebalance my portfolio if my whole portfolio price is less than its moving average. I know to calculate MAVG for each stock in portfolio with data object, but how can moving avergae done for the whole portfolio of stocks returns ?

Thanks,

1 response

You can use the field

context.portfolio.portfolio_value  

to calculate the moving average. This consists of recording down the portfolio value every period (handle_data or some scheduled function) after T number of periods where T is also the length of your MA. Alternatively, you can use

context.portfolio.positions_value  

if you just want to calculate moving average over the pure equity component -- in the case that you have remaining cash.