# Simple Moving Average(SMA), Crossover Technique with Market Price using Python

Moving averages are commonly used in stock market price analysis. It basically smoothes out the price action by filtering out the “noise” caused by random price fluctuation. It is a “lagging” trend following indicator that is based on past prices. It creates different subsets of our whole data set and takes the averages of each subsets. We can therefor see how our average fluctuates over time, and know exactly when to buy or sell stocks.

Moving averages are great for price action analysis, but can we use them in other areas of the market, such as the free market ?

First, let’s see how a free market functions. It is an economic system based on Supply and Demand with little or no government control. Prices and the amount of products are adjusted based on the economic conditions at that time. Craigslist is an example of a free market.

We will be using the SMA used in Technical Analysis also known as the Simple Moving Average. We will apply it on a data set consisting of 2000 real-world prices ranging from 100 \$ to 1275 \$. Note: In Finance, technical analysis uses historical pricing and/or volume to predict price direction.

Simply put, the SMA sums up the prices of a given period and divides it by the number of period used. Which gives us an average for each subset in our data set.

We can apply it easily in Python using the rolling() method coupled with the mean() method which provides us a quick and easy window calculation. We will be using a window of 20. If we plot it. X-axis: Amount of closing price, Y-axis: Simple Moving Average (SMA) closing price

So that’s our moving average for our first price sample. It doesn’t stop here.

Let’s use a technique used and favored by many traders and technical analyst which is the crossover strategy. We will be plotting another SMA line with a window of 100.

But first, let’s see how it works with an example. As we can see in the graph, when two SMA crossover each other pointing upwards it is a uptrend signal and when pointing downwards it is a downtrend signal.

Let’s apply it on our graph.

Let’s instantiate our new window of 100 to rolling2. Let’s plot our 2 SMA. That’s a lot of crossovers!

If you got an eye, there are a couple of crossovers that captures our attention.

Let’s zoom into it, at X-axis 650 and 1000 and Y-axis  200 and 320. As we can see at X-axis between 700 and 750 there is a abrupt upward crossover from the 100 SMA which then gave us a High closing price uptrend with the following data points.

At X-axis 850 and 900, we got a downwards crossover again from our 100 SMA which gave us a Low closing price downtrend.

SMA is a great way to detect price trend direction in the stock market, applying it on a free market is trickier because of the amount of false positives and false negatives in the data which makes it highly variable. But it is still a nice way to get a feel of a given market.
If you want to learn more about SMA and technical analysis: