Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
The Math of the Stock Trading Game is Quite Simple

A stock trader deals in standardized pieces of paper representing his share of ownership which can easily be auctioned in a public marketplace. You buy some shares of some company (q∙p) to resell them later, hopefully, at a better price (q∙p+), thus making money. Every merchant on the planet sees the same kind of problem.

The problem with trading is that one cannot make just one trade, as if by definition. Once it is done, what will you do next? One will need to do many trades over many years and keep an eye on the end result.

To make the problem more complex, you will not know for sure if the price of your selected stocks will be rising going forward. If you knew, you probably would be betting the house on every trade you made. Any player at this game realizes early that going all-in all the time is not the prudent way to play. Too much uncertainty, too much risk. It is simple: you lose it all, you are sort of out of the game. You will need to start over again with a new stake, and a new strategy, since your last one, in the end, proved disastrous.

So, most players learn to average things out (diversify) and make a lot of smaller trades (bets). They develop automated trading methods that can accumulate more profits from winning trades than losses in losing ones to produce an overall profit over the long haul simply by averaging out the risk, and uncertainty, over time. There is an equation to resume all that.

The Portfolio Equation

If all the trading activity could be summarized in a single definitive equation, then it would be great. But, we already have such a contraption. It is old, here it is again:

F(t) = F(0)∙(1+g_bar)^t = F(0) + Σ(H∙ΔP) - Σ(Exp) = F(0) + n∙x_bar = F(0) + (n - λ)∙AW + λ∙AL

The equal sign will stand for every section of this payoff matrix equation.

It starts by saying that a portfolio's final value F(t) is equal to the initial funds F(0) operating as if invested at a rate of return of g_bar for t years. This is equal to having F(0) to which is added the result (+/-) of all the trades minus all trading expenses. Evidently. How could it be otherwise!

The payoff matrix itself Σ(H∙ΔP) could be of any size, thousands of rows (days) by hundreds of columns (stocks) and it would still resume to the penny all the trading activity ever done in that portfolio.

Furthermore, all this trading can be reduced to 2 numbers: n and x_bar. From all the trades (n), the strategy managed to generate an average net profit or loss of x_bar, including expenses. Again, hard to argue the point. The equal sign stands.

Therefore, at the end of it all, only two numbers will prevail, and this whatever your trading strategy does or that you think it can do, even if it does a million trades+. You will be subject to the number of trades the strategy made and to the average net profit per trade that it was able to achieve. Those are the important numbers of any trading strategy. They are the numbers you will end up with. Consequently, those are the numbers to be concerned about.

The last part of the equation is breaking down n∙x_bar into its components: the number of winning and losing trades. So, you have the number of losing trades (λ) times the average net loss per trade (AL). and the rest (n – λ), the number of winning trades times the average net profit per trade (AW). Nothing very complicated, almost trivial and certainly self-evident.

We have a portfolio equation to explain the outcome of any trading strategy whatever its composition and whatever its duration.

So What!

Well, if all trading strategies end up with F(t) = F(0) + n∙x_bar, or, F(t) = F(0) + (n - λ)∙AW + λ∙AL, then those are the numbers that matter. Everything else not having an impact on these numbers might as well be considered as irrelevant or some kind of cosmetic code.

Whenever I see the outcome of a program where AW tends to AL (AW → |AL|), and many programs have this, I look at (n – λ) to see if it was worthwhile. For instance, a hit rate of 51% (n – λ)/n is not that much different than the expected outcome of flipping a fair coin, whatever predictive powers one would like to attribute to their trading program. It most certainly is not the way to show predictive powers.

If your trading program has for outcome: F(t) = F(0) + (n - λ)∙AW + λ∙AL, then those are the only numbers to worry about. Evidently, you want to make AL and λ as small as possible while having n and AW as large as you possibly can. It does translate to having a better hit rate: (n – λ)/λ than just 51%, and a better average win to average loss ratio: (n – λ)∙AW / λ∙|AL|.

Whatever the design of your trading strategy it will end up with the above equality.

Therefore, improving on a trading strategy will require code modification that can have an impact on the above equation. Any code modification or improvement will need to find ways to increase n, reduce λ, increase AW and reduce AL. Whatever you do, there is nothing else that matters if it does not have an impact on those numbers.

The equation stands, and it will stand for centuries to come. That is not an opinion. It is an old statement bearing an equal sign declaring and setting it in stone.

In the end, common sense must prevail. Should you not be interested in looking at what are the long-term consequences of your trading strategy, then don't blame anyone but yourself for not having planned that far. If in the end, you lose, it will be strictly your fault, no one else's. You had all the tools to make it good and even better than everyone else and with ease, but you failed.

The market was not against you, you simply designed your trading strategy to fail from the start, and it did. So, do not be surprised, and do not complain. You were the architect of your own demise. You do not want to plan for the above equation, it is your choice, but it won't make those numbers disappear.

1 response

The Math of the Stock Trading Game is Still Simple

Following my last post, I thought it might be of interest to provide an example with numbers while keeping with the portfolio's payoff matrix equation as presented. You look at a problem long enough and you start to synthesize what it is all about. Not that you master it, but that you can somehow replicate its parts.

The idea here is to show the impact trading mechanics can have on the outcome of a trading strategy. The numbers do not belong to a particular trading strategy. The numbers are, however, representative of some strategies I have seen lately on Quantopian. We could view these numbers as a variation on the same theme.

Nonetheless, they should make the point. Which is: demonstrate that slight changes in the behavior of a trading strategy can have quite an impact when the number of trades is large and executed over extended periods of time.

Trading is a Journey

No one should trade for a week, stop, and then wait for years. It is not productive enough to sustain any long-term objective, except if you were very very lucky.

The following table shows a strategy generating some 200,000 trades over an undetermined time interval. What we are interested in is the portfolio's payoff equation as presented in the above cited article, wherein all the variables are described in detail. The equation is reproduced here for convenience's sake:

F(t) = F(0)∙(1+g_bar)^t = F(0) + Σ(H∙ΔP) - Σ(Exp) = F(0) + n∙x_bar = F(0) + (n - λ)∙AW + λ∙AL

Figure 1: Outcome of Payoff Matrix Equation

From the chart, the initial stake is set at $10 million with 200,000 executed trades. The number of trades (n) is sufficiently large to make it statistically significant and allow us to speak in terms of averages.

Where is the Money

In the first panel, the average win (AW) equals the average loss (|AL|). We have: AW = |AL| = $100. With a 50% hit rate, the strategy's outcome is zero, no reward. It would have made 200,000 trades with not even a penny to show for all the work.

This is equivalent to having an x_bar of zero, no edge, therefore no profit. It is the same as the expected outcome of tossing a fair coin. An x_bar of zero does translate to no gain, but also to no loss. You lose if x_bar goes negative. Those scenarios do exist, but are not considered in the above table.

Designing trading strategies with a negative x_bar is not desirable since, on average, you would be paying to lose money to play the game. Evidently, do not do that. Know your strategy's numbers before you trade live, and a simulation can easily give you an estimate of those numbers before even considering such a decision.

An Edge

A 51% hit rate, or a 1% edge, is sufficient to make some money, even in a scenario where AW = |AL|, as illustrated in panel 1. Tossing a loaded coin having a 1% bias would give the same expected outcome. It gets better as the edge increases.

The middle panel shows the strategy after improving AW, its average win per trade. Not by much. At the 50% hit rate, it is $50 per trade. It could be done simply by requesting, on average, a higher profit target per trade.

The Win/Loss Ratio

In panel 2, even with a 50% hit rate, you win. Even more so than the best in the first panel at a 60% hit rate. The effort to increase the average net profit on the average winning trade was worth it. Going to the 51% ratio does increase performance, but not by that much. It is the hit rate which is improving x_bar and thereby the total outcome.

At a 60% hit rate, panel 2 generates 4 times as much as in panel 1. Yet, AW is just twice |AL| which was set at -$100. The 50% hit rate did $10 million while at the 60% level the total moved up to $16 million in profits. All simple numbers to compute. It also stresses the importance of a better hit rate which has for expression: (n – λ)/n.

Panel 3 shows the impact of doubling AW and |AL|. Evidently, it doubles the outcome whatever the hit rate. But note that, at the 50% hit rate, we have only added $50 to x_bar compared to panel 2. This is not a huge amount when your trading unit is at something like $25k or $50k (a 400 or 200 stock portfolio). Making $100 on a $25k bet is 0.40%. So, I cannot consider this a big move. It is equivalent to a 40¢ move on a $100 stock, 20¢ on a $50 stock and 10¢ on a $25 stock. That kind of price move is available thousands of times a day.

When designing our trading strategies, we definitely could benefit from improving the spread between AW and |AL| as well as improving on our hit rate since these numbers do have an impact on x_bar as shown in Figure 1.

The Value of Time

Figure 1 does not consider time in its calculations since it is all concentrated on execution and the account's bottom line which is the outcome of the portfolio payoff matrix equation. Figure 2 tries to partly remedy the situation:

Figure 2: The Value of Time

Figure 2 makes the compounded annual growth rate (CAGR) calculations for a 3, 5, 10, and 20-year time interval. The first section of the chart is the same as in Figure 1. The CAGR formula is simple: [(F(0) + n∙x_bar) / F(0)]^(1/t) – 1.

We can observe that the longer the time period to execute the 200,000 trades, the lower the CAGR is. Even at its best, in panel 3 with a 60% hit rate, the 20-year CAGR is just 7.44%. Yet, it looked impressive if one did not do the calculations. A $32 million profit on a $10 million investment is still money. Depending on the time interval it was executed, it could have been an impressive CAGR.

The Need to Rush

What Figure 2 shows is that there should be a rush to execute those 200,000 trades, making it as fast as possible. That is how you can improve your CAGR. Increase your hit rate on your positive edge. It does not say how you do it, only that it needs to be done, or is the result of the modifications you bring to your trading procedures. In a way, it is up to you to design the strategy you want. But, if what you do does not impact the numbers in the portfolio equation, then do not be surprised if the overall outcome does not change.

This leaves the door open to any type of trading strategy, whatever the basis of its edge. You want results, they can be had by designing strategies that will respect the portfolio equation. You want to ignore it, it's OK. The equation will still tally the result anyway and keep the score in your trading account. Except that, it will do what it has to do and not necessarily what you want it to do.

Have a Trading Plan

Plan for what you want to see, then program your trading strategy to do the job you want. You can design a table like the one above for your own portfolio scenarios. Your simulations can provide the numbers.

At the end of the game, the portfolio's payoff equation will prevail. It is not an opinion, not even a bold statement. It has been carved in stone for quite some time.

Improving on Your Strategy

If you want to improve your trading strategy, meaning have it do more for you. Then the solution is simple. Find ways to increase the number of trades having a positive impact. Increase the average profit per trade (AW). Reduce the number of losing trades λ. Increase the spread between AW and |AL|, the more you do so, the more your hit rate (n – λ)/n will scale up your profits.

These suggestions are going for the mechanics of the trading strategy and not necessarily for better factors or indicators. For instance, increasing AW could be a simple matter of procedure or policy to simply delay gratification thereby exploiting part of the market's general upward tendencies.

The economic reason is also very simple. The variance of stock prices is proportional to the square-root of time. You will find this all over academic papers starting with Bachelier in 1900. In the end, it is all up to you.

You want to win, that's understandable. Then you better understand the math of the game. Or, else... repeat after me: cash o∙b∙l∙i∙v∙i∙o∙n. Or, I used to play that game.