Beginner’s Guide to Technical Analysis in Python for Algorithmic Trading in 2022


Beginner’s Guide to Technical Analysis in Python for Algorithmic Trading in 2022

Fundamental analysis studies the company’s past & present performance by looking at various factors that can impact the stock price like the company’s profit & loss statements, cash flow, balance sheet, management process, etc. On the other hand, technical analysis uses only the past price & volume movements to identify patterns/trends in the stock.


This article focuses on introducing a few of the straightforward yet effective methods used in technical analysis. Any trade instrument can be used to implement all the concepts presented here (like stock, currency, options, indices, or crypto).


Applying any of these methods separately in practice might not produce the desired outcomes. Before making a BUY/SELL/HOLD decision, analysts typically combine these strategies. The first level of analysis can be automated using a programmable technique.



When you've finished reading this article, you should have picked up the following tips and tricks:


How to obtain stock information


How to apply and interpret some of the fundamental candlestick patterns

- Marubozu Candle and Doji Candle, are single candlestick patterns.

- Several candlestick patterns, including the Morning/Evening Star and Engulfing.

How to use a few well-known trading indicators and techniques in Python

Moving Average Techniques

RSI Indicator, or relative strength index
Indicator of Moving Average Convergence and Divergence (MACD)

Bollinger Bands.


How to use Python to construct a basic trading algorithm




Here, we'll concentrate on utilizing information obtained from the National Stock Exchange (NSE) India stock exchange. The sole requirement is that the data have the OHLC (Open-High-Low-Close) information. Data can also be retrieved from other exchanges.


A candlestick can be used to depict the OHLC (open, high, low, close) price of a stock. An overview of a candlestick depiction is provided below:

Green candle: the stock price closed higher than it did at the open.

Red candle: lower closing price than the stock's opening price.

The area between open and close (Green/Red) is shaded.

Day's high (top wick) and low on a candle (bottom wick)


The graph objects from the plotly package is used to plot the candlestick chart. Each candle in the chart reflects the OHLC values for a day because we downloaded the daily data. Day's high wick (top wick)



The candlesticks might provide important information about the trading trend. A single candlestick or several candlesticks may be employed depending on the analysis.


To recognize the candlestick patterns in this situation, we'll use the TA-Lib library.

Marubozu Candle a single candlestick with either no wicks at all or no wicks at all

It shows if a stock is strong (green) or weak (red).


In the sample dataset, it does not show a trend-reversal Marubozu candle:

Candle Doji a single, little candlestick (opens and closes at almost the same price)

shows hesitancy because there are both buyers and sellers of the stock.


Typically, it denotes a sideways motion.

Candlestick pattern that engulfs

Multiple candlestick patterns: 2 candles are taken into account, with the second candle engulfing the first candle and being marginally longer than it.

A red candle, a green candle, and a bullish engulfing pattern provide a buy signal.

A green candle, red candle, and a bearish engulfing pattern indicate a sell signal.

Pattern of the Morning and Evening Stars

Multiple candlestick pattern, taking three candlesticks into account

These patterns show a change in the trend.

Red Candle, Doji, Green Candle, Morning Star, Buy signal

Evening Star: SELL signal (Green Candle, Doji, and Red Candle).

as in DMART stock.


To learn more about the various other pattern identifiers included in the TA-Lib package, see this link: Pattern RecognitionMARKETING PLAN/INDICATORS

There are other methods/strategies in addition to candlestick analysis that can be used. The following explains a few of these tactics.



The average stock price over the previous 'n' days is known as the simple moving average (sma for short). This can be calculated in in TA-Lib

Moving Average Comparative Method


Comparing the current stock price to its prior n-days moving averages is one of the trading strategies. A 20-day, 50-day, and 200-day moving average is frequently employed.



20-day sma, 50-day sma, 200-day sma, and the current close signal to buy


SELL signal: Current close > 20-day SMA > 50-day SMA > 200-day SMA


Strategy for Moving Average Crossover


Plotting a long-term moving average (such as the 200-day sma) against a short-term moving average is another method known as the moving average crossover method (eg: 20-day sma).


In general, a buy signal is given whenever the short-term sma crosses the long-term sma from below.

Sell indication if the short-term sma crosses the long-term sma from above.

Closing price > 20-d SMA > 50-d SMA > 200-d SMA for the HDFCAMC stock data from 14–12–2021 onwards indicates a BUY signal.

The 50-day SMA crosses the 200-day SMA from top to bottom on 16-11-2021, signaling a sell signal (also known as the death cross).


The window period and the number of moving averages are both customizable. Sometimes, instead of using the basic moving average, the exponential moving average (EMA) is taken into account (SMA). EMA lends higher weightage to the prices closer to the present date as compared to the older dates in the window period.



Many traders employ the RSI, a well-liked technical analysis indicator. It is used to evaluate a stock's momentum. The most recent gains and losses are contrasted. It provides information on whether a stock is oversold or overbought.

The RSI can be calculated mathematically as shown below. The window period used to calculate the RS value is typically 14 days.



Another straightforward and well-liked momentum indicator that provides helpful information based on the stock's most recent movement is the MACD. It displays how two moving averages are related to one another.


It generally consists of three elements, with typical values for each one as follows:


12-day EMA — 26-day EMA MACD Line

9-day signal line MACD line's EMA

MACD Line — Signal Line in the histogram

The MACD line will move more quickly and is more responsive to changes in price. The signal line will be a smoother line since it will respond to price fluctuations more slowly. The strength of the momentum can be calculated from the histogram's size.


There is no universally effective fixed strategy. All of the aforementioned patterns & tactics can only offer direction. There are numerous additional non-technical factors that cannot be codified that can affect stock prices.


The ability to create custom algorithms and automate the process of performing the fundamental technical analysis on a stock is provided to the trader by implementing technical pattern analysis through code.

You can get your Real-Time and Historical Stock Data with Free API key.

Build with us today!

Start Free Trial