Python Algorithmic Trading Cookbook

Book description

Build a solid foundation in algorithmic trading by developing, testing and executing powerful trading strategies with real market data using Python

Key Features

  • Build a strong foundation in algorithmic trading by becoming well-versed with the basics of financial markets
  • Demystify jargon related to understanding and placing multiple types of trading orders
  • Devise trading strategies and increase your odds of making a profit without human intervention

Book Description

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.

What you will learn

  • 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

Who this book is for

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.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Python Algorithmic Trading Cookbook
  3. About Packt
    1. Why subscribe?
  4. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Sections
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Get in touch
      1. Reviews
  6. Handling and Manipulating Date, Time, and Time Series Data
    1. Technical requirements
    2. Creating datetime objects
      1. How to do it…
      2. How it works...
      3. There's more
    3. Creating timedelta objects
      1. How to do it…
      2. How it works...
      3. There's more
    4. Operations on datetime objects
      1. How to do it…
      2. How it works…
      3. There's more
    5. Modifying datetime objects
      1. How to do it…
      2. How it works...
    6. Converting a datetime object to a string
      1. How to do it…
      2. How it works...
    7. Creating a datetime object from a string
      1. How to do it…
      2. How it works...
      3. There's more
    8. The datetime object and time zones
      1. How to do it…
      2. How it works...
      3. There's more
    9. Creating a pandas.DataFrame object
      1. How to do it...
      2. How it works...
      3. There's more
    10. DataFrame manipulation—renaming, rearranging, reversing, and slicing
      1. Getting ready
      2. How to do it…
      3. How it works...
      4. There's more
    11. DataFrame manipulation—applying, sorting, iterating, and concatenating
      1. Getting ready
      2. How to do it…
      3. How it works...
      4. There's more
    12. Converting a DataFrame into other formats
      1. Getting ready
      2. How to do it…
      3. How it works...
    13. Creating a DataFrame from other formats
      1. Getting ready
      2. How to do it…
      3. How it works...
  7. Stock Markets - Primer on Trading
    1. Technical requirements
    2. Setting up Python connectivity with the broker
      1. How to do it…
      2. How it works...
    3. Querying a list of instruments
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Fetching an instrument
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Querying a list of exchanges
      1. Getting ready
      2. How to do it…
      3. How it works…
    6. Querying a list of segments
      1. Getting ready
      2. How to do it…
      3. How it works…
    7. Knowing other attributes supported by the broker
      1. How to do it…
      2. How it works…
    8. Placing a simple REGULAR order
      1. Getting ready
      2. How to do it…
      3. How it works…
    9. Placing a simple BRACKET order
      1. Getting ready
      2. How to do it…
      3. How it works…
    10. Placing a simple DELIVERY order
      1. Getting ready
      2. How to do it…
      3. How it works…
    11. Placing a simple INTRADAY order
      1. Getting ready
      2. How to do it…
      3. How it works…
    12. Querying margins and funds
      1. Getting ready
      2. How to do it…
      3. How it works…
    13. Calculating the brokerage charged
      1. How to do it…
      2. How it works…
    14. Calculating the government taxes charged
      1. How to do it…
      2. How it works…
  8. Fetching Financial Data
    1. Technical requirements
    2. Fetching the list of financial instruments
      1. Getting ready
      2. How to do it…
      3. How it works…
    3. Attributes of a financial instrument
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Expiry of financial instruments
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Circuit limits of a financial instrument
      1. Getting ready
      2. How to do it…
      3. How it works…
    6. The market depth of a financial instrument
      1. Getting ready
      2. How to do it…
      3. How it works…
    7. The total pending buy quantity of a financial instrument
      1. Getting ready
      2. How to do it…
      3. How it works…
    8. The total pending sell quantity of a financial instrument
      1. Getting ready
      2. How to do it…
      3. How it works…
    9. The total volume traded for the day of a financial instrument
      1. Getting ready
      2. How to do it…
      3. How it works…
    10. The last traded price of a financial instrument
      1. Getting ready
      2. How to do it…
      3. How it works…
    11. The last traded time of a financial instrument
      1. Getting ready
      2. How to do it…
      3. How it works…
    12. The last traded quantity of a financial instrument
      1. Getting ready
      2. How to do it…
      3. How it works…
    13. The recorded open price of the day of a financial instrument
      1. Getting ready
      2. How to do it…
      3. How it works…
    14. The recorded highest price of the day of a financial instrument
      1. Getting ready
      2. How to do it…
      3. How it works…
    15. The recorded lowest price of the day of a financial instrument
      1. Getting ready
      2. How to do it…
      3. How it works…
    16. The recorded close price of the last traded day of a financial instrument
      1. Getting ready
      2. How to do it…
      3. How it works…
  9. Computing Candlesticks and Historical Data
    1. Technical requirements
    2. Fetching historical data using the broker API
      1. Getting ready
      2. How to do it…
      3. How it works…
    3. Fetching historical data using the Japanese (OHLC) candlestick pattern
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Fetching the Japanese candlestick pattern with variations in candle intervals
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Fetching historical data using the Line Break candlestick pattern
      1. Getting ready
      2. How to do it…
      3. How it works…
    6. Fetching historical data using the Renko candlestick pattern
      1. Getting ready
      2. How to do it…
      3. How it works…
    7. Fetching historical data using the Heikin-Ashi candlestick pattern
      1. Getting ready
      2. How to do it…
      3. How it works…
    8. Fetching historical data using Quandl
      1. Getting ready
      2. How to do it…
      3. How it works…
  10. Computing and Plotting Technical Indicators
    1. Technical requirements
    2. Trend indicators – simple moving average
      1. Getting started
      2. How to do it…
      3. How it works...
    3. Trend indicators – exponential moving average
      1. Getting started
      2. How to do it…
      3. How it works…
    4. Trend indicators – moving average convergence divergence
      1. Getting started
      2. How to do it…
      3. How it works…
    5. Trend indicators – parabolic stop and reverse
      1. Getting started
      2. How to do it…
      3. How it works…
    6. Momentum indicators – relative strength index
      1. Getting started
      2. How to do it…
      3. How it works…
    7. Momentum indicators – stochastic oscillator
      1. Getting started
      2. How to do it…
      3. How it works…
    8. Volatility indicators – Bollinger Bands
      1. Getting started
      2. How to do it…
      3. How it works…
    9. Volatility indicators – average true range
      1. Getting started
      2. How to do it…
      3. How it works…
    10. Volume indicators – on balance volume
      1. Getting started
      2. How to do it…
      3. How it works…
    11. Volume indicators – volume-weighted average price
      1. Getting started
      2. How to do it…
      3. How it works…
  11. Placing Regular Orders on the Exchange
    1. Technical requirements
    2. Placing a regular market order
      1. Getting ready
      2. How to do it…
      3. How it works…
    3. Placing a regular limit order
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Placing a regular stoploss-limit order
      1. Getting ready...
      2. How to do it…
      3. How it works…
    5. Placing a regular stoploss-market order
      1. Getting ready
      2. How to do it…
      3. How it works…
  12. Placing Bracket and Cover Orders on the Exchange
    1. Technical requirements
    2. Placing a bracket limit order
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    3. Placing a bracket stoploss-limit order
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Placing a bracket limit order with trailing stoploss
      1. Getting ready
      2. How to do it…
      3. How it works...
    5. Placing a bracket stoploss-limit order with trailing stoploss
      1. Getting ready
      2. How to do it…
      3. How it works...
    6. Placing a cover market order
      1. Getting ready
      2. How to do it…
      3. How it works...
    7. Placing a cover limit order
      1. Getting ready
      2. How to do it…
      3. How it works...
  13. Algorithmic Trading Strategies - Coding Step by Step
    1. Technical requirements
    2. EMA-Regular-Order strategy – coding the __init__, initialize, name, and versions_supported methods
      1. Getting ready
      2. How to do it…
      3. How it works…
    3. EMA-Regular-Order strategy – coding the strategy_select_instruments_for_entry method
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. EMA-Regular-Order strategy – coding the strategy_enter_position method
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. EMA-Regular-Order strategy – coding the strategy_select_instruments_for_exit method
      1. Getting ready
      2. How to do it…
      3. How it works…
    6. EMA-Regular-Order strategy – coding the strategy_exit_position method
      1. Getting ready
      2. How to do it…
      3. How it works…
    7. EMA-Regular-Order strategy – uploading the strategy on the AlgoBulls trading platform
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    8. MACD-Bracket-Order strategy – coding the __init__, initialize, name, and versions_supported methods
      1. Getting ready
      2. How to do it…
      3. How it works…
    9. MACD-Bracket-Order strategy – coding the strategy_select_instruments_for_entry method
      1. Getting ready
      2. How to do it…
      3. How it works…
    10. MACD-Bracket-Order strategy – coding the strategy_enter_position method
      1. Getting ready
      2. How to do it…
      3. How it works…
    11. MACD-Bracket-Order strategy – coding the strategy_select_instruments_for_exit method
      1. Getting ready
      2. How to do it…
      3. How it works…
    12. MACD-Bracket-Order strategy – coding the strategy_exit_position method
      1. Getting ready
      2. How to do it…
      3. How it works…
    13. MACD-Bracket-Order strategy — uploading the strategy on the AlgoBulls trading platform
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
  14. Algorithmic Trading - Backtesting
    1. Technical requirements
    2. EMA-Regular-Order strategy – fetching the strategy
      1. How to do it…
      2. How it works…
    3. EMA-Regular-Order strategy – backtesting the strategy
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    4. EMA-Regular-Order strategy – fetching backtesting logs in real time
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    5. EMA-Regular-Order strategy – fetching a backtesting report – profit and loss table
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    6. EMA-Regular-Order strategy — fetching a backtesting report – statistics table
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    7. EMA-Regular-Order strategy – fetching a backtesting report – order history
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    8. MACD-Bracket-Order strategy – fetching the strategy
      1. How to do it…
      2. How it works…
    9. MACD-Bracket-Order strategy – backtesting the strategy
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    10. MACD-Bracket-Order strategy – fetching backtesting logs in real time
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    11. MACD-Bracket-Order strategy – fetching a backtesting report – profit and loss table
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    12. MACD-Bracket-Order strategy – fetching a backtesting report – statistics table
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    13. MACD-Bracket-Order strategy – fetching a backtesting report – order history
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
  15. Algorithmic Trading - Paper Trading
    1. Technical requirements
    2. EMA-Regular-Order strategy – fetching the strategy
      1. How to do it…
      2. How it works…
    3. EMA-Regular-Order strategy – paper trading the strategy
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    4. EMA-Regular-Order strategy – fetching paper trading logs in real time
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    5. EMA-Regular-Order strategy – fetching a paper trading report – profit and loss table
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    6. EMA-Regular-Order strategy – fetching a paper trading report – statistics table
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    7. EMA-Regular-Order strategy – fetching a paper trading report – order history
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    8. MACD-Bracket-Order strategy – fetching the strategy
      1. How to do it…
      2. How it works…
    9. MACD-Bracket-Order strategy – paper trading the strategy
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    10. MACD-Bracket-Order strategy – fetching paper trading logs in real time
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    11. MACD-Bracket-Order strategy – fetching a paper trading report – profit and loss table
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    12. MACD-Bracket-Order strategy – fetching a paper trading report – statistics table
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    13. MACD-Bracket-Order strategy – fetching a paper trading report – order history
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
  16. Algorithmic Trading - Real Trading
    1. Technical requirements
    2. EMA–Regular–Order strategy – fetching the strategy
      1. How to do it…
      2. How it works…
    3. EMA–Regular–Order strategy – real trading the strategy
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    4. EMA–Regular–Order strategy – fetching real trading logs in real time
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    5. EMA–Regular–Order strategy – fetching a real trading report – profit and loss table
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    6. EMA–Regular–Order strategy – fetching a real trading report – statistics table
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    7. MACD–Bracket–Order strategy – fetching the strategy
      1. How to do it…
      2. How it works…
    8. MACD–Bracket–Order strategy – real trading the strategy
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    9. MACD–Bracket–Order strategy – fetching real trading logs in real time
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    10. MACD–Bracket–Order strategy – fetching a real trading report – profit and loss table
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
    11. MACD–Bracket–Order strategy – fetching a real trading report – statistics table
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more...
  17. Appendix I
    1. Setting up your Zerodha account
      1. Opening a Zerodha account online
      2. Logging in to the Zerodha trading platform website
      3. Setting up your Zerodha Developer Options account
      4. Logging in to the Zerodha Developer Options website
      5. Purchasing and enabling the Zerodha Developer Options API
      6. Testing API keys and authorizing the app by firing your first API call
  18. Appendix II
    1. Setting up your AlgoBulls account
      1. Registering on the AlgoBulls platform
      2. Logging in to the AlgoBulls website
      3. Fetching your AlgoBulls Developer Options token
      4. Setting up your AlgoBulls account
  19. Appendix III
    1. Developing and improving strategies
      1. Strategy profitability is subject to seasons
      2. Strategy profitability is subject to its parameter values
      3. Backtesting alone does not ensure strategy profitability
      4. Broker limitations
      5. Staying connected with the community
      6. Be prepared for technology failures during actual trading
  20. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Python Algorithmic Trading Cookbook
  • Author(s): Pushpak Dagade
  • Release date: August 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781838989354