Algorithmic trading used to be a very difficult and expensive process. The time and cost of system setup, maintenance, and commission fees made programmatic trading almost impossible for the average investor. That’s all changing now.
We’re excited to announce that Quantopian has integrated with Robinhood, a zero commission brokerage. This partnership has made the process of algorithmic trading, from start-to-finish, completely free.
From initial brainstorming with research, to testing and optimizing with backtesting, and finally, commission-free execution with Robinhood, algorithmic trading has never been easier.
Here’s What Users Get
How To Get Started
If you have an existing Robinhood account, you can begin trading today. If you’d like to open an account, you can sign up directly at Robinhood - the process takes less than five minutes to complete. For more information and video tutorials, our community post has you covered.
P.S. Attached is a sample algorithm that's geared and ready for live trading. It's based off Mebane Faber’s Tactical Asset Allocation. The allocation Faber proposes is designed to be "a simple quantitative method that improves the risk-adjusted returns across various asset classes." You can read the original academic paper from Meb Faber, or the previous discussion of the strategy on Quantopian.
When I give talks about probabilistic programming and Bayesian statistics, I usually gloss over the details of how inference is actually performed, treating it as a black box essentially. The beauty of probabilistic programming is that you actually don't have to understand how the inference works in order to build models, but it certainly helps.
When I presented a new Bayesian model to Quantopian's CEO, Fawce, who wasn't trained in Bayesian stats but is eager to understand it, he started to ask about the part I usually gloss over: "Thomas, how does the inference actually work? How do we get these magical samples from the posterior?".
Now I could have said: "Well that's easy, MCMC generates samples from the posterior distribution by constructing a reversible Markov-chain that has as its equilibrium distribution the target posterior distribution. Questions?".
That statement is correct, but is it useful? My pet peeve with how math and stats are taught is that no one ever tells you about the intuition behind the concepts (which is usually quite simple) but only hands you some scary math. This is certainly the way I was taught and I had to spend countless hours banging my head against the wall until that euraka moment came about. Usually things weren't as scary or seemingly complex once I deciphered what it meant.
This blog post is an attempt at trying to explain the intuition behind MCMC sampling (specifically, the Metropolis algorithm). Critically, we'll be using code examples rather than formulas or math-speak. Eventually you'll need that but I personally think it's better to start with the an example and build the intuition before you move on to the math.
Table of Contents
Andreas originally hails from Sweden, then moved to the United Kingdom for his university studies. In the UK, he studied mathematics and then stayed to pursue a career in the finance industry, before embarking on a graduate degree in mathematical physics. He is currently a PhD student in Spain continuing his journey in mathematics. Andreas stumbled across Quantopian while traversing the web, and was immediately hooked. With no previous background in Python, he started learning how to create trading algorithms. He shares, "I started coding up some basic algorithms and was impressed by how easy it was to get going. There was also a great community forum and tutorials that had answers to most questions." His Python skills improved and Andreas began coding a variety of algorithms and trying different strategies.
Andreas was focused on the data. "For me, quant research is all about the data. Analysing and understanding the data always comes first (and backtesting last!). Quantopian has a number of interesting data feeds (that I hope it will continue growing!). My algo uses some of these data feeds to select baskets of stocks to trade". Quantopian provides 13 years of pricing data and fundamental data, along with 22 (and growing) datasets in the store.
Andreas continues to improve his current ideas and test new strategies using the research environment and backtester. "I know how much work it goes into creating a proper backtesting and research environment, and that Quantopian makes one available to you for free is quite amazing!" He is currently in the first phase of his prize, undergoing a quant consultation session. Afterward, he will enter the second phase and begin trading a $100,000 brokerage account for 6 months, and keep all the profits. We'll write him a check monthly for his earnings!
We've already paid out over $2300 in contest earnings. Are you the next contest winner? If so, submit your algorithm by the next deadline on Nov. 2 at 9:30AM ET to start your 6 months of paper trading.
Every student in every school should have the opportunity to learn computer science.
Code.org is a non-profit dedicated to expanding access to computer science, and increasing participation
by women and underrepresented students of color in this field. They believe computer science should be part of the core curriculum, alongside other courses such as biology, chemistry, or algebra.
We at Quantopian believe in Code.org's vision to bring computer science to every student. To help them achieve this goal, we have decided to donate all revenue generated by our live stream ticket sales for QuantCon 2016 to them.
QuantCon 2016 will feature a stellar lineup including: Dr. Emanuel Derman, Dr. Marcos López de Prado, Dr. Ernie Chan, and more. It will be a full day of expert speakers and in-depth tutorials. Talks will focus on innovative trading strategies, unique data sets, and new programming tips and tools. The goal? To give you all the support you need to craft and trade outperforming strategies.
A live stream purchase will also include first-access to all QuantCon recordings and presentation decks. For tickets or more information, please visit www.quantcon.com.
Strata, the conference where cutting-edge science and new business fundamentals intersect, will take place September 29th to October 1st in New York City.
The conference is a deep-immersion event where data scientists, analysts, and executives explore the latest in emerging techniques and technologies.
Quantopian Talks & Tutorials
Our team will be presenting several talks and tutorials at Strata. The topics range from how global-sourcing is flattening finance, to a Blaze tutorial, to a review on pyfolio and how it can improve your portfolio and risk analytics, to an out-performing investment algorithm on women-led companies in the Fortune 1000.
To see our entire lineup, please click here.
If you would like to attend the conference, RSVP here and enter discount code QUANT for a a 20% discount on any pass.
We hope to see you there!
Authors: Sepideh Sadeghi and Thomas Wiecki
This blog post is the result of a very successful research project by Sepideh Sadeghi, a PhD student at Tufts who did an internship at Quantopian over the summer 2015. Follow her on twitter here.
All of the models discussed here-within are available through our newly released library for finance performance and risk analysis called pyfolio. For an overview of how to use it see the Bayesian tutorial. Pyfolio is also available on the Quantopian research platform to run on your own backtests.
When evaluating trading algorithms we generally have access to backtest results over a couple of years and a limited amount of paper or real money traded data. The biggest with evaluating a strategy based on the backtest is that it might be overfit to look good only on past data but will fail on unseen data. In this blog, we will take a stab at addressing this problem using Bayesian estimation and prediction of possible future returns we expect to see based on the backtest results. At Quantopian we are building a crowd-source hedge fund and face this problem on a daily basis.
Here, we will briefly introduce two Bayesian models that can be used for predicting future daily returns. These models take the time series of past daily returns of an algorithm as input and simulate possible future daily returns as output. We will talk about the variations of models that can be used for prediction and how they compare to each other in another blog, but here we will mostly talk about how to use the predictions of such models to extract useful information about the algorithms.
All of these models are available through our newly released library for finance performance and risk analysis called pyfolio. For an overview of how to use it see the Bayesian tutorial. Pyfolio is also available on the Quantopian research platform to run on your own backtests.
At Quantopian we have built a world-class backtester that allows everyone with basic Python skills to write a trading algorithm and test it on historical data. The resulting daily returns generated by the backtest will be used to train the model predicting the future daily returns.
Lets not forget that computational modeling always comes with some risks such as estimation uncertainty, model misspecifications and implementation limitations and errors. According to such risk factors, model predictions are not always perfect and 100% reliable. However, model predictions still can be used to extract useful information about algorithms, even if the predictions are not perfect.
For example, comparing the actual performance of a trading algorithm on unseen market data with the predictions generated by our model can inform us whether the algorithm is behaving as expected based on its backtest or whether it is overfit to only work well on past data. Such algorithms may have the best backtest results but they may not necessarily have the best performance in live trading. An example of such an algorithm can be seen in the picture below. As you can see, the live trading results of the algorithm are completely out of our prediction area, and the algorithm is performing worse than our predictions. These predictions are generated by fitting a linear line through the cumulative backtest returns. We then assume that this linear trend continuous going forward. As we have more uncertainty about events further in the future, the linear cone is widening assuming returns are normally distributed with a variance estimated from the backtest data. This is certainly not the best way to generate predictions as it has a couple of strong assumptions like normality of returns and that we can confidently estimate the variance accurately based on limited backtest data. Below we show that we can improve these cone-shaped predictions using Bayesian models to predict the future returns.
In the Bayesian approach we do not get a single estimate for our model parameters as we would with maximum likelihood estimation. Instead, we get a complete posterior distribution for each model parameter, which quantifies how likely different values are for that model parameter. For example, with few data points our estimation uncertainty will be high reflected by a wide posterior distribution. As we gather more data, our uncertainty about the model parameters will decrease and we will get an increasingly narrower posterior distribution. There are many more benefits to the Bayesian approach, such as the ability to incorporate prior knowledge that are outside the scope of this blog post.
Now that we have answered the problem of why predicting future returns and why using Bayesian models for this purpose, lets briefly look at two Bayesian models that can be used for prediction. These models make different assumptions about how daily returns are distributed.
We call the first model the normal model. This model assumes that daily returns are sampled from a normal distribution whose mean and standard deviation are accordingly sampled from a normal distribution and a halfcauchy distribution. The statistical description of the normal model and its implementation in PyMC3 are illustrated below.
This is the statistical model:
mu ~ Normal(0, 0.01) sigma ~ HalfCauch(1) returns ~ Normal(mu, sigma)
And this is the code used to implement this model in PyMC3:
with pm.Model(): mu = pm.Normal('mean returns', mu=0, sd=.01, testval=data.mean()) sigma = pm.HalfCauchy('volatility', beta=1, testval=data.std()) returns = pm.Normal('returns', mu=mu, sd=sigma, observed=data) # Fit the model start = pm.find_MAP() step = pm.NUTS(scaling=start) trace = pm.sample(samples, step, start=start)
We call the second model the T-model. This model is very much similar to the first model except that it assumes that daily returns are sampled from a Student-T distribution. The T distribution is very much like a normal distribution but it has heavier tails, which makes it a better distribution to capture data points that are far away from the center of data distribution. It is well known that daily returns are in fact not normally distributed as they have heavy tails.
This is the statistical description of the model:
mu ~ Normal(0, 0.01) sigma ~ HalfCauchy(1) nu ~ Exp(0.1) returns ~ T(nu+2, mu, sigma)
And this is the code used to implement this model in PyMC3:
with pm.Model(): mu = pm.Normal('mean returns', mu=0, sd=.01) sigma = pm.HalfCauchy('volatility', beta=1) nu = pm.Exponential('nu_minus_two', 1. / 10.) returns = pm.T('returns', nu=nu + 2, mu=mu, sd=sigma, observed=data) # Fit model to data start = pm.find_MAP(fmin=sp.optimize.fmin_powell) step = pm.NUTS(scaling=start) trace = pm.sample(samples, step, start=start)
Here, we describe the steps of creating predictions from our Bayesian model. These predictions can be visualized with a cone-shaped area of cumulative returns that we expect to see from the model. Assume that we are working with the normal model fit to past daily returns of a trading algorithm. The result of this fitting this model in PyMC3 is are the posterior distributions for the model parameters mu (mean) and sigma (variance) – fig a.
Now we take one sample from the mu posterior distribution and one sample from the sigma posterior distribution with which we can build a normal distribution. This gives us one possible normal distribution that has a reasonable fit to the daily returns data. - fig b.
To generate predicted returns, we take random samples from that normal distribution (the inferred underlying distribution) as can be seen in fig c.
Having the predicted daily returns we can compute the predicted time series of cumulative returns, which is shown in fig d. Note that we have only one predicted path of possible future live trading results because we only had one prediction for each day. We can get more lines of predictions by building more than one inferred distribution on top of actual data and repeating the same steps for each inferred distribution. So we take n samples from the mu posterior and n samples from the sigma posterior. For each posterior sample, we can build n inferred distributions. From each inferred distribution we can again generate future returns and a possible cumulative returns path (fig e). We can summarize the possible cumulative returns we generated by computing the 5%, 25%, 75% and 95% percentile scores for each day and instead plotting those. This leaves us with 4 lines marking the 5, 25, 75 and 95 percentile scores. We highlight the interval between 5 and 95 percentiles in light blue and the interval between 25 and 75 percentiles in dark blue to represent our increased credible interval. This gives us the cone illustrated in fig f. Intuitively, if we observe cumulative returns from an algorithm that are very different from the backtest, we would expect it walk outside of our credible region. In general, this procedure of generating data from the posterior is called a posterior predictive check.
Now that we have talked about the Bayesian cone and how it has been generated, you may ask what these Bayesian cones can be used for. Just to give a demonstration of what can be learned from Bayesian cones, look at the cones illustrated below. The cone on the right shows an algorithm whose live trading results are pretty much within our prediction area and to be more accurate even in high confidence interval of our prediction area. This basically means that the algorithm is performing in line with our predictions. On the other hand, the cone on the left
shows an algorithm whose live trading results are pretty much outside of our prediction area, which would prompt us to take a closer look as to why the algorithm is behaving according to specifications and potentially turn it off if it is used for real-money live trading. This underperformance in the live trading might be due to the algorithm being overfit to the past market data or other reasons that should be investigated by the person who is deploying the algorithm or selects whether to invest using this algorithm.
Lets take a look at the prediction cones generated using the simple linear model we described in the beginning of the blog. It is interesting to see that there is nothing worrisome about the algorithm on the left, while we know that the algorithm illustrated on the right is overfit and the fact that the Bayesian cone gets at that but the linear cone does not, is reinforcing.
One of the ways by which the Baysian cone can be useful is detecting the overfit algorithms with good backtest results. In order to be able to numerically measure by how much a strategy is overfit, we have developed Bayesian consistency score. This score is a numerical measure to report the level of consistency between the model predictions and the actual live trading results.
For this, we compute the average percentile score of the paper-trading returns to the predictions and normalize to yield a value between 100 (perfect fit) and 0 (completely outside of cone). See below for an example where we get a high consistency score for an algorithm (the right cone) which stays in the high confidence interval of the Bayesian prediction area (between the 5 to 95 percentiles) and a low value for an algorithm (the left cone) which is mostly out of predicted area.
Estimation uncertainty is one of the risk factors, which becomes relevant with modeling and it is reflected on the width of the prediction cone. The more uncertain our predictions, the wider the cone would be. There are two ways by which we may get uncertain predictions from our model: 1) little data, 2) high volatility in the daily returns. First, lets look at how the linear cone deals with uncertainty due to limited amounts of data. For this, we create two cones from cumulative returns of the same trading algorithm. The first only has the 10 most recent in-sample days of trading data, while the second one is fit with the full 300 days of in-sample trading data.
Note how the width of the cone is actually wider in the case where we have more data. That's because the linear cone does not take uncertainty into account. Now let's look at how the Bayesian cone looks like:
As you can see, the top plot has a much wider cone reflecting the fact that we can't really predict what will happen based on the very limited amount of data we have.
Not accounting for uncertainty is only one of the downsides of the linear cone, the other ones are the normality and linearity assumptions it makes. There is no good reason to believe that the slope of the regression line corresponding to the live trading results should be the same as the slope of the regression line corresponding to the backtest results and normality around such line can be problematic when we have big jumps or high volatility in our data.
Having reliable predictive models that not only provide us with predictions but also with model uncertainty in those predictions allows us to have a better evaluation of different risk factors associated with deploying trading algorithms. Notice the word “reliable” in my previous sentence, which is to refer to the risk of “estimation uncertainty”, a risk factor that becomes relevant with modeling and ideally we would like to minimize it. There are other systematic and unsystematic risk factors as is illustrated in the figure below. Our Bayesian models can account for volatility risk, tail risk as well as estimation uncertainty.
Furthermore, we can use the predicted cumulative returns to derive a Bayesian Value at Risk (VaR) measure. For example the figure below
shows the distribution of predicted cumulative returns over the next five days (taking uncertainty and tail risk into account). The line
indicates that there is a 5% chance of losing 10% or more of our assets over the next 5 days.
Today, we are happy to announce pyfolio, our open source library for performance and risk analysis. We originally created this as an internal tool to help us vet algorithms for consideration in the Quantopian hedge fund. Pyfolio allows you to easily generate plots and information about a stock, portfolio, or algorithm.
Tear sheets, or groups of plots and charts, are the heart of pyfolio. Some predefined tear sheets are included, such as sheets that allow for analysis of returns, transactional analysis, and Bayesian analysis. Each tear sheet produces a set of plots about their respective subject.
Here is part of a tear sheet analyzing the returns of Facebook's (FB) stock:
pyfolio is available now on the Quantopian Research Platform. See our forum post for further information.
Notebooks, backtests, and video lectures. All in one spot: https://www.quantopian.com/lectures.
We have launched a free quantitative finance education curriculum for our community. We have held a series of lectures via meetups and webinars that cover the concepts of the curriculum and demonstrate how to use our platform and tools to write a good algorithm. We have also released cloneable notebooks and algorithms for each topic covered. As promised, we have developed a home to house this curriculum. Please check out our new Lectures page and learn more about:
This curriculum is being developed in concert with our academic program, in which we're working with professors from MIT Sloan, Stanford, and Harvard. The education material we're generating is being vetted by professors as they use our platform to teach their classes, so you can expect to get the same materials that are in use at top schools.
For too long, finance has been a net consumer of open source. Major financial institutions and startups alike have only used major open source projects, but virtually none have launched new projects of any significance. That's simply not good enough. To lead in the technology world, financial firms need to found, maintain, and advance major projects of their own.
So, I was really excited to read today's Wall Street Journal report titled:
Goldman Sachs to Give Out ‘Secret Sauce’ on Trading
New open source platform is an attempt by Goldman to bolster its technology bona fides
Sadly, sharing the Secret Sauce is not the same as Open Source. The article leads me to believe that Goldman will unbundle some of their services into smaller applications. That is a great strategic move, but it isn't open source. It isn't even API access. It is just repackaging – very shrewd repackaging that will tend to lock customers in.
Open source software has become central in the technology industry and to the culture of our profession. Open source projects are where developer communities grow (Python, R, docker), new businesses emerge (github), and technologies mature (hadoop).
Financial services companies need to do more than simply use open source; they need to lead open source projects; they need to contribute code. Otherwise, the financial community is left isolated and outside the mainstream of the software community. Many of the best and most innovative minds stay out of finance because they can't maintain the long term relationship with their work that only open source allows. You can't expect to be competitive in technology without connecting with the software community, and contributing to the culture. Without launching and leading open source, financial technologists risk irrelevance.
Goldman's CTO, R. Martin Chavez, seems to get it. He's built software, teams, and companies. The way he talks about the technology world, I think he really understands the underlying dynamics of the ecosystem. I believe he has the right vision; I'm rooting for him. But, I don't think today's announcement goes far enough.
The article describes SecDB, a legendary system that helps Goldman manage risk. Maybe someday SecDB will be a cloud service with pure API access, and the software packages developed to handle the volume of pricing changes will be opensourced. That would be progress. That would be the visionary leadership our industry needs.
This is the latest edition of the Quantopian product update. It's not that we like tooting our own horn, but sometimes people like to go see new features that they missed.
The biggest feature is Quantopian Research. It's now available to everyone!
In addition to the product work, the Quantopian Open, has awarded its 5th prize of $100,000 to manage. We've done meetups in Boston and New York City. We have started integrating Quantopian into courses at various universities. And, of course, the hedge fund development continues behind the scenes. You can see some of that progress on the fund page.
Tools and Features