What is [-1],2 in "print('Strategy returns:', np.round(df1['strategy returns'].cumprod()[-1],2))"

Hi, Iam trying to code a ema strategy in DMP-01, on the final in order to display the strategy returns, the returns are been compounded. But i didn't get what is [-1],2 at the last after cumprod.

"print('Strategy returns:', np.round(df1['strategy returns'].cumprod()[-1],2))"


 

Hi Anurag,



Let us break down the code:



df1['strategy returns'] is the column of DataFrame d1 that contains (1+strategy returns).

To get the equity/wealth/cumulative returns at any point, we need to take the cumulative product of this column, and hence we use the cumprod() method for that to get a column containing the cumulative returns. The command would look like:



df1['strategy returns'].cumprod()



As we are trying to print the final strategy returns/equity, we need to fetch the very last value in from the cumulative returns, which we do by using the [-1] index. Recall that in Python [-1] indexing gives the very last value in a sequence. Thus, the command would look like:  



df1['strategy returns'].cumprod()[-1]



However, this would output a value with many digits post decimal and we just want to round them up to to 2 digits after the decimal using the np.round() function. The np.round() takes two arguments: one, the expression that we want to round off, and two, the number of digits post the decimal (2 in this case). So the command would be as follows:



np.round(df1['strategy returns'].cumprod()[-1],2)





 

Thanks Ashutosh, It helps.