0% found this document useful (0 votes)
221 views14 pages

Exploring The Best Indicators in TA-Lib - Technical Analysis of Stocks Using Python - Part 1 - by Himanshu Sharma - MLearning - Ai - Medium

This document discusses technical analysis indicators that can be used to analyze stock market data using the TA-Lib library in Python. It explores popular indicators like moving averages, Bollinger Bands, RSI, MACD, stochastic oscillator, and Fibonacci retracements. Code examples are provided to calculate each indicator on stock data downloaded with yfinance.

Uploaded by

chee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
221 views14 pages

Exploring The Best Indicators in TA-Lib - Technical Analysis of Stocks Using Python - Part 1 - by Himanshu Sharma - MLearning - Ai - Medium

This document discusses technical analysis indicators that can be used to analyze stock market data using the TA-Lib library in Python. It explores popular indicators like moving averages, Bollinger Bands, RSI, MACD, stochastic oscillator, and Fibonacci retracements. Code examples are provided to calculate each indicator on stock data downloaded with yfinance.

Uploaded by

chee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

19/09/2023, 21:31 Exploring the Best Indicators in TA-Lib: Technical Analysis of Stocks using Python- Part 1 | by Himanshu Sharma

hu Sharma | MLearning.ai | Medium

Member-only story

Exploring the Best Indicators in TA-Lib:


Technical Analysis of Stocks using Python-
Part 1
Using the python library TA-Lib for performing Technical Analysis on Stock Market
Data

Himanshu Sharma · Follow


Published in MLearning.ai
5 min read · Jan 24

Listen Share More

Photo by Nicholas Cappello on Unsplash

https://medium.com/mlearning-ai/exploring-the-best-indicators-in-ta-lib-technical-analysis-of-stocks-using-python-part-1-b7ad731aeeb2 1/14
19/09/2023, 21:31 Exploring the Best Indicators in TA-Lib: Technical Analysis of Stocks using Python- Part 1 | by Himanshu Sharma | MLearning.ai | Medium

Technical analysis is a method of evaluating securities that involve analyzing market


activity statistics such as past prices and volume. Technical analysts believe that a
stock’s past performance, combined with other market indicators, can be used to
forecast its future performance.

The basic idea behind technical analysis is that market trends, as depicted by charts,
can predict a stock’s future performance. Technical analysts use charts and other
tools to identify patterns and trends indicating buying and selling opportunities.
They also employ a number of indicators,
Open insuch
app as moving averages, the relative

strength index (RSI), and Bollinger bands, to help identify market trends and
potential turning points.

The fundamental assumption of technical analysis is that the market discounts


everything, which means that all information that could affect the stock price is
already reflected in the stock’s current price and volume. According to technical
analysts, the market is efficient and that past market data, such as stock prices and
volume, can be used to forecast future market activity.

It’s important to remember that technical analysis isn’t a perfect science, and its
effectiveness varies depending on the stock and market conditions. Furthermore,
technical analysis should be combined with fundamental analysis and a well-
defined trading strategy.

TA-Lib (Technical Analysis Library) is an open-source software library for the


technical analysis of financial markets. It provides over 150 indicators, including
popular indicators such as Moving Averages, Bollinger Bands, and Relative Strength
Index (RSI).

In this article, we will take a look at some of the best indicators in the TA-Lib library,
along with code examples to help you get started, and we will use the yfinance
library for downloading the stock data.

Let’s get started…

Installing Required Libraries


For this article, we will be installing two main libraries that are Ta-lib and Yfinance.
Installing YFinance is an easy task and we can install it using pip by running the
command given below.

https://medium.com/mlearning-ai/exploring-the-best-indicators-in-ta-lib-technical-analysis-of-stocks-using-python-part-1-b7ad731aeeb2 2/14
19/09/2023, 21:31 Exploring the Best Indicators in TA-Lib: Technical Analysis of Stocks using Python- Part 1 | by Himanshu Sharma | MLearning.ai | Medium

pip install yfinance

Ta-lib installation is different from other python libraries as it is not available to


install directly using pip install. First, we need to visit the link and download the whl
file of Ta-Lib according to our windows version. After that, we can install it using
pip install using the command given below.

pip install <filename.whl>

Importing Required Libraries


Now that we have installed the required libraries, let’s import all the libraries that
we need for this article.

import yfinance as yf
import pandas as pd
import talib

Exploring Indicators
Let’s start exploring the Technical Indicators using the TA-Lib library, and also look
at how we can generate these indicators.
a. Moving Averages
Moving averages are one of the most popular technical indicators used to smooth
out price fluctuations and identify trends. They can be calculated using different
time periods, such as short-term (10-day), medium-term (50-day), and long-term
(200-day).

import yfinance as yf
import pandas as pd
import talib

# Download the historical data for the asset


stock = yf.Ticker("SANOFI.NS")

https://medium.com/mlearning-ai/exploring-the-best-indicators-in-ta-lib-technical-analysis-of-stocks-using-python-part-1-b7ad731aeeb2 3/14
19/09/2023, 21:31 Exploring the Best Indicators in TA-Lib: Technical Analysis of Stocks using Python- Part 1 | by Himanshu Sharma | MLearning.ai | Medium

data = stock.history(period="max")

# Calculate the simple moving average (SMA)


data["sma_10"] = talib.SMA(data["Close"], timeperiod=10)
data["sma_50"] = talib.SMA(data["Close"], timeperiod=50)
data["sma_200"] = talib.SMA(data["Close"], timeperiod=200)

Using the above code you can generate the moving averages for 10, 50, and 200 days.

b. Bollinger Bands
Bollinger Bands are used to measure the volatility of an asset and identify
overbought and oversold conditions. The bands consist of a moving average
(typically the 20-day moving average) and two standard deviation lines above and
below the moving average.

# Calculate the Bollinger Bands


data["upper_band"], data["middle_band"], data["lower_band"] = talib.BBANDS(data

c. Relative Strength Index (RSI)


The RSI is a momentum indicator that compares the magnitude of recent gains to
recent losses to determine overbought and oversold conditions. It is typically used
with a 14-day period.

# Calculate the relative strength index (RSI)


data["RSI"] = talib.RSI(data["Close"], timeperiod=14)

d. Moving Average Convergence Divergence (MACD)


The MACD is a trend-following indicator that measures the difference between a
short-term moving average and a long-term moving average. It is typically used with
a 12-day and 26-day moving average, and a 9-day signal line.

# Calculate the MACD


data["macd"], data["macd_signal"], data["macd_hist"] = talib.MACD(data["Close"]

https://medium.com/mlearning-ai/exploring-the-best-indicators-in-ta-lib-technical-analysis-of-stocks-using-python-part-1-b7ad731aeeb2 4/14
19/09/2023, 21:31 Exploring the Best Indicators in TA-Lib: Technical Analysis of Stocks using Python- Part 1 | by Himanshu Sharma | MLearning.ai | Medium

e. Stochastic Oscillator
The Stochastic Oscillator is a momentum indicator that compares the closing price
of an asset to its price range over a specified period. It is typically used with a 14-day
period.

# Calculate the stochastic oscillator


data["stochastic_k"], data["stochastic_d"] = talib.STOCH(data["High"], data["Lo

f. Fibonacci Retracements
Fibonacci retracements are used to identify potential levels of support and
resistance by measuring the size of a price move and then dividing it by the key
Fibonacci ratios of 23.6%, 38.2%, 50%, 61.8%, and 100%.

# Calculate the Fibonacci retracements


data["retracement_23.6"], data["retracement_38.2"], data["retracement_50.0"], d

It’s important to note that using these indicators alone may not guarantee a
profitable trade. Technical analysis should be used in conjunction with fundamental
analysis and a well-defined trading strategy. Additionally, the effectiveness of these
indicators may vary depending on the asset and market conditions.

These are some of the majorly used Indicators for understanding and analyzing
Stock Performance. Stay tuned for the next parts where we will explore some more
indicators. I will also be explaining How to create a trading strategy using these
indicators in the upcoming articles.

Go ahead and try this with different stock data and analyze these indicators. In case
you find any difficulty please let me know in the response section.

This article is in collaboration with Piyush Ingale

Before You Go
Thanks for reading! If you want to get in touch with me, feel free to reach me at
[email protected] or my LinkedIn Profile. You can view my Github profile for different

https://medium.com/mlearning-ai/exploring-the-best-indicators-in-ta-lib-technical-analysis-of-stocks-using-python-part-1-b7ad731aeeb2 5/14
19/09/2023, 21:31 Exploring the Best Indicators in TA-Lib: Technical Analysis of Stocks using Python- Part 1 | by Himanshu Sharma | MLearning.ai | Medium

data science projects and packages tutorials. Also, feel free to explore my profile and read
different articles I have written related to Data Science.

Mlearning.ai Submission Suggestions


How to become a writer on Mlearning.ai
medium.com

Data Science Stock Market Machine Learning Deep Learning Ml So Good

Follow

Written by Himanshu Sharma


1.93K Followers · Writer for MLearning.ai

I write about my learnings in the field of Data Science, Visualization, Artificial Intelligence, etc.| Linkedin:
https://www.linkedin.com/in/himanshusharmads/

More from Himanshu Sharma and MLearning.ai

https://medium.com/mlearning-ai/exploring-the-best-indicators-in-ta-lib-technical-analysis-of-stocks-using-python-part-1-b7ad731aeeb2 6/14
19/09/2023, 21:31 Exploring the Best Indicators in TA-Lib: Technical Analysis of Stocks using Python- Part 1 | by Himanshu Sharma | MLearning.ai | Medium

Himanshu Sharma in DataDrivenInvestor

From Data to Destiny: A Guide to Time Series Forecasting using


FinancialModelingPrep
Predicting the Future with Historical Data and Cutting-Edge API Insights

6 min read · Aug 20

Maximilian Vogel in MLearning.ai

https://medium.com/mlearning-ai/exploring-the-best-indicators-in-ta-lib-technical-analysis-of-stocks-using-python-part-1-b7ad731aeeb2 7/14
19/09/2023, 21:31 Exploring the Best Indicators in TA-Lib: Technical Analysis of Stocks using Python- Part 1 | by Himanshu Sharma | MLearning.ai | Medium

I Scanned 1000+ Prompts so You Don’t Have to: 10 Need-to-Know


Techniques
Free Prompt Engineering Course: The Art of the Prompt — Updated Sep-18 2023.

10 min read · Aug 11

1K 16

FS Ndzomga in MLearning.ai

LangChain Is The Past, Here Is The Future Of LLM-based Apps


I recently came across EmbedChain, a framework for building chatbots using LLMs that can
interact with various types of data.

· 4 min read · Aug 29

155 9

https://medium.com/mlearning-ai/exploring-the-best-indicators-in-ta-lib-technical-analysis-of-stocks-using-python-part-1-b7ad731aeeb2 8/14
19/09/2023, 21:31 Exploring the Best Indicators in TA-Lib: Technical Analysis of Stocks using Python- Part 1 | by Himanshu Sharma | MLearning.ai | Medium

Himanshu Sharma in MLearning.ai

Automate Your Tasks with PyAutoGUI


A Comprehensive Guide to Python Automation with PyAutoGUI

· 3 min read · Mar 8

94

See all from Himanshu Sharma

See all from MLearning.ai

Recommended from Medium

https://medium.com/mlearning-ai/exploring-the-best-indicators-in-ta-lib-technical-analysis-of-stocks-using-python-part-1-b7ad731aeeb2 9/14
19/09/2023, 21:31 Exploring the Best Indicators in TA-Lib: Technical Analysis of Stocks using Python- Part 1 | by Himanshu Sharma | MLearning.ai | Medium

TradeDots

90% SUCCESS RATE?! Debunk “Too Good To Be True” Chart Patterns with
TradeDots
Trading is a complex subject that can be difficult to fully comprehend. It goes against our
natural inclination for simplicity, as we often…

7 min read · Jul 20

63 1

Venujkvenk

https://medium.com/mlearning-ai/exploring-the-best-indicators-in-ta-lib-technical-analysis-of-stocks-using-python-part-1-b7ad731aeeb2 10/14
19/09/2023, 21:31 Exploring the Best Indicators in TA-Lib: Technical Analysis of Stocks using Python- Part 1 | by Himanshu Sharma | MLearning.ai | Medium

Exploring Time Series Data: Unveiling Trends, Seasonality, and Residuals


Exploratory Data Analysis (EDA) is crucial for time series data because it serves as the
foundation for understanding, modeling

8 min read · Sep 2

Lists

Predictive Modeling w/ Python


20 stories · 395 saves

Practical Guides to Machine Learning


10 stories · 447 saves

Natural Language Processing


620 stories · 229 saves

New_Reading_List
174 stories · 108 saves

Prajjwal Chauhan

Stock Prediction and Forecasting Using LSTM(Long-Short-Term-


Memory)
https://medium.com/mlearning-ai/exploring-the-best-indicators-in-ta-lib-technical-analysis-of-stocks-using-python-part-1-b7ad731aeeb2 11/14
19/09/2023, 21:31 Exploring the Best Indicators in TA-Lib: Technical Analysis of Stocks using Python- Part 1 | by Himanshu Sharma | MLearning.ai | Medium

In an ever-evolving world of finance, accurately predicting stock market movements has long
been an elusive goal for investors and traders…

6 min read · Jul 8

200 8

martin vizzolini in Coinmonks

How to Beat the Stock Market with Maths: A Dual Strategy Approach
Is it possible to discover a foolproof winning strategy? What if we could simultaneously bet on
red and black in a casino roulette while…

11 min read · Jul 27

488 3

https://medium.com/mlearning-ai/exploring-the-best-indicators-in-ta-lib-technical-analysis-of-stocks-using-python-part-1-b7ad731aeeb2 12/14
19/09/2023, 21:31 Exploring the Best Indicators in TA-Lib: Technical Analysis of Stocks using Python- Part 1 | by Himanshu Sharma | MLearning.ai | Medium

Miryam Chen

Predicting Stock Prices with Machine Learning


In the world of finance, predicting stock prices has always been a challenge that captures the
imagination of investors, researchers, and…

8 min read · Aug 16

90 4

Cris Velasquez, MSc.

https://medium.com/mlearning-ai/exploring-the-best-indicators-in-ta-lib-technical-analysis-of-stocks-using-python-part-1-b7ad731aeeb2 13/14
19/09/2023, 21:31 Exploring the Best Indicators in TA-Lib: Technical Analysis of Stocks using Python- Part 1 | by Himanshu Sharma | MLearning.ai | Medium

Future Stock Price Movements with Historical & Implied Volatility using
Python and Monte Carlo
1. Introduction

· 10 min read · Aug 31

191

See more recommendations

https://medium.com/mlearning-ai/exploring-the-best-indicators-in-ta-lib-technical-analysis-of-stocks-using-python-part-1-b7ad731aeeb2 14/14

You might also like