Back to Community
Fundamental analysis using Pipeline

Hi all,

I attached a backtest of a simple fundamental analysis using the pipeline. I used P/B, P/E, Roa, Roe and Roic and some filters like market cap, momentum and volatility.
I think the result is good but maybe it might be maximized grouping the stocks by sector before rank them but I don't know exactly how to do it.
For example technology Sector has very high P/E compared to consumer defensive one so I think technology stock should be ranked within their Sector and so on for all other Sector and ratio.

Thanks

Michele

Clone Algorithm
48
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5a097d8d28aefd403823a7b0
There was a runtime error.
6 responses

Hi Michele,

The rank attribute method can take a groupby parameter, which should be a pipeline classifier. When you set this parameter, the rank will be computed across individual buckets corresponding to the different classifications provided.

In the attached modified version of your algo I applied this parameter to the pe ranking. You will notice I removed the Sector CustomFactor and used the built-in classifier instead, since using a factor for the groupby parameter will cause an error.

I hope this helps you continue with your research.

Clone Algorithm
14
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5a09c79f0feb853f1d07934c
There was a runtime error.
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.

Thanks a lot Ernesto!

It is exactly what I need!

Hi @Michele,
I am relatively new to Quantopian and to python, but i am particularly interested in using Fundamentals data, as well as hoping to encourage other people here to do so too. With that shared interest, i looked at your algo to see what it is doing, in the context of what Q is also looking for. I noted that most of your gain is pure beta, i.e. you are mainly just tracking the overall market. So i re-framed your algo in the context of an Equity Long-Short strategy. After doing that (and using the same time period & account size) your beta comes down nicely from 0.99 to -0.06, Sharpe goes up from 0.62 to 0.82, and max drawdown comes down very nicely from 50.41% to 6.73%. What is left is an expression of what you are actually getting from your momentum & Fundamental factors. [Note: this did not include Ernesto's additional suggestions above]

Lots of people have been looking for years at exactly the same fundamental factors as you have, and used in the same way, so it needs something more creative now. With a little experimentation on your part, both with other fundamentals and with your representation of the momentum factor, you should be able to take this modified version of your algo, improve it, enter it in the Q contest and get within the top 10%, if that is what you are interested in. Enjoy ;-)

Please, in return, could you take a look at the thread "No Price Data At All" and at the one called "Fundamentals ... python... help", see if you can use that to help you further, then make some improvements on my code there to help me and other people who are interested in Fundamentals. Good luck & best wishes, Tony.

[My market-neutral EquityLS version of your algo attached below].

Attached - market neutral, DD gone, now ready to work on improved Fundamentals & momentum.

Clone Algorithm
15
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5a0a95eeeedb1b4574fb7fd7
There was a runtime error.

Hi Tony,

thank you for your hint. I was working on this because even though the draw down is gone the return from 2009 is very low.
I worked on a short side model but I don't know yet how to mix it up with the long side I attached above (i did not include grouping by Sector cos it doesn't improve the model).

As you can see in the backtest attached, it helps during bear market but during the bull market is flat most of the times. So I would like to mix up the long and the short versions (if there are no short position the model can be long 100% otherwise 50% long and 50% short).
I don't know how to manage the two different screens for long and short version. I will post the backtest of the mixed model when I will fix this issue :)

Clone Algorithm
6
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5a0c8ab7971e1340b507a776
There was a runtime error.

Hi @Michelle,
What i do for my own personal trading (long only in selected stocks) is quite different to what i do for the Quantopian contest (market-neutral LongShort) and i keep these strategies separate because the constraints are quite different. My suggestion would be for you also to work on 2 separate versions, one being the way you describe: long 100% otherwise 50% long and 50% short, or perhaps you could even consider using long 100% in bull-market conditions and short 100% (or use inverse ETFs) in bear-market conditions for your own personal use, and a separate version suitable for entry in the Quantopian contest (assuming you also want to do that) which will be market-neutral with equal long and short positions at all times.

In the personal version, your goal presumably is simply to maximize your ratio of return to DD, irrespective of correlation with the overall market (i.e. beta) and to use a relatively small number of equities. Stopping drawdowns as you have done, is a great place to start.

In the version for Q however, it is important that beta remains as small as possible and that you use a large number of equities. The returns in this case will be smaller, but that's OK. As long as beta is small, alpha & Sharpe are large, and DD is very small, then Q can leverage it up as they want to achieve their desired portfolio objectives. The EquityLongShort version i provided does those things very well.

In both cases now, the goal is to find the stocks with the best potential to rise in price (i.e. those with the BEST fundamental value relative to current price) for going long, and to find the stocks with the best potential to fall in value (i.e. those with the WORST fundamental value relative to current price) for going short. The easiest way is just to rank the stocks and then take the top of the list for Longs and the bottom of the list for Shorts. Anything else (e.g. MktCap, trading volume, etc) can go in as filters. Of course you also want to do the same sort of ranking with regard to momentum as well, but my suggestion is to simplify the problem by working on the momentum part and the fundamentals parts separately. You can definitely improve the momentum part by using (and combining) some different timeframes in addition to the one you are already using.

The fundamentals part is somewhat more difficult and will probably involve a lot of trial & error, with only small improvements at each step. Whether you use my "Fundamentals ... python... help" NoteBook or some other method, i think it helps a lot to be able to visualize what the Fundamental data actually looks like over time and how the changing fundamentals correlate with price gains.