For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

How can I construct matrix of returns for 4 stocks

  • Hello,

    Have a question and hoping someone can help. Basically, I am taking the file with headings and values that looks like this:


    What I have done in Quantopian before is take this spreadsheet of asset values and create 4 different matrix of returns, being 1 month returns, 3 month returns, 6 month returns, and 12 month returns. Then after creating these different matrices, combining them all into one final matrix. I have saved the file in my code as 'spy_ief_uup_gld'.

    The code I used in quantopian was similar to:

     #Use returns history of the 4 tickers with different look back period as factor, and combine into 1 matrix
            self.returns_mat_1m = self.spy_ief_uup_gld[1:4].PercentChange(20)
            self.returns_mat_1m.columns = ['SPY_1m', 'IEF_1m', 'UUP_1m', 'GLD_1m']
            self.returns_mat_3m = self.spy_ief_uup_gld[1:4].PercentChange(60)
            self.returns_mat_3m.columns = ['SPY_3m', 'IEF_3m', 'UUP_3m', 'GLD_3m']
            self.returns_mat_6m = self.spy_ief_uup_gld[1:4].PercentChange(120)
            self.returns_mat_6m.columns = ['SPY_6m', 'IEF_6m', 'UUP_6m', 'GLD_6m']
            self.returns_mat_12m = self.spy_ief_uup_gld[1:4].PercentChange(240)
            self.returns_mat_12m.columns = ['SPY_12m', 'IEF_12m', 'UUP_12m', 'GLD_12m']
            # combine 1 month, 3 month, 6 month, and 12 month returns into one matrix
            X = self.returns_mat_1m.join(self.returns_mat_3m).join(self.returns_mat_6m).join(self.returns_mat_12m)
            # X = X.dropna(axis=1)

    Howver, having a hard time changing the code synax above to suit Backtrader syntax. I was wondering if anyone had any idea on how to construct this matrix with a similar code format to fit Backtrader syntax? Appreciate the guidance and help as always and thanks in advance.


Log in to reply

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.