35,99 €
Master the art of AI-driven algorithmic trading strategies through hands-on examples, in-depth insights, and step-by-step guidance
Hands-On AI Trading with Python, QuantConnect, and AWS explores real-world applications of AI technologies in algorithmic trading. It provides practical examples with complete code, allowing readers to understand and expand their AI toolbelt.
Unlike other books, this one focuses on designing actual trading strategies rather than setting up backtesting infrastructure. It utilizes QuantConnect, providing access to key market data from Algoseek and others. Examples are available on the book's GitHub repository, written in Python, and include performance tearsheets or research Jupyter notebooks.
The book starts with an overview of financial trading and QuantConnect's platform, organized by AI technology used:
Written by domain experts, including Jiri Pik, Ernest Chan, Philip Sun, Vivek Singh, and Jared Broad, this book is essential for hedge fund professionals, traders, asset managers, and finance students. Integrate AI into your next algorithmic trading strategy with Hands-On AI Trading with Python, QuantConnect, and AWS.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 545
Veröffentlichungsjahr: 2025
Cover
Table of Contents
Title Page
Copyright
Dedication
Biographies
Preface: QuantConnect
Introduction
Part I Foundations of Capital Markets and Quantitative Trading
Chapter 1 Foundations of Capital Markets
Market Mechanics
Market Participants
Data and Data Feeds
Brokerages and Transaction Costs
Security Identifiers
Assets and Derivatives
Chapter 2 Foundations of Quantitative Trading
Research Process
Testing and Debugging Tools
Time and Look-ahead Bias
Strategy Styles
Parameter Sensitivity Testing and Optimization
Margin Modeling
Diversification and Asset Selection
Indicators and Other Data Transformations
Sourcing Ideas
Part II Foundations of AI and ML in Algorithmic Trading
Chapter 3 Step 1: Problem Definition
Chapter 4 Step 2: Dataset Preparation
Data Collection
Exploratory Data Analysis
Data Preprocessing
Feature Selection
Splitting of Dataset into Training, Testing, and Possibly Validation Sets
Chapter 5 Step 3: Model Choice, Training, and Application
Regression
Classification
Ranking
Clustering
Language Models
Part III Advanced Applications of AI in Trading and Risk Management
Chapter 6 Applied Machine Learning
Example 1—ML Trend Scanning with MLFinlab
Example 2—Factor Preprocessing Techniques for Regime Detection
Example 3—Reversion vs. Trending: Strategy Selection by Classification
Example 4—Alpha by Hidden Markov Models
Example 5—FX SVM Wavelet Forecasting
Example 6—Dividend Harvesting Selection of High-Yield Assets
Example 7—Effect of Positive-Negative Splits
Example 8——Stop Loss Based on Historical Volatility and Drawdown Recovery
Example 9—ML Trading Pairs Selection
Example 10—Stock Selection through Clustering Fundamental Data
Example 11—Inverse Volatility Rank and Allocate to Future Contracts
Example 12—Trading Costs Optimization
Example 13—PCA Statistical Arbitrage Mean Reversion
Example 14—Temporal CNN Prediction
Example 15—Gaussian Classifier for Direction Prediction
Example 16—LLM Summarization of Tiingo News Articles
Example 17—Head Shoulders Pattern Matching with CNN
Example 18—Amazon Chronos Model
Example 19—FinBERT Model
Chapter 7 Better Hedging with Reinforcement Learning
Introduction
Overview of the Reinforcement Learning
Implementation on QuantConnect
Results
Conclusion
Chapter 8 AI for Risk Management and Optimization
What Is Corrective AI and Conditional Parameter Optimization?
Feature Engineering
Applying Corrective AI to Daily Seasonal Forex Trading
What Is Conditional Parameter Optimization?
Applying Conditional Parameter Optimization to an ETF Strategy
Unconditional vs. Conditional Parameter Optimizations
Performance Comparisons
Conditional Portfolio Optimization
Conclusion
Definitions of Spread_EMA & Spread_VAR
Chapter 9 Application of Large Language Models and Generative AI in Trading
Role of Generative AI in Creating Alpha
Selecting an LLM for Building a Generative AI Application
Prompt Engineering
Question Answering Using a Retrieval Augmented Application in SageMaker Canvas
Summarization
Useful AI Platforms and Services
References
Subject Index
Code Index
End User License Agreement
Chapter 1
Figure 1.1 Flow of retail and institutional traffic across public and private markets, an...
Figure 1.2 Relative performance of various assets during the market crash on May 6, 2010....
Figure 1.3 Trade bar formation and properties.
Figure 1.4 Quote bar formation and properties.
Figure 1.5 Order messaging between the algorithm and brokerage.
Figure 1.6 Spread crossing: sell market orders fill at the bid price, and buy market orde...
Figure 1.7 Slippage costs are different between liquid and illiquid assets.
Figure 1.8 Security identification for a complex ticker change history.
Figure 1.9 Security identification for a simple ticker change history.
Figure 1.10 Stock value and number of shares owned before and after a split event.
Chapter 2
Figure 2.1 Running backtests in QuantConnect Cloud with debugging mode.
Figure 2.2 A data event occurs at the end of the data consolidation period. Tick data doe...
Figure 2.3 Overfitting and underfitting a model leads to poor predictions on samples outs...
Chapter 4
Figure 4.1 Sweetviz dashboard with no comparison target.
Figure 4.2 Sweetviz dashboard with gender as the comparison target.
Figure 4.3 Box plot to identify outliers.
Figure 4.4 Example of a non-stationary time series.
Figure 4.5 Example of a stationary time series that results from differentiating a non-st...
Figure 4.6 The original non-stationary time series before performing fractional different...
Figure 4.7 The stationary time series that results from performing fractional differentia...
Figure 4.8 Two non-stationary assets that are cointegrated have a stationary spread.
Figure 4.9 Two cointegrated assets and their stationary spread.
Figure 4.10 Correlation matrix of the target and factors.
Figure 4.11 Feature importance plot of features 1 and 3. Feature 3 could be discarded to s...
Figure 4.12 Importance plot of features 1 and 3 that were selected by recursive feature el...
Figure 4.13 PCA transformed the feature matrix from three dimensions to two new dimensions...
Chapter 5
Figure 5.1 Example of linear regression.
Figure 5.2 Example of polynomial regression.
Figure 5.3 Example of LASSO regression.
Figure 5.4 Example of LASSO regression coefficients.
Figure 5.5 Example of ridge regression.
Figure 5.6 Example of ridge regression coefficients.
Figure 5.7 Example of Markov switching dynamic regression.
Figure 5.8 Smoothed regime probabilities of the Markov switching dynamic regression.
Figure 5.9 Example of decision tree regression.
Figure 5.10 Decision tree structure.
Figure 5.11 Example of SVM regression.
Figure 5.12 Example of class distribution in a dataset.
Figure 5.13 Example of confusion matrix.
Figure 5.14 Example of feature importance.
Figure 5.15 Example of ROC curve.
Figure 5.16 Example of hidden Markov model.
Figure 5.17 ROC curve of Gaussian Naive Bayes.
Figure 5.18 Example of neural network regression.
Figure 5.19 Example of model loss.
Figure 5.20 Example of true versus predicted ranking for multiple groups.
Figure 5.21 Example of OPTICS clustering.
Figure 5.22 Example of Amazon Chronos prediction.
Chapter 6
Figure 6.1 Example of trend identification using linear regression models.
Figure 6.2 Equity curves of the strategy and selected benchmark.
Figure 6.3 Predictions and observed log price.
Figure 6.4 Scatter plot that colors each closing price based on the direction of future r...
Figure 6.5 A bar chart showing the count of each label.
Figure 6.6 Time series plots that show the probability of each label from the model...
Figure 6.7 Time series plots that show the probability of each label from the model...
Figure 6.8 Time series plots that show the probability of each label from the model...
Figure 6.9 Time series plots that show the probability of each label from the model...
Figure 6.10 The price and volatility of SPY, along with highlighted zones of low/high vola...
Figure 6.11 Payoffs of a call option, put option, and an option straddle
Figure 6.12 Equity curve, performance plots, and custom plots for Example 4.1.
Figure 6.13 Monthly returns, crisis events, and sensitivity tests for Example 4.1.
Figure 6.14 Equity curve, performance plots, and custom plots for Example 4.2.
Figure 6.15 Monthly returns and crisis events for Example 4.2.
Figure 6.16 Equity curve, performance plots, and custom plots for Example 4.3.
Figure 6.17 Monthly returns and crisis events for Example 4.3.
Figure 6.18 Equity curve, performance plots, and custom plots for Example 5.
Figure 6.19 Monthly returns, crisis events, and sensitivity tests for Example 5.
Figure 6.20 Equity curve, performance plots, and custom plots for Example 6.
Figure 6.21 Monthly returns, crisis events, and sensitivity tests for Example 6.
Figure 6.22 Equity curve, performance plots, and custom plots for Example 7.
Figure 6.23 Monthly returns, crisis events, and sensitivity tests for Example 7.
Figure 6.24 Equity curve and performance plots for Example 8.1.
Figure 6.25 Monthly returns, crisis events, and sensitivity tests for Example 8.1.
Figure 6.26 Equity curve, performance plots, and custom plots for Example 8.2.
Figure 6.27 Monthly returns, crisis events, and sensitivity tests for Example 8.2.
Figure 6.28 Equity curve, performance plots, and custom plots for Example 8.3.
Figure 6.29 Monthly returns and crisis events for Example 8.3.
Figure 6.30 Asset representations in 3D space.
Figure 6.32 Asset representations in 3D space, color coded by group clusters.
Figure 6.34 Prices of candidate pairs and their normalized spread.
Figure 6.35 Equity curve, performance plots, and custom plots for Example 10.
Figure 6.36 Monthly returns, crisis events, and sensitivity tests for Example 10.
Figure 6.37 Equity curve, performance plots, and custom plots for Example 11.
Figure 6.38 Monthly returns, crisis events, and sensitivity tests for Example 11.
Figure 6.39 Plots of trading costs. Trading costs begin to decrease after the model has g...
Figure 6.40 Distribution of costs saved for delayed orders that filled before the time lim...
Figure 6.41 Distribution that shows the number of orders that filled at each delay time.
Figure 6.42 Distribution of costs saved for delayed orders that hit the time limit.
Figure 6.43 Equity curve, performance plots, and custom plots for Example 13.
Figure 6.44 Monthly returns, crisis events, and sensitivity tests for Example 13.
Figure 6.45 Architecture of the temporal CNN model.
Figure 6.46 Equity curve, performance plots, and custom plots for Example 14.
Figure 6.47 Monthly returns, crisis events, and sensitivity tests for Example 14.
Figure 6.48 Equity curve, performance plots, and custom plots for Example 15.
Figure 6.49 Monthly returns, crisis events, and sensitivity tests for Example 15.
Figure 6.50 Equity curve, performance plots, and custom plots for Example 16.
Figure 6.51 Monthly returns for Example 16.
Figure 6.52 Head and shoulder pattern and the typical entry timing.
Figure 6.53 Equity curve, performance plots, and custom plots for Example 17.
Figure 6.54 Monthly returns, crisis events, and sensitivity tests for Example 17.
Figure 6.55 Line plot of our synthetic H&S pattern.
Figure 6.56 Equity curve and performance plots for Example 18.1.
Figure 6.57 Monthly returns and crisis event for Example 18.1.
Figure 6.58 Equity curve and performance plots for Example 18.2.
Figure 6.59 Monthly returns and crisis event for Example 18.2.
Figure 6.60 Equity curve, performance plots, and custom plots for Example 19.1.
Figure 6.61 Monthly returns and crisis events for Example 19.1.
Figure 6.62 Equity curve, performance plots, and custom plots for Example 19.2.
Figure 6.63 Monthly returns and crisis events for Example 19.2.
Chapter 7
Figure 7.1 The volatility “smile” and “skew”: implied volatil...
Figure 7.2 Illustrative diagram of the continuous two-stage RL. The RL model can b...
Figure 7.3 Daily closing price of AAPL (Apple, Inc.) shares and APPL Call options with 18...
Figure 7.4 Convergence of loss function of the base model, that is, delta-mimicking stage...
Figure 7.5 Convergence of the PnL-based penalty function (loss function) during the refin...
Figure 7.6 Comparison of the decision evolution of the full model compared to the evoluti...
Figure 7.7 Cumulative changes of the total wealth of the hedging portfolios of static hed...
Figure 7.8 Cumulative changes of the net wealth of the hedged portfolio (i.e., the option...
Chapter 8
Figure 8.1 All these names are synonymous.
Figure 8.3 Average EURSUD return by time of day (New York time).
Figure 8.4 Equity curve of Primary trading strategy in out-of-sample period.
Figure 8.5 Equity curve of Corrective AI model in out-of-sample period.
Figure 8.6 Cumulative returns of strategies based on conditional vs. unconditional parame...
Figure 8.7 Training a portfolio performance prediction machine.
Figure 8.8 Live prediction of portfolio performance (inferences).
Figure 8.9 Optimization step.
Figure 8.10 Comparative performances of various portfolio optimization methods on Tech Por...
Figure 8.11 Cash allocation vs. market regime of tech portfolio.
Figure 8.12 Time evolution of allocations of TAA. portfolio (GLD is deep blue.)
Chapter 9
Figure 9.1 Prompt engineering complexity.
Figure 9.2 Intersection of the prompt components.
Figure 9.3 Amazon AWS architecture for LLM.
Figure 9.4 Amazon S3 dashboard.
Figure 9.5 Amazon Kendra wizard.
Figure 9.6 Amazon SageMaker wizard.
Figure 9.7 Snapshot from SageMaker Canvas.
Chapter 8
Table 8.1 Example trade ticks
Table 8.2 An 1-day slice of features table as input to the machine-learning model.
Table 8.3 Out-of-sample performances of unconditional vs. conditional parameter optimiza...
Table 8.4 A 1-day slice of features table as input to the neutral network
Table 8.5 MESH ETF
Table 8.6 Tech Portfolio
Table 8.7 Crypto portfolio
Table 8.8 WSG’s FX strategies portfolio
Table 8.9 In- and Out-of-sample performance of TAA portfolio
Chapter 9
Table 9.1 Popular LLM by use case.
Table 9.2 On-demand pricing on Amazon Web Services (AWS) for popular models.
Table 9.3 RAG application monthly cost.
Cover
Table of Contents
Title Page
Copyright
Dedication
Biographies
Preface: QuantConnect
Introduction
Begin Reading
References
Code Index
Subject Index
End User License Agreement
iii
iv
v
xiii
xiv
xv
xvi
xvii
xviii
xix
xx
xxi
xxiii
xxiv
xxv
xxvi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
391
Jiri Pik
Ernest P. Chan
Jared Broad
Philip Sun
Vivek Singh
Copyright © 2025 by Jiri Pik, Ernest P. Chan, Jared Broad, Philip Sun, and Vivek Singh. All rights reserved.
Published by John Wiley & Sons, Inc., Hoboken, New Jersey.
Published simultaneously in Canada.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning, or otherwise, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, (978) 750F400, fax (978) 750-4470, or on the web at www.copyright.com. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permission.
Trademarks: Wiley and the Wiley logo are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates in the United States and other countries and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.
Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts in preparing this book, they make no representations or warranties with respect to the accuracy or completeness of the contents of this book and specifically disclaim any implied warranties of merchantability or fitness for a particular purpose. No warranty may be created or extended by sales representatives or written sales materials. The advice and strategies contained herein may not be suitable for your situation. You should consult with a professional where appropriate. Further, readers should be aware that websites listed in this work may have changed or disappeared between when this work was written and when it is read. Neither the publisher nor authors shall be liable for any loss of profit or any other commercial damages, including but not limited to special, incidental, consequential, or other damages.
For general information on our other products and services or for technical support, please contact our Customer Care Department within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic formats. For more information about Wiley products, visit our web site at www.wiley.com.
Library of Congress Cataloging-in-Publication Data:
Names: Pik, Jiri, author. | Chan, Ernest P., author. | Broad, Jared, author. | Sun, Philip, author. | Singh, Vivek, author.
Title: Hands-on AI trading with Python, QuantConnect and AWS / Jiri Pik, Ernest P. Chan, Jared Broad, Philip Sun, Vivek Singh.
Description: This book is a comprehensive guide designed for finance students, traders, and quantitative analysts who aim to elevate trading strategies with artificial intelligence. It is tailored for readers with a foundational understanding of Python and basic financial concepts. It provides a practical approach to integrating AI into trading, emphasizing intuition, real-world applications, and ease of experimentation. Key concepts include AI algorithms, data preprocessing, and model selection, focusing on practical implementations. The book features a hands-on approach using QuantConnect, eliminating the need for complex data management or infrastructure setup. It covers essential AI techniques like regression models, hidden Markov models, reinforcement learning, and generative AI, highlighting how these methods optimize risk management, trend prediction, and trading efficiency. Pre-trained models from AWS Bedrock, and PredictNow.ai are utilized, making AI applications accessible and actionable. The book demystifies algorithmic trading by providing over 20 fully implemented AI algorithms with detailed source code examples. It balances theoretical knowledge and practical insights, enabling readers to understand AI’s impact on modern finance, experiment effectively, and adapt trading strategies to evolving market conditions. The book is a valuable resource for those seeking to leverage AI for financial success.
Identifiers: LCCN 2024039575 (print) | LCCN 2024039576 (ebook) | ISBN 9781394268436 (hardback) | ISBN 9781394267675 (ebook) | ISBN 9781394267668 (epub)
Subjects: LCSH: Artificial Intelligence – Applications in Finance. | Algorithmic Trading – Computer Programs. | Financial Engineering – Data Processing. | Machine Learning – Financial Applications. | Investment Analysis – Technological Innovations. | Quantitative Finance – Software.
Classification: LCC HG4515.95 .P428 2025 (print) | LCC HG4515.95 (ebook) | DDC 332.64/20285—dc23/eng/20240911
LC record available at https://lccn.loc.gov/2024039575
LC ebook record available at https://lccn.loc.gov/2024039576
Cover Image(s): © sore.studios/Shutterstock, © Troundless/Shutterstock
Cover Design: Jon Boylan
This book is dedicated to Singapore, a place that has become my home and fostered my professional growth. I am deeply grateful for the supportive and inspiring environment Singapore has provided, which has significantly contributed to the development of this book. Thank you, Singapore, for being a constant source of inspiration and support.
—Jiri Pik
To my family: Ben, Sarah, and Ethan.
—Ernest Chan
This book is dedicated to my parents, who are my guiding light, and my family, Ranu and Adri, who are my source of joy.
—Vivek Singh
Dedicated to my loving and supportive wife, Maria.
—Jared Broad
This book is dedicated to James Harris Simons (April 25, 1938–May 10, 2024), a genius mathematician, an accomplished entrepreneur and innovator, and above all a teacher and inspiration for math students and quants.
—Philip Sun
Jiri Pik
Jiri Pik is the founder and CEO of RocketEdge.com, a creative consultancy specializing in cloud computing, artificial intelligence (AI), and ultra-low-frequency financial trading. He has more than 20 years of experience in financial technology, working with leading banks and hedge funds, such as Citi, Goldman Sachs, JPMorgan, and UBS, to build and optimize trading, risk management, and market data systems. As a cloud architect, he holds all 12 Amazon Web Services (AWS) Certifications and several Microsoft Azure Certifications. He lives in Singapore.
Jared Broad
Jared Broad is the founder and CEO of QuantConnect, an open-source algorithmic trading platform that has served thousands of quantitative trading funds since 2012. He obtained his biomedical engineering degree from the University of Auckland. QuantConnect provided the full support of their team, including Derek Melchin, a senior quant at QuantConnect, to create the suite of examples.
Ernest Chan
Ernest Chan (Ernie) is the founder and chief scientific officer of Predictnow.ai, a machine learning Software as a Service (SaaS) and consultancy for risk management and adaptive optimization. He started his career as a machine-learning researcher at IBM’s T. J. Watson Research Center’s Human Language Technologies group, which produced some of the best-known quant fund managers. He was also one of the first few employees of Morgan Stanley’s AI group. He is the founder and non-executive chairman of QTS Capital Management, a quantitative CPO/CTA, and the acclaimed author of three books on quantitative trading (Quantitative Trading, Algorithmic Trading, Machine Trading), all published by Wiley. He obtained his Ph.D. in physics from Cornell University and his B.Sc. in physics from the University of Toronto.
Philip Sun
Philip Sun, CEO of Adaptive Investment Solutions, automates the construction and trading of options overlay strategies utilizing innovative approaches, including ML/AI. Philip teaches algorithmic and high-frequency trading as an adjunct faculty in the Mathematical Finance Program at Boston University. Before Adaptive, Philip led quantitative research at Sentinel Investments and was a senior quant at Fidelity Investments and Wellington Management.
Vivek Singh
Vivek Singh, senior product manager at AWS, is part of the core leadership team developing generative AI suite of services and leads GenAI data annotation, fine-tuning and evaluation services to improve the performance, trust, safety, and responsible use of LLMs and GenAI applications. Before AWS, Vivek worked at a large hedge fund performing fundamental stock analysis.
QuantConnect provides a comprehensive environment for researching, backtesting, optimizing, and deploying live algorithmic trading strategies. Founded in 2012 it has a community of more than 300,000 registered quants, engineers, and data scientists.
QuantConnect supports 11 asset classes, including equities, futures, forex, options, and cryptocurrencies, in a cloud or on-premise environments. It provides a comprehensive data library, including price, fundamental, and alternative data.
It hosts a professional caliber, co-located, low-latency live-trading environment with direct connectivity to 20 brokerages and routing to more than 1,200 destinations (qnt.co/book-cloud-live-trading).
Transparency, freedom, and security are key focuses of the community ethos. Users own all their intellectual property (IP) and code, and can use the open-source version of QuantConnect, LEAN (https://www.lean.io), to run independently hosted strategies.
Maintaining equal access to the technology is an important part of QuantConnect’s mission.
A robust framework helps create, evaluate, and launch trading algorithms much more efficiently. For video walk-throughs of QuantConnect, see qnt.co/book-videos.
We wanted this book to focus on fully implemented algorithmic trading strategies, allowing our readers to wrestle with code and explore the markets for alpha, rather than learning about scaffolding or parsing data. By using a robust framework, all of the data loading and basic portfolio management are handled, saving you weeks to years of effort.
Efficiency
Frameworks offer hundreds of ready-made components and libraries, which save development time. These include basic alpha signals, portfolio construction systems, and open-source execution algorithms. Through building on top of a framework, you can leverage thousands of lines of code within a few statements.
Standardization
Frameworks provide demonstration template algorithms and a community of members familiar with the Application Program Interface (API).
Error Reduction
Leveraging frameworks will dramatically reduce the likelihood of coding errors and potential losses in production trading. No quant or engineer is perfect; for a critical system managing capital, you need something with a track record and reviewed by thousands of people. Frameworks generally have less errors as they are covered with unit and regression tests.
Focus on Strategy
The traditional process of building an algorithmic trading strategy devotes 95% of efforts to the same three activities: data parsing and loading, brokerage connectivity, and dashboards or user interfaces, none of which improve your investment performance. By harnessing a framework, traders can focus on their trading strategies instead of infrastructure.
QuantConnect serves an open-source algorithmic trading engine, LEAN, with an extensive library of financial data and cloud-computing resources. With minimal effort, you can start a quantitative trading technology stack and deploy strategies for production trading (see Figure 0.1).
Figure 0.1 QuantConnect enables you to run LEAN via three platforms.
Its technology centers around three elements:
Cloud Platform (
qnt.co/book-cloud
)—A cloud-hosted managed environment that lets your team focus entirely on alpha creation and trading. Data updates, cloud infrastructure, and team onboarding are all handled automatically.
Local Platform (
qnt.co/book-local
)—An on-premises environment serving the same cloud user interface but giving you the freedom to add custom libraries, load proprietary datasets, and operate entirely on your organization’s servers.
LEAN CLI (
qnt.co/book-cli
)—A simple PIP package, CLI offers LEAN’s entire power via an easy-to-use command line interface. Results are rendered as JSON objects for processing with your favorite charting program.
For all these platforms, the loading and parsing of data is handled automatically. There are integrations to historical and live-streaming data vendors and 60 alternative data vendors letting you focus on alpha research.
To run most of the examples in this book, you will need an account on QuantConnect. Creating an account is free and takes less than 2 minutes. Register at quantconnect.com/signup.
QuantConnect cloud-based research terminals attach to terabytes of financial, fundamental, and alternative data, preformatted and ready to use (Figure 0.2). Hosted alternative data are linked to the underlying securities, tagged with the FIGI, CUSIP, and ISIN to facilitate building strategies. Users can access popular machine learning and feature selection libraries to quantify factor importance, and install custom packages on request.
Figure 0.2 QuantConnect cloud-hosted Jupyter research environment.
With minimal-to-no code changes, clients can move from research to point-in-time, fee, slippage, and spread-adjusted backtesting on lightning-fast cloud cores (Figure 0.3). It is easy to perform multi-asset backtesting on portfolios comprised of thousands of securities with realistic margin-modeling.
Figure 0.3 Accurate point-in-time backtesting, across 11 asset classes, down to tick resolution data.
You can import custom and alternative-data, linked to underlying securities, for realistically modeling live-trading portfolios and avoiding common pitfalls like look-ahead bias. More than 20,000 backtests are performed by the QuantConnect user-base daily.
The parameter sensitivity testing allows you to run thousands of full backtests on scalable cloud compute, completing weeks of work in minutes. Visualize all the iterations of parameters on heatmaps to quickly understand your strategy’s sensitivity to parameters for robust out-of-sample trading. Explore further by opening each result and seeing its individual trades and backtest logs to completely understand the source of your alpha, and how sensitive it is to parameter changes (Figure 0.4).
Figure 0.4 Scalable cloud parameter optimizer for rapid sensitivity testing.
QuantConnect has deployed more than 300,000 live strategies to a managed, co-located live-trading environment (Figure 0.5). The platform processes more than $45B in notional volume per month, serving hundreds of fund clients. Quants can execute trades directly through our 20 integrations, or route to 1,200 liquidity providers.
Figure 0.5 Instantly deploy strategies to live-trading with all data, user-interface, and order management handled automatically.
QuantConnect is powered by LEAN (qnt.com/book-lean), an open-source algorithmic trading engine designed to handle the entire life cycle of a trading strategy through research, backtesting, optimization, and live deployment.
LEAN has a permissive, commercially friendly license so you are free to download it and run it on your own servers. The LEAN engine is built in C# for the data parsing and loading, and bridges to python for the algorithm analysis.
Emphasizing its commitment to open-source, QuantConnect has built the LEAN CLI—a command line interface to run the research, backtesting, optimization, and live trading technology anywhere in the world.
Its high-level architecture diagram is illustrated in Figure 0.6.
Figure 0.6 LEAN architecture.
The detailed description of the engine is outside the scope of this book, and we recommend that readers review the LEAN documentation (qnt.co/book-lean-docs).
This book has been written for students interested in finance; traders and quants in hedge funds and pension funds; and investment firms looking to build intuition into when (or when not) and why (or why not) modern trading strategies work well and how to enrich a strategy with artificial intelligence (AI) algorithms to make it more robust.
The book builds upon the following:
A working knowledge of Python 3.x, including pandas, numpy, and sci-kit libraries.
Some familiarity with modern finance and trading.
A reader’s constant curiosity to keep asking himself/herself what a change of any part of the algorithm would result in.
We had three principal goals in writing this book:
To provide an overview of the key artificial intelligence (AI) algorithms, techniques, and best practices used in modern financial trading,
To teach intuition behind the algorithms via a significant number of worked-out real-world examples with detailed explanations, and
To provide an easy-to-setup and use environment where readers could instantly experiment with the algorithms to build their confidence without spending any time setting up the required infrastructure.
Unlike other books on financial trading, this book does not focus on setting up market data, backtesting, and trading infrastructure. Such setups are notoriously difficult, expensive, and require major investments. Consider the issue of market data quality, which requires a permanent team of data scientists and software engineers to ensure the data is correct and always delivered without delay.
Instead, the book fully utilizes QuantConnect, an open-source, financial-trading-as-infrastructure platform that allows you to start building your trading algorithms and test trading hypotheses using QuantConnect’s market data feeds, backtesting, and trading infrastructure.
Similarly, the book does not focus on explaining the intricate details of training large language models or sophisticated financial models. Instead, it depends on AWS Bedrock, which has already trained foundation models, or MLFinLab or PredictNow.ai trained financial models.
The book is organized as follows:
In the Preface, we introduce the QuantConnect platform.
In
Part I
, we introduce foundational concepts of modern markets (
Chapter 1
) and quantitative trading (
Chapter 2
).
Part II
introduces foundational concepts of designing a robust AI algorithm, starting with defining your inputs and output, shaping your data, and choosing the right models to use.
Part III
is the core of the book, describing more than 20 fully implemented algorithms that harness different data sources, models, and technology platforms. Included are algorithms built on QuantConnect with source code and results, allowing readers to see each algorithm from different perspectives, which is critical for mastering its applications in readers’ strategies.
The fully worked-out real-world examples have been designed to illustrate key components of modern algorithmic trading strategies.
Their parameters, such as the assets to be traded or the backtesting start and end dates, have been chosen to illustrate the algorithm’s performance. Readers are encouraged to modify the input parameters to see the effect of their changes.
The source code of all book examples is available in QuantConnect’s library for readers to clone and experiment with. To access them, navigate to hands-on-ai-trading.com/examples, and you’ll be directed to complete, maintained examples with which to experiment. To see how to modify an algorithm’s parameters, see Parameter Optimization in Chapter 2.
For readers with issues regarding bugs in the book code examples, please create an issue in the GitHub repository for the book (qnt.co/book-repo), and the QuantConnect team will address them directly.
For general support regarding issues related to QuantConnect, please log in to the QuantConnect Discord server at quantconnect.com/discord, where the QuantConnect team, AI-quant support agents, and thousands of community members can assist.
We’re incredibly grateful to the dozens of contributors who helped to make this book possible. There were combined efforts from teams of people at RocketEdge, QuantConnect, PredictNow, and Agile Finance.
Specifically, we’re grateful for Derek Melchin’s outstanding work over almost a year of researching, designing, building, and debugging more than 20 QuantConnect demonstration strategies. In addition, we’re grateful for the QuantConnect team who built entirely new application program interfaces (APIs) to make the book example code more elegant.
PredictNow thanks Sergei Belov, Haoyu Fan, Akshay Nautiyal, Sudarshan Sawal, and Quentin Viville for their research that contributed to the successes of the CAI and CPO methods. We thank Pavan Dutt, Nancy Khullar, and Jai Sukumar for their assistance in features engineering, software implementation of the CPO method, and Guillaume Goujard for his mathematical insights. Client feedback has been indispensable for improving these methods, and we thank all of our cannot-be-named clients for them. Finally, we appreciate the insightful questions raised by the audiences at UBS & Cornell Financial Engineering Manhattan AI Speaker Series, NYU Mathematical Finance and Financial Data Science seminar, CIBC’s Finance-AI seminar, Fidelity AI Asset Management group, Google Innovation Day, and many other public and private forums where we presented CPO.
Philip Sun would specifically like to thank Yilin Liu, a research assistant responsible for some of the initial AI hedging model development; Hao Xing, associate professor of finance at Boston University; and Chris Kelliher, senior quant researcher at Fidelity and adjunct faculty of Boston University’s Mathematical Finance Program, who provided valuable guidance for AI hedging research.
Finally, thank you to Chris Bartlett of Algoseek.com for bringing the author team together and providing the sample data for the book; and all of the team at Wiley, Bill Falloon, Katherine Cording, Delainey Henson, Purvi Patel, Rajesh Venkatraman, Sudhagaran Thandapani, Sheryl Nelson, and Susan Cerrafor their patience as we stretched the deadlines.