For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
CrossSectional Mean Reversion Strategy

Here is my crosssection 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])

Thanks for the contribution and nice post.