As 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
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
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
> Go up 50% then down 50% and you're not even, you're down 25%.
It helps if you measure in the right units[0], namely bits, orders of magnitude, or fractions thereof. Up 50% is log(1.5) = +0.58 bits, down 50% is log(0.5) = -1 bits, and indeed 0.58-1 = -0.42, or 1.5*0.5=0.75, down 25%.
0: Well, strictly speaking the problem is that up/down X% isn't even in units at all.
The 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
No, you were correct, arithmetic mean of returns is not standard in finance. If it was, financial crashes would be much worse. They do arithmetic means of "log returns" because 1/n sum(log(r)) = log(product(r)^(1/n)). That is, in log world, the arithmetic mean is the geometric mean.
Is it normal to deal with returns over time as log-returns log(R) rather than simple returns (R - 1) for this reason? The average log return does the right thing.
The uniform distribution is the wrong distribution for this. A normal distribution is better, but also wrong- the normal distribution significantly under-predicts extreme events relative to the stock market.
Can you try it with the Laplace distribution? It's a bell curve like the normal distribution, but has fat tails. Extreme events aren't common, but much more common than with a normal distribution.
The question isn’t backtesting vs no backtesting. The question is do you use the arithmetic mean of daily returns as your metric or use the final return over the entire period. The arithmetic mean hides the fact that large downswings hurt your net return more than they would otherwise seem to, and is therefore misleading, making the strategy looking better than it actually is.
There 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.
I'll spare you the years of money + research, the majority of it all funnels back into "mean reversion" as the core operating principle. Ceteris paribus in terms of information advantages of course.
You can get good at eeking out those advantages and exploiting them, but you're talking about making it your job to find financial "security holes" where the reward is printing cash. And there's a lot of really smart people spending a lot of time doing that. And you have to consistently find new holes as each one gets closed by market participants as you reveal your hand. And then you have to ask yourself if that's how you really want to contribute your time to the world.
If you don't think it's your calling, best to find products/companies you really believe in and make calculated risk-taking investments. As Carnegie would say "put all your eggs in one basket, and then watch that basket"
If 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.
I would expect a youtube channel on stock trading to be similar to a youtube channel about cryptocurrency trading: not just wrong, but actively harmful.
Learning about trading on youtube is different from learning about, say, Python on youtube. If a video on programming is incorrect, your program doesn't work. If a video on equity trading is incorrect, the author of the video can take all your money.
Video views, upvotes and subscribers can all be purchased. If you have profitable trading strategy that reaps newbies who implement a bad algorithm that you publish in a public video, then you have created a perpetual motion machine.
Here's a search for "how to win slots" on youtube: https://www.youtube.com/results?search_query=how+to+win+slot... Thousands of results, millions of views each. Every video is either fake or wrong, by definition. These videos make money for the authors, and the casinos, by taking it from the marks dumb enough to watch and believe them.
Actually, I speak as someone who has lost a fair bit of money on algorithmic trading. The pattern I’ve observed is consistent, small gains punctuated by sizable losses. That was in a big market, but if the market segment is small enough, then I would certainly grant that your acquaintances could have had that success. Does put a cap on the gains.
* 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?
* 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.
Congrats. 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).
This 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.
Pretty 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.
Average 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?
Any 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.
This 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 ;)
It 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.
No 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.
Not 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.
Understanding 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.
there are a whole lot of people losing money in the markets while knowing all about prior art and terminology.
There is a good reason successful hedge funds like RenTech are not hiring finance people but mathematicians who have no idea about "mean reversion" and other pseudo-scientific terms.
Quant funds hire theoretical physicists and mathematicians because it’s the math part in “financial mathematics” that is hard, it’s just easier to teach a mathematician finance and Python/C++, than taking an average finance major or developer.
It’s definitely not the case that they want people to reinvent the wheel. Options pricing for example is a Nobel prize winning discovery.
Many 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":
As 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.
Unrelated, 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!
I’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.
It 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...
What 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.
Does 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.
As 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".
This 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.”
This 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.
Last 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.
My 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.
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.
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.
My 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%
So 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.
Most 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.
Over 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.
Short 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.
If you paired it with a "long everything" strategy (to reduce risk or whatever -- fewer eggs in this basket) the net strategy is something like "buy everything except yesterday's top performer", which can be done quite cheaply. Though fees on sector ETFs are high relative to broader ones.
There 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.
Sounds 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!
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...?
It's very simple, the best strategy for the last 20 years (with hindsight) would be to buy the shares that have performed best over this period, but it wouldn't work better than chance going forward.
Obviously, sitting down and creating that "strategy" would be silly, nobody would think that would work. But if you're using ML or just test a million random strategies, you could end up with something along those lines, a strategy that is optimised for the particular paths stock prices took up to know.
> the best strategy for the last 20 years (with hindsight) would be to buy the shares that have performed best over this period,
The best strategy doesn't seem to me to be straightforward to calculate, even with perfect hindsight.
It might be to continually switch in the short-term most profitable asset, after taking into account the transaction costs and risks of influencing the market.
Sure, it's just an example. Let's say you're looking for the best buy and hold strategy then, the point is that what you actually want in the future is a strategy that doesn't depend on the particular circumstances in the preceding 20 years.
This is the layman’s explanation I was given to me by my finance professor in college:
In the extreme: If you take completely random price fluctuations and plot them on a graph then review them, they are no longer “random” - they become fixed because they’ve been recorded. It becomes possible to look for patterns, of which there will always be some (that’s the nature of randomness). It doesn’t matter the time scale. So your system that works over 20, 30, 50 years of past data is just fitting to the randomly-generated but not-actually-random once recorded set of data. From now into the future, the randomness will re-emerge and the system will fail.
Another way of thinking about it: imagine you click a button to generate a random time series over 20 years, then build a trading algorithm based on that single click. Great - it works! Now click again and regenerate the entire series and see if it still works :)
trading costs, liquidity, structural changes in the economy etc. del prado's machine learning in finance book gives some good insight into common backtesting pitfalls/thinking that are present here. the "safe" assumption if you find a systematic strategy that backtests well is that your actual, go-forward sharpe ratio will print 50% of what your backtests suggest.
I don't know, but it surely depends in part on whether you've published the algorithm. Once a strategy becomes well-known, its gains are likely to be arbitraged away.
On {day/week/month} n, determine which {n} product(s) of {product brand}'s {product type} of {underlying asset type} gave the {highest/lowest/some of each} return.
At n + {a number} {days/weeks/months} go {short/long/some of each} at {market price/limit price} at {market open/market close/time in day} the previously identified securities. Close your position on day n + {a number} afterward at {market price/limit price} at {market open/market close/time of day}.
If you really want the answer to this question on your own, try out his algorithm and you will see what happens. Either he's right, and you make the suggested returns in a year, or he's wrong and you slowly realize why. It will be a learning experience. Don't take anyone's word for it.
The concern with these sorts of strategies is that you might discover the problem quickly. Say for instance if the stock you shorted doubles in value and you lose all your money. Even if there's only a small probability of that happening in a given year, going bankrupt by definition negates all the gains you got previously. Worse still, when you short stocks it is actually possible to lose more than your entire investment so a strategy can work amazingly 99/100 times and still have negative expected value.
I think you're right and overfitting certainly wouldn't explain bad performance of this algorithm, you can't overfit with such a simple strategy. The market can change in response to discovering this strategy though.
There'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.
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
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
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