Stock Technical Analysis using Python – Introduction

In this note series we will use python programming language to do the stock technical analysis.

Stock Technical Analysis

Stock technical analysis is a methodology that uses technical indicators to identify price trends, momentum such as tendency of either rising or falling prices, and volatility. These indicators are important irrespective of whether we are doing day trade or swing trade.

Stock technical indicators are calcuated by applying a certain formula to stock prices and volume data. They are used to alert on the need to study price action with greater detail, confirm other technical indicators’ signals or predict future stock prices direction. These are either plotted on top of price bars as overlays or below as oscillators. There are two main types, lagging and leading stock technical indicators. These indicators help in decision make.

Lagging stock technical indicators

Lagging stock technical indicators are used to follow price movements by identifying uptrends or downtrends from the past prices of stock data.

The most popular tools or methods are simple moving averages (SMA)exponential moving averages EMA), cumulative moving averages (CMA), bollinger bands, parabolic stop & reverse, among others.

  • The lagging stock technical tools or methods are used to get the delayed feedback, which means the indicator tells the traders to confirm the price trend of whether it is going upward or downward before executing any trade.
  • It is always recommended to use more than two lagging indicators to confirm the price trends before entering executing any trade.
  • It does not predict the future prices of an investigating stock. However, it is suitable for understanding the momentum or trend of a particular stock. 

Moving Average (MA)

A moving average is overlays that smooth stock price to identify trends. It is a calculation by creating a series of averages of different subsets of the full data set.  It helps in smooth out the price data by creating a constantly updated average price. The most popular moving average types are Simple Moving Average (SMA) and Exponential Moving Average (EMA).

Simple Moving Average (SMA)

It calculates the average price value of particular stock at a particular interval.

\text{SMA} = \frac{\text{Sum of prices of a particular interval}}{\text{interval lenght}}

For example, if a particular stock price values are [21, 21.5, 22, 21, 21.5] and interval lenght is equal to 4, then

  • The first moving average is sum of first four price values: 21+21.5+22+21 = 85.5/4 = 21.375.
  • The second moving average is sum of second to fifth price values: 21.5+22+21+21.5 = 86/4 = 21.5.

In this way, we can compute the moving average manually. However, we can also calculate moving averages using simple python code, that is as follows:

data = [1,5,8,2,3,1,3,4,5,6,7,7]
window_size = 4

iterate = 0
moving_averages = []
while iterate < len(data) - window_size + 1:
    current_window_data = data[iterate : iterate + window_size]
    current_window_average = sum(current_window_data)/window_size
    moving_averages.append(current_window_average)
    iterate += 1

print(moving_averages)

The above approach is suitable for a small set of data. However, we can use the pandas series class to calculate the moving averages for a larger set of data. The pandas.Series() class converts input python list data into pandas.Series object, which provides functionalities to manipulate or perform operations on the input data. Series.rolling() function to find the rolling window sum of the underlying data for the given Series object.

import pandas as pd

data = [1,5,8,2,3,1,3,4,5,6,7,7]
window_size = 4

# Convert list data into pandas.Series object.
series_data_obj = pd.Series(data)

# Calculate the moving averages based on the windows size.
moving_averages = series_data_obj.rolling(window_size).mean()

# Convert this dataframe into list object.
moving_averages_list = moving_averages.tolist()

# Remove data elements with nan values as window_size-1 placeholder values will be nan.
mv_without_nans = moving_averages_list[window_size - 1:]

print(mv_without_nans)

Weighted Moving Average (WMA)

Exponential Moving Average (EMA)

 263 total views,  1 views today

Scroll to Top
Scroll to Top
%d bloggers like this: