Build Technical Indicators in Python - Matlab, R project and Python | futures trading

Go Back

> Futures Trading, News, Charts and Platforms > Platforms and Indicators > Matlab, R project and Python

Build Technical Indicators in Python
Started:June 16th, 2016 (07:12 AM) by algoisgame Views / Replies:361 / 2
Last Reply:July 8th, 2016 (09:18 AM) Attachments:0

Welcome to

Welcome, Guest!

This forum was established to help traders (especially futures traders) by openly sharing indicators, strategies, methods, trading journals and discussing the psychology of trading.

We are fundamentally different than most other trading forums:
  • We work extremely hard to keep things positive on our forums.
  • We do not tolerate rude behavior, trolling, or vendor advertising in posts.
  • We firmly believe in openness and encourage sharing. The holy grail is within you, it is not something tangible you can download.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community. It's free and simple, and we will never resell your private information.

-- Big Mike

Thread Tools Search this Thread

Build Technical Indicators in Python

Old June 16th, 2016, 07:12 AM   #1 (permalink)
Trading Apprentice
mumbai india
Futures Experience: None
Platform: ninjaTrader
Favorite Futures: currency futures
Posts: 1 since Jun 2016
Thanks: 1 given, 1 received

Build Technical Indicators in Python

Technical Indicator is essentially a mathematical representation based on data sets such as price (high, low, open, close, etc.) or volume of a security to forecast price trends. There are several kinds of technical indicators that are used to analyse and detect the direction of movement of the price. Traders use them to study the short-term price movement, since they do not prove very useful for long-term investors. They are employed to primarily to predict the future price levels.

Technical Indicators do not follow a general pattern, meaning, they behave differently with every security. What can be a good indicator for a particular security, might not hold the case for the other. Thus, using a technical indicator requires jurisprudence coupled with good experience.

In the following post, I will highlight a technical indicators that are popularly used in the markets to study the price movement.


As these analyses can be done in python, a snippet of code is also inserted along with the description of the indicators. Sample charts with examples are also appended for clarity.

Commodity Channel Index (CCI)

The commodity channel index (CCI) is an oscillator which was originally introduced by Donald Lambert in 1980. CCI can be used to identify cyclical turns across asset classes, be it commodities, indices, stocks, or ETFs. CCI is also used by traders to identify overbought/oversold levels for securities.


The CCI looks at the relationship between price and a moving average. Steps involved in the estimation of CCI include:

Computing the typical price for the security. Typical price is obtained by the averaging the high, low and the close price for the day.
Finding the moving average for the chosen number of days based on the typical price.
Computing the standard deviation for the same period as that used for the MA.
The formula for CCI is given by:

CCI = (Typical price  MA of Typical price) / (0.015 * Standard deviation of Typical price)
The index is scaled by an inverse factor of 0.015 to provide for more readable numbers.
The index is scaled by an inverse factor of 0.015 to provide for more readable numbers.


CCI can be used to determine overbought and oversold levels. Readings above +100 can imply an overbought condition, while readings below −100 can imply an oversold condition. However, one should be a careful because a security can continue moving higher after the CCI indicator becomes overbought. Likewise, securities can continue moving lower after the indicator becomes oversold.

Whenever the security is in overbought/oversold levels as indicated by the CCI, there is a good chance that the price will see corrections. Hence a trader can use such overbought/oversold levels to enter in short/long positions.

Traders can also look for divergence signals to take suitable positions using CCI. A bullish divergence occurs when the underlying security makes a lower low and the CCI forms a higher low, which shows less downside momentum. Similarly, a bearish divergence is formed when the security records a higher high and the CCI forms a lower high, which shows less upside momentum.

Python code for computing the Commodity Channel Index

In the code below we use the Series, rolling_mean, rolling_std, and the join functions to compute the Commodity Channel Index. The Series function is used to form a series which is a one dimensional array-like object containing an array of data. The rolling_mean function takes a time series or a data frame along with the number of periods and computes the mean. The rolling_std function computes the standard deviation based on the price provided. The join function joins a given series with a specified series/dataframe.

# Commodity Channel Index Python Code

# Load the necessary packages and modules
import pandas as pd
import as web
import matplotlib.pyplot as plt

# Commodity Channel Index 
def CCI(data, ndays): 
 TP = (data['High'] + data['Low'] + data['Close']) / 3 
 CCI = pd.Series((TP - pd.rolling_mean(TP, ndays)) / (0.015 * pd.rolling_std(TP, ndays)),
 name = 'CCI') 
 data = data.join(CCI) 
 return data

# Retrieve the Nifty data from Yahoo finance:
data = web.DataReader('^NSEI',data_source='yahoo',start='1/1/2014', end='1/1/2016')
data = pd.DataFrame(data)

# Compute the Commodity Channel Index(CCI) for NIFTY based on the 20-day Moving average
n = 20
NIFTY_CCI = CCI(data, n)

# Plotting the Price Series chart and the Commodity Channel index below
fig = plt.figure(figsize=(7,5))
ax = fig.add_subplot(2, 1, 1)
plt.title('NSE Price Chart')
plt.ylabel('Close Price')
bx = fig.add_subplot(2, 1, 2)
plt.ylabel('CCI values')
plt.setp(plt.gca().get_xticklabels(), rotation=30)

Reply With Quote
The following 2 users say Thank You to algoisgame for this post:

Old June 16th, 2016, 07:12 AM   #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.


Old July 8th, 2016, 09:18 AM   #3 (permalink)
Trading Apprentice
Cookeville, TN
Futures Experience: Advanced
Platform: R|Trader, Thinkorswim
Favorite Futures: CL, RB, 6E, ZB, DX
Posts: 20 since Oct 2011
Thanks: 3 given, 7 received

Quantiacs had a good post today about constructing Heiken-Ashi candles in Python, using the numpy library, see here:

Reply With Quote

Reply > Futures Trading, News, Charts and Platforms > Platforms and Indicators > Matlab, R project and Python > Build Technical Indicators in Python

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Upcoming Webinars and Events (4:30PM ET unless noted)

An Afternoon with FIO trader bobwest

Elite only

NinjaTrader 8: Programming Profitable Trading Edges w/Scott Hodson

Elite only

Anthony Drager: Executing on Intermarket Correlations & Order Flow, Part 2

Elite only

Adam Grimes: Five critically important keys to professional trading

Elite only

Machine Learning Concepts w/FIO member NJAMC

Elite only

MarketDelta Cloud Platform: Announcing new mobile features

Dec 1

NinjaTrader 8: Features and Enhancements

Dec 6

Similar Threads
Thread Thread Starter Forum Replies Last Post
Python and QuickFIX Branzol Matlab, R project and Python 33 September 20th, 2016 06:07 PM
IB with Python jamesico Matlab, R project and Python 2 September 9th, 2014 07:19 AM
Tradier with R and or Python Keith Leesus Matlab, R project and Python 2 September 5th, 2014 12:23 AM
Python Help Wanted $ wlblount Hire a Consultant or Programmer 0 December 25th, 2013 09:14 AM
Technical Analysis Library with 200 indicators sigjanu Platforms and Indicators 1 March 17th, 2011 11:53 AM

All times are GMT -4. The time now is 05:34 PM.

Copyright © 2016 by All information is for educational use only and is not investment advice.
There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
no new posts

Page generated 2016-10-22 in 0.08 seconds with 19 queries on phoenix via your IP