Portfolio Value

Hi,



I want to take out the data of portfolio value in blueshift. So, I took the following print. But in the output log, I am not able to get the entire data as it is for 10 years. How to get the data?

 

def handle_data(context,data):
    print(get_datetime(),context.portfolio['portfolio_value'])


 

At present this is a bit painful. We will eventually introduce download feature for backtest results. But it will take us a while to get there, given we are working on so many other stuff!



Print output is truncated on Blueshift. We have to print within the limits (around 50 rows at a time should work). One way will be to define the analyze function as below

 

def analyze(context, perf):
    monthly_perf = perf.algorithm_period_return.resample('M').last()

We pick this particular column from the supplied 'perf' variable to track cumulative algo returns. We also resample this to monthly data (the supplied data is daily). Then we can print the `monthly_perf' variable in slices in a few runs to print the entire series. For example if you are running 10 year back-test, we will have 120 monthly points and running the same back-test 3 times and printing the [:50], [50:100] and [100:] will print the entire content.
 

Thank you sir.



Actually I would like to calculate 5 year rolling return. Is it possible in coding?



I would also like to know when rebalancing, how many stocks moved out of the portfolio completely? can we get this information through programming?

Printing info is difficult on Blueshift, everything else is pretty easy (mostly)



To compute 5 year rolling returns of your strategy, define the analyze function as before and manipulate the perf data structure. First try printing perf in analyze and see what are the fields available in that data-frame. Then pick one suitable for your purpose. For rolling returns calc, you can extract the period returns as above, multiply with your starting capital to get actual net value levels and then compute rolling returns. See here for how to calculate rolling returns in general for pandas series or frames.



To see portfolio changes, fetch the positions dict (context.portfolio.positions) and keep a record of the keys as a set. In the next rebalance, compute this set again and take a diff to see the changes.

Thank you sir.