Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Newbie trying to replicate mean reversion strategy.

Hola, I'm pretty new to using quantopian and wanted to try and replicate the simple strategy described here: https://alvarezquanttrading.com/blog/simple-ideas-for-a-mean-reversion-strategy-with-good-results/

I'd really appreciate any feedback on the correctness or simplicity of the implementation of these rules.

For the period stated in the article above the returns/dd look quite consistent. My backtest is potentially a more realistic simulation since it actually simulates the limit orders, as opposed to doing a MC to select a random subset of the setups that meet the entry condition and averaging.

Interestingly, the strategy has completely broken down since and exhibits a 55% dd! Having a look at some tweaks to see if I can improve that.

Appreciate any ideas.

4 responses

Ok, so one thing I just realised is that my attempted control of max_positions=10 does not work since there is a race between the filling of open limit orders and the cancelling of open ones. This means in the rare case when many limit orders are filled in the same bar I can get way over leveraged. I'm not sure if there is a way to cap leverage using the framework such that orders would be rejected?

One approach to limiting positions, and therefore leverage, is to only try to place orders for the number of securities needed to keep the number of positions equal to 'context.max_positions'. Maybe something like this:

def create_limit_orders(context,data):  
    open_order_qty = context.max_positions - len(context.portfolio.positions)  
    if  open_order_qty > 0:  
        # open the first positions in the list up to the open_order_qty  
        open_these = context.setups[0: open_order_qty]  
        for setup in open_these:  
       ....the rest of the code...

See the attached backtest. The quantity of positions never goes above 10 and leverage never above 1 (I added some record logic to show those values on the data graph). One issue with this simple approach is many times the limit orders don't fill and therefore many times the portfolio doesn't use all its cash (ie leverage is way below 1). Another improvement could be to choose the 'best' (however one defines that) securities to try and purchase and not simply the first in the list. It's a start.

Good luck.

Hi Dan,

Thanks for your reply.

It's a bit tricky since often there are 100s of setups and only a few will hit the entry criteria. Since the strategy requires all the limits to be entered then above the above modification doesn't do it justice.

An "on_order_filled" callback would solve the problem, as you could immediately cancel all open limit orders from it when the Nth one was filled. Otherwise we can only approximate. I can keep the leverage down just by only entering a fraction of the limits but that's not the strategy.

Another approach I tried was to make the filters more aggressive to reduce the number of setups, this is working well and I would almost trade it. It's just a little sad you can't replicate the original strategy exactly on Q.

Cheers

I also tried (with limited success) replacing the limit orders with market orders which get submitted as soon as the entry criteria is met.