From Build a Robo Advisor with Python (From Scratch) by Rob Reider and Alexander Michalka.

Build a Robo Advisor with Python (From Scratch): Automate your financial and investment decisions teaches you how to construct a Python-based financial advisor of your very own!

What are Robo-Advisors?

Robo-Advisors have become a popular alternative to human financial advisors. Historically, financial advisors would meet with clients, discuss their goals, create a financial plan, and then manage their clients’ money over time. In exchange for this personal attention, they would charge clients fees, often in excess of 1% per year of their assets under management.

Numerous companies have been trying to disrupt this business through online platforms that provide automated, algorithmic investment services similar to those of a financial advisor. Some of these automated systems “advise” clients through algorithmic implementations of modern portfolio theory, based on the Nobel Prize-winning work of Harry Markowitz in the 1950s, while others use machine learning and optimization techniques borrowed from other disciplines. These companies have collectively become known as “Robo-advisors”.

In this book, we show how anyone with a basic understanding of Python can build their own Robo-advisor. We hope this will be useful for anyone who wants to work in this area or wants to apply these algorithms for their own portfolio or advise others.

Key Features of Robo-Advisors

The most basic feature that Robo-advisors provides is a personalized asset allocation – a portfolio of investments designed to match the level of riskiness suitable for the client. The core asset allocations offered by different Robo-advisors vary in their choice of asset classes but usually contain a diversified mix of stocks and bonds. Some advisors may choose to include more non-US assets in their allocations, or an allocation that’s more heavily weighted to growth or value stocks, but in essentially all cases, the instruments that Robo-advisors invest in are liquid and traded on exchanges, and not things like venture capital, private equity, real estate.

Aside from the core asset allocation, there are a handful of features that Robo-advisors provide. We will cover most of these in more detail later in the book; here we only provide a high-level description of each one.


A client’s portfolio will start with weights close to the target allocation at the time of the initial investment, differences in returns will cause the portfolio to drift away from the target over time. Rebalancing may be drift-based, meaning the portfolio is rebalanced whenever it deviates from the targets by a pre-specified amount, or time-based, meaning that rebalancing occurs on a fixed schedule. Additionally, rebalancing may or may not be tax-aware. In tax-aware rebalancing, some appreciated positions may not be sold if doing so would involve a high expected tax cost. In some cases, the Robo-advisor may be able to keep the portfolio “on track” simply by intelligently directing dividends and deposits towards assets that have drifted below their target weights.

Financial Projections

A key question for most individual investors is “When can I retire?”. Robo-advisors may offer tools that show projections of the client’s net worth over time, based on assumptions for income, spending, inflation, and investment returns. By varying these assumptions, clients can assess the feasibility of retirement at different ages.

Tax-Loss Harvesting

The basic idea of tax-loss harvesting is to reduce the investor’s current tax burden by opportunistically realizing losses in assets that have declined in value. You can use the realized losses to offset realized gains or some income. Tax-loss harvesting is a common feature offered by Robo-advisors, but one that is tricky to understand properly.

Glide Paths

As clients age and approach retirement, the amount of risk that they should take in their investments decreases. A glide path is a series of portfolios or asset allocations that gradually decreases in riskiness over time. By the time a client reaches retirement and gives up their employment income, the glide path will assign a low-risk portfolio. While some Robo-advisors use glide paths in their investment process, others let the client control how much risk they are willing to take throughout the lifetime of their investments.

Aside from these important features, Robo-advisors vary in two additional dimensions – their management fee, and the minimum account size. While there is some dispersion in these values, both are usually quite low, making most Robo-advisors accessible to clients in the earliest stages of their careers.

Comparison of Robo-Advisors

As of 2020, there were over 200 Robo-advisors based in the US alone. The table below compares the feature sets of some of the largest, as reported by Investopedia in March of 2021.

Things Robo-Advisors Don’t Do

Software is useful for performing simple, repeatable tasks very quickly. Even the most complex-looking algorithms are just sequences of simple conditions and steps. The above examples are ones that naturally suit to be accomplished using the software.

However, there are some services that traditional advisors perform that software can’t replicate. These are generally infrequent or one-time events that may require detailed personal information. Examples of services that are (for now) best accomplished by human advisors include estate planning, management of non-traditional assets like art or real estate, and specialized tax advice for things like stock options.

This is not to say that Robo-advisors can’t expand into areas that have traditionally been the domain of human advisors. For example, we often call upon financial advisors to help retirees with defined-benefit pension plans on whether to take a lump sum or monthly payments for life.

You can adopt the same Python programs to analyze Social Security as to analyze pensions. Of course, there are some things that Robo-advisors will never be able to do – software will never get you basketball tickets (no matter how large your account gets) or treat you to dinner.

Advantages of Using a Robo-Advisor

There are several advantages of using a Robo-advisor over either using a human advisor or do-it-yourself investing. We highlight three of those advantages here.

Low Fees

Surveys show that about 30% of Americans use a financial advisor of some kind. Fee-based financial advisors charge fees based on a percentage of assets under management (AUM), an annual retainer, an hourly charge, and sometimes a combination of these.

In addition to fee-based financial advisors, some advisors instead follow a commission-based model, where they compensate by charging commissions on financial transactions and products like life insurance or annuities. For those that charge an AUM fee, which is the largest category, the average fee is about 1% per year. Robo-advisor fees are a fraction of that (see table above). In addition, Robo-advisors usually have much lower minimum account size than financial advisors.

Even small savings, when accumulated over decades, can make a big difference. A 1% annual fee charged by a traditional human advisor may not seem like much, but the cost compounds over time. Imagine starting out with a $100 investment which earns a 7% return each year, before fees. The 1% fee charged by a traditional advisor reduces the return to 6% annually.

This means that after 30 years, the $100 investment would grow to about $574. Not bad, but let’s compare this to a typical Robo-advisor charging 0.25% per year. With the Robo-advisor, the investment would grow to about $710 – almost 25% more!

Tax Savings

In the last section of this book, we describe several tax-saving strategies that a Robo-advisor could automate. How much money can people save through tax strategies with Robo-advisors? It’s very difficult to give an exact number because it’s different for each individual depending on their circumstances. Someone that has only taxable accounts and no retirement accounts like an IRA or 401(k) would not benefit from several of the automated Robo-advisor functions that we talk about in later chapters.

With the tools covered in this book, you will be able to estimate, through Monte Carlo simulations, the amount of savings for a specific set of circumstances, and see that the savings can be significant.

To give one example, consider a topic we will cover in detail in the last chapter: optimal sequencing of withdrawals.

As just a brief introduction, in the retirement stage of life when people are “decumulating” assets as opposed to accumulating, there are numerous options available for withdrawing assets. For example, you can take money out of your taxable accounts first, and when that is depleted, start taking money out of retirement assets (“Taxable first”). You can also switch the order and take money out of retirement accounts first (“IRA first”). A third strategy is to take money out of retirement accounts each year up to the top of the nearest tax bracket, so that future IRA withdrawals don’t push you into a higher tax bracket (“IRA fill bracket”). Finally, the fourth strategy is similar to the third but you convert your IRA distribution into a Roth IRA (“Roth conversion”).

To illustrate how consequential those decisions can be, and how much you can save by employing the best strategy for a given set of circumstances, the figure below shows how long your money will last for those four strategies. We will cover later the specific set of assumptions and the details of the strategy, but the point is that it makes a big difference. The optimal strategy can extend your assets by many years.

Avoiding Behavioral Biases

It is well documented that investors are subject to numerous behavioral biases, many of which can be avoided by algorithm-based automated trading.

● Disposition effect Studies have shown that investors tend to hold onto losing stocks hoping to get back to break even. However, Robo-advisors realize it’s often useful to sell losing stocks to harvest tax losses.

● Herd behavior Investors tend to be influenced by others, which explains why they dive into stocks during bubbles and panic during crashes. Herding might have conferred benefits when fleeing predators in prehistoric times, but it is not a great investment strategy. Robo-advisors, on the other hand, unemotionally rebalance gradually toward stocks during crashes and away from stocks during bubbles.

● Overtrading Several studies on overtrading have all reached the same conclusion: the more active a retail investor tends to be, the less money they make. Individual investors may incorrectly assume that if they are not paying brokerage commissions, there is no cost to frequent trading. However, commissions are only one cost. The bid/ask spread, the difference between the price you can buy a stock and sell a stock on an exchange is a significant component of trading costs. And frequent trading leads to short-term capital gains, which is not tax efficient. Robo-advisors methodically factor these costs into account when they make trades.

Saving Time

By automating simple tasks associated with investing, a Robo-advisor can save investors huge amounts of time compared to a “do it yourself” approach. Monitoring the portfolio for drift away from its targets or for tax-loss harvesting opportunities, and placing trades to rebalance, harvest losses, or invest deposits aren’t especially difficult but aren’t especially fun either. Robo-advisors automate these tasks, leaving investors more time for more enjoyable pursuits.

Example Application: Social Security Benefits

One thing that gets us excited about this topic is that Robo-advising is still in its infancy. Consider one example: the important decision on when to claim Social Security benefits. Many Americans consider their Social Security check as one of their most valuable assets, in many cases worth more than half a million dollars on a present value basis. People must elect when they want to start receiving or claiming, their Social Security payments, which is anytime between 62 and 70, and the longer you wait, the larger your payment will be. It is one of many important retirement decisions that are consequential and mostly irreversible. So what kind of advice is out there on when to claim Social Security? If you do a google search on when to claim Social Security, you’ll find numerous calculators that mostly all do the same thing. They usually have a simple break-even analysis like the one we created below. To see a similar one, see for example the one offered by Charles Schwab (

In this break-even analysis, the x-axis represents the age you expect to die, and the y-axis represents the present value of your Social Security payments up until the time of the death. As you can see from the graph, in this simple analysis that compares claiming at 66 vs. 70, if you expect to live past 82, you are better off waiting until 70, and if you expect to die before 82, you are better off claiming at 66. As the chart shows, if you live to 90 and you claim at 70, the present value of all payments is about $500k.

What is this analysis missing, and how can we use Python to improve it? First of all, the analysis gets exponentially more complex when we consider spouses, and the Social Security rules can get very complex. Although you can do the break-even analysis above in a spreadsheet, taking into account couples and all the Social Security rules would overwhelm a spreadsheet and you’d have to code in a computer language like Python.

In order to accurately predict Social Security benefits, you have to estimate the trajectory of future income up to retirement. Python has several libraries for forecasting a time series like income. In this book, we will use a library called Prophet, which was developed by Facebook and is particularly good at modeling non-linear trends that you would find in a person’s lifetime income. Prophet requires income data for the investor, available for download from the Social Security website, and you can use Python for scraping the file.

The break-even analysis above is not customized in any way, and Python can help here too. Since Social Security benefits are similar to an asset like a bond, they should be incorporated into an investor’s asset allocation — larger Social Security payments would mean that even a conservator investor could hold fewer bonds. The break-even analysis also ignores taxes on Social Security income. And it ignores the Social Security benefits that may be withheld when someone’s income exceeds a threshold while they are collecting benefits. You can extend the Social Security analysis claiming analysis to include an investor’s particular tax situation.

Perhaps one of the most important and overlooked shortcomings of the break-even approach is that it doesn’t take risk into account. In this case, the risk is longevity risk — the chance that you survive beyond your life expectancy and outlive your money. Social Security benefits, as well as defined-benefit pensions and annuities, reduce that risk by guaranteeing lifetime benefits. Throughout the book, we show how to take risks into account with examples in Python.

Python and Robo-Advising

The designers of Python programming intended the language to be fast, powerful, and fun. We think that they have succeeded. Python is easy to start with: the classic “Hello World” program is not much harder than just typing the words “Hello World” — and easy to learn. Python’s documentation is extensive, and its wide adoption means it’s usually quite easy to find solutions to tricky problems – chances are, someone has run into them already. Finally, Python is flexible — while it supports some more sophisticated features of lower-level languages like Java, Python doesn’t require them. If you want to use Python in an object-oriented way, you can — but you don’t have to. Likewise, you can add “hints” for data types in Python functions, even if it doesn’t require them. Python lets you choose.

All of these qualities make Python easy to learn and work with for any programmer. But what makes Python a good language for Robo-advising? As Python’s popularity has grown, the number of mathematical and statistical packages has grown as well. Applications in this book will lean heavily on packages including:

● NumPy

A general-purpose package for numerical computing, NumPy provides tools ranging from the basics (basic vector and matrix operations or least-squares solutions to linear systems) to the complex (such as random number generation or matrix factorizations). The NumPy package achieves high speed by implementing many numerical subroutines in C.

● Pandas

QAR Capital Management developed Pandas starting in 2008 and it became open-source in 2009. It adds a level of abstraction on top of arrays and matrices to make data manipulation effortless.

● cvxpy

cvxpy is a mathematical modeling language allowing users to formulate and solve convex optimization problems using a natural and easy-to-read syntax.

We should also talk about what Python isn’t.

Python’s interpreted nature means it will never be as fast as low-level compiled languages like C or Fortran. If your application requires top speed, a compiled language may be a better choice. This also means that bugs only show up when code is run, and won’t be found during compilation.

Overall, we still think that despite these limitations, Python is a great choice for this book. Robo-advising doesn’t require lightning speed, we think that the ease of use and extensive libraries and documentation outweigh any disadvantages in development speed.

This book doesn’t assume that readers are Python experts but does assume a basic familiarity. For an introduction to programming in Python, we recommend The Quick Python Book by Naomi Ceder as a great place to start.

Who Might be Interested in Learning About Robo-Advising?

There are several groups of people who might find interest in the topics covered in this book.

● You want to better understand personal finance to help you with your own finances. There is no shortage of books on personal finance for do-it-yourself investors, but this book focuses on topics that can clearly save you money and goes into them in depth. You won’t see chapters found in other personal finance books like “Live within your means” or “Don’t buy complex financial products”. Even if you have no interest in applying these techniques in Python, with this book you can skip the Python examples and still understand the principles behind what the algorithms do.

● You are interested in working for a financial advisor or wealth manager. As we mentioned, the number of robo-advisors is growing, and the incumbents are also getting into robo-advising. And traditional wealth managers are using the same techniques for their clients. A quick search on for jobs as a “financial advisor” currently lists over 20,000 jobs. This book provides relevant skills that are used in the industry.

● You are a Financial Advisor and would like to provide your clients with a larger set of tools. According to the Bureau of Labor Statistics, as of 2020 there were 218,000 financial advisors in the United States. The financial advisory business is obviously very competitive, and providing sophisticated services gives a firm a competitive advantage. An advisor can differentiate themselves in this crowded field and create a competitive advantage by offering more sophisticated services, like the ones described in this book. Also, a financial advisor that can automate advice can service many more clients, while still providing customized advice.
● You are interested in useful, practical applications of Python. There is no better way to learn Python than applying it to interesting, practical problems and observing intuitive results. In this book, we will use numerous Python libraries to solve wealth management problems. We will use a convex optimization library and a hierarchical tree clustering library to perform asset allocation, a statistical and random number library for Monte Carlo simulations, a root finding library for measuring portfolio performance, and a reinforcement learning library to estimate a glidepath.

What will you learn from this book?

In this book you will learn how to:
1. How to measure returns and estimate the benefits of robo-advisors.
2. How to use Monte Carlo simulations to build and test financial planning tools.
3. How to construct diversified, efficient portfolios using optimization and other advanced methods.
4. How to implement and evaluate rebalancing methods to track a target portfolio over time.
5. How to decrease taxes through tax-loss harvesting and optimized withdrawal sequencing.
6. How to use reinforcement learning to find the optimal investment path up to and after retirement.


  • ● Robo-advisors use algorithms to automate some of the functions of human financial advisors.
    ● Robo-advisors have several advantages over human advisors: they have lower fees, they can save a considerable amount of money using tax strategies, and they can help investors avoid some well-documented behavioral biases that detract from their performance.
    ● You can use Python, with its extensive libraries, to implement many of the functions of a Robo-advisor, from asset allocation to tax loss optimization to Monte Carlo simulations for financial planning.

Learn more about the book here.