Back to Community
How to do a hedged algorithm

Hello I want to know how to have a hedged algorithm. For the contest is really important to be hedged but I know how to be hedged combining options and stocks. I dont know how to do it only with stock and how to code that.

8 responses

A simple way to do it is using 0.5 leverage for long orders and balancing it with SPY at -0.5 leverage.

I use 0.5 leverage in my positions.
You mean the SPY etf? Etfs aren't allowed in the contest.

This is one of my algorithms, it's hedged and doesn't trade ETFs. https://www.quantopian.com/posts/momentum-slice

@José Álvarez "Etfs aren't allowed in the contest."

That isn't exactly true. ETFs ARE allowed in cntests just not leveraged ETFs See the contest rules here https://www.quantopian.com/open.

Actually, bond, currency, commodity, international, and especially the volatility ETFs can be used in interesting ways because they often have low correlation with US stocks.

Hello,

In general using ETFs to hedge isn't as good as buying a large number of stocks short. The reason for this is that in buying the ETF you incur slippage risk, position concentration risk, and estimation risk in hoping the ETF will always do what you want. The purest and simplest way to achieve negative exposure to the market is to invest in many stocks short. This also diversifies your holdings. On the other hand buying many things can incur more transaction costs, so depending on your capital base it can be better to buy an ETF.

For Quantopian, unless you're building a pairs trading strategy (which is a separate discussion), we want strategies that are hedged across many many instruments. Number of names traded is a big factor in our allocation process. I recommend using the optimize API to hedge yourself. It's super easy and takes only a few lines of code. The optimize API accepts the output of your forecasting model and some constraints that the final weights must satisfy (such as being beta hedged), and computes an estimated optimal set of portfolio weights. Doing it this way frees you to think more about your actual model that decides what to buy. Here are some resources to help you implement this quickly.

https://www.quantopian.com/lectures/example-long-short-equity-algorithm
https://www.quantopian.com/posts/optimize-api-now-available-in-algorithms
https://www.quantopian.com/posts/wsj-example-algorithm
https://www.quantopian.com/tutorials/pipeline

If you are unfamiliar with the notion of cross sectional alpha factors and optimization, I recommend that you listen through our podcast collaboration with Chat with Traders.
https://www.quantopian.com/posts/quantopian-plus-chat-with-traders

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Thank you all for your help, I checked all your contributions. I'm starting to test a hedged algorithm but I'm worried about something. The algo is considered hedged even if the amount of money invested in the long positions and in the short positions is not equal?. For example if I have 5000 dollars in short positions and 5500 in long positions. I mean the diference it's not huge but I haven't been able to reduce it.
I believe I can use the max_short_position_size and max_short_position_size from the Example: Long-Short Equity Algorithm: https://www.quantopian.com/lectures/example-long-short-equity-algorithm

Being dollar neutral is not always going to provide precise market beta neutrality (hedged against market beta). Sometimes there is a bias that causes the stocks you selected long and short to have different betas to the market, and so being dollar neutral will actually not be beta neutral. If the difference is small then you can be slightly non-neutral in either dollars or market beta to within set tolerances and you'll be okay. However in general you want to figure out what about the alpha is causing the bias and figure out if you can structurally fix it. If you can't simultaneously achieve dollar and beta neutrality to within a smallish tolerance, then you need to fix your alpha.

Equal amounts long and short is "dollar neutral"
Beta-to-SPY of 0 is "market neutral"

Your example is still hedged to a large degree but not completely. It is most likely not totally market neutral, and it's not dollar neutral either, but really it's not that far off. That said, the market has an average 8%/year positive bias, so it makes complete sense to me to put more emphasis on long positions.

For the Q hedge fund they want dollar neutral and market neutral. For the Q Open contest though it's less strict -- it's enough to have beta between 0.3 and -0.3 (though as close to 0 as you can if you intend to win) and I'm not sure what the requirement is for long/short balance but it definitely does not need to be dollar neutral. If you're getting better stats with more weight towards longs than shorts, I'd say just try to enter it into the contest and if it's not automatically rejected it's probably fine. So long as you are hedged, how much you are hedged doesn't seem to affect rankings at all.