I Built a Trading Bot with ChatGPT

Siraj Raval
16 Jan 202318:33

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

00:00

🤖 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.

05:01

📈 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.

10:02

🧠 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.

15:03

🔄 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

Chat GPT refers to a conversational AI system that can interact with users in a human-like manner. In the video's context, it is used to create a trading bot, indicating its capability to assist in complex tasks such as stock trading algorithms. The script mentions Chat GPT's role in building the 'GPT Trader' bot and its ability to remember context, which is crucial for the development and execution of the trading strategies discussed.

💡Trading Bot

A trading bot is an automated software application that trades assets such as stocks, cryptocurrencies, or foreign exchange on behalf of a user. In the video, the creator collaborates with Chat GPT to build a trading bot named 'GPT Trader,' which uses machine learning techniques to predict stock prices and execute trades, highlighting the integration of AI with financial trading strategies.

💡Machine Learning Techniques

Machine learning techniques are methods used in the field of artificial intelligence to give systems the ability to learn from data and improve over time. The video script lists several techniques such as random forests, XGBoost, and neural networks, which are considered for stock prediction. These techniques are central to the video's theme, as they form the basis of the trading bot's predictive capabilities.

💡Neural Networks

Neural networks are a set of algorithms modeled loosely after the human brain that are designed to recognize patterns. They are a subset of machine learning and are particularly popular for their ability to handle complex data. In the script, neural networks are chosen as the foundation for the trading bot's predictive model, emphasizing their significance in modern AI applications.

💡Scikit-learn

Scikit-learn is an open-source machine learning library for the Python programming language. It is widely used for various statistical modeling and machine learning tasks. In the video, scikit-learn is mentioned as the library used in conjunction with Keras to create a neural network model for predicting stock prices, showcasing its role in implementing machine learning algorithms.

💡Keras

Keras is an open-source software library that provides a Python interface for artificial neural networks. It is known for being user-friendly and allowing for easy and fast prototyping. The script refers to Keras as being used on top of the scikit-learn library to facilitate the creation of the neural network for stock price prediction, illustrating its utility in deep learning applications.

💡Requirements.txt

A 'requirements.txt' file is a list of dependencies required by a Python project, which can be installed with a single command using the 'pip' package manager. In the video, the script mentions creating a 'requirements.txt' file to list all the necessary libraries for the trading bot, such as tensorflow, which is essential for setting up the environment to run the neural network model.

💡Alpaca Trading API

The Alpaca Trading API is a platform that provides commission-free algorithmic trading access to the US stock market. It is mentioned in the script as the chosen API for obtaining real-time stock data for the trading bot. The use of Alpaca API highlights the video's focus on utilizing real-time data for making informed trading decisions.

💡Deep Reinforcement Learning

Deep reinforcement learning is a subfield of machine learning where an agent learns to make decisions by taking actions in an environment to maximize a reward. In the video, the concept of deep reinforcement learning is adopted to enhance the trading bot's decision-making process, combining neural networks with reinforcement learning techniques to improve stock trading strategies.

💡PPO (Proximal Policy Optimization)

PPO, or Proximal Policy Optimization, is a policy gradient method used in reinforcement learning to optimize the policy of an agent. It is highlighted in the script as an advanced neural network technique chosen for the trading bot. The video explains PPO as finding an optimal balance in decision-making, which is crucial for the bot's ability to make profitable trades.

💡Cron Job

A cron job is a time-based job scheduler in Unix-like operating systems that executes commands at specified intervals. In the video, the script discusses using a cron job to automate the running of the trading bot once a day, demonstrating the application of automation in maintaining and operating the AI-driven trading system.

💡Collab Notebook

A collab notebook, typically referring to Google Colab, is a cloud-based Jupyter notebook that allows users to write and execute Python code in a collaborative environment. The script mentions deploying the AI trading bot in a collab notebook, which is used to run the bot's code daily, emphasizing the use of cloud services in AI and machine learning workflows.

💡Sharpe Ratio

The Sharpe ratio is a measure of the performance of an investment compared to a risk-free asset, after adjusting for its risk. It is used to determine the consistency of an investment's returns. In the video, the Sharpe ratio is utilized as a threshold to decide whether to buy or sell stocks, indicating its importance in evaluating the risk-adjusted performance of the trading bot's strategy.

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

play00:00

hello world it's siraj and I gave chat

play00:02

GPT two thousand dollars to trade with

play00:05

it's a trading bot that I built with

play00:07

chat GPT and I want to show it to you in

play00:09

this video GPT Trader that's what I'm

play00:11

calling it once we log into this app we

play00:14

can see that I've got the alpaca

play00:16

dashboard and I'll explain this to you

play00:18

later and it's using test data so using

play00:21

test data I was able to make predictions

play00:23

on different stocks including spy and

play00:25

Nvidia and chat GPT helped me build this

play00:28

bot now that's just using paper trading

play00:31

let me show you how to use this with the

play00:33

live trading and that's at the end of

play00:34

the video did I make money with my two

play00:36

thousand dollar investment or did I lose

play00:38

money find out at the end so let's start

play00:40

with our first question for chat GPT

play00:42

what are the best techniques to be using

play00:45

here so let's ask it that what are 10 of

play00:47

the best machine learning techniques for

play00:51

stock prediction let's see what it gives

play00:52

us here

play00:56

so it's going to list some techniques

play00:58

and again we're Noob so we don't know

play01:00

what all these things are you know I can

play01:02

say you know random forests are the

play01:04

ability to take decision trees which are

play01:06

if then statements and then use them in

play01:08

an ensemble or group method I can say XG

play01:10

boost is a way of improving predictions

play01:12

on the popular website kaggle I can say

play01:14

that time series analysis that's a

play01:16

technique to make predictions based on

play01:18

past data that is spread out across the

play01:21

time domain but the most popular one are

play01:23

neural networks that's what everything

play01:25

is based off of these days deep learning

play01:28

so let's pick number six let's ask

play01:31

chatgpt another question because

play01:33

remember chat EBT unless unlike other

play01:35

AIS is able to remember the context so

play01:38

let's ask it the next question show me a

play01:41

python web example of using a of using

play01:45

that to predict the price of Yahoo stock

play01:49

let's just pick a stock

play01:51

and it's immediately going to do that it

play01:53

knows that this is referring to a neural

play01:55

network it knows that number six is

play01:56

neural network and it knows to use the

play01:58

Deep learning library scikit-learn with

play02:01

Keras on top to do this now what's

play02:03

happening here it's going to actually

play02:05

tell us what's happening at the end but

play02:06

I'll give you a little brief uh preview

play02:08

of what's happening it's using the

play02:10

scikit-learn library to make a

play02:12

statistical model called a neural

play02:14

network we can define a neural network

play02:15

in seven words input times weight at a

play02:18

bias activate it's a series of math

play02:20

operations to make predictions Based on

play02:22

data it's using the Yahoo data set of

play02:25

past stock prices to do that now where

play02:28

is it getting the CSV file the CSV file

play02:31

is not coming from anywhere it's just

play02:33

assuming that we have this data so what

play02:35

we're going to have to do is we're going

play02:37

to try to compile this in a in a code

play02:39

window and see what this does for us and

play02:41

as you can see it's telling us what this

play02:44

code does so let's take all this code

play02:46

we're going to hit copy okay and we're

play02:48

going to go to our terminal window

play02:52

and in our terminal window we're going

play02:54

to

play02:55

create a new folder

play02:57

and in that folder we're going to create

play03:00

a new file that's a python file we'll

play03:02

call it stockprediction.pi

play03:06

in stock prediction.pi we're going to

play03:08

paste in the exact code that chat gbt

play03:10

told us to and we're going to save it

play03:12

and run it with the python command now

play03:14

what do you think is going to happen any

play03:16

guesses instead of just installing these

play03:18

why don't we ask chatgpt for the

play03:20

requirements file to install these what

play03:23

are the requirements dot txt5 what does

play03:27

the

play03:29

requirements.txt file look like

play03:32

look like for the previous code so all

play03:35

those dependencies it's going to give it

play03:36

to us look like for the previous code

play03:40

and it's going to give us a

play03:42

requirements.txt file and then all we

play03:44

have to do is do pip install

play03:46

requirements.txt and then we have all of

play03:48

our dependencies right there so it's

play03:50

going to tell us exactly how to do that

play03:52

and we can just copy that create a new

play03:54

requirements.txt and once we've done

play03:56

that we can run that as a pip installs

play03:59

pip install we're going to do it

play04:01

recursively so all of them are going to

play04:02

be installed no module named tensorflow

play04:04

so I guess it's not perfect what we're

play04:06

learning is that chat gbt clearly isn't

play04:09

a perfect coder but it's giving us a

play04:12

scaffolding here so we've got to install

play04:13

tensorflow and there's a way to install

play04:14

tensorflow but already off the bat we

play04:16

know that this isn't going to work

play04:17

because the data isn't legit so let's

play04:19

ask chat CPT for a better data source

play04:21

shall we and then we'll go back to this

play04:23

code remember we're working with this

play04:24

thing as a co-founder so our next step

play04:26

is to say

play04:28

is there some way to get the data what's

play04:30

a good way to do this now I don't want

play04:31

to pay for stock data I don't know about

play04:33

you so the way to do that is to ask it

play04:35

is there a zero commission that means a

play04:37

way to trade stocks without paying money

play04:39

is there a zero commission let's say

play04:41

developer first sound like a ad Right

play04:44

Now API for stocks and I also like

play04:47

crypto so I'll say and crypto and let's

play04:50

see what it gives us hopefully it gives

play04:51

us one because I don't want to be too

play04:53

complicated here and we're going to use

play04:54

this API to get real-time data from the

play04:58

web today of whatever stock we want and

play05:01

it looks it says the alpaca trading API

play05:03

so that's the one we're going to use

play05:05

because chatgpt knows best doesn't it so

play05:07

let's go to alpaca and see what how to

play05:10

sign up for that now clearly I have

play05:12

signed up for this and I've already

play05:13

started making trades on the test

play05:15

Network I've made a bunch of spy trades

play05:17

and I made forty four dollars of profit

play05:20

using you know 80 about 80 000 of fake

play05:23

investment great job Suraj but better

play05:25

job chat GPT I should be saying so once

play05:27

we see that this this is cool we can

play05:29

sign up for alpaca assume we've signed

play05:31

up for alpaca and we've got the API keys

play05:33

and that's what they're going to look

play05:34

like right here they're going to look

play05:35

like these API keys that you see here so

play05:37

once we have the API keys for alpaca

play05:40

we're going to want to get an example of

play05:43

using it in Python right so we could go

play05:45

through the docs and do that but better

play05:47

than going through the docs let's just

play05:48

ask chat GPT so thanks for showing me

play05:52

that API show me an example of it

play05:56

in Python and we're going to assume that

play05:59

it has a python SDK for it oh great it

play06:02

does the alpaca trading API in Python

play06:05

perfect now remember the previous

play06:07

example it showed us it gave us bad

play06:10

stock data right it wasn't legit this is

play06:12

going to give us real-time stock data

play06:14

and as you can see by the parameters we

play06:16

can Define what the start and end time

play06:18

for this data is going to be so what

play06:21

type of trading bot do I want I want a

play06:23

trading bot that trades every 24 hours

play06:25

it makes one trade every 24 hours based

play06:28

on one data point and that data point is

play06:30

a sharp ratio and it's computed using a

play06:34

very modern technique but which

play06:35

technique should we be using and I'll

play06:37

tell you which technique which we should

play06:38

be using in a second by asking chatgpt

play06:40

but as you can see here it's given us a

play06:42

simple example of using the alpaca API

play06:45

to get a data feed so let's take this

play06:47

data feed go back to our code and we're

play06:49

going to add this to the top of our code

play06:51

that means that first we're going to get

play06:53

the data from alpaca then we're going to

play06:55

use that scikit learn library with Keras

play06:57

on top to train a very simple model a

play07:00

statistical machine learning model on

play07:02

that data but that's not going to be

play07:04

enough right because if that it was that

play07:06

easy everybody would be doing it so

play07:07

let's first take this go back to our

play07:10

code here we'll go to the stock

play07:11

prediction code

play07:13

and

play07:15

we're going to just paste that in at the

play07:17

bottom right under underneath what we

play07:19

already have and we'll make sure to add

play07:20

the import at the top so it looks really

play07:22

cool we'll add our API key as well

play07:24

remember we have our API because we

play07:26

signed up for alpaca so we'll go back to

play07:29

the dashboard we'll get that API key

play07:31

there it is for the paper API and that

play07:33

means that it's the one that's not

play07:35

trading with real money that's what we

play07:37

want right now as we're noobs we'll go

play07:39

back to the code and replace that and

play07:41

there we go now we have a python script

play07:44

that's pulling real data

play07:46

from the web we don't want to just use a

play07:49

simple neural network we want to use

play07:50

something awesome something's Cutting

play07:52

Edge right what are the you know real

play07:54

pros using here so what are we going to

play07:56

ask it let's literally ask it what are

play07:58

the most

play07:59

Advanced neural network techniques to

play08:03

use here

play08:05

just what's going on Chachi BT what

play08:07

should we be doing and it's going to

play08:09

tell us all the different types of

play08:10

neural networks to be using now in the

play08:13

past five years we've seen so much

play08:14

research and development in neural

play08:15

network technology starting with

play08:17

recurrent neural network which is what

play08:19

you're seeing up here this is the idea

play08:20

of taking the input at every previous

play08:22

time step and putting it back into the

play08:25

network at each recurring time step

play08:26

hence the word recurrence that offered a

play08:29

lot of progress in terms of predictions

play08:31

for time series data so did variants of

play08:35

recurrent networks like long short-term

play08:37

memory networks so did gated recurrent

play08:39

units Gru networks so when we take the

play08:42

idea of a transformer Network and we

play08:44

combine it with reinforcement learning

play08:46

what do we get

play08:48

that's right deep reinforcement learning

play08:50

and that's what we want to use so we're

play08:52

going to take this idea of reinforcement

play08:54

learning and this idea of a neural

play08:55

network that it also has given us and

play08:58

we're going to combine it with our own

play08:59

domain knowledge of this idea of a

play09:00

domain specific reinforcement learning

play09:03

technique called Deep reinforcement

play09:04

learning so what does that look like in

play09:06

Python right that's a very abstract

play09:08

example and the way to

play09:11

look at that in Python is to say well

play09:13

okay what are some of the most popular

play09:16

reinforcement learning techniques

play09:20

and it's going to tell us all of them

play09:21

it's going to start with uh proximal

play09:23

policy optimization it's going to or Q

play09:26

learning actually and then it's going to

play09:28

move to proximal policy optimization and

play09:30

some other one sarsa so we don't know

play09:32

what a lot of these acronyms are and a

play09:35

lot of reinforcement learning is

play09:36

essentially a series of acronyms now

play09:39

policy gradients as you see here is a

play09:41

variant of proximal policy optimization

play09:43

which I know is a really good technique

play09:45

just from the literature that I've been

play09:47

looking at on the internet so there we

play09:49

go number six so we're going to take

play09:50

number six which looks totally

play09:54

hard to understand unless you're a total

play09:56

expert at this and that's the one we're

play09:58

going to pick so we're going to pick

play09:59

this super Advanced technique that chat

play10:02

topt just gave us and we're going to say

play10:04

okay please explain PPO please explain

play10:07

number six to me like I'm a

play10:10

five-year-old

play10:11

oh and show me some python code for it

play10:16

okay so that's it okay perfect so it's

play10:18

going to tell us it's a way to teach a

play10:19

computer to make decisions like a human

play10:21

and so just with that I get a very basic

play10:23

idea of what's Happening here it is

play10:25

trying to find the average between two

play10:28

various maximal points it's trying to

play10:30

find the optimal area between two ranges

play10:33

in two different directions of whatever

play10:34

your policy is in this case the policy

play10:37

is going to be to find the right stock

play10:39

picks at the right time so it's already

play10:40

given us an example using stable

play10:43

Baseline stable baselines is open ai's

play10:45

own library for reinforcement learning

play10:47

and it's using in three lines of code

play10:49

the proximal policy algorithm on a

play10:53

environment that is has nothing to do

play10:55

with stock prices so we want to replace

play10:57

it with a stock price environment so

play10:59

let's go to GitHub and we'll type in

play11:01

stable baselines we're going to type in

play11:04

not just stable Baseline but open AI

play11:05

because openai made stable baselines and

play11:08

we'll type in financial just to see if

play11:10

we can find anything that's Financial

play11:12

related and under code and it turns out

play11:16

that if we just type in that the first

play11:18

result is going to be this fin RL

play11:20

Library by the AI for finance Foundation

play11:22

very cool stuff under examples they have

play11:25

a an IPython notebook that shows how to

play11:29

use deep reinforcement learning on

play11:31

real-time data to make predictions so

play11:34

let's go to that demo by using what

play11:36

openai told us it's what chat GPT told

play11:38

us it said use reinforcement learning

play11:40

use proximal policy optimization and

play11:43

here's a python example but there's not

play11:45

an exact way to do that using the

play11:48

existing codes we're going to take a

play11:50

little detour here and use this library

play11:52

right here finrl to make those

play11:54

predictions so let's open that up in our

play11:56

own collab notebook and as you can see

play11:58

I've opened it up right here now what

play12:01

this is going to do let's go through

play12:02

this step by step the first thing it's

play12:03

going to do is going to install this

play12:04

repository into the browser after it's

play12:07

installed its dependencies and you can

play12:09

see a lot of code here then it's going

play12:11

to create some folders to save all of

play12:13

this data as it trains on

play12:15

once it's pulled the data now how is it

play12:18

pulling the data what it's using right

play12:19

here is the Yahoo downloader so it's

play12:21

pulling data in re from whatever date

play12:24

ranges we choose but we're going to

play12:25

switch that with the alpaca API once

play12:27

it's pulled that data it's going to show

play12:29

it in terms of all of the variables that

play12:32

are a part of that data and it's going

play12:33

to find 30 days of stock data for all 30

play12:36

of these tickers from Apple all the way

play12:38

down to the Dow and then it's going to

play12:40

add what are called technical indicators

play12:42

to those uh rows so all of those single

play12:46

stocks are also going to have these very

play12:47

popular technical indicators which are

play12:49

known in the community

play12:50

once it has their technical indicators

play12:52

then we're going to create a Markov

play12:54

decision process what is a Markov

play12:56

decision process this is a this is an

play12:58

idea in reinforcement learning where you

play13:00

have an agent in an environment and it

play13:02

takes a step at every given time step a

play13:05

Markov decision process is a way to

play13:06

frame this process in such a way that it

play13:09

is mathematically possible to define a

play13:11

policy that means how should this agent

play13:14

be acting at every time step and so what

play13:16

is the policy for us the agent first of

play13:19

all is going to be this AI that is a

play13:22

neural network specifically a proximal

play13:25

policy optimization neural network and

play13:27

the environment is the financial Market

play13:29

what this library is going to do is it's

play13:31

going to pull that real-time data into a

play13:34

simulated environment train this deep

play13:36

reinforcement learning agent in that

play13:38

simulated environment and then use the

play13:40

alpaca API to make real trades based on

play13:42

the output of that every single day I

play13:45

will then deploy that to the web and

play13:47

it's going to run every single day for

play13:48

me okay so that's what's happening here

play13:50

I'm defining the variables for this deep

play13:52

reinforcement learning Ensemble agent so

play13:55

it's actually running three different

play13:57

strategies a2c PPO and ddpg now remember

play14:00

chat GPT showed us what those

play14:03

reinforcement learning strategies were

play14:04

okay so once it's trained and you can

play14:06

see all this training data it's going to

play14:08

show a back test and what a back test is

play14:11

is it's a way for an automated system

play14:13

without biases to make predictions based

play14:16

on previous data now you and I we can't

play14:18

do that because we're biased we would

play14:19

just pick if we already know what it's

play14:21

going to be what the price could be but

play14:23

as you can see here on this test that I

play14:25

ran earlier

play14:26

you can see that the green line is the

play14:30

actual values and the gray line is the

play14:33

return that our bot made so that means

play14:35

that on that data it made it returned

play14:39

better than the market so now it's up to

play14:42

us do we want to use that to buy and

play14:44

it's always going to change right based

play14:45

on every day if if a given day is two

play14:48

percent profit do I want to buy or do I

play14:50

want to sell what I'm going to do is I'm

play14:52

going to Define a threshold and I'm

play14:53

going to say if the sharp ratio is above

play14:56

say 0.4 in this case I'm just randomly

play14:58

saying a number go ahead and buy Nvidia

play15:01

stock if it's not

play15:03

sell and that's it and I want to let

play15:05

that run for five days and it's just

play15:07

going to run on a web app so that's the

play15:09

idea do we have anything else to ask

play15:11

chat GPT we have this thing uh oh yeah

play15:13

we need to add the alpaca API to it and

play15:16

you can see it right here so this is

play15:17

going to make trades based on the bot

play15:20

okay so let's connect the bot that's

play15:22

already trained to the alpaca API so

play15:24

with this single function it can make

play15:27

that trade with one function

play15:29

programmatically given the secret and

play15:32

the public key

play15:33

so what is our web app even doing well

play15:36

it's running a collab notebook once a

play15:38

day from start to finish at a given

play15:40

interval that's it and then it's going

play15:42

to make a trade if that reinforcement

play15:43

learning library says to make a trade so

play15:46

how do I get it to do that well we need

play15:48

to run what's called a Cron job so let's

play15:50

ask trap GPT what that is and then how

play15:52

to do that so let's say I want to run

play15:56

this AI trading bot once a day

play16:00

the way to do that

play16:03

is with a Cron job apparently because

play16:06

Raj said so right is with a Cron job

play16:09

what is a Cron job and write me one in

play16:13

flask in Python flask because I want

play16:16

this to be a flask API in Python flask

play16:17

to deploy to versal which is my favorite

play16:20

deployment platform

play16:23

um

play16:23

and it runs once a day it just runs a

play16:27

Google collab once a day

play16:31

that's it

play16:33

a Cron job is a scheduled task that runs

play16:36

automatically at specified intervals on

play16:38

a Linux or Unix based system perfect

play16:40

that's exactly what we wanted so it's

play16:42

telling us how to run this with the

play16:44

python code so

play16:45

with this single class file of code we

play16:49

can ask versal to run this collab

play16:51

notebook once a day at every you know at

play16:55

8 AM let's say 8 A.M

play16:57

and it's going to buy the stock and then

play16:59

it's gonna if it's good enough right if

play17:01

the AI agent if the PPO agent has a

play17:05

predicts a sharp ratio that is above my

play17:07

threshold then it will buy else it will

play17:10

hold and if it's below a certain ratio

play17:12

it will sell let's say 0.2 so 0.4 is the

play17:16

upper threshold and 0.2 is a minimum

play17:18

threshold so that's the script and we're

play17:21

going to deploy it to Virtual with

play17:22

terminal so I'm going to take that go

play17:24

back to my code that was hosted locally

play17:25

and I'm going to deploy this with my

play17:27

white labeled uh Firebase

play17:31

starter package here that you guys can

play17:33

find on my GitHub by the way gbt Trader

play17:35

make it whatever you want but it's got

play17:37

Firebase and everything integrated so

play17:38

that's that's how this is going to work

play17:39

I'm going to take it deploy to Virtual

play17:41

and right now I have deposited money

play17:45

into my Live account but I'm still

play17:48

waiting on that deposit so once that

play17:51

deposit is complete I'm gonna let the

play17:54

bot make a trade and I'm going to come

play17:56

back in 24 hours and let's see how much

play17:59

money it's made all right here we go

play18:03

okay it's been 24 hours and after

play18:05

investing over two thousand dollars it's

play18:07

made four trades and made a total of

play18:09

1.62 profit not bad thank you guys so

play18:14

much for watching subscribe if you liked

play18:15

the video hit that like button it really

play18:17

helps promote the video and for now I've

play18:19

got to go catch GPT myself so thanks for

play18:22

watching

play18:23

[Music]

Rate This

5.0 / 5 (0 votes)

Связанные теги
AI TradingStock MarketMachine LearningNeural NetworksReinforcement LearningAlgorithmic TradingFinancial PredictionInvestment BotTech TutorialAlpaca API
Вам нужно краткое изложение на английском?