I Built a Trading Bot with ChatGPT

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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]
Info
Channel: Siraj Raval
Views: 888,522
Rating: undefined out of 5
Keywords: siraj raval, chatGPT, make money, stock prediction
Id: fhBw3j_O9LE
Channel Id: undefined
Length: 18min 33sec (1113 seconds)
Published: Mon Jan 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.