Backtrader Community

    • 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/

    Cross-Sectional Mean Reversion Strategy

    Indicators/Strategies/Analyzers
    mean reversion strategy universe spy
    2
    2
    1557
    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.
    • teddykoker
      teddykoker last edited by

      Here is my cross-section mean reversion strategy from my most recent blog post. It uses an algorithm outlined in Ernie Chan's "Algorithmic Trading: Winning Strategies and Their Rationale." For more information see my blog post. Let me know if you have any suggestions:

      
      class CrossSectionalMR(bt.Strategy):
          def prenext(self):
              self.next()
      
          def next(self):
              # only look at data that existed yesterday
              available = list(filter(lambda d: len(d), self.datas))
      
              rets = np.zeros(len(available))
              for i, d in enumerate(available):
                  # calculate individual daily returns
                  rets[i] = (d.close[0]- d.close[-1]) / d.close[-1]
      
              # calculate weights using formula
              market_ret = np.mean(rets)
              weights = -(rets - market_ret)
              weights = weights / np.sum(np.abs(weights))
      
              for i, d in enumerate(available):
                  self.order_target_percent(d, target=weights[i])
      
      

      results

      1 Reply Last reply Reply Quote 1
      • B
        backtrader administrators last edited by

        Thanks for the contribution and nice post.

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