Sketch of Me Excel Blog @ TVMCalcs.com

Coin Tosses and Stock Price Charts

Posted on June 14, 2008

Prior to the 1960’s, most investors believed that future securities prices could be predicted (and that great riches were to be had) if only they could discover the secret. Many investors still believe this today, despite much evidence that suggests that they would be best served by simply owning the entire market (investing in index funds) rather than trying to pick individual stocks.

The efficient markets hypothesis (EMH) essentially states that techniques such as fundamental and technical analysis cannot be used to consistently earn excess profits in the long run.  The EMH began with the observation that changes in securities prices appear to follow a random walk (technically, geometric Brownian motion with a positive drift). The random walk hypothesis was first proposed by mathematician Louis Bachelier in his doctoral thesis in 1900, and then promptly forgotten. Further evidence of randomness was occasionally reported by others, most famously by the statistician M.G. Kendall in 1953. Many others followed in the late 1950’s and 1960’s.

One of the best-known stories regarding the randomness of changes in stock prices is told by Burton Malkiel in A Random Walk Down Wall Street (a fascinating practitioner-oriented book now in its ninth edition). In that book he tells of having fooled a friend, who was a committed technical analyst, by showing him a "stock chart" that was generated by coin tosses, rather than actual stock prices. Apparently, the friend was quite interested in learning the name of the stock.

The purpose of this post is not to debate market efficiency (so please don’t leave comments on that subject — it is almost a religious debate among some), or to even state that the EMH is correct. Instead, as this blog is focused on Excel, it is to demonstrate how we can simulate coin tosses and use those simulated tosses to generate "stock charts" of the kind that Malkiel discussed.

Generating Coin Tosses with Rand()

Our first task is to generate a sufficient number of coin tosses to create the chart. There are many ways that we might go about doing so, and I will first show the easy way.

Realize that a coin toss can be represented by a binary variable, where 0 is tails and 1 is heads. This is a binomially distributed variable with a probability of a "success" of 50% (p = 0.5), assuming that the coin is fair. We could use the random number generator from the Analysis ToolPak add-in to generate these tosses, but it suits my purposes better to create a formula:

=Round(Rand(),0)

The Rand() function generates a uniformly distributed random number between 0 and 1. I apply the Round() function to the result so that if the random number is 0.5 or greater it will get converted to a 1. If it is less than 0.5 it will get converted to a 0. We will treat a 1 as a head, and a 0 as a tail. Furthermore, if the result is a head the stock will go up, and if it is a tail then the stock price will decline.

Now, we know that stock prices tend to rise over time so we will say that daily price changes are slightly asymmetric. Specifically, let’s say that the price will rise by 0.25% if a head is tossed, or fall by 0.20% if a tail is tossed for a given day. Also, we need to set a starting price, so let’s choose $100. Create a worksheet that looks like the one in the picture below:

Picture of Coin Toss Worksheet

Enter the data as shown in A1:B5. In B6, just link to the beginning price with the formula: =B3. In A7 we will calculate our first coin toss with the formula:

=Round(Rand(),0)

In B7 we will calculate the new stock price with an IF() statement:

=IF(A7=1,B6*(1+$B$1),B6*(1-$B$2))

Finally, copy the formulas from A7:B7 down the sheet to row 156. That will give us 150 coin tosses and 151 days of stock price "history."

We can now create a line chart of the resulting stock prices:

Picture of Stock Price Chart Generated by Coin Tosses

Notice that I have marked a few typical technical chart patterns that make an appearance in the coin flip-generated chart. There is a double top, a resistance level, and an up trend. Press F9 to recalculate the sheet and you will get a fresh round of coin tosses and a new chart. If you do that enough times, you can see all of the chart patterns that are discussed in such books as Technical Analysis of Stock Trends by Edwards and Magee (often called the Bible of technical chart patterns).

A More Realistic Chart

The chart generated above is perfectly functional and makes the point, but it doesn’t look as realistic as it could. There are at least two improvements that could be made:


  • Use a more realistic model for price evolution over time.

  • Generate a High-Low-Close chart instead of a simple line chart.

Typically, stock prices are simulated using a stochastic process known as geometric Brownian motion. Using this model, we can generate returns by using the following formula:

Equation for Geometric Brownian Motion

That formula simply says that the change in the stock price is equal to the mean change per unit of time (known as the drift) plus a random shock. The shock is simply the standard deviation of the returns times a standard normal deviate (mean of 0, standard deviation of 1). If the standard deviation is 0, then the stock price would simply increase by the amount of the drift in each period. So it is the second term that determines the random up and down fluctuations in the price. We can calculate the next stock price by simply multiplying the previous price by one plus the result of that formula.

I no longer need the coin toss metaphor, but I’m going to keep it. It doesn’t hurt anything, and it makes the demonstration more interesting. So, the closing price will go up or down by the absolute value of the result of the Brownian motion equation. For heads the price will go up, for tails it will go down. Note that the coin toss is still determined by generating a uniformly distributed random number as done originally.

To generate the high and low prices for the day, I use the same formula. However, the base price is the day’s closing price instead of the previous day’s price. This way, I can tie the high and low to the same day’s close. This also allows me to make sure that the high price is always greater than the close, and the low price is always less than the close. Still, the amounts by which the high and low deviate from the close are determined randomly using the same mean and standard deviation.

To do all of this requires that I generate four random numbers for each day. The first is for the coin toss. The other three are the standard normal variables that get fed into the Brownian motion formula to determine the high, low, and close for the day. I use VBA to generate these numbers and to change the scale of the Y axis in the HLC chart so that it looks reasonably good. The code is quite straightforward, so I won’t cover it here. If you are interested, you can download the example workbook (Excel 2003 version) and examine the code by opening the VBA editor (Alt-F11). Note that the random numbers are in columns A:D, which are hidden from view. Simply unhide those columns to see the output.

The picture below shows the resulting worksheet.

High-Low-Close Chart Generated by Coin Tosses

Note that there are two buttons on the worksheet. Clicking the "Flip Coins" button will cause new coin tosses to be regenerated and the new values for the chart to be calculated. The "Copy Chart to Clipboard" button does exactly that. The purpose of that button is to make it easy to capture a particularly interesting chart so that you can show it to your technical analyst friends just like Malkiel did!

The workbook discussed in this post is available in Excel 2007 format and Excel 2003 format.


Posted by on June 14, 2008 at 12:18 AM

Categories: Charts Excel Excel 2003 Excel 2007 Functions Rand Round Simulation VBA

Comments:

Interesting. How about sophisticated models such as Black-Scholes, which incorporate price volatility into decisions regarding options pricing?

Posted by Jon Peltier  on  June 21, 2008  at  07:00 AM | #

Thanks Jon. The Black-Scholes model makes the same assumption regarding the future evolution of securities prices (geometric Brownian motion). In fact, we can value options using Monte-Carlo simulation of stock prices as demonstrated here.

For example, the value of a call option at expiration is simply Max(0,S-X), where S is the stock price and X is the strike price. So, we can simulate the stock price many times and calculate the value of the option at expiration. Do this thousands of times and then calculate the average value. The present value of that average value at expiration is today’s option value.

I should note that the beauty of the Black-Scholes model is that it is a simple closed-form solution for the value of a European-style option. So, that meant that traders could program it into their calculators and instantly know the value of the options that they were trading. The timing of the publication of their paper (1973) was perfect as the Chicago Board Options Exchange opened at virtually the same time. You would probably enjoy that paper because at one point in their derivation they end up with “the heat-transfer equation from physics...” grin

Posted by  on  June 21, 2008  at  11:39 AM | #

It is interesting when you move out over a longer period of time - say 5000 flips vs 150, seems like the one striking characteristic to the charts is thy are almost all upward sloping.  Is there something in the code I need to change in order see the occasional “correction”?

Posted by Tim D  on  August 19, 2008  at  01:12 PM | #

Hi Tim, I guess you modified the worksheet and code? That shouldn’t matter if you didn’t change anything fundamental. You will definitely see an overall uptrend because of the positive drift.

In other words, the positive expected return over time causes the stock price to act kind of like an unbalanced coin that would generate more heads than tails in the long run. However, you should definitely see some downtrends along the way. If the chart isn’t wide enough, the down turns might not be so visible.

Also note that we are using a geometric (multiplicative) process, so you will never see it go to zero or below, and if you give it long enough it will always trend upwards.

Posted by  on  August 19, 2008  at  11:22 PM | #

Name:

Email (will not be displayed):

Location:

URL:

Smileys

Remember my personal information

Notify me of follow-up comments?

Submit the word you see below:


<< Back to main