0%

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 in stock markets without human intervention

Book Description

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.

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
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Sections
    8. Getting ready
    9. How to do it…
    10. How it works…
    11. There's more…
    12. See also
    13. Get in touch
    14. Reviews
  6. Handling and Manipulating Date, Time, and Time Series Data
    1. Technical requirements
    2. Creating datetime objects
    3. How to do it…
    4. How it works...
    5. There's more
    6. Creating timedelta objects
    7. How to do it…
    8. How it works...
    9. There's more
    10. Operations on datetime objects
    11. How to do it…
    12. How it works…
    13. There's more
    14. Modifying datetime objects
    15. How to do it…
    16. How it works...
    17. Converting a datetime object to a string
    18. How to do it…
    19. How it works...
    20. Creating a datetime object from a string
    21. How to do it…
    22. How it works...
    23. There's more
    24. The datetime object and time zones
    25. How to do it…
    26. How it works...
    27. There's more
    28. Creating a pandas.DataFrame object
    29. How to do it...
    30. How it works...
    31. There's more
    32. DataFrame manipulation—renaming, rearranging, reversing, and slicing
    33. Getting ready
    34. How to do it…
    35. How it works...
    36. There's more
    37. DataFrame manipulation—applying, sorting, iterating, and concatenating
    38. Getting ready
    39. How to do it…
    40. How it works...
    41. There's more
    42. Converting a DataFrame into other formats
    43. Getting ready
    44. How to do it…
    45. How it works...
    46. Creating a DataFrame from other formats
    47. Getting ready
    48. How to do it…
    49. How it works...
  7. Stock Markets - Primer on Trading
    1. Technical requirements
    2. Setting up Python connectivity with the broker
    3. How to do it…
    4. How it works...
    5. Querying a list of instruments
    6. Getting ready
    7. How to do it…
    8. How it works…
    9. Fetching an instrument
    10. Getting ready
    11. How to do it…
    12. How it works…
    13. Querying a list of exchanges
    14. Getting ready
    15. How to do it…
    16. How it works…
    17. Querying a list of segments
    18. Getting ready
    19. How to do it…
    20. How it works…
    21. Knowing other attributes supported by the broker
    22. How to do it…
    23. How it works…
    24. Placing a simple REGULAR order
    25. Getting ready
    26. How to do it…
    27. How it works…
    28. Placing a simple BRACKET order
    29. Getting ready
    30. How to do it…
    31. How it works…
    32. Placing a simple DELIVERY order
    33. Getting ready
    34. How to do it…
    35. How it works…
    36. Placing a simple INTRADAY order
    37. Getting ready
    38. How to do it…
    39. How it works…
    40. Querying margins and funds
    41. Getting ready
    42. How to do it…
    43. How it works…
    44. Calculating the brokerage charged
    45. How to do it…
    46. How it works…
    47. Calculating the government taxes charged
    48. How to do it…
    49. How it works…
  8. Fetching Financial Data
    1. Technical requirements
    2. Fetching the list of financial instruments
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. Attributes of a financial instrument
    7. Getting ready
    8. How to do it…
    9. How it works…
    10. Expiry of financial instruments
    11. Getting ready
    12. How to do it…
    13. How it works…
    14. Circuit limits of a financial instrument
    15. Getting ready
    16. How to do it…
    17. How it works…
    18. The market depth of a financial instrument
    19. Getting ready
    20. How to do it…
    21. How it works…
    22. The total pending buy quantity of a financial instrument
    23. Getting ready
    24. How to do it…
    25. How it works…
    26. The total pending sell quantity of a financial instrument
    27. Getting ready
    28. How to do it…
    29. How it works…
    30. The total volume traded for the day of a financial instrument
    31. Getting ready
    32. How to do it…
    33. How it works…
    34. The last traded price of a financial instrument
    35. Getting ready
    36. How to do it…
    37. How it works…
    38. The last traded time of a financial instrument
    39. Getting ready
    40. How to do it…
    41. How it works…
    42. The last traded quantity of a financial instrument
    43. Getting ready
    44. How to do it…
    45. How it works…
    46. The recorded open price of the day of a financial instrument
    47. Getting ready
    48. How to do it…
    49. How it works…
    50. The recorded highest price of the day of a financial instrument
    51. Getting ready
    52. How to do it…
    53. How it works…
    54. The recorded lowest price of the day of a financial instrument
    55. Getting ready
    56. How to do it…
    57. How it works…
    58. The recorded close price of the last traded day of a financial instrument
    59. Getting ready
    60. How to do it…
    61. How it works…
  9. Computing Candlesticks and Historical Data
    1. Technical requirements
    2. Fetching historical data using the broker API
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. Fetching historical data using the Japanese (OHLC) candlestick pattern
    7. Getting ready
    8. How to do it…
    9. How it works…
    10. Fetching the Japanese candlestick pattern with variations in candle intervals
    11. Getting ready
    12. How to do it…
    13. How it works…
    14. Fetching historical data using the Line Break candlestick pattern
    15. Getting ready
    16. How to do it…
    17. How it works…
    18. Fetching historical data using the Renko candlestick pattern
    19. Getting ready
    20. How to do it…
    21. How it works…
    22. Fetching historical data using the Heikin-Ashi candlestick pattern
    23. Getting ready
    24. How to do it…
    25. How it works…
    26. Fetching historical data using Quandl
    27. Getting ready
    28. How to do it…
    29. How it works…
  10. Computing and Plotting Technical Indicators
    1. Technical requirements
    2. Trend indicators – simple moving average
    3. Getting started
    4. How to do it…
    5. How it works...
    6. Trend indicators – exponential moving average
    7. Getting started
    8. How to do it…
    9. How it works…
    10. Trend indicators – moving average convergence divergence
    11. Getting started
    12. How to do it…
    13. How it works…
    14. Trend indicators – parabolic stop and reverse
    15. Getting started
    16. How to do it…
    17. How it works…
    18. Momentum indicators – relative strength index
    19. Getting started
    20. How to do it…
    21. How it works…
    22. Momentum indicators – stochastic oscillator
    23. Getting started
    24. How to do it…
    25. How it works…
    26. Volatility indicators – Bollinger Bands
    27. Getting started
    28. How to do it…
    29. How it works…
    30. Volatility indicators – average true range
    31. Getting started
    32. How to do it…
    33. How it works…
    34. Volume indicators – on balance volume
    35. Getting started
    36. How to do it…
    37. How it works…
    38. Volume indicators – volume-weighted average price
    39. Getting started
    40. How to do it…
    41. How it works…
  11. Placing Regular Orders on the Exchange
    1. Technical requirements
    2. Placing a regular market order
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. Placing a regular limit order
    7. Getting ready
    8. How to do it...
    9. How it works...
    10. Placing a regular stoploss-limit order
    11. Getting ready...
    12. How to do it…
    13. How it works…
    14. Placing a regular stoploss-market order
    15. Getting ready
    16. How to do it…
    17. How it works…
  12. Placing Bracket and Cover Orders on the Exchange
    1. Technical requirements
    2. Placing a bracket limit order
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. There's more…
    7. Placing a bracket stoploss-limit order
    8. Getting ready
    9. How to do it…
    10. How it works…
    11. Placing a bracket limit order with trailing stoploss
    12. Getting ready
    13. How to do it…
    14. How it works...
    15. Placing a bracket stoploss-limit order with trailing stoploss
    16. Getting ready
    17. How to do it…
    18. How it works...
    19. Placing a cover market order
    20. Getting ready
    21. How to do it…
    22. How it works...
    23. Placing a cover limit order
    24. Getting ready
    25. How to do it…
    26. 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
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. EMA-Regular-Order strategy – coding the strategy_select_instruments_for_entry method
    7. Getting ready
    8. How to do it…
    9. How it works…
    10. EMA-Regular-Order strategy – coding the strategy_enter_position method
    11. Getting ready
    12. How to do it…
    13. How it works…
    14. EMA-Regular-Order strategy – coding the strategy_select_instruments_for_exit method
    15. Getting ready
    16. How to do it…
    17. How it works…
    18. EMA-Regular-Order strategy – coding the strategy_exit_position method
    19. Getting ready
    20. How to do it…
    21. How it works…
    22. EMA-Regular-Order strategy – uploading the strategy on the AlgoBulls trading platform
    23. Getting ready
    24. How to do it…
    25. How it works…
    26. There's more…
    27. MACD-Bracket-Order strategy – coding the __init__, initialize, name, and versions_supported methods
    28. Getting ready
    29. How to do it…
    30. How it works…
    31. MACD-Bracket-Order strategy – coding the strategy_select_instruments_for_entry method
    32. Getting ready
    33. How to do it…
    34. How it works…
    35. MACD-Bracket-Order strategy – coding the strategy_enter_position method
    36. Getting ready
    37. How to do it…
    38. How it works…
    39. MACD-Bracket-Order strategy – coding the strategy_select_instruments_for_exit method
    40. Getting ready
    41. How to do it…
    42. How it works…
    43. MACD-Bracket-Order strategy – coding the strategy_exit_position method
    44. Getting ready
    45. How to do it…
    46. How it works…
    47. MACD-Bracket-Order strategy — uploading the strategy on the AlgoBulls trading platform
    48. Getting ready
    49. How to do it…
    50. How it works…
    51. There's more…
  14. Algorithmic Trading - Backtesting
    1. Technical requirements
    2. EMA-Regular-Order strategy – fetching the strategy
    3. How to do it…
    4. How it works…
    5. EMA-Regular-Order strategy – backtesting the strategy
    6. Getting ready
    7. How to do it…
    8. How it works…
    9. There's more…
    10. EMA-Regular-Order strategy – fetching backtesting logs in real time
    11. Getting ready
    12. How to do it…
    13. How it works…
    14. There's more...
    15. EMA-Regular-Order strategy – fetching a backtesting report – profit and loss table
    16. Getting ready
    17. How to do it…
    18. How it works…
    19. There's more...
    20. EMA-Regular-Order strategy — fetching a backtesting report – statistics table
    21. Getting ready
    22. How to do it…
    23. How it works…
    24. There's more...
    25. EMA-Regular-Order strategy – fetching a backtesting report – order history
    26. Getting ready
    27. How to do it…
    28. How it works…
    29. There's more...
    30. MACD-Bracket-Order strategy – fetching the strategy
    31. How to do it…
    32. How it works…
    33. MACD-Bracket-Order strategy – backtesting the strategy
    34. Getting ready
    35. How to do it…
    36. How it works…
    37. There's more…
    38. MACD-Bracket-Order strategy – fetching backtesting logs in real time
    39. Getting ready
    40. How to do it…
    41. How it works…
    42. There's more...
    43. MACD-Bracket-Order strategy – fetching a backtesting report – profit and loss table
    44. Getting ready
    45. How to do it…
    46. How it works…
    47. There's more...
    48. MACD-Bracket-Order strategy – fetching a backtesting report – statistics table
    49. Getting ready
    50. How to do it…
    51. How it works…
    52. There's more...
    53. MACD-Bracket-Order strategy – fetching a backtesting report – order history
    54. Getting ready
    55. How to do it…
    56. How it works…
    57. There's more...
  15. Algorithmic Trading - Paper Trading
    1. Technical requirements
    2. EMA-Regular-Order strategy – fetching the strategy
    3. How to do it…
    4. How it works…
    5. EMA-Regular-Order strategy – paper trading the strategy
    6. Getting ready
    7. How to do it…
    8. How it works…
    9. There's more…
    10. EMA-Regular-Order strategy – fetching paper trading logs in real time
    11. Getting ready
    12. How to do it…
    13. How it works…
    14. There's more...
    15. EMA-Regular-Order strategy – fetching a paper trading report – profit and loss table
    16. Getting ready
    17. How to do it…
    18. How it works…
    19. There's more...
    20. EMA-Regular-Order strategy – fetching a paper trading report – statistics table
    21. Getting ready
    22. How to do it…
    23. How it works…
    24. There's more...
    25. EMA-Regular-Order strategy – fetching a paper trading report – order history
    26. Getting ready
    27. How to do it…
    28. How it works…
    29. There's more...
    30. MACD-Bracket-Order strategy – fetching the strategy
    31. How to do it…
    32. How it works…
    33. MACD-Bracket-Order strategy – paper trading the strategy
    34. Getting ready
    35. How to do it…
    36. How it works…
    37. There's more…
    38. MACD-Bracket-Order strategy – fetching paper trading logs in real time
    39. Getting ready
    40. How to do it…
    41. How it works…
    42. There's more...
    43. MACD-Bracket-Order strategy – fetching a paper trading report – profit and loss table
    44. Getting ready
    45. How to do it…
    46. How it works…
    47. There's more...
    48. MACD-Bracket-Order strategy – fetching a paper trading report – statistics table
    49. Getting ready
    50. How to do it…
    51. How it works…
    52. There's more...
    53. MACD-Bracket-Order strategy – fetching a paper trading report – order history
    54. Getting ready
    55. How to do it…
    56. How it works…
    57. There's more...
  16. Algorithmic Trading - Real Trading
    1. Technical requirements
    2. EMA–Regular–Order strategy – fetching the strategy
    3. How to do it…
    4. How it works…
    5. EMA–Regular–Order strategy – real trading the strategy
    6. Getting ready
    7. How to do it…
    8. How it works…
    9. There's more…
    10. EMA–Regular–Order strategy – fetching real trading logs in real time
    11. Getting ready
    12. How to do it…
    13. How it works…
    14. There's more...
    15. EMA–Regular–Order strategy – fetching a real trading report – profit and loss table
    16. Getting ready
    17. How to do it…
    18. How it works…
    19. There's more...
    20. EMA–Regular–Order strategy – fetching a real trading report – statistics table
    21. Getting ready
    22. How to do it…
    23. How it works…
    24. There's more...
    25. MACD–Bracket–Order strategy – fetching the strategy
    26. How to do it…
    27. How it works…
    28. MACD–Bracket–Order strategy – real trading the strategy
    29. Getting ready
    30. How to do it…
    31. How it works…
    32. There's more…
    33. MACD–Bracket–Order strategy – fetching real trading logs in real time
    34. Getting ready
    35. How to do it…
    36. How it works…
    37. There's more...
    38. MACD–Bracket–Order strategy – fetching a real trading report – profit and loss table
    39. Getting ready
    40. How to do it…
    41. How it works…
    42. There's more...
    43. MACD–Bracket–Order strategy – fetching a real trading report – statistics table
    44. Getting ready
    45. How to do it…
    46. How it works…
    47. There's more...
  17. Appendix I
    1. Setting up your Zerodha account
    2. Opening a Zerodha account online
    3. Logging in to the Zerodha trading platform website
    4. Setting up your Zerodha Developer Options account
    5. Logging in to the Zerodha Developer Options website
    6. Purchasing and enabling the Zerodha Developer Options API
    7. Testing API keys and authorizing the app by firing your first API call
  18. Appendix II
    1. Setting up your AlgoBulls account
    2. Registering on the AlgoBulls platform
    3. Logging in to the AlgoBulls website
    4. Fetching your AlgoBulls Developer Options token
    5. Setting up your AlgoBulls account
  19. Appendix III
    1. Developing and improving strategies
    2. Strategy profitability is subject to seasons
    3. Strategy profitability is subject to its parameter values
    4. Backtesting alone does not ensure strategy profitability
    5. Broker limitations
    6. Staying connected with the community
    7. 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
3.19.31.73