Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
long short sector neutral strategy based on fundamentals

Hi everyone, this is my first post. I thought I'd share this algo as its based on the work by other generous people on here.

The pe_ratio fundamental is the best one I've found (tried about 4 so far). But its nice to see something so sensible working ok.

The algo allocates long and short to each sector, so each sector is 'neutral'

Since its my first one I'd welcome any comments or suggestions if its doing something wrong that I havent spotted!

Cheers

11 responses

ok, so I tidied up the code a little and tried another fundamental pcf_ratio. However the algo goes nuts and over levers and I havent been able to figure out why yet. I suspected that the order target percent was being passed the wrong values because when you look at the transactions it has bought/sold too many, but that doesn't seem to be the case.

Anyone got any tips on debugging? the logs get truncated so its like flying blind - or, is quantopian backtester not fully reliable !? ;-)

ah, ok, so it seems the order_target_percent will queue up orders if they are too big. This resulted in being over leveraged. So the fix is to keep the initial quantity small.

Add this line to your regular_allocation() function and it will behave itself:

def regular_allocation(context, data):  
    if get_open_orders(): return  

It should be possible to write a dynamic_order_target_percent function that would take a look at any open orders (including partial fills) and adjust targets accordingly, by cancelling previous orders first rather than adding to them. Until then, the route usually taken is to skip any frame that has open orders, like that.

On another note, I think the code does not spend any of the initial capital, the gains are all fund raising and profit starting with shorts, and those represent a risk just like buying stocks with cash are a risk, so if you take shorting into account in a returns calculation of profit/risk, you may find that effective returns--while positive (it is reasonable code)--are actually quite a bit below the benchmark if I'm not mistaken.

A good start.

Thanks for the feedback, much appreciated. However I'm confused about the code not spending the initial capital. Do you mean that because the short position effectively funds the long position the capital is not deployed ? If so I take your point, however the gross capital employed is pretty much double the starting capital while the net is zero. Think I've missed your point tbh... cheers

Maybe this can help, it adds some code that I've been using lately to evaluate algos, without too too much overhead. I'm not claiming it is perfect, just useful, for me. Also I'm not sure if adding shorts in the MaxSpntRet calc makes sense, just something I added today and am mulling over.
Everything can be boiled down to output vs input and here we want that as profit vs input. I'm saying input is: Risk. What was my risk?
Click on the words 'cash' and 'shorts' in the legend to the custom chart below to toggle those off, here, right now, hover over the chart, and then compare what you see to SPY. Hopefully will be an eye-opener.

Edit: Well, ok, if you do that, here's a screenshot, the custom chart will look like this. Then compare those two curve values.

Unfortunately, near the end, $5.8 million in shorts and only $3.1 million in cash to cover them.

Ah, I see your point, and after enter the competition I can see the cash - I was wondering about investing that in 'risk free' assets to boost returns. However does the competition stipulate you have to be able to cover the shorts ?

Do you know if the backtester includes unrealized pnl ? I thought it did...

Also, the long position can be liquidated to cover if necessary (but that probably implies a failure of the strategy). I wonder if the shorts should be restricted to the size of the cash position. Intuitively that should limit losses on short positions somewhat, boost returns and smooth equity curve.

Great input, hopefully my analysis of your points will help, thanks.

looks interesting, but I suspect I've fitted it somewhat. I also changed the fundamental to pcf_ratio (which seems to perform be better than pe_ratio). It accumulates an essentially leveraged long position, and performs terribly during downturns, but the curve is good until 2007 (sry, tested without the info function. will add and check cash position).

Wondering how to balance properly during downturns and need to understand the cash position better.

Very good work here. I like the inclusion of the fundamental data. Since it is beating the SPY it is successful in it's own right. I have a suggestion that might improve returns. If I understand this correctly, it takes the best and worst (PE, or pcf, etc.) depending on your choice. It then goes long 1 and short 1 for each of the 11 sectors. My suggestion would be to find the sector 200 day MA and then if and the sector price is above it's 200 day MA you go long with the 2 best positions. If the sector goes below it's 200 day MA, you go short with 2 positions. So in a strong bull market, why not have 22 positions long, and if say 2 sectors fall below their 200 day MA, then you have 18 long and 4 short, etc. You could even have a zone say if price is within 5% of the 200 day MA that it is long 1 and short 1 to express the uncertainty of the market. In my opinion, why fight the current in a bull market or bear one? You might be able to find inspiration here: https://www.quantopian.com/posts/rules-based-sector-rotation-strategy-based-on-mebane-faber-research Regardless, still very good work.

I just run the algo in the min. mode and got completely different results... For some reason the rebalancing was done only first 3 moths and after that the algo just kept the positions. Any ideas why?

Perpetually open order for a particular security. Since an order ought to be filled well before a month goes by, this makes an effort to exclude any with that problem. As you'll see, some show up again, I'm sure you can figure out why.

Leaving the effort of increasing returns to you, this is just to pitch one possible route for making it over the roadblock and offer some other (hopefully helpful) code examples, notes, etc. (By the way PvR is currently low merely to the degree that shorts are higher than starting capital, sort of, i.e the leverage over 1).