I Built a Trading Bot with ChatGPT
Summary
TLDRIn this video, Siraj showcases his trading bot, 'GPT Trader,' built with the help of ChatGPT. He demonstrates how the bot uses machine learning techniques, like neural networks and reinforcement learning, to make stock predictions using test data and the Alpaca API. The bot executes trades based on these predictions, aiming to maximize profits. Siraj also explains the setup process, including code implementation and deployment. After testing the bot for 24 hours with a $2000 investment, he reveals a profit of 1.62%. The video is a tutorial on creating and deploying an AI-powered trading bot.
Takeaways
- 🤖 The video introduces 'GPT Trader', a trading bot built with the help of Chat GPT for stock trading using machine learning techniques.
- 📈 The bot uses test data for paper trading and makes predictions on stocks like SPY and Nvidia, aiming to make profit with a $2000 investment.
- 🔮 The video demonstrates the process of selecting machine learning techniques for stock prediction, starting with neural networks as the base.
- 💡 Chat GPT provides a Python code example for a neural network using scikit-learn and Keras to predict Yahoo stock prices, emphasizing the importance of data sources.
- 🛠 The script shows troubleshooting the initial code by addressing missing dependencies and suggesting the installation of TensorFlow.
- 📊 The Alpaca trading API is introduced as a source for real-time stock data, which is crucial for the bot's live trading functionality.
- 📝 The video outlines the process of signing up for Alpaca, obtaining API keys, and integrating them into the trading bot for fetching real-time data.
- 🔬 Advanced neural network techniques such as Recurrent Neural Networks (RNNs), Long Short-Term Memory networks (LSTMs), and Gated Recurrent Units (GRUs) are discussed for improving predictions.
- 🤹♂️ The integration of reinforcement learning with neural networks, specifically Proximal Policy Optimization (PPO), is highlighted for making trading decisions.
- 📚 The finrl library is recommended for implementing deep reinforcement learning on stock data, providing an example of how to set up the environment and agent.
- 🌐 The final deployment of the trading bot involves using a Cron job in a Flask API to run the trading bot daily on a web app, making trades based on the bot's predictions.
Q & A
What is the name of the trading bot built by Siraj and Chat GPT?
-The trading bot built by Siraj and Chat GPT is called GPT Trader.
What is the purpose of the GPT Trader bot?
-The GPT Trader bot is designed to make predictions on different stocks using machine learning techniques and to execute trades based on these predictions.
What machine learning techniques does the video mention for stock prediction?
-The video mentions several techniques including random forests, XGBoost, time series analysis, and neural networks, with a focus on deep learning.
What is the role of the Alpaca API in the GPT Trader bot?
-The Alpaca API is used to get real-time stock data for the GPT Trader bot to make informed trading decisions.
How does the video demonstrate the use of neural networks for stock prediction?
-The video shows a Python code example provided by Chat GPT, which uses the scikit-learn library with Keras on top to create a neural network model for predicting Yahoo stock prices.
What is the significance of the requirements.txt file mentioned in the video?
-The requirements.txt file lists all the necessary dependencies needed to run the Python code for the GPT Trader bot, ensuring that all required libraries are installed.
What is the role of the Sharpe ratio in the GPT Trader bot's trading strategy?
-The Sharpe ratio is used as a measure to determine the bot's trading actions. If the Sharpe ratio is above a certain threshold, the bot will buy the stock; if it's below another threshold, it will sell.
What advanced neural network techniques does the video suggest for improving the GPT Trader bot?
-The video suggests using deep reinforcement learning techniques, such as Proximal Policy Optimization (PPO), to improve the bot's trading strategy.
How does the video address the issue of getting legitimate stock data for the GPT Trader bot?
-The video suggests using the Alpaca trading API to obtain real-time stock data, which is then used to train the neural network model for the GPT Trader bot.
What is the final outcome of the GPT Trader bot's live trading after 24 hours?
-After 24 hours of live trading with an initial investment of over two thousand dollars, the GPT Trader bot made four trades and achieved a total profit of 1.62 dollars.
How does the video describe the process of deploying the GPT Trader bot as a web app?
-The video describes deploying the GPT Trader bot as a web app using a Cron job in a Flask API, which runs a Google Colab notebook once a day to execute trades based on the bot's predictions.
What is the significance of the threshold values for the Sharpe ratio in the GPT Trader bot's decision-making process?
-The threshold values for the Sharpe ratio determine the bot's buying and selling actions. A Sharpe ratio above the upper threshold (e.g., 0.4) triggers a buy, while a ratio below the lower threshold (e.g., 0.2) triggers a sell.
What does the video suggest as a way to automate the GPT Trader bot's daily operation?
-The video suggests using a Cron job to automate the daily operation of the GPT Trader bot, running a specified Python Flask API at a set time each day.
How does the video handle the issue of module installation when the initial Python code does not work?
-The video acknowledges the issue and suggests installing the missing module, TensorFlow, manually to resolve the 'no module named tensorflow' error.
What is the role of the finrl library in the GPT Trader bot's implementation?
-The finrl library is used to demonstrate how to apply deep reinforcement learning to real-time financial data for making predictions, which is then integrated into the GPT Trader bot.
What is the video's stance on the reliability of Chat GPT as a coding assistant?
-The video recognizes that Chat GPT is not a perfect coder but appreciates it for providing a scaffolding or starting point for building the GPT Trader bot.
What does the video imply about the importance of testing trading bots with paper trading before live trading?
-The video implies that testing with paper trading is crucial for understanding the bot's performance and making necessary adjustments before risking real money in live trading.
What is the video's approach to combining machine learning with domain-specific knowledge in the GPT Trader bot?
-The video's approach involves integrating advanced machine learning techniques, such as deep reinforcement learning, with domain-specific knowledge of financial markets to enhance the GPT Trader bot's performance.
Outlines
🤖 Building a Trading Bot with Chat GPT
In this paragraph, the creator introduces a trading bot named 'GPT Trader' that they developed in collaboration with Chat GPT. The bot is designed to make predictions on stock trading using machine learning techniques. The creator demonstrates the bot's capabilities using test data with platforms like the alpaca dashboard and discusses the process of integrating the bot with live trading. They also engage Chat GPT to list the top ten machine learning techniques for stock prediction, highlighting neural networks as the most popular choice. The creator then proceeds to ask Chat GPT for Python code examples to predict stock prices using neural networks, emphasizing the bot's ability to learn and adapt in the context of stock trading.
📈 Implementing Neural Networks for Stock Prediction
The creator delves into the specifics of using neural networks for stock prediction, starting with obtaining a Python code example from Chat GPT. They discuss the process of setting up the environment, including installing necessary libraries and handling potential issues like missing dependencies. The paragraph covers the integration of real-time stock data using the Alpaca trading API, which provides a more legitimate data source than the initially assumed CSV file. The creator also explores advanced neural network techniques, such as recurrent neural networks (RNNs), long short-term memory networks (LSTMs), gated recurrent units (GRUs), and transformer networks, ultimately deciding to use deep reinforcement learning for the trading bot's strategy.
🧠 Advanced Techniques: Proximal Policy Optimization (PPO)
This section focuses on the advanced neural network technique of Proximal Policy Optimization (PPO), which the creator intends to use for their trading bot. Chat GPT provides an explanation of PPO suitable for a five-year-old, illustrating it as finding the optimal balance between different outcomes. The creator then seeks Python code examples for implementing PPO and discovers the 'stable-baselines' library by OpenAI. They decide to use the 'finrl' library, which is tailored for financial reinforcement learning, to integrate PPO with stock trading. The paragraph outlines the process of setting up a simulated environment, training the PPO agent, and using the Alpaca API for real trades based on the agent's predictions.
🔄 Automating Trades with a Daily Cron Job
The final paragraph details the deployment of the AI trading bot as a web app that operates on a daily schedule, facilitated by a Cron job. The creator explains how the bot uses reinforcement learning to decide whether to buy or sell stocks based on the sharp ratio threshold. They describe the process of setting up a Cron job in a Flask API to run a Google Colab notebook at a specified time daily, which in turn triggers the bot to make trades via the Alpaca API. The creator also shares their experience after 24 hours of live trading with the bot, reporting a profit of $1.62 from four trades, and expresses gratitude to the audience for watching the video.
Mindmap
Keywords
💡Chat GPT
💡Trading Bot
💡Machine Learning Techniques
💡Neural Networks
💡Scikit-learn
💡Keras
💡Requirements.txt
💡Alpaca Trading API
💡Deep Reinforcement Learning
💡PPO (Proximal Policy Optimization)
💡Cron Job
💡Collab Notebook
💡Sharpe Ratio
Highlights
Introduction of GPT Trader, a trading bot built with chat GPT for stock trading.
Use of test data for making predictions on stocks like SPY and Nvidia with the help of machine learning techniques.
Chat GPT's role in assisting with the development of the trading bot and its capabilities for paper trading.
Discussion on the best machine learning techniques for stock prediction, including neural networks and deep learning.
Demonstration of using Python and scikit-learn with Keras to create a neural network model for stock price prediction.
Challenges faced with the initial code, including the need for a legitimate CSV file for stock data.
Request for a requirements.txt file to install necessary dependencies for the trading bot.
Encountering issues with module installation and the need for a better data source.
Introduction of the Alpaca trading API as a zero-commission method for obtaining stock data.
Explanation of how to sign up for Alpaca and use its API keys for trading.
Integration of Alpaca API with Python to fetch real-time stock data for the trading bot.
Discussion on advanced neural network techniques like recurrent neural networks, LSTM, GRU, and transformer networks.
Introduction of deep reinforcement learning as a cutting-edge technique for the trading bot.
Explanation of Proximal Policy Optimization (PPO) as a reinforcement learning technique.
Use of the finrl library for implementing deep reinforcement learning on financial data.
Setting up a web app to run the trading bot daily using a Cron job and Flask in Python.
Deployment of the trading bot to Versal for automated daily trading based on reinforcement learning predictions.
Outcome of the trading bot's performance after 24 hours, making a profit with real money investment.
Transcripts
hello world it's siraj and I gave chat
GPT two thousand dollars to trade with
it's a trading bot that I built with
chat GPT and I want to show it to you in
this video GPT Trader that's what I'm
calling it once we log into this app we
can see that I've got the alpaca
dashboard and I'll explain this to you
later and it's using test data so using
test data I was able to make predictions
on different stocks including spy and
Nvidia and chat GPT helped me build this
bot now that's just using paper trading
let me show you how to use this with the
live trading and that's at the end of
the video did I make money with my two
thousand dollar investment or did I lose
money find out at the end so let's start
with our first question for chat GPT
what are the best techniques to be using
here so let's ask it that what are 10 of
the best machine learning techniques for
stock prediction let's see what it gives
us here
so it's going to list some techniques
and again we're Noob so we don't know
what all these things are you know I can
say you know random forests are the
ability to take decision trees which are
if then statements and then use them in
an ensemble or group method I can say XG
boost is a way of improving predictions
on the popular website kaggle I can say
that time series analysis that's a
technique to make predictions based on
past data that is spread out across the
time domain but the most popular one are
neural networks that's what everything
is based off of these days deep learning
so let's pick number six let's ask
chatgpt another question because
remember chat EBT unless unlike other
AIS is able to remember the context so
let's ask it the next question show me a
python web example of using a of using
that to predict the price of Yahoo stock
let's just pick a stock
and it's immediately going to do that it
knows that this is referring to a neural
network it knows that number six is
neural network and it knows to use the
Deep learning library scikit-learn with
Keras on top to do this now what's
happening here it's going to actually
tell us what's happening at the end but
I'll give you a little brief uh preview
of what's happening it's using the
scikit-learn library to make a
statistical model called a neural
network we can define a neural network
in seven words input times weight at a
bias activate it's a series of math
operations to make predictions Based on
data it's using the Yahoo data set of
past stock prices to do that now where
is it getting the CSV file the CSV file
is not coming from anywhere it's just
assuming that we have this data so what
we're going to have to do is we're going
to try to compile this in a in a code
window and see what this does for us and
as you can see it's telling us what this
code does so let's take all this code
we're going to hit copy okay and we're
going to go to our terminal window
and in our terminal window we're going
to
create a new folder
and in that folder we're going to create
a new file that's a python file we'll
call it stockprediction.pi
in stock prediction.pi we're going to
paste in the exact code that chat gbt
told us to and we're going to save it
and run it with the python command now
what do you think is going to happen any
guesses instead of just installing these
why don't we ask chatgpt for the
requirements file to install these what
are the requirements dot txt5 what does
the
requirements.txt file look like
look like for the previous code so all
those dependencies it's going to give it
to us look like for the previous code
and it's going to give us a
requirements.txt file and then all we
have to do is do pip install
requirements.txt and then we have all of
our dependencies right there so it's
going to tell us exactly how to do that
and we can just copy that create a new
requirements.txt and once we've done
that we can run that as a pip installs
pip install we're going to do it
recursively so all of them are going to
be installed no module named tensorflow
so I guess it's not perfect what we're
learning is that chat gbt clearly isn't
a perfect coder but it's giving us a
scaffolding here so we've got to install
tensorflow and there's a way to install
tensorflow but already off the bat we
know that this isn't going to work
because the data isn't legit so let's
ask chat CPT for a better data source
shall we and then we'll go back to this
code remember we're working with this
thing as a co-founder so our next step
is to say
is there some way to get the data what's
a good way to do this now I don't want
to pay for stock data I don't know about
you so the way to do that is to ask it
is there a zero commission that means a
way to trade stocks without paying money
is there a zero commission let's say
developer first sound like a ad Right
Now API for stocks and I also like
crypto so I'll say and crypto and let's
see what it gives us hopefully it gives
us one because I don't want to be too
complicated here and we're going to use
this API to get real-time data from the
web today of whatever stock we want and
it looks it says the alpaca trading API
so that's the one we're going to use
because chatgpt knows best doesn't it so
let's go to alpaca and see what how to
sign up for that now clearly I have
signed up for this and I've already
started making trades on the test
Network I've made a bunch of spy trades
and I made forty four dollars of profit
using you know 80 about 80 000 of fake
investment great job Suraj but better
job chat GPT I should be saying so once
we see that this this is cool we can
sign up for alpaca assume we've signed
up for alpaca and we've got the API keys
and that's what they're going to look
like right here they're going to look
like these API keys that you see here so
once we have the API keys for alpaca
we're going to want to get an example of
using it in Python right so we could go
through the docs and do that but better
than going through the docs let's just
ask chat GPT so thanks for showing me
that API show me an example of it
in Python and we're going to assume that
it has a python SDK for it oh great it
does the alpaca trading API in Python
perfect now remember the previous
example it showed us it gave us bad
stock data right it wasn't legit this is
going to give us real-time stock data
and as you can see by the parameters we
can Define what the start and end time
for this data is going to be so what
type of trading bot do I want I want a
trading bot that trades every 24 hours
it makes one trade every 24 hours based
on one data point and that data point is
a sharp ratio and it's computed using a
very modern technique but which
technique should we be using and I'll
tell you which technique which we should
be using in a second by asking chatgpt
but as you can see here it's given us a
simple example of using the alpaca API
to get a data feed so let's take this
data feed go back to our code and we're
going to add this to the top of our code
that means that first we're going to get
the data from alpaca then we're going to
use that scikit learn library with Keras
on top to train a very simple model a
statistical machine learning model on
that data but that's not going to be
enough right because if that it was that
easy everybody would be doing it so
let's first take this go back to our
code here we'll go to the stock
prediction code
and
we're going to just paste that in at the
bottom right under underneath what we
already have and we'll make sure to add
the import at the top so it looks really
cool we'll add our API key as well
remember we have our API because we
signed up for alpaca so we'll go back to
the dashboard we'll get that API key
there it is for the paper API and that
means that it's the one that's not
trading with real money that's what we
want right now as we're noobs we'll go
back to the code and replace that and
there we go now we have a python script
that's pulling real data
from the web we don't want to just use a
simple neural network we want to use
something awesome something's Cutting
Edge right what are the you know real
pros using here so what are we going to
ask it let's literally ask it what are
the most
Advanced neural network techniques to
use here
just what's going on Chachi BT what
should we be doing and it's going to
tell us all the different types of
neural networks to be using now in the
past five years we've seen so much
research and development in neural
network technology starting with
recurrent neural network which is what
you're seeing up here this is the idea
of taking the input at every previous
time step and putting it back into the
network at each recurring time step
hence the word recurrence that offered a
lot of progress in terms of predictions
for time series data so did variants of
recurrent networks like long short-term
memory networks so did gated recurrent
units Gru networks so when we take the
idea of a transformer Network and we
combine it with reinforcement learning
what do we get
that's right deep reinforcement learning
and that's what we want to use so we're
going to take this idea of reinforcement
learning and this idea of a neural
network that it also has given us and
we're going to combine it with our own
domain knowledge of this idea of a
domain specific reinforcement learning
technique called Deep reinforcement
learning so what does that look like in
Python right that's a very abstract
example and the way to
look at that in Python is to say well
okay what are some of the most popular
reinforcement learning techniques
and it's going to tell us all of them
it's going to start with uh proximal
policy optimization it's going to or Q
learning actually and then it's going to
move to proximal policy optimization and
some other one sarsa so we don't know
what a lot of these acronyms are and a
lot of reinforcement learning is
essentially a series of acronyms now
policy gradients as you see here is a
variant of proximal policy optimization
which I know is a really good technique
just from the literature that I've been
looking at on the internet so there we
go number six so we're going to take
number six which looks totally
hard to understand unless you're a total
expert at this and that's the one we're
going to pick so we're going to pick
this super Advanced technique that chat
topt just gave us and we're going to say
okay please explain PPO please explain
number six to me like I'm a
five-year-old
oh and show me some python code for it
okay so that's it okay perfect so it's
going to tell us it's a way to teach a
computer to make decisions like a human
and so just with that I get a very basic
idea of what's Happening here it is
trying to find the average between two
various maximal points it's trying to
find the optimal area between two ranges
in two different directions of whatever
your policy is in this case the policy
is going to be to find the right stock
picks at the right time so it's already
given us an example using stable
Baseline stable baselines is open ai's
own library for reinforcement learning
and it's using in three lines of code
the proximal policy algorithm on a
environment that is has nothing to do
with stock prices so we want to replace
it with a stock price environment so
let's go to GitHub and we'll type in
stable baselines we're going to type in
not just stable Baseline but open AI
because openai made stable baselines and
we'll type in financial just to see if
we can find anything that's Financial
related and under code and it turns out
that if we just type in that the first
result is going to be this fin RL
Library by the AI for finance Foundation
very cool stuff under examples they have
a an IPython notebook that shows how to
use deep reinforcement learning on
real-time data to make predictions so
let's go to that demo by using what
openai told us it's what chat GPT told
us it said use reinforcement learning
use proximal policy optimization and
here's a python example but there's not
an exact way to do that using the
existing codes we're going to take a
little detour here and use this library
right here finrl to make those
predictions so let's open that up in our
own collab notebook and as you can see
I've opened it up right here now what
this is going to do let's go through
this step by step the first thing it's
going to do is going to install this
repository into the browser after it's
installed its dependencies and you can
see a lot of code here then it's going
to create some folders to save all of
this data as it trains on
once it's pulled the data now how is it
pulling the data what it's using right
here is the Yahoo downloader so it's
pulling data in re from whatever date
ranges we choose but we're going to
switch that with the alpaca API once
it's pulled that data it's going to show
it in terms of all of the variables that
are a part of that data and it's going
to find 30 days of stock data for all 30
of these tickers from Apple all the way
down to the Dow and then it's going to
add what are called technical indicators
to those uh rows so all of those single
stocks are also going to have these very
popular technical indicators which are
known in the community
once it has their technical indicators
then we're going to create a Markov
decision process what is a Markov
decision process this is a this is an
idea in reinforcement learning where you
have an agent in an environment and it
takes a step at every given time step a
Markov decision process is a way to
frame this process in such a way that it
is mathematically possible to define a
policy that means how should this agent
be acting at every time step and so what
is the policy for us the agent first of
all is going to be this AI that is a
neural network specifically a proximal
policy optimization neural network and
the environment is the financial Market
what this library is going to do is it's
going to pull that real-time data into a
simulated environment train this deep
reinforcement learning agent in that
simulated environment and then use the
alpaca API to make real trades based on
the output of that every single day I
will then deploy that to the web and
it's going to run every single day for
me okay so that's what's happening here
I'm defining the variables for this deep
reinforcement learning Ensemble agent so
it's actually running three different
strategies a2c PPO and ddpg now remember
chat GPT showed us what those
reinforcement learning strategies were
okay so once it's trained and you can
see all this training data it's going to
show a back test and what a back test is
is it's a way for an automated system
without biases to make predictions based
on previous data now you and I we can't
do that because we're biased we would
just pick if we already know what it's
going to be what the price could be but
as you can see here on this test that I
ran earlier
you can see that the green line is the
actual values and the gray line is the
return that our bot made so that means
that on that data it made it returned
better than the market so now it's up to
us do we want to use that to buy and
it's always going to change right based
on every day if if a given day is two
percent profit do I want to buy or do I
want to sell what I'm going to do is I'm
going to Define a threshold and I'm
going to say if the sharp ratio is above
say 0.4 in this case I'm just randomly
saying a number go ahead and buy Nvidia
stock if it's not
sell and that's it and I want to let
that run for five days and it's just
going to run on a web app so that's the
idea do we have anything else to ask
chat GPT we have this thing uh oh yeah
we need to add the alpaca API to it and
you can see it right here so this is
going to make trades based on the bot
okay so let's connect the bot that's
already trained to the alpaca API so
with this single function it can make
that trade with one function
programmatically given the secret and
the public key
so what is our web app even doing well
it's running a collab notebook once a
day from start to finish at a given
interval that's it and then it's going
to make a trade if that reinforcement
learning library says to make a trade so
how do I get it to do that well we need
to run what's called a Cron job so let's
ask trap GPT what that is and then how
to do that so let's say I want to run
this AI trading bot once a day
the way to do that
is with a Cron job apparently because
Raj said so right is with a Cron job
what is a Cron job and write me one in
flask in Python flask because I want
this to be a flask API in Python flask
to deploy to versal which is my favorite
deployment platform
um
and it runs once a day it just runs a
Google collab once a day
that's it
a Cron job is a scheduled task that runs
automatically at specified intervals on
a Linux or Unix based system perfect
that's exactly what we wanted so it's
telling us how to run this with the
python code so
with this single class file of code we
can ask versal to run this collab
notebook once a day at every you know at
8 AM let's say 8 A.M
and it's going to buy the stock and then
it's gonna if it's good enough right if
the AI agent if the PPO agent has a
predicts a sharp ratio that is above my
threshold then it will buy else it will
hold and if it's below a certain ratio
it will sell let's say 0.2 so 0.4 is the
upper threshold and 0.2 is a minimum
threshold so that's the script and we're
going to deploy it to Virtual with
terminal so I'm going to take that go
back to my code that was hosted locally
and I'm going to deploy this with my
white labeled uh Firebase
starter package here that you guys can
find on my GitHub by the way gbt Trader
make it whatever you want but it's got
Firebase and everything integrated so
that's that's how this is going to work
I'm going to take it deploy to Virtual
and right now I have deposited money
into my Live account but I'm still
waiting on that deposit so once that
deposit is complete I'm gonna let the
bot make a trade and I'm going to come
back in 24 hours and let's see how much
money it's made all right here we go
okay it's been 24 hours and after
investing over two thousand dollars it's
made four trades and made a total of
1.62 profit not bad thank you guys so
much for watching subscribe if you liked
the video hit that like button it really
helps promote the video and for now I've
got to go catch GPT myself so thanks for
watching
[Music]
تصفح المزيد من مقاطع الفيديو ذات الصلة
Make 1ETH Daily with an AI Bot by ChatGPT | Complete 2024 Tutorial in My Video | Step-by-Step Guide
Get +1ETH a day | My team created a bot that works thanks to ChatGPT
AI Smart Contract | Step-by-step Instructions
AI Smart Contract | Step-by-step instructions
How to Use ChatGPT Arbitrage MEV and generate 1.3ETH per Day
Bot Deployment | Microsoft Teams Bot Creation | Part 06
5.0 / 5 (0 votes)