Improving Your Algo Trading By Using Monte Carlo Simulation and Probability Cones

Using Statistical Techniques To Better Your Odds Of Trading Success

Kevin Davey
Towards Data Science

--

My introduction to the world of applying statistical tools to real world situations was not a good one. I was in charge of Quality Assurance for a medium size aerospace company. Although we extensively used Statistical Process Control (SPC) on the factory floor, we now decided to use some advanced statistical tools, such as Design of Experiments, to rectify a thorny manufacturing contamination problem.

I gave the assignment to my PhD statistician. After reviewing the project (and before conducting any experiments), he returned to me and asked “Now that I understand the project, what conclusion do you want to present to your boss (the CEO)? I can experimentally and statistically make a case for whatever conclusion you choose.”

Wow, I thought. Using statistics not to find the answer, but rather to support a “pet” answer, any pet answer. That made me cringe. Not surprisingly, I turned the project over to someone who wanted the experiment to drive the conclusion, not the other way around.

These days, I like using statistics not to justify a belief, but rather to guide me down a sensible path.

I am an algo trader, and statistical tools help me quite a bit. In this article, I will introduce how I use Monte Carlo simulation and Probability Cones to trade more effectively.

A few words of caution before I jump into the discussion. First off, I am not an expert in statistics by any means. Beyond taking a few undergrad and graduate level Statistics courses, I am clueless about many aspects of statistics. I definitely am not an expert in Monte Carlo analysis or Probability Cones.

What I am adept at is using these tools as a daily practitioner. I may be doing things that a purist would scoff at, but that is OK with me. In the end, I know these tools, and how I deploy them, have helped my trading quite a bit. Trading profits talk, and they tell me I am doing something right — maybe just not theoretically right.

Also, all trading results shown here are hypothetical. Past performance is not indicative of future results, and you should only trade with money you can afford to lose. Trading is very risky.

With those disclaimers out of the way, let’s talk about these tools and how I use them in my algo trading.

What is Monte Carlo Analysis?

Flip a fair coin, and you know the odds of it landing heads is 50%. And let’s say you win $1.25 for every heads that comes up, and lose $1 for every tails. You have a bankroll of $10, and you’ll quit if you ever fall to $6. That is your ruin point. The question is, what are the odds of that happening?

For a simple example such as this, you can find a theoretical equation which approximates the result at Wikipedia: https://en.wikipedia.org/wiki/Risk_of_ruin

But, what if the situation was much more complicated? What if there was a wide variety of possible outcomes, and instead of a two-headed coin, there were 25 sides to the coin, all with a different probability of being chosen? A theoretical calculation, in that case, becomes much more difficult, if not impossible.

Enter Monte Carlo analysis. Monte Carlo analysis utilizes computer simulation to solve problems, calculate probabilities and more — without having to solve theoretical equations. It is especially helpful in situations where theoretical answers are not even possible to derive.

Monte Carlo analysis is used in a wide variety of fields, including some you might not imagine. In the 1940s Monte Carlo simulation was actually used in the development of the first atomic bombs. I use it in trading, a much more benign approach (although trading accounts do blow up on occasion!).

When used in trading, Monte Carlo can help establish probabilities of a number of important performance metrics:

Risk of ruin

Maximum and median drawdowns

Annual rates of return

Return / drawdown ratios

With that information, a trader can make smarter decisions on strategy selection, position sizing and capital deployment and allocation.

How to Use Monte Carlo In Trading

In its typical form, Monte Carlo analysis takes all the trades generated by a hypothetical backtest and randomly selects trade after trade to generate an equity curve. The idea is that with a properly designed backtest, the best estimate of future performance is derived from using past trades (later on, I will discuss many reasons why this might not hold true).

An example of this is shown in Figure 1.

Figure 1- Hypothetical Backtest Curve, With Multiple Possible Future Paths

The thick dark blue curve is the hypothetical backtest, while the thin curves show some of the possible future paths the equity curve could take. The future curves are based on random selection of trades from the backtest curve. (Side Note: In this example, a process called “sampling with replacement” was employed. Each trade in the backtest can be chosen numerous times, based on the random selection process. Some trades may not be chosen at all. This leads to a wide range of ending equity values. If “sampling without replacement” was used, then eventually all equity curves would converge to a common end point, since each backtest trade was used once and only once).

When the Monte Carlo is run, thousands of potential future curves are generated. These results can then be analyzed, with probabilities established for future performance.

I usually think about Monte Carlo in this way: when you perform a historical backtest, before you run the Monte Carlo analysis, you get an equity curve of what happened, such as the equity curve in Figure 2.

Figure 2- Hypothetical Backtest Curve

There is no debate — that is what actually happened. But, what if things were slightly different? What if the trades, for example, came in a different order during the backtest?

In that case, any of the equity curves (paths) shown in Figure 3 COULD have happened.

Figure 3- Slightly Altered History Leads To Different Possible Equity Curves

And of course, many other paths not shown could have happened, too. So, by using the Monte Carlo analysis, and combining the results, we can arrive at probabilities of certain events happening.

Advantages Of Using Monte Carlo

For me, the biggest advantage of Monte Carlo analysis is the way it has changed my mindset about algo trading. When I started to think about probabilities in trading, which Monte Carlo forces you to do, I found myself making better decisions. I no longer thought “the market is definitely going down,” for example. Instead, I thought “the market is likely going down, but will I be safe if it does not?”

Take, for example, the typical trader we have all been at one point or another in our trading career. You start trading a new strategy on Monday, a strategy that historically has performed very well. But after 4 consecutive losses in real time, you scrap the system, since in backtest you only had 3 consecutive losses. You then move on to the next strategy, in a never ending chase for perfection. Of course, shortly after you quit, your original method then recovered from the 4 losers. Unfortunately, you did not get to enjoy that recovery!

If you had run your system thru Monte Carlo simulation, maybe it would have showed a 40% chance of 4 consecutive losers. Then, when the 4 losers actually happened, you would have been dismayed, but you may not have abandoned the strategy. After all, you knew there was a 40% chance of that many consecutive losers occurring.

In this way, Monte Carlo can really help you understand your trading strategy. Although I use it with my algo trading, it can be used by any trader, as long as he or she has a valid and stable trade history. Requiring a valid trade history, however, also leads to some serious disadvantages with the technique.

Disadvantages of Monte Carlo

There are quite a few dangers in using Monte Carlo for algo trading strategy analysis. The first disadvantage is that the historical results need to be stable. By “stable” I am not referring to the actual trade results, but rather the trading method. A trader who consistently trades the same system or approach has a stable method. A trader who tries algo trading on Monday, random guessing on Tuesday and order flow trading on Wednesday does not have a stable approach.

If you constantly tweak your trading approach, your historical trades are meaningless going forward. The historical results should be consistent for a valid Monte Carlo analysis; only one strategy, or one approach, for the whole backtest.

A related concept is if the market changes significantly. When a “step” change in the market occurs, Monte Carlo analysis is no longer valid. An example of this would the Swiss National Bank action in 2015. On January 15, 2015, the Swiss Bank removed a so called “peg” it had established on the Swiss Franc in 2011. This peg tied the value of the Swiss Franc to the Euro.

With the peg suddenly removed, the market reacted violently, and never returned to its previous state. Thus, using Monte Carlo with historical results from 2011–2015 would be pointless, given the structural change to the market in 2015. One could not reasonably expect trades made during the past of 2011–2015 to continue into 2015 and beyond. The market had fundamentally changed.

Another possible disadvantage of Monte Carlo is with serial correlation of trades. Serial correlation is a characteristic of trading histories where the current trade result is dependent (correlated) to a previous trade result. Standard Monte Carlo analysis assumes each trade is independent of the previous trade. My experience has shown that this is generally true (or for all practical purposes true) for many trading systems.

So, if you have a strategy that has serial correlation (either specifically built in or not), Monte Carlo results will be erroneous. An example of this is a simple moving average crossover system, with signals changing based on the previous trade’s profit or loss. Pseudo code for a serially correlated strategy is given below.

Non-Serially Correlated Strategy

If close crosses above the 10 bar moving average then buy

If close crosses below the 10 bar moving average then sell short

Serially Correlated Strategy

If close crosses above the 10 bar moving average then

If the previous trade was profitable then sell short

If the previous trade was not profitable then buy

If close crosses below the 10 bar moving average then

If the previous trade was profitable then buy

If the previous trade was not profitable then sell short

The serially correlated system will contain a history where the result of trade[i] depends on the result of trade[i-1]. This important dependency will be lost when traditional Monte Carlo analysis is conducted. Luckily, in such cases there are ways of both detecting serial correlation, and subsequently dealing with it, with a modified Monte Carlo analysis (beyond the scope of this article).

A final Monte Carlo disadvantage is in the old computer programming adage “Garbage In, Garbage Out.” Except in this case it is more like “Masterpiece In, Masterpiece Out.”

What does this mean? Simply put, if your backtest looks great, it might be that way because of curvefitting, over optimization, hindsight bias, etc. In such cases, the backtest masterpiece will yield a great looking Monte Carlo result — little chance of ruin, small drawdown probabilities and the like.

In this situation, the Monte Carlo simulation can actually give you overconfidence, and lead you to some bad decisions. So, one should be careful if results look too good to be true. In most cases, they probably are!

Getting Started With A Sample Case

There are many free and commercial Monte Carlo simulators on the market, all with different capabilities and features. One good free simulator is available from TickQuest. This particular simulator has some nice ways of visualizing the results.

Another free simulator is one I created for Microsoft Excel, and is available on my website’s Calculator page. The nice thing about the Excel version is that if you can program in Excel Macro language, you can easily extend the capabilities of the simulator. That makes it ideal for do-it-yourself traders wanting to add position sizing and other features.

I’ll use my free Excel Monte Carlo simulator for the example case below.

First, before running the Monte Carlo analysis, you must have a historical backtest of trades. This is generated by the trading platform you use, for a strategy and instrument you select. Trade results are found in the Strategy Performance Report. As cautioned earlier, it is critical that the trade results are not over-optimized or curvefit, since that will give overly optimistic simulation results.

The trades are shown in the equity curve of Figure 4, and the first 10 individual trades are shown in Figure 5.

Figure 4- Sample Equity Curve
Figure 5 — First 10 Trades For Sample Equity Curve (From Strategy Backtest)

These trades represent approximately one year of trading results, with 150 trades, $19,000 profit and a maximum drawdown of about $7,000.

A couple of points to note: first, the trading results used here do not necessarily represent a good or bad strategy — it is merely an example. Second, sound Monte Carlo analysis usually includes 5–10 years of backtest results. This sample strategy only has one year.

To run the simulator, the following numbers are required as shown in Figure 6:

A — The backtested trade results

B — The starting equity for the simulation (the simulator will also run 10 additional starting equity values, all incremented from the value you enter)

C — The quitting equity point –if equity during a simulation drops below this amount, that simulation run is stopped and is considered a case of “ruin.” Many times, this value is set at the trading exchange minimum required margin, since this is the minimum amount required in the account to initiate a new trade.

D — The average number of trades in a year. The simulator runs for a 1 year period, with this many trades taken.

E — System Name identifier

Figure 6- Inputs for Monte Carlo Simulation

Once all the required values are input, the user clicks the “Calculate” button and the simulation begins. The simulator runs 2,500 cases for each of the 11 increasing equity values. Results are shown in the table in Figure 7, section F.

Figure 7- Monte Carlo Output Results

Monte Carlo Results

The yellow table contains the results of the Monte Carlo simulation:

G — Ruin: If you trade this system for one year, what is the probability of dropping below the quitting point equity ( C ) within one year’s time? That is the risk of ruin percentage.

H — Median Drawdown: Based on 2,500 simulation runs, you have a 50% chance that your maximum drawdown in the first year will be greater than the value shown. You want this number to be as low as possible. Remember, even with this drawdown value, you still have a 50% chance that your drawdown will actually be higher. Conversely, you also have a 50% chance that your maximum drawdown will be lower.

I — Median Profit: Based on 2,500 simulation runs, you have a 50% chance that your maximum profit in the first year will be greater than the value shown. You want this number to be as high as possible. Remember, since this is a median profit value, you are running a significant chance that your profit will actually be lower or higher.

J — Median Return: Based on 2,500 simulation runs, you have a 50% chance that your maximum percentage return in the first year will be greater than the value shown. You want this number to be as high as possible. Again, there is a 50% chance of both a higher, or lower, value.

K — Return/DD Ratio: Return to Drawdown ratio. Closely related to the Calmar ratio, which combines return and drawdown into one metric. Higher values of return/dd are generally better. Be careful though — if the return/dd is too high, it may suggest overfitting. If the value is too low, the amount of risk you have to endure for a given return will be too high, and the strategy will not be worth trading.

L — Prob>0: The probability of having a profitable year, if you trade this strategy for a full year.

The astute reader will note that for drawdowns, profit and return, median values from the 2,500 simulation runs are given. This obviously ignores some of the worst case scenarios for drawdown, which many traders are more interested in. The spreadsheet is completely unlocked, so these values can be modified to reflect any percentile desired. The 95th percentile of drawdown is usually pretty popular, for example.

Assuming the Monte Carlo results are acceptable, the trader now may decide to transition to live trading (either simulated or with real money). This live performance can be measured and tracked by the use of probability cones.

What Are Probability Cones?

In manufacturing, one of the best ways to produce quality parts is by utilizing Statistical Process Control (SPC) on the factory floor. SPC helps the production team monitor the manufacturing process, and take action before bad parts can be produced.

In trading, SPC techniques can be used to monitor trading system performance. One way would be to use control charts. An example is shown in Figure 8.

Figure 8- Statistical Process Control Chart

For this approach, each trade result is recorded, and if certain conditions are met (for example, if a part [trade] falls above or below the 3 sigma level), the process (strategy) is considered out of control and action may be warranted. In trading, this could be as simple as turning the strategy off.

There are a few issues with using control charts to monitor trading system performance, though. One issue is there are many rules for an “out of control” situation, and not all them apply to the non-serially correlated trades found in most trading systems. My experience is that control charts can be useful, but they are not ideal.

A better way to monitor real time trading performance is by the use of Probability Cones, also referred to as a cumulative chart. An example chart is shown in Figure 9.

Figure 9- Sample Probability Cone Chart

In this chart, the following values are plotted:

Backtest equity curve (black line)

Average performance of backtest (gray line)

At the end of strategy development, the strategy enters a period of live trading:

Upper probability cone bands (solid and dashed green lines)

Lower probability cone bands (solid and dashed red lines)

Actual trades, real money or simulated (blue line)

If the real time equity curve travels above or below the green and red defined bands, many times it is wise to cease trading, until the trading system returns to a normal area- in between the red and green dashed lines.

How to Use Probability Cones In Trading

Probability Cones can be a relatively easy way to monitor the live performance of a trading system. This is ideal if you have many trading systems to monitor, which is frequently the case for well diversified algo traders.

Only 4 numbers are required to begin the analysis:

M — the average profit per trade from the backtest

N — standard deviation of trade backtest results

O — the inner band threshold, expressed as standard deviations above the mean

P — the outer band threshold, expressed as standard deviations below the mean

Once the analysis is set up, then each individual trade is added to column Q as the trade closes in real time. These trades are depicted in the blue curve.

An example of this is shown in Figure 10.

Figure 10 — Inputs For Probability Cones

Since the Probability Cone analysis is a natural extension of the Monte Carlo analysis, I have included a separate worksheet for it in the Monte Carlo workbook.

Advantages Of Using Probability Cones

The biggest advantage of using probability cones is that there is no hiding from the results. If your live strategy is not performing well, compared to the backtest, it will be readily apparent (see Figure 11).

Figure 11- When Cone Levels Are Crossed, Strategy Should Be Stopped

Probability Cone charts help the trader make unbiased decisions. For instance, this could help in deciding when to stop trading, normally an emotionally charged event (who enjoys turning off a strategy that they spent blood, sweat and tears creating?).

Another advantage of the Probability Cone chart is that it is simple to implement, and easy to understand. If the equity curve is within the upper and lower bands, chances are the trading system is acting normally. If the equity curve exceeds the upper band, caution is warranted, since this good performance is much better than expected. A good thing, to be sure, but has the market changed? Will the system start underperforming soon, and return to its long turn average?

If the equity curve falls below the lower band, maybe the system is broken, or maybe the market has changed. It might be appropriate to cease trading this strategy.

Disadvantages of Probability Cones

Even though these cones are a nice tool, there are some disadvantages. First off, for strategies that have non-normally distributed results, the cones can be inaccurate. In this case, it is recommended that the upper and lower curves be generated via a Monte Carlo simulation with backtest trade results, instead of using just the average and standard deviation. That approach will provide more accurate bands.

A second disadvantage of the tool is in deciding what the upper and lower band values should be. Should 1 standard deviation be used? In that case, the bands will be closer to the average, and strategies may be turned off prematurely as a result of normal fluctuation. On the other hand, if 3 standard deviation bands are used, it might be many trades before the live trading breeches one of the bands. This could represent a significant amount of money lost on a failing strategy.

A final disadvantage of the Probability Cones is that, just as with Monte Carlo analysis, the utility of the analysis is a direct function of the backtest results. In the backtest results are overfit or overoptimized, the cones will not assist a great deal. An overoptimized system will eventually break below the lower band, but only after the trader needlessly loses money trading a system that should never have been traded.

Sample Case

Continuing with the same trading system shown earlier in the Monte Carlo analysis, we can use the Probability Cone analysis to monitor our real time trading performance.

To do this analysis, the only additional numbers needed are the live trade results (Q in Figure 10), and the inner and outer band values (O and P in Figure 10). These are entered in the spreadsheet as shown.

The inner and outer band sigma values, entered as the number of standard deviations, give probabilities based on a normal curve:

1 standard deviation = 68% of the live equity curve should fall within the upper and lower inner bands

2 standard deviation = 95% of the live equity curve should fall within the upper and lower outer bands

A sample of live trading results is shown in Figure 11. In this particular case, the strategy performance is obviously degrading. Many traders would consider turning off the strategy when the lower solid red line is breeched. More conservative traders would turn the strategy off at the dashed red line level.

If such an event happened, though, the strategy should still be tracked. If the strategy performance later improves, then the trader can consider resuming trading the strategy.

Conclusion

Statistical tools such as Monte Carlo analysis and Probability Cones can provide useful information to a trader regarding his or her trading systems. By properly utilizing these tools, a trader can avoid trading a poor strategy, and can also know when to cease live trading an underperforming system. These tools allow the trader to be more in control (no pun intended!) of his trading.

While these tools can be useful, note that they are just that — tools. The trader must still be in charge, and it is up to the trader to determine if the conclusions of each analysis are appropriate and should be followed. Blindly following this (or any analysis) can be dangerous. I personally have found both Monte Carlo and Probability Cones to be useful, but I am cognizant of their limitations and disadvantages. Overall, though, I feel the tools make me a better trader, and that is really the ultimate test.

--

--

kjtradingsystems.com- A champion trader, best-selling critically acclaimed author, and award winning educator, Kevin Davey is an expert in algo trading.