Book Image

Python Algorithmic Trading Cookbook

By : Pushpak Dagade
Book Image

Python Algorithmic Trading Cookbook

By: Pushpak Dagade

Overview of this book

If you want to find out how you can build a solid foundation in algorithmic trading using Python, this cookbook is here to help. Starting by setting up the Python environment for trading and connectivity with brokers, you’ll then learn the important aspects of financial markets. As you progress, you’ll learn to fetch financial instruments, query and calculate various types of candles and historical data, and finally, compute and plot technical indicators. Next, you’ll learn how to place various types of orders, such as regular, bracket, and cover orders, and understand their state transitions. Later chapters will cover backtesting, paper trading, and finally real trading for the algorithmic strategies that you've created. You’ll even understand how to automate trading and find the right strategy for making effective decisions that would otherwise be impossible for human traders. By the end of this book, you’ll be able to use Python libraries to conduct key tasks in the algorithmic trading ecosystem. Note: For demonstration, we're using Zerodha, an Indian Stock Market broker. If you're not an Indian resident, you won't be able to use Zerodha and therefore will not be able to test the examples directly. However, you can take inspiration from the book and apply the concepts across your preferred stock market broker of choice.
Table of Contents (16 chapters)

To get the most out of this book

This book is for anyone who is interested in the field of algorithmic trading. You are not expected to have any background in finance or algorithmic trading. You are expected to have a basic knowledge of the Python programming language. Each chapter introduces a new concept in algorithmic trading and takes you step by step, from zero to hero. This book can help you build a rock-solid foundation in algorithmic trading using Python.

You need to have the latest version of Python 3 installed on your computer. The recipes in this book were tested on Python 3.8.2 and they should work on any future release of Python as well.

You also need a broking account with Zerodha, a modern broker, to try out the recipes covered in most of the chapters. Appendix I provides detailed, step-by-step information on how to set up your Zerodha account in case you do not have one.

To execute trading strategies, you also need an account with AlgoBulls. Appendix II provides detailed, step-by-step information on how to set up your AlgoBulls account in case you do not have one.

Also, almost every chapter expects you to have additional Python packages installed, such as pyalgotrading. You can install these using pip. This is explained in the technical requirements section of every chapter.

All the recipes in this chapter are provided as Jupyter notebooks on our GitHub repository: https://github.com/PacktPublishing/Python-Algorithmic-Trading-Cookbook. You can install Jupyter Notebook as well if you would like to try out the recipes directly without typing any code. You can install this using pip: pip install notebook.

Software/hardware covered in the book

OS requirements

Python 3.7+ (https://www.python.org/downloads/)

Any OS that supports Python 3.7+; Linux, Windows, macOS X, and so on.

The requirements for each chapter are summarized in the following table:

Chapter number

Zerodha account

AlgoBulls account

1

No No

2

Yes No

3

Yes No

4

Yes No

5

Yes No

6

Yes No

7

Yes No

8

No Yes

9

No Yes

10

No Yes

11

No Yes

If you are using the digital version of this book, we advise you to type the code yourself or access the code via the GitHub repository (link available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

Download the example code files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packt.com.
  2. Select the Support tab.
  3. Click on Code Downloads.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Python-Algorithmic-Trading-Cookbook. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://static.packt-cdn.com/downloads/9781838989354_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "The broker provides unique keys to each customer, typically as api-key and api-secret key pairs."

A block of code is set as follows:

>>> plot_candlestick_chart(historical_data, 
PlotType.OHLC,
'Historical Data | '
'Japanese Candlesticks Pattern | '
'NSE:TATASTEEL | 1st Jan, 2020 | '
'Candle Interval: 1 Minute')

Any command-line input or output is written as follows:

$ pip install pyalgotrading

Bold: Indicates a new term, an important word, or words that you see on screen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "A Japanese candle is green in color if its close price is above its open price."

Warnings or important notes appear like this.
Tips and tricks appear like this.