Cover....1
Title Page....2
Copyright and Credits....3
Contributors....4
Table of Contents....6
Preface....16
Acquire Free Financial Market Data with Cutting-Edge Python Libraries....24
Technical requirements....25
Diving into continuous futures data with Nasdaq Data Link....26
Getting ready…....27
How to do it…....27
How it works…....28
There’s more…....28
See also....30
Exploring S&P 500 ratios data with Nasdaq Data Link....31
How to do it…....31
How it works…....32
There’s more…....32
See also....33
Working with stock market data with the OpenBB Platform....33
Getting ready…....33
How to do it…....33
How it works…....34
There’s more…....34
See also....37
Fetching historic futures data with the OpenBB Platform....37
Getting ready…....38
How to do it…....38
There’s more…....39
See also....42
Navigating options market data with the OpenBB Platform....42
Getting ready…....42
How to do it…....42
How it works…....43
There’s more…....44
See also....45
Harnessing factor data using pandas_datareader....45
Getting ready…....46
How to do it…....46
How it works…....47
There’s more…....48
See also....48
Analyze and Transform Financial Market Data with pandas....50
Diving into pandas index types....51
How to do it…....51
How it works…....52
There’s more…....52
See also....54
Building pandas Series and DataFrames....54
Getting ready....54
How to do it…....55
How it works…....56
There’s more…....57
See also....60
Manipulating and transforming DataFrames....60
Getting ready…....61
How to do it…....61
How it works…....67
There’s more…....67
See also....71
Examining and selecting data from DataFrames....71
How to do it…....71
How it works…....74
There’s more…....75
See also....76
Calculating asset returns using pandas....76
How to do it…....77
How it works…....78
There’s more…....79
See also....80
Measuring the volatility of a return series....80
How to do it…....80
How it works…....81
There’s more…....81
See also....83
Generating a cumulative return series....83
Getting ready…....84
How to do it…....84
How it works…....86
See also....87
Resampling data for different time frames....87
How to do it…....87
How it works…....90
There’s more…....90
See also....92
Addressing missing data issues....92
Getting ready…....93
How to do it…....94
How it works…....95
There’s more…....95
See also....96
Applying custom functions to analyze time series data....96
Getting ready…....96
How to do it…....97
How it works…....98
There’s more…....98
See also....99
Visualize Financial Market Data with Matplotlib, Seaborn, and Plotly Dash....100
Quickly visualizing data using pandas....101
How to do it…....101
How it works…....104
There’s more…....104
See also....106
Animating the evolution of the yield curve with Matplotlib....107
How to do it…....107
How it works…....110
There’s more…....110
See also....110
Plotting options implied volatility surfaces with Matplotlib....111
Getting ready…....111
How to do it…....111
How it works…....113
There’s more…....114
See also....114
Visualizing statistical relationships with Seaborn....115
How to do it…....115
How it works…....117
There’s more…....117
See also....120
Creating an interactive PCA analytics dashboard with Plotly Dash....120
Getting ready…....120
How to do it…....121
How it works…....126
There’s more…....127
See also....127
Store Financial Market Data on Your Computer....128
Storing data on disk in CSV format....129
How to do it…....129
How it works…....130
There’s more…....131
See also…....131
Storing data on disk with SQLite....132
Getting ready…....132
How to do it…....132
How it works…....134
There’s more…....135
See also…....137
Storing data in a PostgreSQL database server....137
Getting ready…....138
How to do it…....139
How it works…....141
There’s more…....141
See also…....142
Storing data in ultra-fast HDF5 format....143
Getting ready…....143
How to do it…....143
How it works…....145
There’s more…....146
See also…....146
Build Alpha Factors for Stock Portfolios....148
Identifying latent return drivers using principal component analysis....149
Getting ready....149
How to do it…....149
How it works…....151
There’s more…....151
See also....154
Finding and hedging portfolio beta using linear regression....155
Getting ready....155
How to do it…....155
How it works…....158
There’s more…....159
See also....160
Analyzing portfolio sensitivities to the Fama-French factors....160
Getting ready....160
How to do it…....161
How it works…....163
There’s more…....164
See also....166
Assessing market inefficiency based on volatility....166
How to do it…....167
How it works…....171
There’s more…....172
See also....172
Preparing a factor ranking model using Zipline Pipelines....173
Getting ready....173
How to do it…....174
How it works…....176
There’s more…....177
See also....177
Vector-Based Backtesting with VectorBT....178
Building technical strategies with VectorBT....178
Getting ready....179
How to do it…....179
How it works…....182
There’s more…....183
See also....186
Conducting walk-forward optimization with VectorBT....186
Getting ready....186
How to do it…....186
How it works…....189
There’s more…....190
See also....191
Optimizing the SuperTrend strategy with VectorBT Pro....192
Getting ready....192
How to do it…....193
How it works…....198
There’s more…....199
See also....201
Event-Based Backtesting Factor Portfolios with Zipline Reloaded....202
Technical Requirements....202
For Windows, Unix/Linux, and Mac Intel users....202
For Mac M1/M2 users....203
Backtesting a momentum factor strategy with Zipline Reloaded....203
Getting ready....204
How to do it…....204
How it works…....208
There’s more…....210
See also....212
Exploring a mean reversion strategy with Zipline Reloaded....212
Getting ready....212
How to do it…....213
How it works…....218
There’s more…....219
See also....221
Evaluate Factor Risk and Performance with Alphalens Reloaded....222
Preparing backtest results....223
Getting ready…....223
How to do it…....223
How it works…....226
There’s more…....227
See also....228
Evaluating the information coefficient....228
Getting ready…....229
How to do it…....229
How it works…....231
There’s more…....232
See also....233
Examining factor return performance....233
How to do it…....234
How it works…....238
There’s more…....238
See also....239
Evaluating factor turnover....239
How to do it…....240
How it works…....241
There’s more…....242
See also....244
Assess Backtest Risk and Performance Metrics with Pyfolio....246
Preparing Zipline backtest results for Pyfolio Reloaded....247
Getting ready…....247
How to do it…....247
How it works…....251
There’s more…....252
See also....252
Generating strategy performance and return analytics....253
Getting ready…....253
How to do it…....253
How it works…....256
There’s more…....257
See also....259
Building a drawdown and rolling risk analysis....259
Getting ready…....260
How to do it…....260
How it works…....263
There’s more…....263
See also....264
Analyzing strategy holdings, leverage, exposure, and sector allocations....264
Getting ready…....265
How to do it…....265
How it works…....269
There’s more…....270
See also....270
Breaking Down Strategy Performance to Trade Level....271
Getting ready…....271
How to do it…....271
How it works…....273
There’s more…....274
See also....275
Set Up the Interactive Brokers Python API....276
Building an algorithmic trading app....277
Getting ready…....277
How to do it…....280
How it works…....281
There’s more…....282
See also....283
Creating a Contract object with the IB API....284
Getting ready…....284
How to do it…....285
How it works…....285
There’s more…....286
See also....287
Creating an Order object with the IB API....287
Getting ready…....287
How to do it…....287
How it works…....288
There’s more…....289
See also....289
Fetching historical market data....290
Getting ready…....290
How to do it…....290
How it works…....294
There’s more…....297
See also....298
Getting a market data snapshot....299
Getting ready…....299
How to do it…....299
How it works…....300
There’s more…....300
See also....301
Streaming live market data....301
Getting ready…....301
How to do it…....301
How it works…....305
There’s more…....307
See also....308
Storing live tick data in a local SQL database....308
Getting ready…....308
How to do it…....309
How it works…....312
There’s more…....312
See also....314
Manage Orders, Positions, and Portfolios with the IB API....316
Executing orders with the IB API....317
Getting ready....317
How to do it…....317
How it works…....320
There’s more…....320
See also....322
Managing orders once they’re placed....322
Getting ready....322
How to do it…....323
How it works…....323
There’s more…....324
See also....325
Getting details about your portfolio....325
Getting ready....326
How to do it…....326
How it works…....327
There’s more…....328
See also....329
Inspecting positions and position details....329
Getting ready....329
How to do it…....329
How it works…....331
There’s more…....331
See also....332
Computing portfolio profit and loss....332
Getting ready....332
How to do it…....333
How it works…....334
There’s more…....334
See also....335
Deploy Strategies to a Live Environment....336
Calculating real-time key performance and risk indicators....337
Getting ready....337
How to do it…....339
How it works…....340
There’s more…....343
See also....344
Sending orders based on portfolio targets....344
Getting ready....344
How to do it…....344
How it works…....347
There’s more…....348
See also....349
Deploying a monthly factor portfolio strategy....349
Getting ready....350
How to do it…....350
How it works…....354
There’s more…....355
See also....355
Deploying an options combo strategy....355
Getting ready....356
How to do it…....357
How it works…....358
There’s more…....359
See also....361
Deploying an intraday multi-asset mean reversion strategy....361
Getting ready....362
How to do it…....362
How it works…....365
There’s more…....368
See also....369
Advanced Recipes for Market Data and Strategy Management....370
Streaming real-time options data with ThetaData....371
Getting ready....371
How to do it…....371
How it works…....374
There’s more…....375
See also....379
Using the ArcticDB DataFrame database for tick storage....379
Getting ready....380
How to do it…....380
How it works…....384
There’s more…....386
See also....386
Triggering real-time risk limit alerts....387
Getting ready....387
How to do it…....387
How it works…....388
There’s more…....389
See also....391
Storing trade execution details in a SQL database....391
Getting ready....392
How to do it…....393
How it works…....395
There’s more…....395
See also....398
Index....400
Other Books You May Enjoy....409
Harness the power of Python libraries to transform freely available financial market data into algorithmic trading strategies and deploy them into a live trading environment
Get With Your Book: PDF Copy, AI Assistant, and Next-Gen Reader Free
Discover how Python has made algorithmic trading accessible to non-professionals with unparalleled expertise and practical insights from Jason Strimpel, founder of PyQuant News and a seasoned professional with global experience in trading and risk management. This book guides you through from the basics of quantitative finance and data acquisition to advanced stages of backtesting and live trading.
Detailed recipes will help you leverage the cutting-edge OpenBB SDK to gather freely available data for stocks, options, and futures, and build your own research environment using lightning-fast storage techniques like SQLite, HDF5, and ArcticDB. This book shows you how to use SciPy and statsmodels to identify alpha factors and hedge risk, and construct momentum and mean-reversion factors. You’ll optimize strategy parameters with walk-forward optimization using vectorbt and construct a production-ready backtest using Zipline Reloaded. Implementing all that you’ve learned, you’ll set up and deploy your algorithmic trading strategies in a live trading environment using the Interactive Brokers API, allowing you to stream tick-level data, submit orders, and retrieve portfolio details.
By the end of this algorithmic trading book, you'll not only have grasped the essential concepts but also the practical skills needed to implement and execute sophisticated trading strategies using Python.
Python for Algorithmic Trading Cookbook equips traders, investors, and Python developers with code to design, backtest, and deploy algorithmic trading strategies. You should have experience investing in the stock market, knowledge of Python data structures, and a basic understanding of using Python libraries like pandas. This book is also ideal for individuals with Python experience who are already active in the market or are aspiring to be.