Reader small image

You're reading from  Python Algorithmic Trading Cookbook

Product typeBook
Published inAug 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781838989354
Edition1st Edition
Languages
Right arrow
Author (1)
Pushpak Dagade
Pushpak Dagade
author image
Pushpak Dagade

Pushpak Dagade is working in the area of algorithmic trading with Python for more than 3 years. He is a co-founder and CEO of AlgoBulls, an algorithmic trading platform.
Read more about Pushpak Dagade

Right arrow
Computing Candlesticks and Historical Data

The historical data of a financial instrument is data about all the past prices at which a financial instrument was brought or sold. An algorithmic trading strategy is always vpot_candlestickirtually executed on historical data to evaluate its past performance before it's deployed with real money. This process is called backtesting. Historical data is quintessential for backtesting (covered in detail in Chapter 8, Backtesting Strategies). Also, historical data is needed for computing technical indicators (covered in detail in Chapter 5, Computing and Plotting Technical Indicators), which help in making buy-or-sell decisions in real-time. Candlestick patterns are widely used tools for stock analysis. Various types of candlestick patterns are commonly used by analysts. This chapter provides recipes that show you how to fetch historical...

Technical requirements

You will need the following to successfully execute the recipes in this chapter:

  • Python 3.7+
  • Python packages:
  • pyalgotrading ($ pip install pyalgotrading)
  • quandl ($pip install quandl) this is optional and only needed for the last recipe

The latest Jupyter Notebook for this chapter can be found on GitHub at https://github.com/PacktPublishing/Python-Algorithmic-Trading-Cookbook/tree/master/Chapter04.

The following code will help you set up the broker connection with Zerodha, which will be used by all the recipes in this chapter. Please make sure you have followed these steps before trying out any of the recipes provided.

The first thing you need to do to set connectivity with the broker is to gather the required API keys. The broker provides unique keys to each customer, typically as api-key and api-secret key pairs. These API keys are chargeable, usually on a monthly subscription basis. You need to get your copies of api-key and api-secret from the broker website...

Fetching historical data using the broker API

The historical data of a financial instrument is time-series data for the timestamps in the past. It can be fetched using the Broker API for a given duration. This recipe demonstrates how to set up a broker connection and how to fetch historical data for a financial instrument for the duration of a single day.

Getting ready

Make sure the broker_connection object is available in your Python namespace. Refer to the Technical requirements section of this chapter to learn how to set it up.

How to do it…

Execute the following steps to complete this recipe:

  1. Fetch the historical data for an instrument:
>>> instrument = broker_connection.get_instrument('NSE', 
'TATASTEEL')
>>> historical_data = broker_connection.get_historical_data(
instrument=instrument,
candle_interval='minute',
...

Fetching historical data using the Japanese (OHLC) candlestick pattern

The historical data of a financial instrument is an array of candlesticks. Each entry in the historical data is a single candlestick. There are various types of candlestick patterns.

This recipe demonstrates the most commonly used candlestick pattern the Japanese candlestick pattern. It is a type of candlestick pattern where each candlestick holds a duration and indicates all the prices the instrument would have taken on during that duration. This data is represented using four parameters Open, High, Low, and Close. These can be described as follows:

  • Open: The price of the financial instrument at the beginning of the candle's duration
  • High: The highest recorded price of the financial instrument during the entire duration of the candle
  • Low: The lowest recorded price of the financial instrument during the entire duration of the candle
  • Close: The price of the financial instrument at the end of...

Fetching the Japanese candlestick pattern with variations in candle intervals

The historical data of a financial instrument can be analyzed in the form of Japanese candlesticks pattern with varying candle intervals. Brokers typically support candle intervals of 1 minute, 3 minutes, 5 minutes, 10 minutes, 15 minutes, 30 minutes, 1 hour, 1 day, and so on. A shorter candle interval hints at a localized price movement trend, while a larger candle interval indicates an overall price movement trend. Depending on the algorithmic trading strategy, you may need a shorter candle interval or a larger one. A candle interval of 1 minute is often the smallest available candle interval. This recipe demonstrates the historical data of a financial instrument for a duration of a day in various candle intervals.

Getting ready

Make sure the broker_connection object is available in your Python namespace. Refer to the Technical requirements section of this chapter to learn how to set up broker_connection.

...

Fetching historical data using the Line Break candlestick pattern

The historical data of a financial instrument can be analyzed in the form of the Line Break candlestick pattern, a candlestick pattern that focuses on price movement. This differs from the Japanese candlestick pattern, which focuses on the time movement. Brokers typically do not provide historical data for the Line Break candlestick pattern via APIs. Brokers usually provide historical data using the Japanese candlestick pattern that needs to be converted into the Line Break candlestick pattern. A shorter candle interval hints at a localized price movement trend, while a larger candle interval indicates an overall price movement trend. Depending on your algorithmic trading strategy, you may need the candle interval to be small or large. A candle interval of 1 minute is often the smallest available candle interval.

The Line Break candlestick pattern works as follows:

  1. Each candle has only open and close attributes.
  2. The user...

Fetching historical data using the Renko candlestick pattern

The historical data of a financial instrument can be analyzed in the form of the Renko candlestick pattern, a candlestick pattern that focuses on price movement. This differs from the Japanese candlestick pattern, which focuses on time movement. Brokers typically do not provide historical data as the Renko candlestick pattern via APIs. Brokers usually provide historical data by using the Japanese candlestick pattern, which needs to be converted into the Renko candlestick pattern. A shorter candle interval hints at a localized price movement trend, while a larger candle interval indicates an overall price movement trend. Depending on your algorithmic trading strategy, you may need the candle interval to be small or large. A candle interval of 1 minute is often the smallest available candle interval.

The Renko candlestick pattern works as follows:

  1. Each candle only has open and close attributes.
  2. You define a Brick Count (b) setting...

Fetching historical data using the Heikin-Ashi candlestick pattern

The historical data of a financial instrument can be analyzed in the form of the Heikin-Ashi candlestick pattern. Brokers typically do not provide historical data using the Heikin-Ashi candlestick pattern via APIs. Brokers usually provide historical data using the Japanese candlestick pattern, which needs to be converted to the Heikin-Ashi candlestick pattern. A shorter candle interval hints at a localized price movement trend, while a larger candle interval indicates an overall price movement trend. Based on your algorithmic trading strategy, you may need the candle interval to be small or large. A candle interval of 1 minute is often the smallest available candle interval.

The Heikin-Ashi candlestick pattern works as follows:

  • Each candle has Close, Open, High, and Low attributes. For each candle, the following occurs:
  • Close is calculated as the average of the Open, High, Low, and Close attributes of the current Japanese...

Fetching historical data using Quandl

So far, in all the recipes in this chapter, you have used the broker connection to fetch historical data. In this recipe, you will fetch historical data using a third-party tool, Quandl (https://www.quandl.com/tools/python). It has a free to use Python version which can be easily installed using pip. This recipe demonstrates the use of quandl to fetch historical data of FAAMG stock prices (Facebook, Amazon, Apple, Microsoft, and Google).

Getting ready

Make sure you have installed the Python quandl package. If you haven't, you can install it using the following pip command:

$ pip install quandl

How to do it…

We execute the following steps for this recipe:

  1. Import the necessary modules:
>>> from pyalgotrading.utils.func import plot_candlestick_chart, PlotType
>>> import quandl
  1. Plot a chart for the historical data of Facebook with a 1-day candle interval:
>>> facebook = quandl.get('WIKI/FB', 
...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Python Algorithmic Trading Cookbook
Published in: Aug 2020Publisher: PacktISBN-13: 9781838989354
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Pushpak Dagade

Pushpak Dagade is working in the area of algorithmic trading with Python for more than 3 years. He is a co-founder and CEO of AlgoBulls, an algorithmic trading platform.
Read more about Pushpak Dagade