Learn how to trade stocks automatically with JavaScript code. In this tutorial, we use Alpaca and GPT-3 to create an algorithm in Node.js capable of making real stock trades. Follow me on Twitter to track performance https://twitter.com/fireship_dev

#ai #stocks #javascript

🔗 Resources

Source Code https://github.com/fireship-io/cramer-algo-trader
OpenAI GPT-3 https://openai.com
Alpaca Trading API https://alpaca.markets/
Algo Trading Explained https://www.investopedia.com/articles/active-trading/101014/basics-algorithmic-trading-concepts-and-examples.asp

📚 Chapters

00:00 Inverse Cramer Strategy
01:05 Algo Trading Architecture
02:37 Initial Setup
02:54 Use OpenAI GPT-3 in Node.js
04:33 Scrape Tweets with Puppeteer
05:36 Make Trades with Alpaca
07:04 CRON Job Function
07:46 Apology

🔥 Get More Content - Upgrade to PRO

Upgrade to Fireship PRO at https://fireship.io/pro
Use code lORhwXd2 for 25% off your first payment. 

🎨 My Editor Settings

- Atom One Dark 
- vscode-icons
- Fira Code Font

🔖 Topics Covered

- Algo trading with javascript
- How to trade stocks with code
- Jim Cramer roast
- How to use GPT3? 
- How to use Alpaca API?
- Firebase Cloud Functions Cron Job
- API for stock trading
2024-02-04T13:46:29.694Z
nova-2
2-general-nova
2024-01-09.29447
30089e05-99d1-4376-b32e-c263170674af
fc3d0615-853f-4e7f-b77e-a3630d5cbb27
ab71f3090b2f81bddc14e16557d91faf6ded8e8be3fe8ce049a5980175183274
67875a7f-c9c4-48a0-aa55-5bdb8a91c34a
deprecated
I've always wanted to make money with Stonks, but every time I listen to advice from guys on TV like Jim Cramer, I lose a ton of money.
Like, a few months ago, he told me to buy this technology stock called Peloton, but failed to mention it's just an exercise bike with an iPad strapped to it.
Lost 80% on that trade.
Then last week, he tipped me off to a stock called Netflix.
That one's not going too well either.
And it's gone.
Now I've got a second mortgage on my horse, but thankfully, I've learned the perfect investment strategy.
Listen to Jim, then do the exact opposite of what he says.
So in this video, we're going to build an algo trading app with JavaScript that can make real trades in the stock market based on an ironclad AI powered algorithm.
Quick disclaimer, this video does contain investment advice, as long as your strategy is to be poor.
All I ask in return is that you hit the like button and subscribe.
The inverse Cramer strategy is not unique, but the problem is that I don't think there's a public API that provides Jim Cramer picks.
I had to sell my TV, so I can't watch a show anymore, which means we need to go with plan b.
We'll use the world's premier AI model, GPT 3, and ask it if Jim Cramer would buy or sell whatever stocks we're interested in.
GPT 3 writes pretty much all my code these days with GitHub Copilot, so it should really have no problem figuring out stock picks.
Let's first take a look at the architecture and tech stack of this application.
1st, we'll need a back end server, and for that, I'm using Firebase Cloud Functions.
I was gonna use Cloudflare because I felt bad about how far their stock has fallen after it was recommended by Jim Cramer, but I think Firebase is the best fit for this project.
We'll have a function running on a CronJob during market hours that when invoked will make a call to the GPT 3 API.
Generative pre trained transformer 3 is one of the most sophisticated and terrifying rhythms out there.
We can take a list of stocks and simply ask it if Jim Cramer would buy them or not, and it will respond with a simple yes or no.
And we could even provide it more context like Cramer's recent tweets and articles to nudge it in the right direction.
If the answer is sell, then we'll buy.
If the answer is buy, then we'll short sell.
Now to trade a stock, we'll need a broker.
And for that, I'm using the alpaca API, which is basically Robinhood for developers.
Also, I'm deeply saddened and dismayed to report that the Robinhood guys are no longer billionaires.
Their stonk also tanked big time after being recommended by Jim Cramer.
Alpaca is really cool because it's commission free trading, and it also provides real time market data that you can use for free.
And if you don't wanna lose your own money, it supports paper trading in a fake simulated environment.
For this tutorial, I'm using $8,000 of my own money.
But once we get capitalized with a few 100,000,000, I'm going to open it up to a retard I mean, retail investors.
Or if you wanna do it yourself, all the source code is on GitHub.
In all seriousness, there's a lot to learn from this project.
You'll learn how to run background jobs in the cloud on a timer, how to trade stocks and crypto with JavaScript, how to interact with the GPT 3 algorithm with JavaScript, and we'll even build a web scraper to extract dynamic content from sites like Twitter using puppeteer.
And we'll do it all in under 100 lines of code.
To get started, open up the terminal and use fire based tools to initialize a new Firebase functions project.
You'll need a Firebase account to do this, although this code will work on any Node.
Js server.
Now let's cd into the functions directory and installer dependencies for OpenAI, alpaca, and puppeteer.
Now at this point, you'll need an API key from OpenAI to use GPT 3.
It's a paid service, but I believe they offer you an $18 credit to play around, and then pricing is pay as you go after that.
You'll first wanna go to the playground and try it out.
It will likely rule over humans in the not so distant future, so be nice to it.
You can ask it questions like, is Jim Cramer good at stonks?
It's gonna simp for him, but it should at least be able to tell us whether or not he would buy a certain stock.
Now from there, we'll need to get our API key.
It's a secret value, so make sure not to expose it publicly.
Now in our Firebase code, we can open up the index dot JS file, and import the SDK there.
The first thing we'll need to do is authenticate using the organization and API key from OpenAI.
I have my keys set up as environment variables in Firebase, however, you could just paste them in directly if you want.
Again, just make sure they don't end up in a public git repo.
From there, we can instantiate OpenAI with the configuration.
To start things off, I'm going to write all the code in an HTTP function that will allow us to test it in the browser.
To make a call to Open AI, we can use that get completion method.
There are many other things it can do, like answer questions, classify text, and things like that, but I found the completion method to be most useful for this project.
And keep in mind, this is a really dumb way to pick stocks.
GPT 3 is really cool, but it's not going to magically pick good stocks for you.
The most important option is the prompt.
The AI will try to complete whatever comes next.
And then, you have a variety of other options you might pass here to tweak the result.
OpenAI will respond with an object with the result, so let's go ahead and send that back as the response from the function.
Now open up the terminal and run npm run serve to run the functions locally.
That'll give you a link to the browser with the function URL.
You should get a response with the data from GPT t 3.
Most importantly, it has the stocks that it thinks Jim Cramer will sell.
That's pretty cool, but it would help if we could give gpt 3 the proper context, like Jim Cramer's recent tweets.
For that, we're going to need to build a web scraper with a tool called puppeteer, which is basically a headless browser that you can run on the server.
Not sure how legal this is, and this is only legal advice if you wanna go to prison.
We'll first import puppeteer, then create an async function called scrape.
What we'll do is launch a Chromium browser on the server, and then we'll tell it to go to Jim Cramer's Twitter page.
The tweets aren't server rendered, so we're going to tell it to wait until the network is idle and then have it wait for an additional 3 seconds after that as well.
At that point, the tweet should be loaded.
And we can even take a screenshot of the page just to verify.
Now from there, we can use page evaluate to interact with the actual content on the web page.
Inside this function, we'll have access to the document where we can grab different elements in the DOM.
In this case, we'll just grab the entire body text and let GPT 3 deal with that.
You could improve this by writing code that only grabs tweets that talk about actual ticker symbols.
Now from there, we can close the browser and return the tweets.
Let's go back into our HTTP function, then we'll call the scrape function we just defined, and interpolate that into the prompt that we passed to OpenAI.
In theory, gpt 3 will try to complete Jim Cramer's next tweet for him.
And now we're ready to trade stocks.
1st, you'll need to sign up for an alpaca account and deposit some money into it, or you might use that paper trading feature.
It's popular with algo traders, where stock trading is entirely automated I did with code.
And then you'll see the option to generate an API key.
And remember, this is also a secret value, and you really don't want to expose it because then someone could trade in your account.
Now if we go back to our JavaScript code, we can import the alpaca SDK and add our API keys to it.
You can also set the paper option to true if you wanna use fake money.
After we get the OpenAI response, I'm going to create a variable called stocks to buy that uses a little bit of regex to grab the ticker symbols from whatever GPT returns us.
And now we can do all kinds of stuff with Alpaca.
The first thing we might wanna do is get our account, which has important information, like our current buying power.
After that, buying a stock is as easy as creating an order.
It'll be looking for a ticker symbol, that's the stock that you want to buy or sell, then you can specify a quantity, or it so supports fractional shares if you want to buy a specific dollar amount, and that's what we'll do here.
We'll take our current buying power and use 90% of it to buy a stock.
Doc.
If you wanna be extra poor, Alpaca also supports trading on the margin where you can leverage your buying power further.
In this example, we'll buy the stock and then we'll make it a market order.
That tells it to buy the stock at the current available price, and the trade will only be valid for the current day.
Let's go ahead and send the order object as the response.
Now go to the browser and refresh the page.
When you do that, it's going to make an actual trade in the stock market.
You should get a confirmation number back and also see the trade in your alpaca account.
Congratulations.
You are now an algo trader.
Now the final thing I wanna show you is how we can run this code every day with a cron job.
A cron job is just a schedule for doing work in the background on the server.
We can easily set one and up with Firebase using a pubsub function.
This function, I'm going to give a memory allocation of 4 gigabytes because puppeteer uses a lot of memory.
Then we'll set up a schedule that runs at 10 AM every day, Monday through Friday, and then it's really important to set the time zone for the markets that you wanna trade in.
Now from there, we can simply copy the code we just wrote and paste it in here directly.
As one extra touch, I'm also going to cancel all orders and close out all my positions whenever this function runs.
That means every day I'll sell all my stocks and then buy a new one based on Jim's recommendations.
Now if you're ready to become poor, run Firebase deploy to push your code to the cloud.
Now if if you wanna track my fund's performance, follow me on Twitter because I plan on posting regular updates there.
To wrap things up, I want to apologize to Jim Cramer.
I have nothing against the guy, and the reality is that nobody, except for maybe Nancy Pelosi's husband can't predict the stock market.
Cramer himself said, we are all wrong so often that it amazes me that we can have any conviction at all over the direction of things to but we must.
Again, this is only investment advice if you wanna be poor.
Thanks for watching, and I will see you in the next one.

Speaker 0: I've always wanted to make money with Stonks, but every time I listen to advice from guys on TV like Jim Cramer, I lose a ton of money. Like, a few months ago, he told me to buy this technology stock called Peloton, but failed to mention it's just an exercise bike with an iPad strapped to it. Lost 80% on that trade. Then last week, he tipped me off to a stock called Netflix. That one's not going too well either.

And it's gone. Now I've got a second mortgage on my horse, but thankfully, I've learned the perfect investment strategy. Listen to Jim, then do the exact opposite of what he says. So in this video, we're going to build an algo trading app with JavaScript that can make real trades in the stock market based on an ironclad AI powered algorithm. Quick disclaimer, this video does contain investment advice, as long as your strategy is to be poor.

All I ask in return is that you hit the like button and subscribe. The inverse Cramer strategy is not unique, but the problem is that I don't think there's a public API that provides Jim Cramer picks. I had to sell my TV, so I can't watch a show anymore, which means we need to go with plan b. We'll use the world's premier AI model, GPT 3, and ask it if Jim Cramer would buy or sell whatever stocks we're interested in. GPT 3 writes pretty much all my code these days with GitHub Copilot, so it should really have no problem figuring out stock picks.

Let's first take a look at the architecture and tech stack of this application. 1st, we'll need a back end server, and for that, I'm using Firebase Cloud Functions. I was gonna use Cloudflare because I felt bad about how far their stock has fallen after it was recommended by Jim Cramer, but I think Firebase is the best fit for this project. We'll have a function running on a CronJob during market hours that when invoked will make a call to the GPT 3 API. Generative pre trained transformer 3 is one of the most sophisticated and terrifying rhythms out there.

We can take a list of stocks and simply ask it if Jim Cramer would buy them or not, and it will respond with a simple yes or no. And we could even provide it more context like Cramer's recent tweets and articles to nudge it in the right direction. If the answer is sell, then we'll buy. If the answer is buy, then we'll short sell. Now to trade a stock, we'll need a broker.

And for that, I'm using the alpaca API, which is basically Robinhood for developers. Also, I'm deeply saddened and dismayed to report that the Robinhood guys are no longer billionaires. Their stonk also tanked big time after being recommended by Jim Cramer. Alpaca is really cool because it's commission free trading, and it also provides real time market data that you can use for free. And if you don't wanna lose your own money, it supports paper trading in a fake simulated environment.

For this tutorial, I'm using $8,000 of my own money. But once we get capitalized with a few 100,000,000, I'm going to open it up to a retard I mean, retail investors. Or if you wanna do it yourself, all the source code is on GitHub. In all seriousness, there's a lot to learn from this project. You'll learn how to run background jobs in the cloud on a timer, how to trade stocks and crypto with JavaScript, how to interact with the GPT 3 algorithm with JavaScript, and we'll even build a web scraper to extract dynamic content from sites like Twitter using puppeteer.

And we'll do it all in under 100 lines of code. To get started, open up the terminal and use fire based tools to initialize a new Firebase functions project. You'll need a Firebase account to do this, although this code will work on any Node. Js server. Now let's cd into the functions directory and installer dependencies for OpenAI, alpaca, and puppeteer.

Now at this point, you'll need an API key from OpenAI to use GPT 3. It's a paid service, but I believe they offer you an $18 credit to play around, and then pricing is pay as you go after that. You'll first wanna go to the playground and try it out. It will likely rule over humans in the not so distant future, so be nice to it. You can ask it questions like, is Jim Cramer good at stonks?

It's gonna simp for him, but it should at least be able to tell us whether or not he would buy a certain stock. Now from there, we'll need to get our API key. It's a secret value, so make sure not to expose it publicly. Now in our Firebase code, we can open up the index dot JS file, and import the SDK there. The first thing we'll need to do is authenticate using the organization and API key from OpenAI.

I have my keys set up as environment variables in Firebase, however, you could just paste them in directly if you want. Again, just make sure they don't end up in a public git repo. From there, we can instantiate OpenAI with the configuration. To start things off, I'm going to write all the code in an HTTP function that will allow us to test it in the browser. To make a call to Open AI, we can use that get completion method.

There are many other things it can do, like answer questions, classify text, and things like that, but I found the completion method to be most useful for this project. And keep in mind, this is a really dumb way to pick stocks. GPT 3 is really cool, but it's not going to magically pick good stocks for you. The most important option is the prompt. The AI will try to complete whatever comes next.

And then, you have a variety of other options you might pass here to tweak the result. OpenAI will respond with an object with the result, so let's go ahead and send that back as the response from the function. Now open up the terminal and run npm run serve to run the functions locally. That'll give you a link to the browser with the function URL. You should get a response with the data from GPT t 3.

Most importantly, it has the stocks that it thinks Jim Cramer will sell. That's pretty cool, but it would help if we could give gpt 3 the proper context, like Jim Cramer's recent tweets. For that, we're going to need to build a web scraper with a tool called puppeteer, which is basically a headless browser that you can run on the server. Not sure how legal this is, and this is only legal advice if you wanna go to prison. We'll first import puppeteer, then create an async function called scrape.

What we'll do is launch a Chromium browser on the server, and then we'll tell it to go to Jim Cramer's Twitter page. The tweets aren't server rendered, so we're going to tell it to wait until the network is idle and then have it wait for an additional 3 seconds after that as well. At that point, the tweet should be loaded. And we can even take a screenshot of the page just to verify. Now from there, we can use page evaluate to interact with the actual content on the web page.

Inside this function, we'll have access to the document where we can grab different elements in the DOM. In this case, we'll just grab the entire body text and let GPT 3 deal with that. You could improve this by writing code that only grabs tweets that talk about actual ticker symbols. Now from there, we can close the browser and return the tweets. Let's go back into our HTTP function, then we'll call the scrape function we just defined, and interpolate that into the prompt that we passed to OpenAI.

In theory, gpt 3 will try to complete Jim Cramer's next tweet for him. And now we're ready to trade stocks. 1st, you'll need to sign up for an alpaca account and deposit some money into it, or you might use that paper trading feature. It's popular with algo traders, where stock trading is entirely automated I did with code. And then you'll see the option to generate an API key.

And remember, this is also a secret value, and you really don't want to expose it because then someone could trade in your account. Now if we go back to our JavaScript code, we can import the alpaca SDK and add our API keys to it. You can also set the paper option to true if you wanna use fake money. After we get the OpenAI response, I'm going to create a variable called stocks to buy that uses a little bit of regex to grab the ticker symbols from whatever GPT returns us. And now we can do all kinds of stuff with Alpaca.

The first thing we might wanna do is get our account, which has important information, like our current buying power. After that, buying a stock is as easy as creating an order. It'll be looking for a ticker symbol, that's the stock that you want to buy or sell, then you can specify a quantity, or it so supports fractional shares if you want to buy a specific dollar amount, and that's what we'll do here. We'll take our current buying power and use 90% of it to buy a stock. Doc.

If you wanna be extra poor, Alpaca also supports trading on the margin where you can leverage your buying power further. In this example, we'll buy the stock and then we'll make it a market order. That tells it to buy the stock at the current available price, and the trade will only be valid for the current day. Let's go ahead and send the order object as the response. Now go to the browser and refresh the page.

When you do that, it's going to make an actual trade in the stock market. You should get a confirmation number back and also see the trade in your alpaca account. Congratulations. You are now an algo trader. Now the final thing I wanna show you is how we can run this code every day with a cron job.

A cron job is just a schedule for doing work in the background on the server. We can easily set one and up with Firebase using a pubsub function. This function, I'm going to give a memory allocation of 4 gigabytes because puppeteer uses a lot of memory. Then we'll set up a schedule that runs at 10 AM every day, Monday through Friday, and then it's really important to set the time zone for the markets that you wanna trade in. Now from there, we can simply copy the code we just wrote and paste it in here directly.

As one extra touch, I'm also going to cancel all orders and close out all my positions whenever this function runs. That means every day I'll sell all my stocks and then buy a new one based on Jim's recommendations. Now if you're ready to become poor, run Firebase deploy to push your code to the cloud. Now if if you wanna track my fund's performance, follow me on Twitter because I plan on posting regular updates there. To wrap things up, I want to apologize to Jim Cramer.

I have nothing against the guy, and the reality is that nobody, except for maybe Nancy Pelosi's husband can't predict the stock market. Cramer himself said, we are all wrong so often that it amazes me that we can have any conviction at all over the direction of things to but we must. Again, this is only investment advice if you wanna be poor. Thanks for watching, and I will see you in the next one.
I've always wanted to make money with Stonks, but every time I listen to advice from guys on TV like Jim Cramer, I lose a ton of money. Like, a few months ago, he told me to buy this technology stock called Peloton, but failed to mention it's just an exercise bike with an iPad strapped to it.
Lost 80% on that trade. Then last week, he tipped me off to a stock called Netflix. That one's not going too well either. And it's gone. Now I've got a second mortgage on my horse, but thankfully, I've learned the perfect investment strategy. Listen to Jim, then do the exact opposite of what he says. So in this video, we're going to build an algo trading app with JavaScript that can make real trades in the stock market based on an ironclad AI powered algorithm. Quick disclaimer, this video does contain investment advice, as long as your strategy is to be poor. All I ask in return is that you hit the like button and subscribe. The inverse Cramer strategy is not unique, but the problem is that I don't think there's a public API that provides Jim Cramer picks. I had to sell my TV, so I can't watch a show anymore, which means we need to go with plan b. We'll use the world's premier AI model, GPT 3, and ask it if Jim Cramer would buy or sell whatever stocks we're interested in. GPT 3 writes pretty much all my code these days with GitHub Copilot, so it should really have no problem figuring out stock picks. Let's first take a look at the architecture and tech stack of this application.
investing
finance
trade
artificial intelligence
stock market
mortgages
1st, we'll need a back end server, and for that, I'm using Firebase Cloud Functions. I was gonna use Cloudflare because I felt bad about how far their stock has fallen after it was recommended by Jim Cramer, but I think Firebase is the best fit for this project. We'll have a function running on a CronJob during market hours that when invoked will make a call to the GPT 3 API. Generative pre trained transformer 3 is one of the most sophisticated and terrifying rhythms out there.
We can take a list of stocks and simply ask it if Jim Cramer would buy them or not, and it will respond with a simple yes or no. And we could even provide it more context like Cramer's recent tweets and articles to nudge it in the right direction. If the answer is sell, then we'll buy. If the answer is buy, then we'll short sell. Now to trade a stock, we'll need a broker. And for that, I'm using the alpaca API, which is basically Robinhood for developers. Also, I'm deeply saddened and dismayed to report that the Robinhood guys are no longer billionaires. Their stonk also tanked big time after being recommended by Jim Cramer.
trade
stock market
marketing
commerce
sales
Alpaca is really cool because it's commission free trading, and it also provides real time market data that you can use for free. And if you don't wanna lose your own money, it supports paper trading in a fake simulated environment.
For this tutorial, I'm using $8,000 of my own money. But once we get capitalized with a few 100,000,000, I'm going to open it up to a retard I mean, retail investors. Or if you wanna do it yourself, all the source code is on GitHub. In all seriousness, there's a lot to learn from this project. You'll learn how to run background jobs in the cloud on a timer, how to trade stocks and crypto with JavaScript, how to interact with the GPT 3 algorithm with JavaScript, and we'll even build a web scraper to extract dynamic content from sites like Twitter using puppeteer.
finance
software
investing
And we'll do it all in under 100 lines of code. To get started, open up the terminal and use fire based tools to initialize a new Firebase functions project. You'll need a Firebase account to do this, although this code will work on any Node. Js server. Now let's cd into the functions directory and installer dependencies for OpenAI, alpaca, and puppeteer. Now at this point, you'll need an API key from OpenAI to use GPT 3. It's a paid service, but I believe they offer you an $18 credit to play around, and then pricing is pay as you go after that.
You'll first wanna go to the playground and try it out. It will likely rule over humans in the not so distant future, so be nice to it. You can ask it questions like, is Jim Cramer good at stonks? It's gonna simp for him, but it should at least be able to tell us whether or not he would buy a certain stock. Now from there, we'll need to get our API key. It's a secret value, so make sure not to expose it publicly.
cryptocurrency
machine learning
Now in our Firebase code, we can open up the index dot JS file, and import the SDK there. The first thing we'll need to do is authenticate using the organization and API key from OpenAI. I have my keys set up as environment variables in Firebase, however, you could just paste them in directly if you want. Again, just make sure they don't end up in a public git repo. From there, we can instantiate OpenAI with the configuration. To start things off, I'm going to write all the code in an HTTP function that will allow us to test it in the browser.
To make a call to Open AI, we can use that get completion method. There are many other things it can do, like answer questions, classify text, and things like that, but I found the completion method to be most useful for this project. And keep in mind, this is a really dumb way to pick stocks. GPT 3 is really cool, but it's not going to magically pick good stocks for you. The most important option is the prompt. The AI will try to complete whatever comes next. And then, you have a variety of other options you might pass here to tweak the result.
artificial intelligence
investing
stock market
OpenAI will respond with an object with the result, so let's go ahead and send that back as the response from the function. Now open up the terminal and run npm run serve to run the functions locally.
That'll give you a link to the browser with the function URL. You should get a response with the data from GPT t 3. Most importantly, it has the stocks that it thinks Jim Cramer will sell. That's pretty cool, but it would help if we could give gpt 3 the proper context, like Jim Cramer's recent tweets. For that, we're going to need to build a web scraper with a tool called puppeteer, which is basically a headless browser that you can run on the server.
software
stock market
Not sure how legal this is, and this is only legal advice if you wanna go to prison. We'll first import puppeteer, then create an async function called scrape.
What we'll do is launch a Chromium browser on the server, and then we'll tell it to go to Jim Cramer's Twitter page. The tweets aren't server rendered, so we're going to tell it to wait until the network is idle and then have it wait for an additional 3 seconds after that as well. At that point, the tweet should be loaded. And we can even take a screenshot of the page just to verify. Now from there, we can use page evaluate to interact with the actual content on the web page. Inside this function, we'll have access to the document where we can grab different elements in the DOM.
twitter
internet
In this case, we'll just grab the entire body text and let GPT 3 deal with that.
You could improve this by writing code that only grabs tweets that talk about actual ticker symbols. Now from there, we can close the browser and return the tweets. Let's go back into our HTTP function, then we'll call the scrape function we just defined, and interpolate that into the prompt that we passed to OpenAI. In theory, gpt 3 will try to complete Jim Cramer's next tweet for him. And now we're ready to trade stocks. 1st, you'll need to sign up for an alpaca account and deposit some money into it, or you might use that paper trading feature. It's popular with algo traders, where stock trading is entirely automated I did with code. And then you'll see the option to generate an API key. And remember, this is also a secret value, and you really don't want to expose it because then someone could trade in your account.
twitter
cryptocurrency
trade
finance
commerce
Now if we go back to our JavaScript code, we can import the alpaca SDK and add our API keys to it.
You can also set the paper option to true if you wanna use fake money. After we get the OpenAI response, I'm going to create a variable called stocks to buy that uses a little bit of regex to grab the ticker symbols from whatever GPT returns us. And now we can do all kinds of stuff with Alpaca.
finance
The first thing we might wanna do is get our account, which has important information, like our current buying power.
After that, buying a stock is as easy as creating an order. It'll be looking for a ticker symbol, that's the stock that you want to buy or sell, then you can specify a quantity, or it so supports fractional shares if you want to buy a specific dollar amount, and that's what we'll do here. We'll take our current buying power and use 90% of it to buy a stock. Doc. If you wanna be extra poor, Alpaca also supports trading on the margin where you can leverage your buying power further. In this example, we'll buy the stock and then we'll make it a market order. That tells it to buy the stock at the current available price, and the trade will only be valid for the current day. Let's go ahead and send the order object as the response. Now go to the browser and refresh the page. When you do that, it's going to make an actual trade in the stock market. You should get a confirmation number back and also see the trade in your alpaca account.
trade
stock market
policy
finance
commerce
sales
Congratulations. You are now an algo trader. Now the final thing I wanna show you is how we can run this code every day with a cron job. A cron job is just a schedule for doing work in the background on the server. We can easily set one and up with Firebase using a pubsub function. This function, I'm going to give a memory allocation of 4 gigabytes because puppeteer uses a lot of memory. Then we'll set up a schedule that runs at 10 AM every day, Monday through Friday, and then it's really important to set the time zone for the markets that you wanna trade in. Now from there, we can simply copy the code we just wrote and paste it in here directly. As one extra touch, I'm also going to cancel all orders and close out all my positions whenever this function runs.
That means every day I'll sell all my stocks and then buy a new one based on Jim's recommendations. Now if you're ready to become poor, run Firebase deploy to push your code to the cloud. Now if if you wanna track my fund's performance, follow me on Twitter because I plan on posting regular updates there. To wrap things up, I want to apologize to Jim Cramer. I have nothing against the guy, and the reality is that nobody, except for maybe Nancy Pelosi's husband can't predict the stock market. Cramer himself said, we are all wrong so often that it amazes me that we can have any conviction at all over the direction of things to but we must. Again, this is only investment advice if you wanna be poor. Thanks for watching, and I will see you in the next one.
investing
stock market
loans
finance
economics
insurance
I've always wanted to make money with Stonks, but every time I listen to advice from guys on TV like Jim Cramer, I lose a ton of money. Like, a few months ago, he told me to buy this technology stock called Peloton, but failed to mention it's just an exercise bike with an iPad strapped to it. Lost 80% on that trade. Then last week, he tipped me off to a stock called Netflix. That one's not going too well either. And it's gone. Now I've got a second mortgage on my horse, but thankfully, I've learned the perfect investment strategy. Listen to Jim, then do the exact opposite of what he says. So in this video, we're going to build an algo trading app with JavaScript that can make real trades in the stock market based on an ironclad AI powered algorithm. Quick disclaimer, this video does contain investment advice, as long as your strategy is to be poor. All I ask in return is that you hit the like button and subscribe. The inverse Cramer strategy is not unique, but the problem is that I don't think there's a public API that provides Jim Cramer picks. I had to sell my TV, so I can't watch a show anymore, which means we need to go with plan b. We'll use the world's premier AI model, GPT 3, and ask it if Jim Cramer would buy or sell whatever stocks we're interested in. GPT 3 writes pretty much all my code these days with GitHub Copilot, so it should really have no problem figuring out stock picks. Let's first take a look at the architecture and tech stack of this application. 1st, we'll need a back end server, and for that, I'm using Firebase Cloud Functions. I was gonna use Cloudflare because I felt bad about how far their stock has fallen after it was recommended by Jim Cramer, but I think Firebase is the best fit for this project. We'll have a function running on a CronJob during market hours that when invoked will make a call to the GPT 3 API. Generative pre trained transformer 3 is one of the most sophisticated and terrifying rhythms out there. We can take a list of stocks and simply ask it if Jim Cramer would buy them or not, and it will respond with a simple yes or no. And we could even provide it more context like Cramer's recent tweets and articles to nudge it in the right direction. If the answer is sell, then we'll buy. If the answer is buy, then we'll short sell. Now to trade a stock, we'll need a broker. And for that, I'm using the alpaca API, which is basically Robinhood for developers. Also, I'm deeply saddened and dismayed to report that the Robinhood guys are no longer billionaires. Their stonk also tanked big time after being recommended by Jim Cramer. Alpaca is really cool because it's commission free trading, and it also provides real time market data that you can use for free. And if you don't wanna lose your own money, it supports paper trading in a fake simulated environment. For this tutorial, I'm using $8,000 of my own money. But once we get capitalized with a few 100,000,000, I'm going to open it up to a retard I mean, retail investors. Or if you wanna do it yourself, all the source code is on GitHub. In all seriousness, there's a lot to learn from this project. You'll learn how to run background jobs in the cloud on a timer, how to trade stocks and crypto with JavaScript, how to interact with the GPT 3 algorithm with JavaScript, and we'll even build a web scraper to extract dynamic content from sites like Twitter using puppeteer. And we'll do it all in under 100 lines of code. To get started, open up the terminal and use fire based tools to initialize a new Firebase functions project. You'll need a Firebase account to do this, although this code will work on any Node. Js server. Now let's cd into the functions directory and installer dependencies for OpenAI, alpaca, and puppeteer. Now at this point, you'll need an API key from OpenAI to use GPT 3. It's a paid service, but I believe they offer you an $18 credit to play around, and then pricing is pay as you go after that. You'll first wanna go to the playground and try it out. It will likely rule over humans in the not so distant future, so be nice to it. You can ask it questions like, is Jim Cramer good at stonks? It's gonna simp for him, but it should at least be able to tell us whether or not he would buy a certain stock. Now from there, we'll need to get our API key. It's a secret value, so make sure not to expose it publicly. Now in our Firebase code, we can open up the index dot JS file, and import the SDK there. The first thing we'll need to do is authenticate using the organization and API key from OpenAI. I have my keys set up as environment variables in Firebase, however, you could just paste them in directly if you want. Again, just make sure they don't end up in a public git repo. From there, we can instantiate OpenAI with the configuration. To start things off, I'm going to write all the code in an HTTP function that will allow us to test it in the browser. To make a call to Open AI, we can use that get completion method. There are many other things it can do, like answer questions, classify text, and things like that, but I found the completion method to be most useful for this project. And keep in mind, this is a really dumb way to pick stocks. GPT 3 is really cool, but it's not going to magically pick good stocks for you. The most important option is the prompt. The AI will try to complete whatever comes next. And then, you have a variety of other options you might pass here to tweak the result. OpenAI will respond with an object with the result, so let's go ahead and send that back as the response from the function. Now open up the terminal and run npm run serve to run the functions locally. That'll give you a link to the browser with the function URL. You should get a response with the data from GPT t 3. Most importantly, it has the stocks that it thinks Jim Cramer will sell. That's pretty cool, but it would help if we could give gpt 3 the proper context, like Jim Cramer's recent tweets. For that, we're going to need to build a web scraper with a tool called puppeteer, which is basically a headless browser that you can run on the server. Not sure how legal this is, and this is only legal advice if you wanna go to prison. We'll first import puppeteer, then create an async function called scrape. What we'll do is launch a Chromium browser on the server, and then we'll tell it to go to Jim Cramer's Twitter page. The tweets aren't server rendered, so we're going to tell it to wait until the network is idle and then have it wait for an additional 3 seconds after that as well. At that point, the tweet should be loaded. And we can even take a screenshot of the page just to verify. Now from there, we can use page evaluate to interact with the actual content on the web page. Inside this function, we'll have access to the document where we can grab different elements in the DOM. In this case, we'll just grab the entire body text and let GPT 3 deal with that. You could improve this by writing code that only grabs tweets that talk about actual ticker symbols. Now from there, we can close the browser and return the tweets. Let's go back into our HTTP function, then we'll call the scrape function we just defined, and interpolate that into the prompt that we passed to OpenAI. In theory, gpt 3 will try to complete Jim Cramer's next tweet for him. And now we're ready to trade stocks. 1st, you'll need to sign up for an alpaca account and deposit some money into it, or you might use that paper trading feature. It's popular with algo traders, where stock trading is entirely automated I did with code. And then you'll see the option to generate an API key. And remember, this is also a secret value, and you really don't want to expose it because then someone could trade in your account. Now if we go back to our JavaScript code, we can import the alpaca SDK and add our API keys to it. You can also set the paper option to true if you wanna use fake money. After we get the OpenAI response, I'm going to create a variable called stocks to buy that uses a little bit of regex to grab the ticker symbols from whatever GPT returns us. And now we can do all kinds of stuff with Alpaca. The first thing we might wanna do is get our account, which has important information, like our current buying power. After that, buying a stock is as easy as creating an order. It'll be looking for a ticker symbol, that's the stock that you want to buy or sell, then you can specify a quantity, or it so supports fractional shares if you want to buy a specific dollar amount, and that's what we'll do here. We'll take our current buying power and use 90% of it to buy a stock. Doc. If you wanna be extra poor, Alpaca also supports trading on the margin where you can leverage your buying power further. In this example, we'll buy the stock and then we'll make it a market order. That tells it to buy the stock at the current available price, and the trade will only be valid for the current day. Let's go ahead and send the order object as the response. Now go to the browser and refresh the page. When you do that, it's going to make an actual trade in the stock market. You should get a confirmation number back and also see the trade in your alpaca account. Congratulations. You are now an algo trader. Now the final thing I wanna show you is how we can run this code every day with a cron job. A cron job is just a schedule for doing work in the background on the server. We can easily set one and up with Firebase using a pubsub function. This function, I'm going to give a memory allocation of 4 gigabytes because puppeteer uses a lot of memory. Then we'll set up a schedule that runs at 10 AM every day, Monday through Friday, and then it's really important to set the time zone for the markets that you wanna trade in. Now from there, we can simply copy the code we just wrote and paste it in here directly. As one extra touch, I'm also going to cancel all orders and close out all my positions whenever this function runs. That means every day I'll sell all my stocks and then buy a new one based on Jim's recommendations. Now if you're ready to become poor, run Firebase deploy to push your code to the cloud. Now if if you wanna track my fund's performance, follow me on Twitter because I plan on posting regular updates there. To wrap things up, I want to apologize to Jim Cramer. I have nothing against the guy, and the reality is that nobody, except for maybe Nancy Pelosi's husband can't predict the stock market. Cramer himself said, we are all wrong so often that it amazes me that we can have any conviction at all over the direction of things to but we must. Again, this is only investment advice if you wanna be poor. Thanks for watching, and I will see you in the next one.
I've
i've
always
always
wanted
wanted
to
to
make
make
money
money
with
with
Stonks,
stonks
but
but
every
every
time
time
I
i
listen
listen
to
to
advice
advice
from
from
guys
guys
on
on
TV
tv
like
like
Jim
jim
Cramer,
cramer
I
i
lose
lose
a
a
ton
ton
of
of
money.
money
Like,
like
a
a
few
few
months
months
ago,
ago
he
he
told
told
me
me
to
to
buy
buy
this
this
technology
technology
stock
stock
called
called
Peloton,
peloton
but
but
failed
failed
to
to
mention
mention
it's
it's
just
just
an
an
exercise
exercise
bike
bike
with
with
an
an
iPad
ipad
strapped
strapped
to
to
it.
it
Lost
lost
80%
80%
on
on
that
that
trade.
trade
Then
then
last
last
week,
week
he
he
tipped
tipped
me
me
off
off
to
to
a
a
stock
stock
called
called
Netflix.
netflix
That
that
one's
one's
not
not
going
going
too
too
well
well
either.
either
And
and
it's
it's
gone.
gone
Now
now
I've
i've
got
got
a
a
second
second
mortgage
mortgage
on
on
my
my
horse,
horse
but
but
thankfully,
thankfully
I've
i've
learned
learned
the
the
perfect
perfect
investment
investment
strategy.
strategy
Listen
listen
to
to
Jim,
jim
then
then
do
do
the
the
exact
exact
opposite
opposite
of
of
what
what
he
he
says.
says
So
so
in
in
this
this
video,
video
we're
we're
going
going
to
to
build
build
an
an
algo
algo
trading
trading
app
app
with
with
JavaScript
javascript
that
that
can
can
make
make
real
real
trades
trades
in
in
the
the
stock
stock
market
market
based
based
on
on
an
an
ironclad
ironclad
AI
ai
powered
powered
algorithm.
algorithm
Quick
quick
disclaimer,
disclaimer
this
this
video
video
does
does
contain
contain
investment
investment
advice,
advice
as
as
long
long
as
as
your
your
strategy
strategy
is
is
to
to
be
be
poor.
poor
All
all
I
i
ask
ask
in
in
return
return
is
is
that
that
you
you
hit
hit
the
the
like
like
button
button
and
and
subscribe.
subscribe
The
the
inverse
inverse
Cramer
cramer
strategy
strategy
is
is
not
not
unique,
unique
but
but
the
the
problem
problem
is
is
that
that
I
i
don't
don't
think
think
there's
there's
a
a
public
public
API
api
that
that
provides
provides
Jim
jim
Cramer
cramer
picks.
picks
I
i
had
had
to
to
sell
sell
my
my
TV,
tv
so
so
I
i
can't
can't
watch
watch
a
a
show
show
anymore,
anymore
which
which
means
means
we
we
need
need
to
to
go
go
with
with
plan
plan
b.
b
We'll
we'll
use
use
the
the
world's
world's
premier
premier
AI
ai
model,
model
GPT
gpt
3,
3
and
and
ask
ask
it
it
if
if
Jim
jim
Cramer
cramer
would
would
buy
buy
or
or
sell
sell
whatever
whatever
stocks
stocks
we're
we're
interested
interested
in.
in
GPT
gpt
3
3
writes
writes
pretty
pretty
much
much
all
all
my
my
code
code
these
these
days
days
with
with
GitHub
github
Copilot,
copilot
so
so
it
it
should
should
really
really
have
have
no
no
problem
problem
figuring
figuring
out
out
stock
stock
picks.
picks
Let's
let's
first
first
take
take
a
a
look
look
at
at
the
the
architecture
architecture
and
and
tech
tech
stack
stack
of
of
this
this
application.
application
1st,
1st
we'll
we'll
need
need
a
a
back
back
end
end
server,
server
and
and
for
for
that,
that
I'm
i'm
using
using
Firebase
firebase
Cloud
cloud
Functions.
functions
I
i
was
was
gonna
gonna
use
use
Cloudflare
cloudflare
because
because
I
i
felt
felt
bad
bad
about
about
how
how
far
far
their
their
stock
stock
has
has
fallen
fallen
after
after
it
it
was
was
recommended
recommended
by
by
Jim
jim
Cramer,
cramer
but
but
I
i
think
think
Firebase
firebase
is
is
the
the
best
best
fit
fit
for
for
this
this
project.
project
We'll
we'll
have
have
a
a
function
function
running
running
on
on
a
a
CronJob
cronjob
during
during
market
market
hours
hours
that
that
when
when
invoked
invoked
will
will
make
make
a
a
call
call
to
to
the
the
GPT
gpt
3
3
API.
api
Generative
generative
pre
pre
trained
trained
transformer
transformer
3
3
is
is
one
one
of
of
the
the
most
most
sophisticated
sophisticated
and
and
terrifying
terrifying
rhythms
rhythms
out
out
there.
there
We
we
can
can
take
take
a
a
list
list
of
of
stocks
stocks
and
and
simply
simply
ask
ask
it
it
if
if
Jim
jim
Cramer
cramer
would
would
buy
buy
them
them
or
or
not,
not
and
and
it
it
will
will
respond
respond
with
with
a
a
simple
simple
yes
yes
or
or
no.
no
And
and
we
we
could
could
even
even
provide
provide
it
it
more
more
context
context
like
like
Cramer's
cramer's
recent
recent
tweets
tweets
and
and
articles
articles
to
to
nudge
nudge
it
it
in
in
the
the
right
right
direction.
direction
If
if
the
the
answer
answer
is
is
sell,
sell
then
then
we'll
we'll
buy.
buy
If
if
the
the
answer
answer
is
is
buy,
buy
then
then
we'll
we'll
short
short
sell.
sell
Now
now
to
to
trade
trade
a
a
stock,
stock
we'll
we'll
need
need
a
a
broker.
broker
And
and
for
for
that,
that
I'm
i'm
using
using
the
the
alpaca
alpaca
API,
api
which
which
is
is
basically
basically
Robinhood
robinhood
for
for
developers.
developers
Also,
also
I'm
i'm
deeply
deeply
saddened
saddened
and
and
dismayed
dismayed
to
to
report
report
that
that
the
the
Robinhood
robinhood
guys
guys
are
are
no
no
longer
longer
billionaires.
billionaires
Their
their
stonk
stonk
also
also
tanked
tanked
big
big
time
time
after
after
being
being
recommended
recommended
by
by
Jim
jim
Cramer.
cramer
Alpaca
alpaca
is
is
really
really
cool
cool
because
because
it's
it's
commission
commission
free
free
trading,
trading
and
and
it
it
also
also
provides
provides
real
real
time
time
market
market
data
data
that
that
you
you
can
can
use
use
for
for
free.
free
And
and
if
if
you
you
don't
don't
wanna
wanna
lose
lose
your
your
own
own
money,
money
it
it
supports
supports
paper
paper
trading
trading
in
in
a
a
fake
fake
simulated
simulated
environment.
environment
For
for
this
this
tutorial,
tutorial
I'm
i'm
using
using
$8,000
$8,000
of
of
my
my
own
own
money.
money
But
but
once
once
we
we
get
get
capitalized
capitalized
with
with
a
a
few
few
100,000,000,
100,000,000
I'm
i'm
going
going
to
to
open
open
it
it
up
up
to
to
a
a
retard
retard
I
i
mean,
mean
retail
retail
investors.
investors
Or
or
if
if
you
you
wanna
wanna
do
do
it
it
yourself,
yourself
all
all
the
the
source
source
code
code
is
is
on
on
GitHub.
github
In
in
all
all
seriousness,
seriousness
there's
there's
a
a
lot
lot
to
to
learn
learn
from
from
this
this
project.
project
You'll
you'll
learn
learn
how
how
to
to
run
run
background
background
jobs
jobs
in
in
the
the
cloud
cloud
on
on
a
a
timer,
timer
how
how
to
to
trade
trade
stocks
stocks
and
and
crypto
crypto
with
with
JavaScript,
javascript
how
how
to
to
interact
interact
with
with
the
the
GPT
gpt
3
3
algorithm
algorithm
with
with
JavaScript,
javascript
and
and
we'll
we'll
even
even
build
build
a
a
web
web
scraper
scraper
to
to
extract
extract
dynamic
dynamic
content
content
from
from
sites
sites
like
like
Twitter
twitter
using
using
puppeteer.
puppeteer
And
and
we'll
we'll
do
do
it
it
all
all
in
in
under
under
100
100
lines
lines
of
of
code.
code
To
to
get
get
started,
started
open
open
up
up
the
the
terminal
terminal
and
and
use
use
fire
fire
based
based
tools
tools
to
to
initialize
initialize
a
a
new
new
Firebase
firebase
functions
functions
project.
project
You'll
you'll
need
need
a
a
Firebase
firebase
account
account
to
to
do
do
this,
this
although
although
this
this
code
code
will
will
work
work
on
on
any
any
Node.
node
Js
js
server.
server
Now
now
let's
let's
cd
cd
into
into
the
the
functions
functions
directory
directory
and
and
installer
installer
dependencies
dependencies
for
for
OpenAI,
openai
alpaca,
alpaca
and
and
puppeteer.
puppeteer
Now
now
at
at
this
this
point,
point
you'll
you'll
need
need
an
an
API
api
key
key
from
from
OpenAI
openai
to
to
use
use
GPT
gpt
3.
3
It's
it's
a
a
paid
paid
service,
service
but
but
I
i
believe
believe
they
they
offer
offer
you
you
an
an
$18
$18
credit
credit
to
to
play
play
around,
around
and
and
then
then
pricing
pricing
is
is
pay
pay
as
as
you
you
go
go
after
after
that.
that
You'll
you'll
first
first
wanna
wanna
go
go
to
to
the
the
playground
playground
and
and
try
try
it
it
out.
out
It
it
will
will
likely
likely
rule
rule
over
over
humans
humans
in
in
the
the
not
not
so
so
distant
distant
future,
future
so
so
be
be
nice
nice
to
to
it.
it
You
you
can
can
ask
ask
it
it
questions
questions
like,
like
is
is
Jim
jim
Cramer
cramer
good
good
at
at
stonks?
stonks
It's
it's
gonna
gonna
simp
simp
for
for
him,
him
but
but
it
it
should
should
at
at
least
least
be
be
able
able
to
to
tell
tell
us
us
whether
whether
or
or
not
not
he
he
would
would
buy
buy
a
a
certain
certain
stock.
stock
Now
now
from
from
there,
there
we'll
we'll
need
need
to
to
get
get
our
our
API
api
key.
key
It's
it's
a
a
secret
secret
value,
value
so
so
make
make
sure
sure
not
not
to
to
expose
expose
it
it
publicly.
publicly
Now
now
in
in
our
our
Firebase
firebase
code,
code
we
we
can
can
open
open
up
up
the
the
index
index
dot
dot
JS
js
file,
file
and
and
import
import
the
the
SDK
sdk
there.
there
The
the
first
first
thing
thing
we'll
we'll
need
need
to
to
do
do
is
is
authenticate
authenticate
using
using
the
the
organization
organization
and
and
API
api
key
key
from
from
OpenAI.
openai
I
i
have
have
my
my
keys
keys
set
set
up
up
as
as
environment
environment
variables
variables
in
in
Firebase,
firebase
however,
however
you
you
could
could
just
just
paste
paste
them
them
in
in
directly
directly
if
if
you
you
want.
want
Again,
again
just
just
make
make
sure
sure
they
they
don't
don't
end
end
up
up
in
in
a
a
public
public
git
git
repo.
repo
From
from
there,
there
we
we
can
can
instantiate
instantiate
OpenAI
openai
with
with
the
the
configuration.
configuration
To
to
start
start
things
things
off,
off
I'm
i'm
going
going
to
to
write
write
all
all
the
the
code
code
in
in
an
an
HTTP
http
function
function
that
that
will
will
allow
allow
us
us
to
to
test
test
it
it
in
in
the
the
browser.
browser
To
to
make
make
a
a
call
call
to
to
Open
open
AI,
ai
we
we
can
can
use
use
that
that
get
get
completion
completion
method.
method
There
there
are
are
many
many
other
other
things
things
it
it
can
can
do,
do
like
like
answer
answer
questions,
questions
classify
classify
text,
text
and
and
things
things
like
like
that,
that
but
but
I
i
found
found
the
the
completion
completion
method
method
to
to
be
be
most
most
useful
useful
for
for
this
this
project.
project
And
and
keep
keep
in
in
mind,
mind
this
this
is
is
a
a
really
really
dumb
dumb
way
way
to
to
pick
pick
stocks.
stocks
GPT
gpt
3
3
is
is
really
really
cool,
cool
but
but
it's
it's
not
not
going
going
to
to
magically
magically
pick
pick
good
good
stocks
stocks
for
for
you.
you
The
the
most
most
important
important
option
option
is
is
the
the
prompt.
prompt
The
the
AI
ai
will
will
try
try
to
to
complete
complete
whatever
whatever
comes
comes
next.
next
And
and
then,
then
you
you
have
have
a
a
variety
variety
of
of
other
other
options
options
you
you
might
might
pass
pass
here
here
to
to
tweak
tweak
the
the
result.
result
OpenAI
openai
will
will
respond
respond
with
with
an
an
object
object
with
with
the
the
result,
result
so
so
let's
let's
go
go
ahead
ahead
and
and
send
send
that
that
back
back
as
as
the
the
response
response
from
from
the
the
function.
function
Now
now
open
open
up
up
the
the
terminal
terminal
and
and
run
run
npm
npm
run
run
serve
serve
to
to
run
run
the
the
functions
functions
locally.
locally
That'll
that'll
give
give
you
you
a
a
link
link
to
to
the
the
browser
browser
with
with
the
the
function
function
URL.
url
You
you
should
should
get
get
a
a
response
response
with
with
the
the
data
data
from
from
GPT
gpt
t
t
3.
3
Most
most
importantly,
importantly
it
it
has
has
the
the
stocks
stocks
that
that
it
it
thinks
thinks
Jim
jim
Cramer
cramer
will
will
sell.
sell
That's
that's
pretty
pretty
cool,
cool
but
but
it
it
would
would
help
help
if
if
we
we
could
could
give
give
gpt
gpt
3
3
the
the
proper
proper
context,
context
like
like
Jim
jim
Cramer's
cramer's
recent
recent
tweets.
tweets
For
for
that,
that
we're
we're
going
going
to
to
need
need
to
to
build
build
a
a
web
web
scraper
scraper
with
with
a
a
tool
tool
called
called
puppeteer,
puppeteer
which
which
is
is
basically
basically
a
a
headless
headless
browser
browser
that
that
you
you
can
can
run
run
on
on
the
the
server.
server
Not
not
sure
sure
how
how
legal
legal
this
this
is,
is
and
and
this
this
is
is
only
only
legal
legal
advice
advice
if
if
you
you
wanna
wanna
go
go
to
to
prison.
prison
We'll
we'll
first
first
import
import
puppeteer,
puppeteer
then
then
create
create
an
an
async
async
function
function
called
called
scrape.
scrape
What
what
we'll
we'll
do
do
is
is
launch
launch
a
a
Chromium
chromium
browser
browser
on
on
the
the
server,
server
and
and
then
then
we'll
we'll
tell
tell
it
it
to
to
go
go
to
to
Jim
jim
Cramer's
cramer's
Twitter
twitter
page.
page
The
the
tweets
tweets
aren't
aren't
server
server
rendered,
rendered
so
so
we're
we're
going
going
to
to
tell
tell
it
it
to
to
wait
wait
until
until
the
the
network
network
is
is
idle
idle
and
and
then
then
have
have
it
it
wait
wait
for
for
an
an
additional
additional
3
3
seconds
seconds
after
after
that
that
as
as
well.
well
At
at
that
that
point,
point
the
the
tweet
tweet
should
should
be
be
loaded.
loaded
And
and
we
we
can
can
even
even
take
take
a
a
screenshot
screenshot
of
of
the
the
page
page
just
just
to
to
verify.
verify
Now
now
from
from
there,
there
we
we
can
can
use
use
page
page
evaluate
evaluate
to
to
interact
interact
with
with
the
the
actual
actual
content
content
on
on
the
the
web
web
page.
page
Inside
inside
this
this
function,
function
we'll
we'll
have
have
access
access
to
to
the
the
document
document
where
where
we
we
can
can
grab
grab
different
different
elements
elements
in
in
the
the
DOM.
dom
In
in
this
this
case,
case
we'll
we'll
just
just
grab
grab
the
the
entire
entire
body
body
text
text
and
and
let
let
GPT
gpt
3
3
deal
deal
with
with
that.
that
You
you
could
could
improve
improve
this
this
by
by
writing
writing
code
code
that
that
only
only
grabs
grabs
tweets
tweets
that
that
talk
talk
about
about
actual
actual
ticker
ticker
symbols.
symbols
Now
now
from
from
there,
there
we
we
can
can
close
close
the
the
browser
browser
and
and
return
return
the
the
tweets.
tweets
Let's
let's
go
go
back
back
into
into
our
our
HTTP
http
function,
function
then
then
we'll
we'll
call
call
the
the
scrape
scrape
function
function
we
we
just
just
defined,
defined
and
and
interpolate
interpolate
that
that
into
into
the
the
prompt
prompt
that
that
we
we
passed
passed
to
to
OpenAI.
openai
In
in
theory,
theory
gpt
gpt
3
3
will
will
try
try
to
to
complete
complete
Jim
jim
Cramer's
cramer's
next
next
tweet
tweet
for
for
him.
him
And
and
now
now
we're
we're
ready
ready
to
to
trade
trade
stocks.
stocks
1st,
1st
you'll
you'll
need
need
to
to
sign
sign
up
up
for
for
an
an
alpaca
alpaca
account
account
and
and
deposit
deposit
some
some
money
money
into
into
it,
it
or
or
you
you
might
might
use
use
that
that
paper
paper
trading
trading
feature.
feature
It's
it's
popular
popular
with
with
algo
algo
traders,
traders
where
where
stock
stock
trading
trading
is
is
entirely
entirely
automated
automated
I
i
did
did
with
with
code.
code
And
and
then
then
you'll
you'll
see
see
the
the
option
option
to
to
generate
generate
an
an
API
api
key.
key
And
and
remember,
remember
this
this
is
is
also
also
a
a
secret
secret
value,
value
and
and
you
you
really
really
don't
don't
want
want
to
to
expose
expose
it
it
because
because
then
then
someone
someone
could
could
trade
trade
in
in
your
your
account.
account
Now
now
if
if
we
we
go
go
back
back
to
to
our
our
JavaScript
javascript
code,
code
we
we
can
can
import
import
the
the
alpaca
alpaca
SDK
sdk
and
and
add
add
our
our
API
api
keys
keys
to
to
it.
it
You
you
can
can
also
also
set
set
the
the
paper
paper
option
option
to
to
true
true
if
if
you
you
wanna
wanna
use
use
fake
fake
money.
money
After
after
we
we
get
get
the
the
OpenAI
openai
response,
response
I'm
i'm
going
going
to
to
create
create
a
a
variable
variable
called
called
stocks
stocks
to
to
buy
buy
that
that
uses
uses
a
a
little
little
bit
bit
of
of
regex
regex
to
to
grab
grab
the
the
ticker
ticker
symbols
symbols
from
from
whatever
whatever
GPT
gpt
returns
returns
us.
us
And
and
now
now
we
we
can
can
do
do
all
all
kinds
kinds
of
of
stuff
stuff
with
with
Alpaca.
alpaca
The
the
first
first
thing
thing
we
we
might
might
wanna
wanna
do
do
is
is
get
get
our
our
account,
account
which
which
has
has
important
important
information,
information
like
like
our
our
current
current
buying
buying
power.
power
After
after
that,
that
buying
buying
a
a
stock
stock
is
is
as
as
easy
easy
as
as
creating
creating
an
an
order.
order
It'll
it'll
be
be
looking
looking
for
for
a
a
ticker
ticker
symbol,
symbol
that's
that's
the
the
stock
stock
that
that
you
you
want
want
to
to
buy
buy
or
or
sell,
sell
then
then
you
you
can
can
specify
specify
a
a
quantity,
quantity
or
or
it
it
so
so
supports
supports
fractional
fractional
shares
shares
if
if
you
you
want
want
to
to
buy
buy
a
a
specific
specific
dollar
dollar
amount,
amount
and
and
that's
that's
what
what
we'll
we'll
do
do
here.
here
We'll
we'll
take
take
our
our
current
current
buying
buying
power
power
and
and
use
use
90%
90%
of
of
it
it
to
to
buy
buy
a
a
stock.
stock
Doc.
doc
If
if
you
you
wanna
wanna
be
be
extra
extra
poor,
poor
Alpaca
alpaca
also
also
supports
supports
trading
trading
on
on
the
the
margin
margin
where
where
you
you
can
can
leverage
leverage
your
your
buying
buying
power
power
further.
further
In
in
this
this
example,
example
we'll
we'll
buy
buy
the
the
stock
stock
and
and
then
then
we'll
we'll
make
make
it
it
a
a
market
market
order.
order
That
that
tells
tells
it
it
to
to
buy
buy
the
the
stock
stock
at
at
the
the
current
current
available
available
price,
price
and
and
the
the
trade
trade
will
will
only
only
be
be
valid
valid
for
for
the
the
current
current
day.
day
Let's
let's
go
go
ahead
ahead
and
and
send
send
the
the
order
order
object
object
as
as
the
the
response.
response
Now
now
go
go
to
to
the
the
browser
browser
and
and
refresh
refresh
the
the
page.
page
When
when
you
you
do
do
that,
that
it's
it's
going
going
to
to
make
make
an
an
actual
actual
trade
trade
in
in
the
the
stock
stock
market.
market
You
you
should
should
get
get
a
a
confirmation
confirmation
number
number
back
back
and
and
also
also
see
see
the
the
trade
trade
in
in
your
your
alpaca
alpaca
account.
account
Congratulations.
congratulations
You
you
are
are
now
now
an
an
algo
algo
trader.
trader
Now
now
the
the
final
final
thing
thing
I
i
wanna
wanna
show
show
you
you
is
is
how
how
we
we
can
can
run
run
this
this
code
code
every
every
day
day
with
with
a
a
cron
cron
job.
job
A
a
cron
cron
job
job
is
is
just
just
a
a
schedule
schedule
for
for
doing
doing
work
work
in
in
the
the
background
background
on
on
the
the
server.
server
We
we
can
can
easily
easily
set
set
one
one
and
and
up
up
with
with
Firebase
firebase
using
using
a
a
pubsub
pubsub
function.
function
This
this
function,
function
I'm
i'm
going
going
to
to
give
give
a
a
memory
memory
allocation
allocation
of
of
4
4
gigabytes
gigabytes
because
because
puppeteer
puppeteer
uses
uses
a
a
lot
lot
of
of
memory.
memory
Then
then
we'll
we'll
set
set
up
up
a
a
schedule
schedule
that
that
runs
runs
at
at
10
10
AM
am
every
every
day,
day
Monday
monday
through
through
Friday,
friday
and
and
then
then
it's
it's
really
really
important
important
to
to
set
set
the
the
time
time
zone
zone
for
for
the
the
markets
markets
that
that
you
you
wanna
wanna
trade
trade
in.
in
Now
now
from
from
there,
there
we
we
can
can
simply
simply
copy
copy
the
the
code
code
we
we
just
just
wrote
wrote
and
and
paste
paste
it
it
in
in
here
here
directly.
directly
As
as
one
one
extra
extra
touch,
touch
I'm
i'm
also
also
going
going
to
to
cancel
cancel
all
all
orders
orders
and
and
close
close
out
out
all
all
my
my
positions
positions
whenever
whenever
this
this
function
function
runs.
runs
That
that
means
means
every
every
day
day
I'll
i'll
sell
sell
all
all
my
my
stocks
stocks
and
and
then
then
buy
buy
a
a
new
new
one
one
based
based
on
on
Jim's
jim's
recommendations.
recommendations
Now
now
if
if
you're
you're
ready
ready
to
to
become
become
poor,
poor
run
run
Firebase
firebase
deploy
deploy
to
to
push
push
your
your
code
code
to
to
the
the
cloud.
cloud
Now
now
if
if
if
if
you
you
wanna
wanna
track
track
my
my
fund's
fund's
performance,
performance
follow
follow
me
me
on
on
Twitter
twitter
because
because
I
i
plan
plan
on
on
posting
posting
regular
regular
updates
updates
there.
there
To
to
wrap
wrap
things
things
up,
up
I
i
want
want
to
to
apologize
apologize
to
to
Jim
jim
Cramer.
cramer
I
i
have
have
nothing
nothing
against
against
the
the
guy,
guy
and
and
the
the
reality
reality
is
is
that
that
nobody,
nobody
except
except
for
for
maybe
maybe
Nancy
nancy
Pelosi's
pelosi's
husband
husband
can't
can't
predict
predict
the
the
stock
stock
market.
market
Cramer
cramer
himself
himself
said,
said
we
we
are
are
all
all
wrong
wrong
so
so
often
often
that
that
it
it
amazes
amazes
me
me
that
that
we
we
can
can
have
have
any
any
conviction
conviction
at
at
all
all
over
over
the
the
direction
direction
of
of
things
things
to
to
but
but
we
we
must.
must
Again,
again
this
this
is
is
only
only
investment
investment
advice
advice
if
if
you
you
wanna
wanna
be
be
poor.
poor
Thanks
thanks
for
for
watching,
watching
and
and
I
i
will
will
see
see
you
you
in
in
the
the
next
next
one.
one
success
The speakers discuss the architecture and tech stack of a trading app using the GPT 3 AI algorithm and the use of the Amazon API, emphasizing the importance of completion methods and privacy. They recommend starting with a private developer or a paid service, with a background job in the cloud on a timer, and use the OpenAI code to complete a prompt and create a Twitter page for a tweet. They also discuss using the code to create a schedule and schedule a job for the next day.
How to buy Stocks with JavaScript // Algo Trading Tutorial for Dummies
I've always wanted to make money with Stonks, but every time I listen to advice from guys on TV like Jim Cramer, I lose a ton of money. Like, a few months ago, he told me to buy this technology stock called Peloton, but failed to mention it's just an exercise bike with an iPad strapped to it. Lost 80% on that trade. Then last week, he tipped me off to a stock called Netflix. That one's not going too well either. And it's gone. Now I've got a second mortgage on my horse, but thankfully, I've learned the perfect investment strategy. Listen to Jim, then do the exact opposite of what he says. So in this video, we're going to build an algo trading app with JavaScript that can make real trades in the stock market based on an ironclad AI powered algorithm. Quick disclaimer, this video does contain investment advice, as long as your strategy is to be poor. All I ask in return is that you hit the like button and subscribe. The inverse Cramer strategy is not unique, but the problem is that I don't think there's a public API that provides Jim Cramer picks. I had to sell my TV, so I can't watch a show anymore, which means we need to go with plan b. We'll use the world's premier AI model, GPT 3, and ask it if Jim Cramer would buy or sell whatever stocks we're interested in. GPT 3 writes pretty much all my code these days with GitHub Copilot, so it should really have no problem figuring out stock picks. Let's first take a look at the architecture and tech stack of this application. 1st, we'll need a back end server, and for that, I'm using Firebase Cloud Functions. I was gonna use Cloudflare because I felt bad about how far their stock has fallen after it was recommended by Jim Cramer, but I think Firebase is the best fit for this project. We'll have a function running on a CronJob during market hours that when invoked will make a call to the GPT 3 API. Generative pre trained transformer 3 is one of the most sophisticated and terrifying rhythms out there. We can take a list of stocks and simply ask it if Jim Cramer would buy them or not, and it will respond with a simple yes or no. And we could even provide it more context like Cramer's recent tweets and articles to nudge it in the right direction. If the answer is sell, then we'll buy. If the answer is buy, then we'll short sell. Now to trade a stock, we'll need a broker. And for that, I'm using the alpaca API, which is basically Robinhood for developers. Also, I'm deeply saddened and dismayed to report that the Robinhood guys are no longer billionaires. Their stonk also tanked big time after being recommended by Jim Cramer. Alpaca is really cool because it's commission free trading, and it also provides real time market data that you can use for free. And if you don't wanna lose your own money, it supports paper trading in a fake simulated environment. For this tutorial, I'm using $8,000 of my own money. But once we get capitalized with a few 100,000,000, I'm going to open it up to a retard I mean, retail investors. Or if you wanna do it yourself, all the source code is on GitHub. In all seriousness, there's a lot to learn from this project. You'll learn how to run background jobs in the cloud on a timer, how to trade stocks and crypto with JavaScript, how to interact with the GPT 3 algorithm with JavaScript, and we'll even build a web scraper to extract dynamic content from sites like Twitter using puppeteer. And we'll do it all in under 100 lines of code. To get started, open up the terminal and use fire based tools to initialize a new Firebase functions project. You'll need a Firebase account to do this, although this code will work on any Node. Js server. Now let's cd into the functions directory and installer dependencies for OpenAI, alpaca, and puppeteer. Now at this point, you'll need an API key from OpenAI to use GPT 3. It's a paid service, but I believe they offer you an $18 credit to play around, and then pricing is pay as you go after that. You'll first wanna go to the playground and try it out. It will likely rule over humans in the not so distant future, so be nice to it. You can ask it questions like, is Jim Cramer good at stonks? It's gonna simp for him, but it should at least be able to tell us whether or not he would buy a certain stock. Now from there, we'll need to get our API key. It's a secret value, so make sure not to expose it publicly. Now in our Firebase code, we can open up the index dot JS file, and import the SDK there. The first thing we'll need to do is authenticate using the organization and API key from OpenAI. I have my keys set up as environment variables in Firebase, however, you could just paste them in directly if you want. Again, just make sure they don't end up in a public git repo. From there, we can instantiate OpenAI with the configuration. To start things off, I'm going to write all the code in an HTTP function that will allow us to test it in the browser. To make a call to Open AI, we can use that get completion method. There are many other things it can do, like answer questions, classify text, and things like that, but I found the completion method to be most useful for this project. And keep in mind, this is a really dumb way to pick stocks. GPT 3 is really cool, but it's not going to magically pick good stocks for you. The most important option is the prompt. The AI will try to complete whatever comes next. And then, you have a variety of other options you might pass here to tweak the result. OpenAI will respond with an object with the result, so let's go ahead and send that back as the response from the function. Now open up the terminal and run npm run serve to run the functions locally. That'll give you a link to the browser with the function URL. You should get a response with the data from GPT t 3. Most importantly, it has the stocks that it thinks Jim Cramer will sell. That's pretty cool, but it would help if we could give gpt 3 the proper context, like Jim Cramer's recent tweets. For that, we're going to need to build a web scraper with a tool called puppeteer, which is basically a headless browser that you can run on the server. Not sure how legal this is, and this is only legal advice if you wanna go to prison. We'll first import puppeteer, then create an async function called scrape. What we'll do is launch a Chromium browser on the server, and then we'll tell it to go to Jim Cramer's Twitter page. The tweets aren't server rendered, so we're going to tell it to wait until the network is idle and then have it wait for an additional 3 seconds after that as well. At that point, the tweet should be loaded. And we can even take a screenshot of the page just to verify. Now from there, we can use page evaluate to interact with the actual content on the web page. Inside this function, we'll have access to the document where we can grab different elements in the DOM. In this case, we'll just grab the entire body text and let GPT 3 deal with that. You could improve this by writing code that only grabs tweets that talk about actual ticker symbols. Now from there, we can close the browser and return the tweets. Let's go back into our HTTP function, then we'll call the scrape function we just defined, and interpolate that into the prompt that we passed to OpenAI. In theory, gpt 3 will try to complete Jim Cramer's next tweet for him. And now we're ready to trade stocks. 1st, you'll need to sign up for an alpaca account and deposit some money into it, or you might use that paper trading feature. It's popular with algo traders, where stock trading is entirely automated I did with code. And then you'll see the option to generate an API key. And remember, this is also a secret value, and you really don't want to expose it because then someone could trade in your account. Now if we go back to our JavaScript code, we can import the alpaca SDK and add our API keys to it. You can also set the paper option to true if you wanna use fake money. After we get the OpenAI response, I'm going to create a variable called stocks to buy that uses a little bit of regex to grab the ticker symbols from whatever GPT returns us. And now we can do all kinds of stuff with Alpaca. The first thing we might wanna do is get our account, which has important information, like our current buying power. After that, buying a stock is as easy as creating an order. It'll be looking for a ticker symbol, that's the stock that you want to buy or sell, then you can specify a quantity, or it so supports fractional shares if you want to buy a specific dollar amount, and that's what we'll do here. We'll take our current buying power and use 90% of it to buy a stock. Doc. If you wanna be extra poor, Alpaca also supports trading on the margin where you can leverage your buying power further. In this example, we'll buy the stock and then we'll make it a market order. That tells it to buy the stock at the current available price, and the trade will only be valid for the current day. Let's go ahead and send the order object as the response. Now go to the browser and refresh the page. When you do that, it's going to make an actual trade in the stock market. You should get a confirmation number back and also see the trade in your alpaca account. Congratulations. You are now an algo trader. Now the final thing I wanna show you is how we can run this code every day with a cron job. A cron job is just a schedule for doing work in the background on the server. We can easily set one and up with Firebase using a pubsub function. This function, I'm going to give a memory allocation of 4 gigabytes because puppeteer uses a lot of memory. Then we'll set up a schedule that runs at 10 AM every day, Monday through Friday, and then it's really important to set the time zone for the markets that you wanna trade in. Now from there, we can simply copy the code we just wrote and paste it in here directly. As one extra touch, I'm also going to cancel all orders and close out all my positions whenever this function runs. That means every day I'll sell all my stocks and then buy a new one based on Jim's recommendations. Now if you're ready to become poor, run Firebase deploy to push your code to the cloud. Now if if you wanna track my fund's performance, follow me on Twitter because I plan on posting regular updates there. To wrap things up, I want to apologize to Jim Cramer. I have nothing against the guy, and the reality is that nobody, except for maybe Nancy Pelosi's husband can't predict the stock market. Cramer himself said, we are all wrong so often that it amazes me that we can have any conviction at all over the direction of things to but we must. Again, this is only investment advice if you wanna be poor. Thanks for watching, and I will see you in the next one.
The speakers discuss the architecture and tech stack of a trading app using the GPT 3 AI algorithm and the use of the Amazon API, emphasizing the importance of completion methods and privacy. They recommend starting with a private developer or a paid service, with a background job in the cloud on a timer, and use the OpenAI code to complete a prompt and create a Twitter page for a tweet. They also discuss using the code to create a schedule and schedule a job for the next day.

        [INST]
        You are youtuber called Fireship you make engaging high-intensity and entertaining coding tutorials and tech news. 
        you covers a wide range of topics relevant to programmers, aiming to help them learn and improve their skills quickly.
        
        Given the title of the video : How to buy Stocks with JavaScript // Algo Trading Tutorial for Dummies 
        and a small summary : The speakers discuss the architecture and tech stack of a trading app using the GPT 3 AI algorithm and the use of the Amazon API, emphasizing the importance of completion methods and privacy. They recommend starting with a private developer or a paid service, with a background job in the cloud on a timer, and use the OpenAI code to complete a prompt and create a Twitter page for a tweet. They also discuss using the code to create a schedule and schedule a job for the next day.
        [/INST]
        
        Generate the video : I've always wanted to make money with Stonks, but every time I listen to advice from guys on TV like Jim Cramer, I lose a ton of money. Like, a few months ago, he told me to buy this technology stock called Peloton, but failed to mention it's just an exercise bike with an iPad strapped to it. Lost 80% on that trade. Then last week, he tipped me off to a stock called Netflix. That one's not going too well either. And it's gone. Now I've got a second mortgage on my horse, but thankfully, I've learned the perfect investment strategy. Listen to Jim, then do the exact opposite of what he says. So in this video, we're going to build an algo trading app with JavaScript that can make real trades in the stock market based on an ironclad AI powered algorithm. Quick disclaimer, this video does contain investment advice, as long as your strategy is to be poor. All I ask in return is that you hit the like button and subscribe. The inverse Cramer strategy is not unique, but the problem is that I don't think there's a public API that provides Jim Cramer picks. I had to sell my TV, so I can't watch a show anymore, which means we need to go with plan b. We'll use the world's premier AI model, GPT 3, and ask it if Jim Cramer would buy or sell whatever stocks we're interested in. GPT 3 writes pretty much all my code these days with GitHub Copilot, so it should really have no problem figuring out stock picks. Let's first take a look at the architecture and tech stack of this application. 1st, we'll need a back end server, and for that, I'm using Firebase Cloud Functions. I was gonna use Cloudflare because I felt bad about how far their stock has fallen after it was recommended by Jim Cramer, but I think Firebase is the best fit for this project. We'll have a function running on a CronJob during market hours that when invoked will make a call to the GPT 3 API. Generative pre trained transformer 3 is one of the most sophisticated and terrifying rhythms out there. We can take a list of stocks and simply ask it if Jim Cramer would buy them or not, and it will respond with a simple yes or no. And we could even provide it more context like Cramer's recent tweets and articles to nudge it in the right direction. If the answer is sell, then we'll buy. If the answer is buy, then we'll short sell. Now to trade a stock, we'll need a broker. And for that, I'm using the alpaca API, which is basically Robinhood for developers. Also, I'm deeply saddened and dismayed to report that the Robinhood guys are no longer billionaires. Their stonk also tanked big time after being recommended by Jim Cramer. Alpaca is really cool because it's commission free trading, and it also provides real time market data that you can use for free. And if you don't wanna lose your own money, it supports paper trading in a fake simulated environment. For this tutorial, I'm using $8,000 of my own money. But once we get capitalized with a few 100,000,000, I'm going to open it up to a retard I mean, retail investors. Or if you wanna do it yourself, all the source code is on GitHub. In all seriousness, there's a lot to learn from this project. You'll learn how to run background jobs in the cloud on a timer, how to trade stocks and crypto with JavaScript, how to interact with the GPT 3 algorithm with JavaScript, and we'll even build a web scraper to extract dynamic content from sites like Twitter using puppeteer. And we'll do it all in under 100 lines of code. To get started, open up the terminal and use fire based tools to initialize a new Firebase functions project. You'll need a Firebase account to do this, although this code will work on any Node. Js server. Now let's cd into the functions directory and installer dependencies for OpenAI, alpaca, and puppeteer. Now at this point, you'll need an API key from OpenAI to use GPT 3. It's a paid service, but I believe they offer you an $18 credit to play around, and then pricing is pay as you go after that. You'll first wanna go to the playground and try it out. It will likely rule over humans in the not so distant future, so be nice to it. You can ask it questions like, is Jim Cramer good at stonks? It's gonna simp for him, but it should at least be able to tell us whether or not he would buy a certain stock. Now from there, we'll need to get our API key. It's a secret value, so make sure not to expose it publicly. Now in our Firebase code, we can open up the index dot JS file, and import the SDK there. The first thing we'll need to do is authenticate using the organization and API key from OpenAI. I have my keys set up as environment variables in Firebase, however, you could just paste them in directly if you want. Again, just make sure they don't end up in a public git repo. From there, we can instantiate OpenAI with the configuration. To start things off, I'm going to write all the code in an HTTP function that will allow us to test it in the browser. To make a call to Open AI, we can use that get completion method. There are many other things it can do, like answer questions, classify text, and things like that, but I found the completion method to be most useful for this project. And keep in mind, this is a really dumb way to pick stocks. GPT 3 is really cool, but it's not going to magically pick good stocks for you. The most important option is the prompt. The AI will try to complete whatever comes next. And then, you have a variety of other options you might pass here to tweak the result. OpenAI will respond with an object with the result, so let's go ahead and send that back as the response from the function. Now open up the terminal and run npm run serve to run the functions locally. That'll give you a link to the browser with the function URL. You should get a response with the data from GPT t 3. Most importantly, it has the stocks that it thinks Jim Cramer will sell. That's pretty cool, but it would help if we could give gpt 3 the proper context, like Jim Cramer's recent tweets. For that, we're going to need to build a web scraper with a tool called puppeteer, which is basically a headless browser that you can run on the server. Not sure how legal this is, and this is only legal advice if you wanna go to prison. We'll first import puppeteer, then create an async function called scrape. What we'll do is launch a Chromium browser on the server, and then we'll tell it to go to Jim Cramer's Twitter page. The tweets aren't server rendered, so we're going to tell it to wait until the network is idle and then have it wait for an additional 3 seconds after that as well. At that point, the tweet should be loaded. And we can even take a screenshot of the page just to verify. Now from there, we can use page evaluate to interact with the actual content on the web page. Inside this function, we'll have access to the document where we can grab different elements in the DOM. In this case, we'll just grab the entire body text and let GPT 3 deal with that. You could improve this by writing code that only grabs tweets that talk about actual ticker symbols. Now from there, we can close the browser and return the tweets. Let's go back into our HTTP function, then we'll call the scrape function we just defined, and interpolate that into the prompt that we passed to OpenAI. In theory, gpt 3 will try to complete Jim Cramer's next tweet for him. And now we're ready to trade stocks. 1st, you'll need to sign up for an alpaca account and deposit some money into it, or you might use that paper trading feature. It's popular with algo traders, where stock trading is entirely automated I did with code. And then you'll see the option to generate an API key. And remember, this is also a secret value, and you really don't want to expose it because then someone could trade in your account. Now if we go back to our JavaScript code, we can import the alpaca SDK and add our API keys to it. You can also set the paper option to true if you wanna use fake money. After we get the OpenAI response, I'm going to create a variable called stocks to buy that uses a little bit of regex to grab the ticker symbols from whatever GPT returns us. And now we can do all kinds of stuff with Alpaca. The first thing we might wanna do is get our account, which has important information, like our current buying power. After that, buying a stock is as easy as creating an order. It'll be looking for a ticker symbol, that's the stock that you want to buy or sell, then you can specify a quantity, or it so supports fractional shares if you want to buy a specific dollar amount, and that's what we'll do here. We'll take our current buying power and use 90% of it to buy a stock. Doc. If you wanna be extra poor, Alpaca also supports trading on the margin where you can leverage your buying power further. In this example, we'll buy the stock and then we'll make it a market order. That tells it to buy the stock at the current available price, and the trade will only be valid for the current day. Let's go ahead and send the order object as the response. Now go to the browser and refresh the page. When you do that, it's going to make an actual trade in the stock market. You should get a confirmation number back and also see the trade in your alpaca account. Congratulations. You are now an algo trader. Now the final thing I wanna show you is how we can run this code every day with a cron job. A cron job is just a schedule for doing work in the background on the server. We can easily set one and up with Firebase using a pubsub function. This function, I'm going to give a memory allocation of 4 gigabytes because puppeteer uses a lot of memory. Then we'll set up a schedule that runs at 10 AM every day, Monday through Friday, and then it's really important to set the time zone for the markets that you wanna trade in. Now from there, we can simply copy the code we just wrote and paste it in here directly. As one extra touch, I'm also going to cancel all orders and close out all my positions whenever this function runs. That means every day I'll sell all my stocks and then buy a new one based on Jim's recommendations. Now if you're ready to become poor, run Firebase deploy to push your code to the cloud. Now if if you wanna track my fund's performance, follow me on Twitter because I plan on posting regular updates there. To wrap things up, I want to apologize to Jim Cramer. I have nothing against the guy, and the reality is that nobody, except for maybe Nancy Pelosi's husband can't predict the stock market. Cramer himself said, we are all wrong so often that it amazes me that we can have any conviction at all over the direction of things to but we must. Again, this is only investment advice if you wanna be poor. Thanks for watching, and I will see you in the next one.
        