Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Long-Short Portfolio

Based on the Quality Uptrend Post Series, I've been looking at fundamental factors to choose strong/poor stocks without using a very complex stock-picking strategy or overfitting it. Unfortunately, I have still not cleaned it up... This was more meant to backtest a few ideas I've had for choosing stocks before I began trading a very similar strategy live using similar amounts of leverage. I plan to open a small account based on this strategy relatively soon after adding some technical indicators. One such indicator that I have found very successful is trend following. Unfortunately, I could not figure out how to program it in a way I wanted. However, doing it by hand, I've found that trend following adds to the returns. Anyway, I was hoping someone could critique this as I would otherwise plan to use a very similar system with some technical indicator modifications to trade. Thanks!

19 responses

@John, first impression, this looks interesting. I will have to examine the code more closely.

Isn't it really hard to short small caps and penny stocks?

@Radu the universe should only contain relatively large stocks, but I looked through it and there are some small stocks with low volumeish. Since I don't currently use technical indicators, it's not a huge problem as in real life I'd split this over several days as I don't have an entry signal beyond good momentum and being a quality stock. Since I am starting with a small portfolio, liquidity isn't a huge concern (at least for the initial few years of the model) hence why I set slippage to zero. Of course, this doesn't match what the majority of Quantopian users suggest.

Hi John, that ibd_loss function seems intriguing.... can you explain the rational? It seems to try to detect trend and failed trends. Maybe you can enlighten me what the thinking is?

I copied the IBD distribution rules with some slight modifications from:
@Thomas Chang did it much better than I did in a more neat way though.

A slight caveat I've noticed is that performance is vastly reduced if you take out 2009 and 2020; however, this makes sense as following the biggest crashes, you'd expect the largest gains, and doing so would also remove some of the largest losses.
While removing the IBD rules still makes shorting profitable (in previous backtests), it also leads to more dramatic drops. Finally, the control of leverage via simply whether longs or shorts have been getting stopped out is highly unoptimized and simply a proof of concept of an idea I planned to use.

I keep on getting notifications about updates but don't see the new posts.

I have the same issue. Maybe somebody from @quantopian can look at this

@john tzu

You are repeating the same mistakes you made in the algorithm 5 weeks ago.

Too many unused variables and names (~130 lines of the code).

Commenting line 31 only, reducing total return 1000 times from from astronomical 558246.71 % to 565.01 %.

The code does not control either leverage (which sometimes reaches 2.5, gross leverage 1.58)
or position concentration (TVIX-40515 66.16%).

Is it appropriate to use TVIX in a back test since 2003 as it started trading on November 30, 2010?

Same algorithm with only commented line 31.

@Radu Spineanu and @Peter Bakker . Regarding "getting notifications about updates but don't see the new post" That happens when there is spam posted as a reply (eg a bot posting unrelated links). The system sends out a 'new post' email but, subsequently, the post is deleted by our spam filters (or sometimes manually). In this specific case, I believe it was me who manually deleted a response which was linking to a printer repair site.

Sorry for any inconvenience.


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.

The main changes made were coming up with market switching in response to different market environments as loosely suggested by multiple sources. I also implemented but commented out some very poorly designed trend trading and hedging attempts. (although I do hedge position sizes slightly based on how many of my stocks are currently doing bad)
Some things to work on are definitely the entry of the system, when it predicts market turns will occur, position-sizing, and rebalancing. However, it meets my needs (namely choosing stocks during different market conditions) since I plan to combine it with some discretionary trading. The point of the current general calculations of turns is to get it right over 50% of the time and within a couple of weeks, to emulate the correct market conditions. I plan to eventually add some of the other ideas I have on how markets work into it, but I suspect they won’t add much to returns. Another alternative is raising stop losses whenever the market drops 10% or more. I think if I adjust the factors ratios, I can deliver significant outperformance, but this is a venture for later. This is far from an optimal method, as I'll soon post a notebook of one that outperforms significantly; it merely reflects how I think I would realistically trade.
a) In real life, you can take much higher than 4x leverage if you're on a personal account without nominally taking on higher risk via options.
b) TVIX no longer trades so I believe I switched it to UVXY (can't really remember), but it's more of a way to hedge against downturns. In real life, I would likely buy SPY Puts or something of that ilk
c) Slippage is currently of no concern to mean since I'm not a millionaire and have a small personal account. Plus I plan to time entries in a discretionary manner (which has arguable effectiveness)

While this strategy underperforms, it is because I have intentionally added certain aspects that fit how I think markets should work, which are rather obviously wrong. However, I don’t think I’d trust a system that didn’t account for them. I have attached a notebook simply to show that small changes to the initial base can generate substantially higher returns (although I’m far more likely to invest like above since it fits with my personal strategy more). While I’m sure the strategy can be further optimized, I’ve found that simply changing the time you run the algo can affect the system far more than any individual change (which made me decide against further optimization). Because of this, I am somewhat convinced that discretionary trading would be the way to go with this algorithm.

While I am in some danger of overfitting because most of the factors used have logical explanations behind their respective weighting (at least in my mind), I think the risk is less than what would be expected.

@John: I just tried your last version for the past couple of months and seems to underperform. Any clues on what's happening?

If you make the algorithm delta neutral, it performs significantly better (although it still ends up down a few percentage points to the S&P500) In my mind, this is because the market is correcting given its overexuberance in 2020. See 2010 for a past example of this. The main problem though is that because the market never recovered above 20% and the trend indicator is a delayed one by about a month, the algo never hedges. Hence, why some discretionary oversight is needed in my mind.
Finally, you're leveraged on relatively high beta stocks (since it uses momentum), which exposes you more to small downturns that are not hedged against, another reason I would probably only use such a risky style of investing with options.
Then again, you can also assume that it's due to overfitting, but that's a risk I'll have to take. Considering the somewhat irrational exuberance going out of March though (and that returns were the highest of any month then I believe), I probably would've increased risk for the year anyway.

I've also noted that much of these losses would have come from stocks that had soared. If you were there during the gains, you would have experienced a much less severe drop. Unfortunately, this is because the stoploss is much tighter for a trailing stop loss when turning a profit rather than as a normal stop loss to fit how I would trade options when a loss wouldn't matter.

In hindsight, it's very possible the data has been overfitted or some weird shenanigans have been abounding as copy-pasting my code into different IDEs gives me different results. Also, in light that this algo simply doesn't work how I think it should and that I'm not going to live trade with it (preferring a different one instead), I've decided to post it here, although I think this is the wrong non-cleaned up with comments version

Using the in and out strategy didn't work as I had originally planned.

I'm guessing you either didn't read the article carefully or don't know how options work. First of all, he wasn't actually in the red, he was simply ignorant. Second of all, the extrinsic value on an option can be made negligible depending on how you structure your position. Of course, the unfortunate side effect is you're technically paying for higher volatility due to OTM put pricing/volatility smile. Finally, assuming you use the same stoploss I used, you're actually taking less nominal risk (assuming you don't hold options past rapid theta decay) since when a stock drops 15%, the option can either increase in extrinsic value or at the very least, stop you there no matter what. Trades are structured in the most efficient way possible (not necessarily via options). Just don't take on more leverage than you can margin call, so personally, I wouldn't ever risk more than 35% of my portfolio in stocks, but I can still easily achieve the equivalent of 1.2 exposure (which is regularly used in the algo). Also, don't sell naked options.

If you begin to use semi-unrealistic amounts of leverage with the same Fundamentals only strategy, you can obtain some interesting results. This assumes you use leverage to do portfolio hedging via treasury futures similar to modern portfolio theory aiming for a 60-40 split before predicted downturns and somewhat predicting fed hikes. The risk is somewhat manageable as the total losses have been constrained. Beta actually lowers, although annual volatility increases. Based on some rudimentary backtesting of a few different start times/minor changes to variables, returns appear to be somewhat optimized but changing parameters doesn't largely affect returns. (as in returns don't suddenly drop by 100x if you choose to start on Mondays or use a slightly different stop loss)