9 ms·
Show HN: I discovered a trading algorithm that returns ~24.85% annually
- Kibae 5y agoThis is a simple trading algorithm I discovered that operates on the Vanguard sector ETFs. This backdating algorithm provides on average a return of ~0.0878% for each trading day, or ~24.85% annualized return assuming 253 trading days per year. ## The Algorithm This algorithm is really simple. 1. On day `n`, determine which ETF gave the highest return 2. On day `n+1`, short sell the previous day's highest performing ETF at market open and close your short position at market close. Because this algorithm operates on Vanguard's 11 Sector ETFs, it is resilient against the volatility of individual stocks. ### Caution Hindsight is 20/20 and because this is a backdating algorithm, similar results are not guaranteed in the future. Use at your own risk.
- chasebank 5y agoI forked your project and added a financial metrics analysis package. .067 sharpe ratio, .11 sortino, largest drawdown was ~41% Not very good numbers. Fun stuff though! [0] https://github.com/maxto/ubique https://github.com/maxto/ubique
- jalopy 5y agoIf there a link to your fork?
- fullstackchris 5y agoall forks on GitHub are public, i took the liberty and did some snooping, the only fork which had some new commits was this one: https://github.com/jesshowe/SectorTradingAlgorithm https://github.com/jesshowe/SectorTradingAlgorithm
- akg_67 5y agoCheckout strategy in this book. It has a similar strategy but overlays with 200 day SMA and RSI. Larry Connors, Buy the Fear Sell the Greed
- belter 5y agoNow add trading commissions :-)
- Kibae 5y agoI decided not to add trading commissions because most brokers have removed them after Robinhood entered the market.
- verdverm 5y agoTaxes would be a good one to add
- mypalmike 5y agoThis would all be short term trading, so it would be counted as ordinary income, which is bracketed.
- hungryforcodes 5y agoIf one of my algos makes money, I'm happy to pay.
- throwaway4good 5y agoThere is still a spread.
- throwaway4good 5y agoAnd shorting is not free.
- ucha 5y agoThere is no spread when you trade at the open and the close. There could be slippage (you can create a market impact) but it would be minimal on sectorial ETF because they are extremely liquid.
- Animats 5y agoShort selling, though, incurs an additional borrowing cost. That's been going up for ETFs since 2020. "The price of short-selling U.S. exchange-traded funds has jumped dramatically since the beginning of March as investors seek to stem heavy losses in the wake of the coronavirus epidemic, according to data from S3 Partners."[1] This needs to be re-run with the borrowing costs added. The fact that EFT short selling costs have gone up indicates that others are active in this space, which means the profit opportunities for a simple algorithm have probably been already taken. [1] https://www.reuters.com/article/us-usa-stocks-shorts-etfs-idUSKBN21642I https://www.reuters.com/article/us-usa-stocks-shorts-etfs-id...
- ArtWomb 5y agoThis is mean reversion, right? Essentially fading market volatility. I recall an article on Bloomberg about a quant fund using VIX ETNs to implement something similar. Gradually adding short positions as volatility rises. Knowing it will dissipate once turmoil subsides. My recommendation: try entering a trading contest on Alpaca ;) https://alpaca.markets/data https://alpaca.markets/data
- Kibae 5y agoIt looks like it is mean reversion. This is my first time hearing that term. The way I discovered this algorithm was initially I wanted to buy the previous day's best performing sector ETF, with a hypothesis that the momentum would continue. But I learned that it actually ended up losing money. So I decided to inverse the algorithm. There are still a few optimizations I can test out, e.g. Buying the previous day's worst performing sector.
- Grustaf 5y agoNo offence, but how can you have spent any time trading without knowing about mean reversion? It's the most basic and well known phenomenon in trading, along with momentum.
- sowbug 5y agoNot everyone learns by studying prior art. Some people's brains are wired to need to get their hands dirty before an abstract concept or solution makes sense. I'm sure that in the ~60 seconds I wrote this post, some brilliant future software engineer just reinvented binary search.
- MR4D 5y agoUnderstanding and knowing about mean reversion is essential to anyone writing an algorithm used in real world finance, along with a bunch of other terms. It is also clear from other posts by the author that this has not been tested in the real world, only back tested. Practitioners largely consider back testing to be somewhat irrelevant as it’s hard to achieve real world results that compare favorably to the back test. It is essentially over fitting the curve.
- xwdv 5y agoYou did not discover a trading algorithm that returns ~24.85% annually. You massaged an algorithm until it produced a 24.85% annual return training on historical data. Come back when you are ready to claim you have made ~24.85% per year with an algorithm you created 5-10 years ago. Deny it, Downvote it: Destiny still arrives.
- Exuma 5y agoSerious question: I fully understand the idea of historical algorithms being no true indicator of the future. With that said....... If an algorithm consistently performs over 20+ years of data (through multiple black swan events, multiple major events), then why is it not safe to assume it likely will continue going forward? Wouldn't 20 years of "evidence" be a huge amount, such that future events likely wouldn't deviate much from that...?
- AS37 5y agoOverfitting, even on a 20 year dataset.
- Exuma 5y agoInteresting My ML knowledge is somewhat rusty... does overfitting occur more often on models with many input parameters (ie.. neural networks). His algorithm seems very simple, without really using ML at all, it's more of just a procedural 1-2-3 step thing, with no actual learning. Can you explain how overfitting works into his algorithm?
- dmillar 5y agoThere's no overfitting in the traditional/model sense here. This is a pretty rudimentary momentum strategy (long best performers). Implementing this on any kind of scale would be expensive to trade since it rebalance's daily. For momentum, Jegadeesh-Titman paper is much of the foundation for these types of strategies, if you're interested. But as others have pointed out, the "smart money" saturated this trade decades ago.
- Exuma 5y agoWhat software do you use to implement algorithms like this? Do you have to write your own python/other scripts and interact with trading API's for whatever service you use, or are there nice pre-written open source trading algorithms that make building stuff like this easier.
- Kibae 5y agoI just downloaded historical data on Yahoo Finance and parsed the CSV files. There are definitely more efficient ways to do this.
- droobles 5y agoDoes the endpoint on Yahoo Finance just download the CSV file? Could use a node HTTP fetch go nab the latest CSV, check if it's new, if it is throw it in the data set then run the algo. I'm not familiar with Yahoo Finance so I'm not sure how feasible this is.
- pc86 5y agoSince the question was "How do you implement this" I assume they meant how do you automate the actual trading, not how do you run a backtest.
- asperous 5y agoThere are cloud services which will run your trading algorithms. Here's one I found just with a google search: https://alpaca.markets/ https://alpaca.markets/
- happytrader 5y agoAs previously said, this is probably just mean reversion. It’s a commonly used signal that appears to print money all the time, until you take transaction costs into account. If you want to have even more fun, try back testing this same idea on intraday data. Your performance should look phenomenal, and your Sharpe ratio should be well above 10.
- throwaway4good 5y agoThe algorithm of just selling the best performing etf of yesterday???
- Kibae 5y agoYup
- throwaway4good 5y agoIt is a mean reversal bet. It can work. But what if one of the ETFs goes to zero? Then the algorithm will continously buy that all the way down. Erm - edit - scratch that: But what if one of the ETFs goes to the moon? Then the algorithm will continously sell that all the way up.
- repsilat 5y agoAs I read it, the strategy never goes long -- it's always simply short one ETF. The bad cases look like "one sector consistently outperforms", or "leading sectors have bull runs of consecutive days before losing steam".
- throwaway4good 5y agoSorry guys - it should have said: All the way to the moon.
- throwaway4good 5y agoThe algorithm has a buddy: The algorithm of just buying the worst performing etf of yesterday.
- deleted 5y ago[deleted]
- panarky 5y ago
- ffggvv 5y agowhat time period did you backdate over? i’m curious what the backdated return would be for different time periods
- Kibae 5y agoJanuary 30, 2005 - June 6, 2021. You can try sampling different time periods by downloading data from Yahoo Finance. I may need to make the code more robust because there is some data that I hard-coded just for the dates I selected.
- frakkingcylons 5y agoHook it up to interactive brokers for a year and update us!
- lend000 5y agoCongrats. While it can be exciting to come up with a profitable algorithm, publicizing a mean reversion algorithm is counterproductive if you intended to make any money with it. The returns here are low enough that it could stay under the radar for a while, but all the same. Some other metrics to measure your performance are drawdown, best month/worst month (to see if a small number of events account for the majority of returns), and Sharpe ratio. As other commenters said, try backtesting with fees/slippage. Even if there aren't fees now, you should include fees at points in history when there were higher fees. HFT's have been forced to tighten their spreads as retail traders have become more liquid with lower/nonexistent fees, so that will affect any mean reversion strategy being tested across fee change periods. I do like the idea of spot mean reverting on large indices. Takes a lot of risk out of it (while a company can tank overnight, any decently weighted index will lack that volatility).
- dezmou 5y agoI like those naive way to test trading bot, you can check my attempt here, but mine doesn't work :) https://github.com/dezmou/CryptoGPU https://github.com/dezmou/CryptoGPU
- Majromax 5y agoAverage return is just one statistic. You can earn an arbitrarily high daily average return by taking an ordinary strategy (e.g. buy and hold the S&P 500) and applying large amounts of leverage. Returns will be great until the strategy blows up. What was the volatility of this strategy? When backtested on the historical data, what was the maximum drawdown? What happens when trading costs or slippage (buying at the ask, selling at the bid) are modeled additionally? 252 trading days times two trades per day (short sell at open, buy to close at close) is a lot of trades, and execution quality will be very important. Does this strategy hold up with week-long holding times?
- jliptzin 5y agoHe is trading Vanguard Sector ETFs, I doubt there is any issues with execution quality or getting blown up by a rise in volatility.
- X6S1x6Okd1st 5y agoAt sufficiently high leverage any volatility is high enough to bring on a total loss
- Majromax 5y agoAny issue with execution quality would really hurt profitability, however. The yearly average rate of return is noted as about 25%. If there's a 0.1% slippage on the buy and sell sides, over a year's worth of trades that profit is gone. And I'm not worried about "getting blown up" by a rise in volatility (although since this is fundamentally a short position it would be blown up by a theoretical 100% rise in a sector ETF), but I'm more interested in the risk-adjusted return or Sharpe ratio. Since individual sectors are less diverse than the market as a whole, and since this strategy invests (shorts) one sector at a time, I would expect it to see greater day-to-day variability even before the reversion to mean comes into play. I'm curious about how much greater.
- zucker42 5y agoOver what time period was the 24.85% measured? This seems like it would do better during time periods where stock prices are volatile and/or not increasing, so if the returns were measured over the last year, then results could be misleading.
- ucha 5y agoThere are so many misconceptions on this thread about what makes a good quant trading strategy. First of all, if you're shorting US equities and making 25% annually, that would be awesome. Heck, even being flat would be great because a strategy that is long SP500 could also short your equities and be delta-neutral and likely have a much lower volatility for the same return. Second, so many people are mentioning commissions, trading fees, taxes and so on. Commissions and trading fees are much less than 1 basis point per trade if you use reputable brokerages. That would, at most, amount to a 1-2% in fees per year. Market impact matters but opening and closing auctions are very liquid and represent respectively more than 1% and 5% of the daily volume, probably even more for these kind of ETFs. Shorting fees are also quite small, in the range of 0-2% for liquid ETFs. If you don't hold positions overnight which is your case, you also don't pay to short! Finally, here's what really matters. Returns by themselves don't matter. If you want a very high return strategy, you can short a long VIX ETF like VXX but every once in a while, you will be down more than a 100% ; it will bankrupt you if your available capital is less than the value of your short. You also need to look at your Sharpe ratio and maximum drawdown. Anyone somewhat experienced could tell you if the strategy is valid by having a look at plot of returns. If it's not too volatile, it could be a good strat. Edit: addressing shorting fees
- paulpauper 5y agoThis would get killed in a bull market. Financial stocks doubled in 2009
- yaitsyaboi 5y agoYou seem to know your stuff about this. Do you know any good starting points to learn about algorithmic trading? Any youtube channel or book?
- blake1 5y agoIf you’re looking for information on quantitative trading, and are considering relying on a YouTube channel, please just park your money in an index fund and go read about LTCM, Black Tuesday/Friday, Knight Capital, Orange County, and the Global Financial Crisis. Just remember, finance is not like betting on the ponies. It’s worse, because the odds aren’t posted.
- hazard 5y agoThere's kind of a lot of missing pieces here: * This is a simple strategy, which is fine, but also means you are not the only person who has noticed this. Why do you think this makes money? Is there some risk you are being compensated for, or is there some forced trading you're picking up the other side of, or something else? * Which of the common equity factors (https://mba.tuck.dartmouth.edu/pages/faculty/ken.french/data_library.html https://mba.tuck.dartmouth.edu/pages/faculty/ken.french/data...) is your strategy exposed to, and by how much? * What are the basic return statistics of the strategy, like Sharpe and drawdown? * How sensitive is the strategy to parameter variations? What if you sell the second best ETF instead of the best? What if you sell on day n+2 instead of n+1? What if you buy the worst ETF? And about a dozen other things that you should look into before you actually try trading.
- TacticalCoder 5y agoIt is great to see a trading strategy that actually make gains while short-selling, during mostly bull markets. "Everybody looks like a genius during a bull market". Short-selling is something else...
- paulpauper 5y agoPretty smart. Tempt the reader with a 'killer' but incomplete statrgy and make us do all the work to test it and find the flaws so you don't have to. I am sure this is way too good to be true.
- anti-nazi 5y agoi just don't care. it's all worthless to me
- ryanmonroe 5y agoAs others have said, "Average return is just one statistic". When trading, losses hit harder than wins. Go up 50% then down 50% and you're not even, you're down 25%. The degree of overestimation from this mean return -> "annualized return" calculation depends on what the returns distribution looks like. Here's the calculation used in main.js line 77 applied to a very extreme unrealistic example. I simulated 253 days of return percentages from a uniform distribution between -5.5% and 5.6%, and then the actual total return percent, calculated in R set.seed(2020) n <- 253 daily_gain <- runif(n, -.055, .056) total_gain <- sum(daily_gain) avg <- total_gain/n annualizedReturn <- (1 + avg)^n -1 annualizedReturn # [1] 0.2933685 prod(1 + daily_gain) - 1 # [1] 0.1324846 Edit: In reality the actual numbers are likely to be not nearly as different as this example. I chose uniformly distributed returns with a wide range to make the reason against this calculation very obvious. Here's an example return distribution where there's hardly any difference. Normal returns with average of 0.085% and standard deviation of .05 i.e. daily_gain <- rnorm(n, .085/100, .05/100) gives annualizedReturn # 1] 0.2414539 prod(1 + daily_gain) - 1 # [1] 0.2414051 For good measure here's one in the middle where your returns are normally distributed with an average of 0.35% and a sd of .2%, but then you have on average 10 bad days a year where returns are 5 percentage points lower than that distribution i.e. daily_gain <- rnorm(n, .35/100, .2/100) - rbinom(n, 1, 10/n)*.05 gives annualizedReturn # [1] 0.2712024 prod(1 + daily_gain) - 1 # [1] 0.2490317
- hervature 5y agoDon’t know why this is on HN front page given it is an error.
- ryanmonroe 5y agoThe wording in the original comment was too strong, I've edited it. It's probably not best to consider it a plain "error" since this calculation is actually a typical one provided in finance. It's just that you usually look at other stats too rather than just this one, which gives you an idea about its accuracy wrt realized return e.g. Sharpe, Max Drawdown, Skew, Kurtosis
- joneholland 5y agoI’m amused that a node app was used to do this rather than a Google sheet. The =GOOGLEFINANCE function is super handy for building backdated simulations.
- herpderperator 5y agoThis doesn't take into account taxes. If you are consistently profitable on a yearly basis you're expected to pay 100% of last year's capital gains split into Estimated Taxes every quarter going forward. That eats into your return if you're reinvesting profits.
- CawCawCaw 5y agoThankfully we have 0% tax on capital gains.
- ffggvv 5y agovar total = 0; for (var i = 0; i < performance.length; i++) { total += performance[i]; } var avg = total / performance.length; var tradingDays = performance.length; var annualizedReturn = (1 + avg) \* 253 - 1; } I think this math is wrong. You cant just add the daily performances up to get the total return. unless im missing something. Though seems like your "cash" variable is correctly calculated.
- johnwheeler 5y agoSo let’s assume the trick works and everyone catches on. Surely we can’t all get 25% And that’s the problem with any successful algorithm except buy and hold value investing. The latter being hard because it requires doing little, and nobody believes that which requires the least effort to be the best.
- EMM_386 5y agoMany moons ago I ran a site called ETF Timing that automated technical analysis against ETFs. There is so much wrong with this I don't know where to start. But this seems common these days, I think it's due to the influx of inexperienced traders who have no proper statistical background. I'll let ryanmonroe point out the first glaring problem with these types of simple "algorithms": https://news.ycombinator.com/item?id=27415821 https://news.ycombinator.com/item?id=27415821
- joshxyz 5y agoHi, do you have any recommended resources to have good foundationss on these?
- mgamache 5y agoMost mean reversion systems have a high win rate. But the profits from wins are usually small and a losses are large. You have a hard time avoiding losses because you have to let mean reversion trades run and you can't have small stop loss settings.
- bionhoward 5y agoSounds promising if true, and it's cool that you're working on this. What happens when you paper trade with it? Test it going forward and see if it still works, please make another post if you do!
- bronzeage 5y agoThis is a shorting strategy, which means you need to decide on how much of a collateral do you set aside for the short. As you increase the collateral, your real returns are reduced, but as you reduce the collateral, your chances of getting short squizzed increase and you depend more on the intraday volatility.
- wernercd 5y ago"Caution Hindsight is 20/20 and because this is a backdating algorithm, similar results are not guaranteed in the future. Use at your own risk." You don't say...
- lightbendover 5y agoThis is a good time to note that it is incredibly easy to overestimate your ability to determine a trend based on historical data and also incredibly easy to underestimate the likelihood of a never-before-seen occurrence. “The turkey that gets fed well every day relies on that trend continuing and never sees the week before Thanksgiving coming.”
- MR4D 5y agoClearly the author does not have real world experience with this algorithm. If so, it would be clear that buying anything at the opening price is not easy.
- S_A_P 5y agoLast year I had a bunch of cash right as Covid hit so I bought into a few indexes as well as some Energy and Tech stocks. My annual return on these assets was 48%. I don't expect anywhere near that next year though. I just so happened to have bought at the bottom of the market. I probably could have done a return on the order of 100% or more had I researched a bit deeper. I took a gamble on healthcare stocks that just didn't pan out. All that was was just that- speculation and gambling.
- MR4D 5y ago“In theory, there is no difference between theory and practice. In practice, there is. “ –Richard P. Feynman Wise words for anyone wishing to try out an algorithm on Wall Street.
- nl 5y agoUnrelated, but it's surprisingly hard to get backtesting right. I found a bug[1] in a popular (4000 star) stock forecasting model on github where future knowledge subtly leaks into the training data. People seem to keep using the project though! [1] https://github.com/huseinzol05/Stock-Prediction-Models/issues/61 https://github.com/huseinzol05/Stock-Prediction-Models/issue...
- hmate9 5y agoNow Backtest it from 2015 onwards. 4% yearly return. All your returns come from the first 2-3 years.
- htrp 5y agoIt's times like this I wish HN had a downvote button.
- insaider 5y agoMy golf betting algorithm averages just over 20% BUT It's so consistent that by steadily increasing the stakes to take advantage of compound interest it hits ROIs of over 1000% Check it out: https://www.golfforecast.co.uk/profitgraphs https://www.golfforecast.co.uk/profitgraphs
- InfiniteRand 5y agoMy impression is that the idea that the market average beats most strategies in the long term hides the fact that some strategies perform horribly, and some perform great in the long term, but many of the strategies that perform horribly look good in the short term, and distinguishing between the strategies that are attractive in the short term but horrible in the longer term, and those strategies that perform well in the longer term is a very hard problem. Of course, this might be a rationalization of me not wanting to spend the time and effort to construct an effective trading strategy.
- slava_kiose 5y agoWhat platforms does the algorithm work on?