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.


  • administrators

    What you did is not Quantopian specific. You are using Dataframes. It seems that your returns are simply the PercentChange in a given period.

    There is a PercentChange indicator in backtrader (and you can simply use the closing values)

    It would be along the lines of ...

    class MyStrategy(bt.Strategy):
        def __init__(self):
            self.rets_1m = [bt.ind.PercentChange(x, period=20) for x in self.datas]
            self.rets_3m = [bt.ind.PercentChange(x, period=60) for x in self.datas]
        def next(self):
            # Now you need to get the current data of each indicator, make it into a matrix and join the matrices
            # you probably want to accumulate ...
            ret_1m_mat = [x[0] for x in self.rets_1m]

    But the point is that what it seems is that you actually want to run data and create those matrices in real time, when it seems that would be preprocessing done with pandas.

  • @backtrader said in How can I construct matrix of returns for 4 stocks:

    ret_1m_mat = [x[0] for x in self.rets_1m]

    Hello there,

    Thanks for the response. Much appreciated. Ok so for the second part, to create the individual matrices, I continued your logic and have:

     # combine 1 month, 3 month, 6 month, and 12 month returns into one matrix
            ret_1m_mat = [x[0] for x in self.rets_1m]
            ret_3m_mat = [x[0] for x in self.rets_3m]
            ret_6m_mat = [x[0] for x in self.rets_6m]
            ret_12m_mat = [x[0] for x in self.rets_12m]

    My question is, how would I now go about in combining all of the 4 lines of code above into one giant matrix by joining them all together?

    Thanks in advance for all the help as always!


  • Also, I want to set the name of this giant Matrix that is combined from these 4 lines of code to 'X'............

  • @backtrader

    I am trying:

    X = ret_1m_mat.join(ret_3m_mat).join(ret_6m_mat).join(ret_12m_mat).dropna()

    However, getting error saying:

    AttributeError: 'list' object has no attribute 'join'

    Thanks for the help in advance as always..........

Log in to reply