Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Pairs trading research and execution on backtrader - any tips?

    General Code/Help
    3
    7
    2379
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      tommark last edited by

      Hi, I am new to backtrader. Wonder if there is any existing frameworks on pair trading which I can learn and build from, in python / backtrader space.

      https://www.bloomberg.com/professional/blog/pair-trading-its-complicated-part-i/
      https://www.bloomberg.com/professional/blog/pair-trading-its-complicated-part-iii-this-is-not-your-standard-algorithm/

      1 Reply Last reply Reply Quote 0
      • P
        Paska Houso last edited by

        There is a Pair Trading contributed sample for backtrader in the sources under samples/contrib/pair-trading

        But I recall that the OLS technique it uses may be broken due to changes in the OLS API. This thread tackled it a few days ago:

        • https://community.backtrader.com/topic/734/ols_slope_interceptn

        Apparently add_constant was missing after some API got updated.

        1 Reply Last reply Reply Quote 0
        • T
          tommark last edited by

          Can you please elaborate on the above please. Or anyone else with the knowledge.

          When I run 'pair-trading.py' script, yes I get the error 'NameError: name 'prepend_constant' is not defined', as it brings up the 'ols.py' script.

          Diving into the problem, it stems from the class below. Pardon the code rendering, what should I do to render the code display properly?

          Is there any copy and paste solution I can use to get it working? Think the issue lies in 'sm.add_constant(p1, prepend=prepend_constant)'

          I have referred to https://stackoverflow.com/questions/2820453/display-html-code-in-html and https://community.backtrader.com/topic/734/ols_slope_interceptn/7 but to no avail.

          class OLS_Slope_InterceptN(PeriodN):
          '''
          Calculates a linear regression using statsmodel.OLS (Ordinary least
          squares) of data1 on data0
          Uses pandas and statsmodels
          Use prepend_constant to influence the paramter prepend of
          sm.add_constant
          '''
          _mindatas = 2 # ensure at least 2 data feeds are passed

          packages = (
              ('pandas', 'pd'),
              ('statsmodels.api', 'sm'),
          )
          lines = ('slope', 'intercept',)
          params = (
              ('period', 10),
              ('prepend_constant', True),
          )
          
          def next(self):
              p0 = pd.Series(self.data0.get(size=self.p.period))
              p1 = pd.Series(self.data1.get(size=self.p.period))
              p1 = sm.add_constant(p1, prepend=prepend_constant)
              slope, intercept = sm.OLS(p0, p1).fit().params
          
              self.lines.slope[0] = slope
              self.lines.intercept[0] = intercept
          
          1 Reply Last reply Reply Quote 0
          • T
            tommark last edited by tommark

            Took me 2 full days to realize that I only needed to restart my kernel to reset the namespace after making the edits

            1 Reply Last reply Reply Quote 0
            • jxm262
              jxm262 last edited by

              hi @tommark , I'm getting the same error as you mentioned preprend_constant is not defined.

              Can you please let me know what you meant by restarting the kernel in your case? I was assuming you meant restart the actual python interpreter (or your machine)? I'm not actually running on a Jupyter notebook or anything, so was a bit confused on your comment.

              Anyway, after looking at @jirathh 's last comment here - https://community.backtrader.com/topic/734/ols_slope_interceptn/12, I took a wild guess and changed that line offending line to this -

              p1 = sm.add_constant(p1, prepend=True)
              

              Seems to work, but I haven't dug into any details on the actual root cause yet.

              1 Reply Last reply Reply Quote 0
              • T
                tommark last edited by

                change
                sm.add_constant(p1, prepend=prepend_constant)
                to
                sm.add_constant(p1, prepend=self.params.prepend_constant)

                the 'self.params' should be intact

                jxm262 1 Reply Last reply Reply Quote 0
                • jxm262
                  jxm262 @tommark last edited by

                  @tommark said in Pairs trading research and execution on backtrader - any tips?:

                  self.params

                  ah perfect, that works.

                  thanks @tommark

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    Last post
                  Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors