This is the code repository for Python Algorithmic Trading Cookbook, published by Packt.
All the recipes you need to implement your own algorithmic trading strategies in Python
Python is a very popular language used to build and execute algorithmic trading strategies. If you want to find out how you can build a solid foundation in algorithmic trading using the language, 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 through this algorithmic trading book, 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 discover how to place various types of orders, such as regular, bracket, and cover orders, and understand their state transitions. You’ll also uncover challenges faced while devising and executing powerful algorithmic trading strategies from scratch. Later chapters will take you through backtesting, paper trading, and finally real trading for the algorithmic strategies that you've created from the ground up. 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 for algorithmic trading by implementing Python libraries to conduct key tasks in the algorithmic trading ecosystem.
In this repo, you will find the code examples used in the book. I also include here parts of the code omitted in the book, such as the data visualization styling, additional formatting, etc.
This book covers the following exciting features:
- Use Python to set up connectivity with brokers
- Handle and manipulate time series data using Python
- Fetch a list of exchanges, segments, financial instruments, and historical data to interact with the real market
- Understand, fetch, and calculate various types of candles and use them to compute and plot diverse types of technical indicators
- Develop and improve the performance of algorithmic trading strategies
- Perform backtesting and paper trading on algorithmic trading strategies
- Implement real trading in the live hours of stock markets
If you feel this book is for you, get your copy today!
All of the code is organized into folders.
The code will look like the following:
>>> plot_candlestick_chart(historical_data,
PlotType.OHLC,
'Historical Data | '
'Japanese Candlesticks Pattern | '
'NSE:TATASTEEL | 1st Jan, 2020 | '
'Candle Interval: 1 Minute')
Following is what you need for this book:
If you are a financial analyst, financial trader, data analyst, algorithmic trader, trading enthusiast or anyone who wants to learn algorithmic trading with Python and important techniques to address challenges faced in the finance domain, this book is for you. Basic working knowledge of the Python programming language is expected. Although fundamental knowledge of trade-related terminologies will be helpful, it is not mandatory.
You need to have the latest version of Python 3 installed on your computer. The recipes of this Chapter were tested on Python 3.8.2. 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 for most of the chapters. Appendix I gives detailed step-by-step information to set up your Zerodha account in case you do not have it.
For executing trading strategies, you also need an account with AlgoBulls. Appendix II gives detailed step-by-step information to set up your AlgoBulls account in case you do not have it.
Also, almost every chapter expects you to have additional Python packages installed like ‘pyalgotrading'. You can install them using pip
. This is explained in the technical requirements section of every chapter. All the recipes in this chapter are provided as Jupyter Notebooks ih this GitHub repository. 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
.
- Summary of requirements for each Chapter:
Chapters | Python 3.7+ | Zerodha Account | AlgoBulls Account |
---|---|---|---|
Chapter 1 | ✓ | ✗ | ✗ |
Chapter 2 | ✓ | ✓ | ✗ |
Chapter 3 | ✓ | ✓ | ✗ |
Chapter 4 | ✓ | ✓ | ✗ |
Chapter 5 | ✓ | ✓ | ✗ |
Chapter 6 | ✓ | ✓ | ✗ |
Chapter 7 | ✓ | ✓ | ✗ |
Chapter 8 | ✓ | ✗ | ✓ |
Chapter 9 | ✓ | ✗ | ✓ |
Chapter 10 | ✓ | ✗ | ✓ |
Chapter 11 | ✓ | ✗ | ✓ |
- For each chapter, the additional Python packages required to run the recipes are provided in a
requirements.txt
file in the chapter specific folder. You can easily install the additional dependencies usingpip
:
# For example, to install dependencies needed for Chapter 5:
$ source <virtualenv> # optional, if you use a virtualenv
$ cd <path-to-this-repo>/Chapter05
$ pip install -r requirements.txt
-
Page 297 (Chapter 7, Recipe 5, Step 5): TRIGGER PENDING should be COMPLETE
-
Page 379 (Chapter 9, Recipe 8, Step 2): In the code instrument should be instruments
-
Page 379 (Chapter 9, Recipe 8, Step 3): instrument = instrument[0]['value'] should be instrument = instruments[0]['value']
-
Page 423 (Chapter 10, Recipe 7, Step 6): 49287246f9704bbcbad76ade9e2091d9 should be 4faf514fe096432b8e9f80f5951bd2ea
-
Page 427 (Chapters 10, Recipe 8, Step 2): In the code instrument should be instruments
-
Page 427 (Chapters 10, Recipe 8, Step 3): instrument = instrument[0]['value'] should be instrument = instruments[0]['value']
-
Page 471 (Chapter 11, Recipe 7, Step 2): In the code instrument should be instruments
-
Page 471 (Chapter 11, Recipe 7, Step 3): instrument = instrument[0]['value'] should be instrument = instruments[0]['value']
-
Page 467 (Chapter 11, Recipe 6, Step 6): 49287246f9704bbcbad76ade9e2091d9 should be 4faf514fe096432b8e9f80f5951bd2ea
-
Chapter 5: All instances of plot_candlesticks_chart in the code format (
plot_candlesticks_chart
) should be plot_candlestick_chart
With the following software and hardware list you can run all code files present in the book (Chapters 1-11).
Chapter | Software required | OS required |
---|---|---|
1 - 11 | Python 3.7+, Jupyter Notebook | Windows, Mac OS X, and Linux (Any) |
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. Click here to download it.
Pushpak Dagade has been working in the area of algorithmic trading for more than 3 years. He is a co-founder and the CEO of AlgoBulls, an algorithmic trading platform. He is also a long time Pythonista with more than a decade of Python experience. He is a pass out from Indian Institute of Technology (Delhi) and holds engineering degrees in the fields of Computer Science, Electronics and Physics.
If you have any doubts related to the concepts or code samples in the book, you can ask it in the AlgoBulls forum created by the author.
Click here if you have any feedback or suggestions.
If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.