PYTHON FINANCE

Stock Market Trends

Lets get this money right

Frank Ceballos
Analytics Vidhya

--

Photo by Alex Knight on Unsplash

Purpose: The purpose of this article is to introduce the reader to some of the tools used to spot stock market trends.

Materials and Methods: We will utilize a data set consisting of five years of daily stock market data for Analog Devices. The time period we consider starts on January 1, 2013 and ends on December 31, 2017. We will start analyzing the data using line plots, then introduce candlestick charts. Patterns that can be seen in the candlestick chart will be introduced which can be used to spot changes in the market. We add another of level analysis by overlaying moving averages and discussing how these can help confirm trend changes. Finally, we construct a figure that concisely summarizes the stock price data for any company.

Hardware: We use a workstation equipped with Inter(R)Core(TM) i7–8700 with 12 CPU @ 3.70 Ghz and NVIDIA GeForce RTX 2080.

Note: In the case you’re starting from scratch, I will advise you to follow this article and install all the necessary libraries. It will be assumed that the reader is familiar with Python and Pandas. The whole contents of this article can be found on my GitHub. You’re welcome to fork it.

Disclaimer and Terms of Use: The information and methods published in this article are solely meant to be used for informational and educational purposes. The contents of this article are not trading advice.

A Word from the Author

I started this article with the purpose of predicting the closing price of a stock. Using the stock data from different companies, I build a Keras based long-short-term memory (LSTM) network which took multiple channels of data as input. I optimized the architecture using a fancy algorithm, and after all that work, I realized that the exercise of predicting the closing price was trivial. For example, the close price for tomorrow is likely to be near the close price for today. To show this is the case, let’s built a model that predicts the price for tomorrow based on the average price of the last two days. Here are the results:

The actual close price is in blue and the predictions are in magenta.

So did we just predicted the behavior of the stock market using a model based on elementary level math? Well, we did but the usefulness of such a model is practically none. You might as well just flip a coin to determine if you’re going to buy or sell stock. The models tells that the price for tomorrow is going to be similar to the price it was today. You tell me how can we develop a trading strategy with that type of knowledge.

What is more useful is to predict the trend of the market. For example, traders that invest in a company with an upward trend (riding up) will constantly monitor the stock price and look for indicators that show is time to sell. From what I’ve gathered, trying to uncover a weak trade signal that indicates when is time to sell or buy is what you should be after. The reason you want the signal to be weak is so that you can notice it before anyone does. In that way, you will able to buy cheap stocks that will rise in price or sell stocks that will decrease in price.

Before I embark on the journey of predicting trend changes using machine learning, I would like to introduce the reader to stock market data and the tools used to analyze it. We will (sort of) build the tools used by traders from scratch to confirm and anticipate trend changes. If you reach the end of this article, you will be able to make and understand some really cool graphs. So go grab a beer, and let’s get started.

Collecting Data

To obtain our data, we will use Yahoo Finance to download the historical stock market prices for Analog Devices.

You can choose to download the data yourself for a more updated version or you can download it from my GitHub repository.

To obtain data for companies you might be interested in:

  1. Visit Yahoo Finance. You will be greeted by the Yahoo Finance homepage — see Figure 1.
Figure 1 — Yahoo finance homepage.

2. In the search bar, search for the company you’re interested in. A drop down menu will appear with a list of the companies that most closely match your search. Select the company you are interested in. In this example, we will be downloading Texas Instruments Incorporated stock market data— see Figure 2.

Figure 2 — Searching for Texas Instruments stock market data.

3. The results will return a snapshot summary of Texas Instrument stock market data — see Figure 3. Click on Historical Data.

Figure 3 — Texas Instruments results.

4. You will be directed to a page where you can download the historical data for the company. You can select the Time Period and Frequency for which this data was acquired. For our purposes we will set the Time Period to MAX and Frequency to daily. This will return all the daily data that is available for Texas Instruments. To download, click on Download Data to obtain a csv file, see Figure 4. It’s really cool that you can download decades worth of data for free — thank you Yahoo!

Figure 4 — Texas Instruments historical data.

The downloaded historical data will contain the following 7 columns. These are:

Date: The date the data was acquired.

Open: The price of the first trade on the given trading day.

High: The highest price at which a stock traded for the given trading day.

Low: The lowest price at which a stock traded for the given trading day.

Close: The price of the final trade before the end of the trading day.

Adj Close: According to Yahoo Finance, this is the “the closing price after adjustments for all applicable splits and dividends distributions”.

Volume: The number of shares traded for the given trading day.

Make sure you place all the files in the same folder. This is done so that later we can write a function to easily read the data. I also renamed each of these files so that they can be read in numerical order. For example, I saved all csv files in a folder named data, see Figure 5 for the contents of this folder.

Figure 5 — Example how data was saved for two different companies

As a final note, you can also collect data by writing a script. Here you can find the details in how to do so.

Importing Libraries

Let’s start by importing all the libraries we need.

Script 1 — Importing libraries.

You might have some ‘ModuleNotFoundError’s. So let me show you how to install the missing modules using Anaconda. If you don’t have Anaconda installed, I will suggest you to follow this tutorial to setup a Python-based machine learning environment.

Now open Anaconda Prompt, activate the environment you’re planning to work in, and execute the following command to install mpl_finance:

pip install mpl_finance

This library is no longer maintained, but we can still use it to create OHLC charts.

Importing Data

To read the data, we will write a helper function that will take as input the path to the folder that contains the data, the starting date, and the end date for the time period we are interested in. The dates should be strings and should follow the following format: Year-Month-Day. For example if your start date is January 15, 2015, you should write start_date = "2015-01–15". The output of get_data() would be a dictionary where the keys are the file names (without the extension) and each entry a Pandas Dataframe object that contains the data for the company denoted by the key.

Script 2 — Helper function to read data from the folder specified by data_prefix.

To read data we execute the following script:

Script 3 — Reading data.

In line 5 of Script 3, we specify the path to the folder where our data is saved.

In line 8–9 in Script 3, we define the time period for which we decided to read data. The syntax for the date should be Year-Month-Day.

In line 12 of Script 3, we read all the data saved in the folder specified by data_prefix into a pandas DataFrame object named data.

Let’s do a quick inspection of the data we imported. Since I used Spyder, I go to the Variable Explorer tab and click on data to inspect the entries of the dictionary. If you inspect the entry of 01ADI the following window should open up.

Figure 6 — First five rows of all_data, a pandas DataFrame object.

Each row in corresponds to the same day. In total there is 6 columns. I’ve seen NULL present in some of rows from stock market data downloaded from Yahoo Finance. Make sure you that you don’t have any funny things happening with your data.

Visualizing Stock Market Data

Line Plots

Is always a good idea to visualize the data you collect. This allows to access its quality, gain valuable insight to decide the following steps, and determine if any grotesque errors are present. We will create line plots with the Open, High, Low, Close, and Adj Close columns from each of these companies. For the Volume, we will create its own plot since the difference between the opening price and the volume can be up to six orders of magnitude. As always, plotting in Python is not intuitive but here we go.

Script 4— Line plots. To change the time interval that will be displayed in the graph, change line 20 and 21. The y-axis limits are controlled by line 30.
Figure 7 — Summary of Analog Devices stock market historical data. The time period considered is between 2015–01–01 and 2018–01–01.

Let’s inspect Figure 7 and see what we can find out. Notice that in top subplot the Open, High, Low, and Close price are highly correlated — they basically overlap each other. The Adj Close price follows the trend of the Open, High, Low, and Close price; however, there is a constant offset.

If you inspect the Volume graph (the lower subplot in Figure 7), you can see that there is multiple peaks that appear to occur at “random” times. Furthermore, upon closer inspection you will notice that when the peaks in Volume occur the Open, High, Low, and Close price goes up or down. To illustrate this and make it more obvious that the peaks in Volume correspond to changes in stock market prices, we will recreate Figure 7 using Script 4, using a shorter time interval. We first consider the time period between 2015–03–01 and 2015–05–01, see Figure 8.

Figure 8 — Summary of Analog Devices historical data for the time period between 2015–03–01 and 2015–05–01.

In article published in Investopedia, it was mentioned that in March 2015 the stock price of Analog Devices increased about 9.8 % after its partnership with Apple was made public. Precisely, we can see this in Figure 8, where the stock price rapidly increased on Monday, March 30, 2015. If I had to guess what happened, I will say that Apple and Analog Devices made their business plans public on articles published on Friday, March 28 2015. The news spread through the weekend and on Monday, the start of trading week, there was a demand for Analog Device stocks.

Let me mention that the peaks in Volume are not entirely random. They are driven by human decisions and public interest. If you can identify what events are more likely to change the stock market price you can anticipate how stock price will change. This is way easier said than done. Figuring out this is way out of scope of this article and I am certain that millions of dollars are being invested just in this problem.

Peaks in volume can also correspond to a decrease in stock price. To show this lets consider the time period between 2016–05–01 and 2016–09–01, see Figure 9.

Figure 9 — Summary of Analog Devices historical data for the time period between 2016–05–01 and 2016–09–01.

Without any explanation we can conclude from Figure 7–9, that peaks in Volume constitute events that:

  1. Can indicate a rapid fall or rise of stock prices.
  2. Can indicate the beginning of a steady stock price increase.
  3. Can indicate the end of a steady increase in stock price.
  4. Can indicate the start of a steady decrease in stock price.

Is encouraged that you perform a similar inspection of the data from other companies you might be using just to make sure that there is no funny things happening within the period you’re considering.

Candlestick Charts

Now let’s create something nicer than a line plot that can more closely inspect the relationship between the Open, High, Low, and Close price; namely, we will create a candlestick chart. Candlestick charts are a trading tool that effectively visualize the stock price changes over time. Specifically, each candlestick in the chart displays the tendency of the market for the given time interval. Before we make the candlestick chart, lets first inspect the anatomy of the symbols used to create them.

Figure 12 — Anatomy of candlestick.

A candlestick is composed of a bar, an upper and lower line that sticks out from the bar known as wicks, and its color. The color denotes if within the given time interval the market price went up (green: the Close price is higher than the Open price) or down (red: the Close price is lower than the Open price). The top wick represents the High price and lower wick represents the daily Low price within the given time interval. By using candlestick charts, traders are able to visually analyze the trend of the market.

Let’s make candlestick chart for Analog Devices where each bar represents the daily tendency of the market. To do so, we will first have to create a DataFrame with the correct data format. Specifically, the first column should be the date, the second column the Open, the third column the High, the fourth column the Low, and the fifth column the Close.

Script 8— Preparing data for candlestick chart. Is important that your dates are converted to the proper format using date2num(). This is a requirement for candlestick_ohlc().

To give you an idea of how the data should be format lets inspect the head of ADI_candle:

Figure 13 — ADI_candle first five rows.

The reason that candlestick data needs to be organized this way is because is a requirement of the candlestick_ohlc() function that is used to create the candlestick chart.

With the data prepared into the correct format, we now need to define a time interval we want to inspect. For the sake of illustration let’s look at the time interval between 2015–05–18 and 2015–07-10. To create the candlestick chart execute the following script:

Script 9 — Creates a candlestick chart. To control the x-axis limits change line 5 and 6. The y-axis limits are controlled by line 19.
Figure 14 — Candlestick chart produced by Script 9. The annotations were added with Powerpoint.

Before we inspect the candlestick chart let’s us appreciate how much better this summarizes the trading day tendency when compared to the line plots we created before. Just by looking at the color of a bar and its wicks, we know what happen for that trading day. For example, the second bar in Figure 14 is red (the first bar is green), that means that the the Close price was lower than the Open price. The length of the bar gives you an idea of how much lower was the Close price than the Open price. Additionally, with the top (bottom) wick we can determine how high (low) the stock traded.

Now some technical jargon:

  • Bullish: When the market trend is going up.
  • Bearish: When the market trend is going down.

With all of these, lets determine the market direction using Figure 14 and develop a trading strategy. Suppose that we bought Analog Device stock at $50.00 on a previous date. After inspecting the candlestick chart shown in Figure 14, we notice a pattern. Initially, we see that the market trend is bullish (upwards), reaches top 1, falls down to a neckline, and recovers closely to the same price as that of top 1 — this is top 2. After the market price reaches top 2, the following candlestick is red meaning that the daily tendency was bearish. If the bearish trend continues and falls below the neckline (red dotted line) this pattern will be identified as a Double Top Forex Chart Pattern and can be seen after an extended bullish trend. This is were things start to get interesting. After traders notice that the second top does not go above the first top they know that the market might start a downwards trend. If we were an aggressive trader we would sell shortly after the second peak anticipating the downwards fall of the market. This would make us a profit of about $18 dollars for each stock we owned; however, the negative trend of the market is more likely to happen if it falls below the neckline. If we were more conservative trader, we will wait for the market to drop below the neckline and sell shortly after this happens. This action would result in a profit of about $15–16. It’s important to note that stock market is not a well behaved system which implies that every action has a risk associated it with it. The aggressive trader has a higher risk of making a bad decision since he trades before the confirmation of the pattern; however, his profits would be the largest if he correctly anticipates this trend. The more conservative trader risks less and as a consequence will have smaller profits.

There is other trends that can be found in candlestick charts that can be used to trade. For example, single candlestick patterns, candlestick patterns compose of a three or more candlesticks, and a bit more complex patterns such as the 1–2–3 chart pattern or a double bottom pattern. However, since this article is already getting pretty long I will leave it to the interested reader to read more about them elsewhere.

Moving Averages

Along with candlestick charts, traders often use moving averages to remove noise, to identify and confirm trends, and as support and resistance levels (more of this later). Since moving averages are based on past prices they are are known as lagging indicators. Consequently, they do not predict the future trend but they are used as signals to confirm a trend change has taken place.

The two most popular moving averages are known as the simple moving average (SMA) and the exponential moving average (EMA). The SMA is calculated by adding the closing prices of the most recent days and dividing by the number of days. In equation form this is

where C is the closing price and n the number of days. Notice how the close prices in a SMA are weighted equally. Because of this, the SMA slowly reacts to rapid price changes. A longer SMA period will result in a larger lag.

Here is a concrete example:

Closing prices: 10, 9, 12, 14, 11, 13, 15First day of 5-day SMA : (10 + 9 + 12 + 14 + 11 ) / 5 = 11.2Second day of 5-day SMA: (9 + 12 + 14 + 11 + 13) / 5  = 11.8Third day of 5-day SMA : (12 + 14 + 11 + 13 + 15) / 5 = 13.0

To calculate the SMA will use the function shown in Script 10. It takes as input a Pandas Series containing the closing price and an integer specifying the time period to consider for each average.

Script 10 — Function to calculate the SMA.

The calculation of the EMA is a bit more involved but is not too bad. The major difference between EMA and SMA is that in an EMA the closing price of the most recent days are given more weight.

Here is the algorithm to calculate the EMA for the current day:

  1. Find the previous day’s EMA. If this is the initial day you’re calculating the EMA, the initial EMA will be set to be the SMA.
  2. Calculate the weighting multiplier:

where N is the time period you’re considering in your moving average.

3. Calculate the EMA for the current day using:

Now that we know that, let’s write a function for it.

Script 11 — Function to calculate the EMA.

Before we move on I will like to point out a few things out. First, if you’re using a 10-day moving average the first day you can calculate the SMA or EMA would be for the 11th day. This is simply because you’re using the first 10 days to calculate the average for the 11th day. Second, moving averages are not predictors but simply indicators of trends. Thirdly, moving averages by design lag behind the price. As you will see shortly, there is a difference in lag between an SMA and EMA. Fourthly, is not clear what days in the average should be given more weight. As a consequence, SMA and EMA are plotted along candlestick charts. Finally, the moving averages are calculated over different periods of time to reveal short term and long term information about the trend.

With all of this said, we will calculate moving averages for short, intermediate, and longer terms to analyze market trends. The following list summarizes the difference between this time frames:

  • Short-term: Time frames between 5–20 days and are useful for identifying trends that can last a few days to a few weeks. Use to determine minor trends.
  • Intermediate: Time frames between 20–65 days and are useful for identifying trends that last a few weeks to a few months. Use to determine secondary trends.
  • Long: Time frames between 100–200 days and are useful for identifying trends lasting a few months to a few years. Use to determine primary trend.

We will use the following specific trends as suggested in this video:

  • 50 and 200 day SMA: Use to visualize the intermediate and long term trends of the market.
  • 9 and 20 day EMA: Use to visualize the short-term trends of the market.

You can read more about time frames here.

Let’s get back to coding:

Script 12 — Computing popular moving averages.

Finally, let’s add moving averages to our candlestick chart and see how they look. To do so, we will recycle Script 9 and implement a few changes.

Script 13 — Candlestick chart with moving averages.
Figure 15 — Candlestick chart produced by Script 9. The 200-SMA is trading below the price window shown. All the annotations were added later in PowerPoint.

Well that looks pretty neat but what does it all mean? Well, before we start to analyze the technicalities of the Figure 15 let’s reduce the analysis to its simplest form:

  1. When the close price falls below a moving average sell.
  2. When the close price goes above a moving average buy.
  3. If a shorter-term moving average moves above (below) a longer-term moving average buy (sell).

If you apply these simple rules to Figure 15, you will notice that when the close price falls below the moving average the trend start to go downhill. That’s is basically how you use moving averages.

Now let’s get a bit more technical and start defining crossovers, support levels, and resistance levels:

  • Price crossovers — Happens when the close price falls below or crosses over a moving average line — see Figure 15.
  • Double crossovers — Happens when a one moving average line moves above or below another moving average line — see Figure 15.
  • Support level — Happens when the close price trades above a given moving average. In Figure 15, we highlighted in green where the 20-EMA acts like a support level. This is referred as support because it is keeps the close price from falling below a certain point. Different moving averages can act as support as levels. The larger the time frame for a given moving average, the stronger of a support level it is. For example, it’s harder for the close price to fall below the 50-SMA than it is to fall below the 9-EMA.
  • Resistance level — Happens when the close price trades below a given moving average. In Figure 15, we highlighted in red where the 20-EMA acts like a resistance level. It is thought that the close price experience resistance as it moves closer to a resistance level. Different moving averages can act as a resistance levels. The larger the time frame for a given moving average, the stronger of a resistance level it is. To be as obvious as possible, it’s harder for the close price to go above the 50-SMA than it is for it to go above the 9-EMA.

Take a look at Figure 15 and convince yourself that you can identify crossovers, support levels, and resistance levels. Now let’s see how we can use moving averages as signals to determine potential changes in the trend.

Bearish Trade Signals

A bearish signal is composed of the signal itself and its strength. The signal can either be a price crossover or a double crossover. The strength of the signal is determined by the time frame of the moving average that triggered the signal. A larger time frame will indicate a stronger signal. Specifically, we can have a bearish price crossover or a bearish double crossover. Here are a list of signals and their strengths associated with them:

  • A weak bearish price crossover signal — If the close price drops below a short-time frame moving average such as the 9-EMA or 20-EMA, an early but weak signal of bearish trend is said to happen. This means that a possible drop in price coming up soon; however, is not very likely. See Figure 15–16 for an examples of this.
Figure 16 — Examples of price cross overs. Notice that after the price crossover every moving average acts like a resistance level. Annotations added in PowerPoint.
  • A strong bearish price crossover signal — This happens when the close price falls below a long-term moving average. The chances that a possible down fall off the market is coming is strong. In Figure 16, we can see that when the close price falls below the 200-SMA the price proceeds to falls from $58 to $48 in a matter of weeks. As a matter of fact, the close price falls below all the moving averages around the same point! If you see this happening is time to quickly sell those suckers and make all kinds of profit! With the earnings you made celebrate by buying the red Ferarri you always wanted. LOL.
Photo by Joshua Rawson-Harris on Unsplash — Success!
  • Bearish double crossover — A weak early signal is said to happen when a short-term moving average falls below an intermediate-term moving average. A stronger late signal is generated when a short or intermediate-term moving average falls below a long-term moving average. See Figure 17 for an example of this. If you see this happen, this would be a good indication is time to sell boys.
Figure 17 — Examples of price double cross overs.

Bullish Trade Signals

Bullish signals indicate that a rising trend has taken place. There are bullish price crossovers (close price goes above moving average) and bullish double crossovers (one moving average goes above another). The following determines the strength of each signal:

  • Bullish price crossover —This happens when the close price goes above a moving average. The longer the moving average time frame, the stronger the signal is said to be. For example, if the close price goes above the 200-SMA this would be considered a strong late signal that indicates that the market trend will start to increase. See Figure 18 for examples.
  • Bullish double crossover — When a short-term moving average moves above an intermediate-term moving average a weak early trade signal is said happen. If a short or intermediate-term moving average moves above a longer-term moving average a late but stronger trade bullish signal is confirmed. See Figure 18 for examples.
Figure 18 — Examples of bullish trade signals.

Putting Everything Together

We have learned that changes in volume, candlestick patterns, and moving averages can be used to confirm trend changes. Let’s finalize this article by putting everything into a single graph and using it to determine trend changes.

First, lets crunch all the numbers:

Script 14—Crunching all the numbers to make cool figure.

Then let’s make a candlestick chart with moving averages and a volume line chart:

Script 15 —Candlestick chart, with moving averages, and volume.
Figure 19 — Magic!

In Figure 19, we can see that a lot of bullish signals happen at the same time. For example, a strong bullish price and double crossover happen around the same time while a peak in Volume is observed. All these signals indicate a bullish trend is going to take place and as you can see over the course of 8 months the price goes from $55 all the way up to $85. Additionally, most of the times all the moving averages are acting as support levels preventing the the close price from falling. With all those profits is time to live the life of luxury you always wanted. Throw in a Lambo and a jet while you’re act it.

Photo by Superlative Co on Unsplash — Luxury!

Closing Remarks

If you reached the end of the article, I would like to thank you for your time. You should now feel more confident in using and building your own candlestick charts, moving averages, and volumes. Each of these trend indicators should be used together and not individually. The more signals you get the higher the chances that a trend change has taken place. You’re welcomed to take everything is this article and use it for your own purposes. If you expand upon it and build some fancy tool, I would love to see it. I’m planning in make some sort of GUI so that with a few clicks I can rapidly analyze the stock market data. Moreover, the end goal is to add some predictive modeling functionality using machine learning. We have learned quite a lot but the journey does not end here. Stay tune!

Find me at LinkedIn, comment if you found these content useful, and until next time my friends. Code everyday!

--

--