Generative AI for Trading and Asset Management - Hamlet Jesse Medina Ruiz - E-Book

Generative AI for Trading and Asset Management E-Book

Hamlet Jesse Medina Ruiz

0,0
37,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

Expert guide on using AI to supercharge traders' productivity, optimize portfolios, and suggest new trading strategies

Generative AI for Trading and Asset Management is an essential guide to understand how generative AI has emerged as a transformative force in the realm of asset management, particularly in the context of trading, due to its ability to analyze vast datasets, identify intricate patterns, and suggest complex trading strategies. Practically, this book explains how to utilize various types of AI: unsupervised learning, supervised learning, reinforcement learning, and large language models to suggest new trading strategies, manage risks, optimize trading strategies and portfolios, and generally improve the productivity of algorithmic and discretionary traders alike. These techniques converge into an algorithm to trade on the Federal Reserve chair's press conferences in real time.

Written by Hamlet Medina, chief data scientist Criteo, and Ernie Chan, founder of QTS Capital Management and Predictnow.ai, this book explores topics including:

  • How large language models and other machine learning techniques can improve productivity of algorithmic and discretionary traders from ideation, signal generations, backtesting, risk management, to portfolio optimization
  • The pros and cons of tree-based models vs neural networks as they relate to financial applications. How regularization techniques can enhance out of sample performance
  • Comprehensive exploration of the main families of explicit and implicit generative models for modeling high-dimensional data, including their advantages and limitations in model representation and training, sampling quality and speed, and representation learning.
  • Techniques for combining and utilizing generative models to address data scarcity and enhance data augmentation for training ML models in financial applications like market simulations, sentiment analysis, risk management, and more.
  • Application of generative AI models for processing fundamental data to develop trading signals.
  • Exploration of efficient methods for deploying large models into production, highlighting techniques and strategies to enhance inference efficiency, such as model pruning, quantization, and knowledge distillation.
  • Using existing LLMs to translate Federal Reserve Chair's speeches to text and generate trading signals.

Generative AI for Trading and Asset Management earns a well-deserved spot on the bookshelves of all asset managers seeking to harness the ever-changing landscape of AI technologies to navigate financial markets.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 507

Veröffentlichungsjahr: 2025

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Cover

Table of Contents

Title Page

Copyright

Dedication

Preface

Acknowledgments

About the Authors

Part I: Generative AI for Trading and Asset Management: A No-code Introduction

Chapter 1: No-code Generative AI for Basic Quantitative Finance

1.1 Retrieving Historical Market Data

1.2 Computing Sharpe Ratio

1.3 Data Formatting and Analysis

1.4 Translating Matlab Codes to Python Codes

1.5 Conclusion

Chapter 2: No-code Generative AI for Trading Strategies Development

2.1 Creating Codes from a Strategy Specification

2.2 Summarizing a Trading Strategy Paper and Creating Backtest Codes from It

2.3 Searching for a Portfolio Optimization Algorithm Based on Machine Learning

2.4 Explore Options Term Structure Arbitrage Strategies

2.5 Conclusion

2.6 Exercises

Appendix 2A.1 Computing Next-day’s Return

Appendix 2A.2 Uploading the Fama-French Factors

Appendix 2A.3 Combining Fama-French Factors with Next-day’s Returns

Chapter 3: Whirlwind Tour of ML in Asset Management

3.1 Unsupervised Learning

3.2 Supervised Learning

3.3 Deep Reinforcement Learning

3.4 Data Engineering

3.5 Feature Engineering

3.6 Conclusion

Part II: Deep Generative Models for Trading and Asset Management

Chapter 4: Understanding Generative AI

4.1 Why Generative Models

4.2 Difference with Discriminative Models

4.3 How Can We Use Them?

4.4 Illustrating Generative Models with ChatGPT

4.5 Hybrid Modeling: Combining Generative and Discriminative Models

4.6 Taxonomy of Generative Models

4.7 Conclusion

Chapter 5: Deep Autoregressive Models for Sequence Modeling

5.1 Representation Complexity

5.2 Representation and Complexity Reduction

5.3 A Short Tour of Key Model Families

5.4 Model Fitting

5.5 Conclusions

Chapter 6: Deep Latent Variable Models

6.1 Introduction

6.2 Latent Variable Models

6.3 Examples of Traditional Latent Variable Models

6.4 Learning

6.5 Variational Autoencoder (VAE)

6.6 VAEs for Sequential Data and Time Series

6.7 Conclusion

Chapter 7: Flow Models

7.1 Introduction

7.2 Model Training

7.3 Linear Flows

7.4 Designing Nonlinear Flows

7.5 Coupling Flows

7.6 Autoregressive Flows

7.7 Continuous Normalizing Flows

7.8 Modeling Financial Time Series with Flow Models

7.9 Conclusion

Chapter 8: Generative Adversarial Networks

8.1 Introduction

8.2 Training

8.3 Some Theoretical Insight in GANs

8.4 Why Is GAN Training Hard? Improving GAN Training Techniques

8.5 Wasserstein GAN (WGAN)

8.6 Extending GANs for Time Series

8.7 Conclusion

Chapter 9: Leveraging LLMs for Sentiment Analysis in Trading

9.1 Sentiment Analysis in Fed Press Conference Speeches Using Large Language Models

9.2 Data: Video + Market Prices

9.3 Speech-to-text Conversion

9.4 Sentiment Analysis

9.5 Experiment Results

9.6 Conclusion

Chapter 10: Efficient Inference

10.1 Introduction

10.2 Scaling Large Language Models: High Performance, High Computational Cost, and Emergent Abilities

10.3 Making FinBERT Faster

10.4 Model Quantization

10.5 Customizing Your LLM: Adapting Models to Your Needs

10.6 Conclusions

Chapter 11: Afterword

11.1 Diffusion Models

11.2 Combining Generative Model Variants

11.3 LLMs as Financial Advisors

References

Appendix

A.1 Retrieving Adjusted Closing Prices and Computing Daily Returns

A.2 Installing Python

A.3 Plotting the Risk-free-rate over the Years

A.4 Computing the Sharpe Ratio of SPY

A.5 Matlab Code for Computing Efficient Frontier and Finding the Tangency Portfolio

Index

End User License Agreement

List of Illustrations

Chapter 1

Figure 1.1 Efficient frontier based on Python code generated by ChatGPT.

Chapter 2

Figure 2.1 Cumulative returns of Fama-French three-factor strategy.

Figure 2.2 Incorrect plot of annualized time value of put options as function o...

Figure 2.3 Incorrect plot of implied volatility of put options as function of t...

Figure 2.4 Annualized put option prices as function of time to expiration based...

Figure 2.5 Annualized call option prices as function of time to expiration base...

Chapter 3

Figure 3.1 Dendrogram of five stocks based on the correlations of their daily r...

Figure 3.2 Principal components of two correlated series.

Figure 3.3 Illustration of a sigmoid function.

Figure 3.4 Illustration of why L1 regularization can more easily get us zero we...

Figure 3.5 The Anscombe quartet. All four data sets have same means, variances,...

Figure 3.6 A typical precision-recall curve.

Figure 3.7 A typical ROC (Receiver Operating Characteristics) curve.

Figure 3.8 A one-hidden-layer MLP that takes a vector of features to classify...

Figure 3.9 A simple MLP for time-series prediction.

Figure 3.10 Tying the weights to the same values and connecting the hidden nodes...

Figure 3.11 Same as Figure 3.10, but rotated 90° counterclockwise so time procee...

Figure 3.12 Daily minimum and maximum sentiment scores that show structural brea...

Chapter 4

Figure 4.1 Model taxonomy.

Figure 4.2 Conditional probability of the first token given the prompt .

Figure 4.3 Conditional probability of the second token given the prompt and p...

Figure 4.4 Conditional probability of the third token given the prompt and pr...

Figure 4.5 Conditional probability of the fourth token given the prompt and p...

Figure 4.6 Conditional probability of the fourth token given the prompt and p...

Chapter 5

Figure 5.1 Model taxonomy: Autoregressive models.

Figure 5.2 Sample from the Binarized MNIST dataset. Larochelle and Murray (2011).

Figure 5.3 MADE Generation on MNIST. Left: samples from a MADE model. Right: Ne...

Figure 5.4 Visualization of a stack of causal convolutional layers. Figure 2 fr...

Figure 5.5 Visualization of a stack of dilated causal convolutional layers. Fig...

Figure 5.6 Visualizing attention.

Figure 5.7 Scaled Dot-Product Attention. Figure 2 (left) from Vaswani et al. (2...

Figure 5.8 The Transformer encoder-decoder architecture, developed for machine ...

Figure 5.9 Multi-head Attention. Figure 2 (right) from Vaswani et al. (2023).

Figure 5.10 An illustration showing how the current and lagged values of the ser...

Figure 5.11 An illustration showing how the model input, comprising the time-ser...

Chapter 6

Figure 6.1 Model taxonomy: variational autoencoders.

Figure 6.2 Illustration of model selection for homoscedastic noise.

Figure 6.3 Illustration of model selection for heteroscedastic noise.

Figure 6.4 Illustration of clustering using Gaussian Mixture Models (GMMs). (a)...

Figure 6.5 Gaussian Mixture Model for market regime detection.

Figure 6.6 VAE.

Figure 6.7 Illustration of the learned data manifold for generative models with...

Figure 6.8 Illustration of the encoder-decoder architecture of Base TimeVAE. Th...

Figure 6.9 Illustration of the main components in Interpretable TimeVAE. Specia...

Chapter 7

Figure 7.1 Model taxonomy: flow models.

Figure 7.2 Illustration of the operations involved in coupling flows, including...

Figure 7.3 Illustration of unbiased samples generated by the NICE model when tra...

Figure 7.4 Illustration of unbiased samples generated by the NICE model when tra...

Figure 7.5 Samples generated by the Real-NVP model across four datasets: CIFAR-10...

Figure 7.6 Illustration of new images generated through interpolations between f...

Chapter 8

Figure 8.1 Model taxonomy: GANs.

Figure 8.2 Evolution of GAN-generated images over time, illustrating the progre...

Figure 8.3 The rightmost column displays the nearest training example to each c...

Figure 8.4 Table 1 from Goodfellow et al. (2014): Parzen window-based log-likel...

Figure 8.5 Illustration of one reason why training GANs can be difficult. In th...

Figure 8.6 Illustration of two different cases for the data distribution and th...

Figure 8.7 Illustration of the optimal discriminator and critic when distinguis...

Chapter 9

Figure 9.1 Fed Chair Powell discusses latest Fed rate hike.

Figure 9.2 System block diagram.

Figure 9.3 SPY Price series during Fed press conference.

Figure 9.4 Enriched price series.

Figure 9.5 Scatter plot of sentiment signal vs forward returns.

Figure 9.6 Available models and languages.

Figure 9.7 Whisper output on FED data.

Figure 9.8 Figure 3 from Devlin et al. (2019): Illustration of differences in p...

Figure 9.9 Figure 1 from Devlin et al. (2019): Illustration of the input/output...

Figure 9.10 Figure 2 from Devlin et al. (2019): Illustration of the BERT input r...

Figure 9.11 Time-series sentiment signal and forward returns.

Figure 9.12 Scatter plot of sentiment signal vs forward returns.

Chapter 10

Figure 10.1 Figure 1 from Kaplan et al. (2020): Illustration of how language mod...

Figure 10.2 Figure 4 from Wei et al. (2023): An illustration of how performance ...

Figure 10.3 Figure 2 from Wei et al. (2022): Illustration of performance, measur...

Figure 10.4 Softmax distribution.

Figure 10.5 Weights distribution.

Figure 10.6 Distribution of quantized weights.

Figure 10.7 Figure 1 from Hu et al. (2021): This figure illustrates the reparame...

Appendix

Figure A.1 BIL annualized returns.

Figure A.2 Twenty-day moving average of annualized BIL returns.

Figure A.3 Efficient Frontier produced by Matlab codes.

List of Tables

Chapter 3

Table 3.1 Results of cluster-based features importance ranking based on their ...

Table 3.2 Hypothetical design matrix for three features with four samples.

Table 3.3 The confusion matrix for binary classification.

Table 3.4 Confusion matrix for three-class classification.

Chapter 7

Table 7.1 Computational complexity of the inverse and determinant of the Jacob...

Chapter 9

Table 9.1 Performance table.

Chapter 10

Table 10.1 Performance metrics of teacher vs. student models.

Table 10.2 Inference speed of teacher vs. student models.

Table 10.3 Integer range for different bit widths.

Table 10.4 Performance table for speedups because of quantization.

Table 10.5 Inference speed after quantization.

Guide

Cover

Table of Contents

Title Page

Copyright

Dedication

Preface

Acknowledgments

About the Authors

Begin Reading

Appendix

References

Index

End User License Agreement

Pages

iii

iv

v

vi

xv

xvi

xvii

xviii

xix

xx

xxi

xxii

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

52

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

Generative AI for Trading and Asset Management

Hamlet Jesse Medina Ruiz

Ernest Chan

Copyright © 2025 by John Wiley & Sons, Inc. All rights reserved, including rights for text and data mining and training of artificial intelligence technologies or similar technologies.

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) 750-8400, 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.

The manufacturer’s authorized representative according to the EU General Product Safety Regulation is Wiley-VCH GmbH, Boschstr. 12, 69469 Weinheim, Germany, e-mail: [email protected].

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 Applied for:

Print ISBN: 9781394266975

ePDF ISBN: 9781394267002

epub ISBN: 9781394266999

Cover Image: © imaginima/Getty Images

Cover Design: Wiley

Author Photos: Courtesy of the Authors

To my parents, Denis and Herinarco, and my grandmother, Gloria María.

To my family: Ben, Sarah, and Ethan

Preface

There are broadly three types of modern AI models: discriminative models, generative models, and reinforcement learning. Most quantitative asset managers are familiar with discriminative models (e.g., given yesterday’s return, what is the probability of today’s return being positive); some are also familiar with reinforcement learning (e.g., how can we optimize the selling price to get a better profit). But Generative AI, commonly referred to as GenAI, is a recent invention that receives a lot of buzz but is often mistaken as a synonym with Large Language Models (LLMs) or image generation. But GenAI can learn from anything, not just from text or images. In particular, it can learn from time series of asset returns, which is perhaps most relevant for asset managers.

In this book, we delve into both the applications as well as the fundamentals of GenAI. It is divided into two broad parts: (1) No-code usage of Generative AI for traders and asset managers with little coding experience; (2) the fundamentals of Generative AI and their applications in finance for those who are well-versed in coding and discriminative AI. As a result, readers of each category can feel free to just skim the chapters of the other part.

In the first two chapters of Part 1, we will show you examples of how to use the no-code version of GenAI to do stuff that most traders and quantitative investors will encounter in their lifetime. For example, how to retrieve adjusted prices of an ETF from the internet, how to compute the most common performance metrics based on a spreadsheet full of their daily prices; how to convert a trading strategy’s backtest code from Matlab to Python (using Matlab code from Ernie’s book “Machine Trading” as example); how to come up with Python code based on a strategy specification; and how to summarize a paper about a trading strategy and turn that into backtest code. What we will not be able to show you is “ChatGPT, just come up with a profitable trading strategy that I can use.” At this stage of GenAI development, this level of creativity hasn’t been achieved yet.

The strategies that we asked GenAI to help create include a long-short factor strategy, a VIX futures carry strategy, and a SPX options calendar spread strategy. We also asked GenAI to conduct a literature search for portfolio optimization techniques based on deep reinforcement learning.

For these two chapters, we will use the most commonly known interface for these examples: the web-based ChatGPT GPT-4o which at the time of writing was the most current version of ChatGPT, and its cousin Microsoft's Copilot. Of course, you can probably perform most of these tasks equally admirably using Google’s Gemini Pro, X.AI’s Grok, or DeepSeek, but we haven’t tried.

The third chapter of Part 1 of this book is a whirlwind tour of machine learning techniques commonly used in asset management. They range from unsupervised learning to supervised learning and reinforcement learning. The chapter also covers techniques useful for avoiding overfitting and for model selection, such as regularization and hyperparameter optimization. It also covers various nuances in data and feature engineering that are often as important as what machine learning model to choose. It can be used as a primer for finance professionals new to AI, or as a refresher for those who are already dabbling in AI. Until around 2022 when ChatGPT was launched, this is all the AI that most asset managers would ever learn.

Part 2 of the book delves into the fundamentals and technical details of GenAI. Chapter 4 highlights the difference between discriminative and generative AI and introduces the major generative families: deep autoregressive models, and deep latent variable models such as variational autoencoders, flow models, and GANs. Each of these families get their own chapters in the remainder of Part 2. Each chapter explores how these models were originally developed and demonstrates how to adapt them to the dynamics of financial time series, with practical notebook code examples using financial data. The book concludes with end-to-end applications, showing how these models can preprocess alternative data, generate trading signals, and be optimized for efficient inference. Chapter 9 is all about application: how to leverage LLMs for sentiment analysis in trading. Chapter 10 is about deploying these systems in practice—especially how to optimize these models for efficient inference. This chapter is unique, not typically found in generative modeling resources, at least at the time of writing. Efficient inference is something Hamlet worked on the last few years at his company, where scalability and cost-effectiveness were non-negotiable. Once again, Python notebooks that implement most of these techniques are provided throughout. We conclude with Chapter 11, summarizing the main techniques covered in this book. We also emphasize the role of domain expertise in designing meaningful trading strategies, particularly when using LLMs as copilots, and discuss why GenAI, despite its success in other domains, may require more empirical work to achieve similar results in finance. Finally, we outline promising research directions where GenAI could significantly impact finance.

This book is not about proving mathematical theorems. Instead, we try to provide the reader with enough foundational knowledge to help the reader adapt the techniques to your specific problems. From Hamlet’s own experience solving challenging problems at his company, his own projects, and in machine learning competitions, he found that foundational understanding—going back to the mathematical formulation—often holds the key to good solutions. This is especially important when the most popular or widely accepted published solutions tend to fail on real-world datasets! So, this book is his attempt to bring these principles to traders and asset managers. At the same time, he includes many practical code examples that the reader can use to conduct their own research, improve models, and develop new solutions.

Where possible, we have used real-world datasets from the financial domain, limiting the use of standard datasets used for research publications just to explain foundational principles. Throughout the book, we also use numerous examples involving images and text data. These examples not only make the concepts easier to understand but also demonstrate techniques that have been very successful in other fields. They also have direct implications for preprocessing alternative data in finance and integrating it into trading strategies. Additionally, we offer insights into how these techniques can capture the unique characteristics of financial data. Some of the techniques may seem remote from financial applications, but the key to finding alpha where none existed before often comes from borrowing techniques from a different domain.

Part 1 is what Ernie has learned practicing AI and machine learning over the last 30 years, starting at the machine learning group at IBM T. J. Watson Lab, and ending with applying AI in the hedge fund and AI startup he founded (qtscm.com and predictnow.ai). He eats his own dog food.

Part 2 is built on Hamlet’s experience at Criteo as its chief data scientist, where he worked on multiple large-scale AI applications, his experience developing his own trading strategies, his participation in achieving top 1% rankings in various machine learning competitions, both finance and non-finance-related, on platforms like Kaggle and Numerai, and his own learning and formal finance education.

Hamlet’s journey into finance began, like many others, as an outsider. He started by reading any book he found interesting (fun fact: that included all of Ernie’s books). He realized that, like many people entering a new field, it’s common to discover that similar methods often exist in your own field but under different names, notations, and applications. Later, as he formalized his journey by completing a MicroMasters in Finance from MIT, he realized there wasn’t a single resource covering all the topics he had learned or applied over the years. This book is his attempt to fill that gap.

If Part 2 of this book seems hard, it is because the concepts are truly deep and revolutionary. Read it again, again, and again. After the fourth pass, and after trying out some of the code examples, you too can apply deep learning and GenAI techniques to discover alpha that no one found before.

We are sure that by the time this book is published, we have already gained better understanding and better tools in applying AI to finance, and we will post our latest learnings in our social media accounts (x.com/echanQT, x.com/hamletjmedina, and at our blog substack.com/@gatambook). Also, as Blaise Pascal famously said, “If I had more time, I would have written a shorter letter.” We hope to elucidate further some of the essential concepts here via Q&A and short tweets on our social media accounts, and via our workshops.

Acknowledgments

By Hamlet

The journey of writing this book has been deeply rewarding, but also, as my first book, quite challenging. I want to acknowledge the many people who supported me during this journey:

To Criteo, for allowing me to work on the applied side of the business, tackling a variety of challenging, large-scale applications over many years while also supporting me to spend time on the research side in this fast-evolving field. To my colleagues there: this combination has deeply shaped the way I approach problems and find solutions.

To my brother, Yanick Medina, currently a master’s student in AI, who has been an invaluable contributor to the code in

Part 2

of this book. He meticulously reviewed and tested the code while providing feedback that helped us ensure a balance between code modularity and accessibility, a principle we try to keep throughout.

To my family and friends, whose support and encouragement have been my foundation. Special thanks to my father, who taught me the beauty of mathematics from a very young age.

To God, whose infinite mercy has made all of this possible.

By Ernest

I would like to thank Dr. Roger Hunter, CTO at QTS Capital Management (qtscm.com), for his partnership in working on many AI in finance projects throughout the years, especially in co-developing the Lifecycle of Trading Strategy Development with Machine Learning workshop (epchan.com/workshops), and the Generative AI for Asset Managers workshop (predictnow.ai/generative-ai-workshop). I am also grateful that the current CEO of QTS Capital Management (qtscm.com), Dr. Nahid Jetha, has continued to advance AI development there to the benefits of our investors.

I also would like to thank my current and former technical team members at Predictnow.ai for their contributions to bringing AI to many asset managers: Johann Abraham, Sergei Belov, Pavan Dutt, Haoyu Fan, Guillaume Goujard, Andrew Inscore, Nancy Khullar, Nancy Xin Man, Uttej Mannava, Akshay Nautiyal, Sudarshan Sawal, Jean Silva, Jai Sukumar, and Quentin Viville.

Of course, this book would not have come into existence without the support of Wiley, especially the invaluable contributions of our managing editor, Stacey Rivera, editorial assistant, Katherine Cording, copyeditor Sheryl Nelson, content specialist Bala Shanmugasundaram, and executive editor, Bill Falloon. Working with them was truly a pleasure.

About the Authors

Hamlet Jesse Medina Ruiz holds the position of chief data scientist at Criteo. He specializes in time series forecasting, machine learning, deep learning, and Generative AI. He actively explores the potential of cutting-edge AI technologies, such as Generative AI across diverse applications.

He holds an electronic engineering degree from Universidad Rafael Belloso Chacin in Venezuela, as well as two master’s degrees with honors in mathematics and machine learning from the Institut Polytechnique de Paris and Université Paris-Saclay. Additionally, he earned a PhD in physics from Université Paris-Saclay. Hamlet has consistently achieved first place and top ten rankings in global machine learning contests, earning the titles of Kaggle Expert and Numerai Expert for these challenges. Recently, he also earned a MicroMaster’s in finance from MIT’s Sloan School of Management.

Ernest Chan (Ernie) is the founder and chief scientific officer of Predictnow.ai (www.predictnow.ai), which offers AI-driven adaptive optimization solutions to the finance industry and beyond. He is also the founder and nonexecutive chairperson of QTS Capital Management (www.qtscm.com), a quantitative CTA/CPO since 2011. He started his career as a machine learning researcher at IBM’s T.J. Watson Research Center’s language modeling group, which produced some of the best-known quant fund managers. Ernie is the acclaimed author of four previous books, Quantitative Trading (2nd ed.), Algorithmic Trading, Machine Trading, and Hands-on AI Trading all published by Wiley. More about these books and Ernie’s workshops on topics in quantitative investing and machine learning can be found at www.epchan.com. He obtained his PhD in physics from Cornell University and his BS in physics from the University of Toronto.

Part IGenerative AI for Trading and Asset Management: A No-code Introduction

Chapter 2No-code Generative AI for Trading Strategies Development

In the previous chapter, we found that with sufficient patience and prompting, we can get ChatGPT to perform basic financial data science tasks such as retrieving, exploring, and plotting data and computing performance statistics from them. Though the instructions are all in English, the output are frequently codes (especially Python codes). We still consider that no-code usage of Gen AI.

In this chapter, we will see if no-code Gen AI can handle more challenging tasks such as turning a trading strategy specification in English into backtest codes, and even more ambitious, suggest a trading strategy based on a vague human insight. As a reminder, Ernie has basic Python programming skills, is much more proficient in MATLAB, and is a hedge fund founder. This means he has extensive experience and domain knowledge in finance. In the following examples, the author approaches the development of the examples as someone who is an expert in the finance field, but not a professional programmer or a specialist in prompt engineering (like many professional asset managers). He needs help coding in a new language. Let’s see if ChatGPT can help him. Spoiler alert: ChatGPT’s responses will likely improve in later releases.

As in Chapter 1, we use ChatGPT GPT-4o for our tests. You can also try Microsoft’s Copilot, Google’s Gemini Pro, xAI’s Grok, or DeepSeek to see if they can do better. We will ask ChatGPT to do the following:

Create Python codes to backtest a long-short equity investment strategy that Ernie specified, which uses Fama-French factors to predict individual stocks’ returns.

Create Python codes to backtest a hedged VIX futures carry strategy from a published paper.

Conduct a literature search to find a portfolio optimization technique that uses deep reinforcement learning and that can adapt to regime changes.

Suggest and conduct exploratory analysis of a SPX options calendar spread trading strategy.

Once again, we will use the following boxes to denote prompts to ChatGPT, responses from ChatGPT, and our commentaries.

For user prompts, we will use the following format:

User Prompt

This is a user prompt.

For output from ChatGPT, including code, we will use:

ChatGPT Response

A response from ChatGPT

# Here

is

an amazing Python code example.

import

my_genai_trading_system

# :)

% Or a MATLAB code example.

load_system

(

'my_genai_trading_system'

)

% :)

If we embed our own commentary within its response, we will enclose it with [Author Note: …], for example:

ChatGPT Response

# Here is an amazing Python code example.

import

spicy

  # [Author Note: This should be 'scipy', not 'spicy'.]

Once again, code, data, and relevant resources produced or required for this chapter can be found in the corresponding chapter folder within the book’s repository: https://github.com/genai-for-traders/genai-for-trading-and-am. For this chapter, the files are located in the chapter02 folder and include scripts, Jupyter notebooks, input data files, and any necessary code for downloading or generating data.

2.1 Creating Codes from a Strategy Specification

In Machine Trading (Chan, 2017), Ernie created a toy strategy that used the Fama-French three-factor model to predict stocks’ returns and backtested the performance of a trading strategy based on those predictions. Let’s ask ChatGPT to create all the Python codes necessary for the backtest by just specifying the strategy in English. (Recall that Chan, 2017 only has Matlab codes.)

User Prompt

In the following dialogue, pretend you are a quantitative finance expert.

ChatGPT Response

… [Author Note: The response is a lot of unimportant babble. Some research suggests it results in better responses if we first prime ChatGPT on a particular application area. See Kıcıman et al. (2023).]

User Prompt

Fama-French factors are often used to explain the returns of a portfolio of stocks. What are the Fama-French factors?

ChatGPT Response

The Fama-French factors are a set of three factors (and later expanded to five) introduced by Eugene F. Fama and Kenneth R. French. They are used in finance to explain the returns of a stock portfolio, going beyond the traditional Capital Asset Pricing Model (CAPM), which uses only market risk (beta) to explain returns. Here’s a breakdown of these factors:

Original Three Factors

Market Risk (Rm - Rf):

This is the same as in CAPM, representing the excess return of a portfolio over the risk-free rate, capturing the market risk. Rm stands for the return of the market portfolio, and Rf is the risk-free rate.

Size Factor (SMB - Small Minus Big):

SMB stands for “Small [market capitalization] Minus Big”. It captures the historical excess returns of small-cap stocks over big-cap stocks. The factor is based on the observation that, over long periods, small-cap stocks tend to outperform large-cap stocks.

Value Factor (HML - High Minus Low):

HML stands for “High [book-to-market ratio] Minus Low”. This factor captures the excess returns of value stocks (with high book-to-market ratios) over growth stocks (with low book-to-market ratios). Value stocks are those that are typically undervalued by the market but have strong fundamentals.

So far so good—at least ChatGPT regurgitates the correct information that you can find in any quantitative finance textbook (e.g., Ruppert and Matteson, 2015) or even Wikipedia. Now let’s get it to produce codes.

User Prompt

Let’s focus only on the original Fama-French three factors. Instead of using them to explain contemporaneous returns of stocks in a portfolio, let’s use them to predict the next day’s returns of these stocks. Can you display the mathematical setup for this prediction?

ChatGPT Response

Certainly! To use the original Fama-French three factors for predicting the next day’s returns of stocks in a portfolio, we set up a predictive linear regression model. In this model, the future stock returns are the dependent variable, and the current values of the Fama-French factors are the independent variables. The mathematical setup for this predictive model is as follows: