Thumbnail for I Built a 24/7 Stock Trading Bot With Claude Code (Copies Politicians) by Damian Malliaros

I Built a 24/7 Stock Trading Bot With Claude Code (Copies Politicians)

Damian Malliaros

23m 14s4,713 words~24 min read
Auto-Generated

[0:00]I let Claude code trade stocks for me by copying the exact trades of US politicians who consistently out perform the S&P 500 by over two times.

[0:13]And in this video, I will show you exactly how you can do the same from connecting Claude to a real brokerage to setting it up so it can follow real profitable trading strategies and monitoring the movements of the biggest whales in the market.

[0:29]So by the end, you will have your own bot buying and selling stocks on autopilot even when your computer is off.

[0:37]Now if you don't know who I am, my name is Damian, and I make over $10,000 per month testing out different AI tools.

[0:44]So close all of your other tabs, let's dive right into it. Now the first thing that we have to do is to go over in a platform that is called Alpaca, which will allow Claude Code to trade stocks for us programmatically.

[1:01]And you will be able to do that because Alpaca allows API trading, what that basically means is it allows any AI agent to send a request over to the platform and then it is going to go over and perform the necessary action.

[1:12]Now, I'm going to leave a link for this platform in the description below, and all you have to do after you click on it is to simply come over here and sign up for a completely free account.

[1:21]And once you sign up, you will be brought over in this interface over here. Now, what I want you to do is to simply come over here to the left of the page, and first we're going to create a paper account, which what that means is that we're going to be able to trade but not with real money.

[1:36]Since if you want to trade with real money, you need to use your live trading account that you have over here.

[1:42]But for the purposes of this example, I'm going to use a paper account, since I'm making this video for demonstration purposes and this is definitely not financial advice. That's why I will simply come over here and I'm going to create a new paper account, which I'm going to call Damian.

[1:56]And let's say we want to give this account access to $100,000, and we click on save, which is going to create us a brand new paper trading account.

[2:04]And now all we have to do is to simply come over here to the homepage, where we can see how much money we have in this account, and underneath over here, we will be able to see all the trades that Claude code is going to place for us.

[2:17]But the main reason we are in this interface right now is because of this section over here, which is called API keys, because Claude will be able to place trades on our Alpaca account by using our API keys.

[2:30]So what you need to do now is to simply come over here and click on generate new keys, and what this is going to do is create you two different keys, your normal key and your secret key.

[2:44]Now, these keys are going to disappear after we close out this tab, so make sure you come over here and you copy them, because we're going to give all of these keys to Claude code.

[2:51]Now, to access Claude code, simply go over to Claude.com/download, which I will also leave a link for in the description below, and then simply come over here and download the Claude desktop app for your operating system.

[3:02]And once you download it and you sign in with your account, you're going to see an interface that looks like this. Now, what I want you to do is to make sure you navigate over in the code tab over here, and then simply come down here to the chat interface and click on the folder over here, so we can create a brand new folder in which Claude code is going to be safely running to.

[3:23]Now, what I'm going to do is I will create this folder over here in my document section, and I'm going to call this Claude trading, and I will simply create it and open it up.

[3:33]What that now means is that Claude code is going to be safely running inside that folder.

[3:39]And now all we have to do is to simply come over here to the chat box and explain to Claude code exactly what kind of bot we want to build.

[3:46]So I will come over here and say, hey, I want to build a trading board, which is going to trade stocks for me. Now, how this is going to work is by using a platform that is called Alpaca, for which this is the endpoint and the API keys that you're going to need.

[4:02]And now what I'm going to do is I will simply paste in everything that I just copied. And then what I'm also going to do is I will come back over here to Alpaca, and I'm going to click on the API section over here, which is going to open up all the API docs.

[4:16]And I will simply copy this link, and I'm going to paste it over to Claude code, and I'm going to say, also, these are the docs. Make sure you read them to understand exactly how the service works, and then go over and connect to Alpaca for me.

[4:28]And now I will simply send over this prompt and click on trust war space, so Claude code can go and connect to Alpaca for us.

[4:36]It is asking us whether we want to allow it access to this folder. I'll simply click on allow, and I will just let it read the docs over here, and then connect to the endpoint using the two API.

[4:46]And boom, as you can see, this has just finished connecting to Alpaca. It is telling us that the connection is live and verified, and over here, it is also explaining how this connection actually works, which you don't really need to understand, since Claude code understands it for you.

[5:00]But now just to test this out and see that Claude code can actually place trades on our Alpaca account, let me come over here and say, hey great, now just to test this out, I want you to go over in my paper account called Damian and buy Tesla shares that are worth $2,000.

[5:17]And now if I send over this prompt, this is going to trigger Claude code and it is going to go over and place a market buy for $2,000 of Tesla stock.

[5:26]And if I actually come back to Alpaca, we can see over here that we did in fact buy the Tesla stock over here for $2,000, which is exactly what we wanted.

[5:37]And now just to test that it is also able to sell the stock, let me say, all right, now sell the Tesla stock that you just bought and buy Apple stock instead for $5,000.

[5:48]And again if I send over this prompt, this is going to go over, sell my Tesla stock and buy me some Apple stock for $5,000.

[5:56]And if we actually come back over here, we will be able to see that's exactly what it did. It sold the Tesla stock, and then it went over and bought some Apple stock for $5,000, which means that the connection with Alpaca works correctly.

[6:09]But now is probably the most important part of the video, which is actually implementing a profitable trading strategy to this board, so it can then go over and execute the strategy and start making some money.

[6:22]Again, this is just for demo purposes though. Every strategy does have the risk of losing your money, I do want to point that out.

[6:29]And because I'm not a trading expert, what I went over and did was basically ask Claude to do deep research and find me the best trading strategies on the market for trading stocks at a mid-length time horizon.

[6:44]And that's exactly what it went over and did, and it also created me this presentation over here with the exact strategy that it is going to use.

[6:50]And if we actually go over it, you can see that it is telling us that we're going to do swing trading, which is basically trading in days and weeks, and not in minutes and hours, which is day trading.

[7:00]And we're going to use the three golden rules, which are trade with the trend and not against it, risk a fixed amount, which needs to be small slice per trade, and we should trail the trends and don't try to predict them.

[7:13]Plus we're going to use these specific indicators, when the price is above the 50-day MA, our trailing stop method will be 3 ATR on the daily chart, equivalent buffer will be 2 to 4%, and every trade will always be capped for maximum 1% of capital, and we will aim for at least a 3 on 1 return.

[7:35]So that is the strategy that we're going to follow. Over here, Claude also explained it way more in depth with text, and it told us that this is the strategy the most profitable traders use.

[7:46]So what I'm going to do is I will simply just copy this full strategy over here, and after I'm done copying it, I will simply come back over to Claude, I'm going to paste it in over here, and I'm going to say something like, hey, this is the strategy that Claude suggested that we do.

[8:00]So what I want you to do is to go over and implement the strategy, or if you want to improve upon it, feel free to first let me know what you want to change, and I can then either approve or disapprove your suggestions.

[8:14]And I will simply come over here and send over this prompt, so Claude code can now go and implement the strategy into our board.

[8:20]But as you can see, first it is telling us that before it starts, our current strategy does have some gaps, and it suggested some improvements that we can make to it over here.

[8:31]Now, I'm going to listen to it, since in a lot of other videos that I have seen, the suggestions that Opus 4.7 specifically gives are very good for trading.

[8:40]So I will say, sure, these improvements look good. Make sure you go and implement them into current strategy, and I'm going to send this over, so Claude code can now go and build out our board for us.

[8:49]And boom, Claude code has just finished building out our board, and as you can see, it is telling us that the strategy is live and working.

[8:57]And what it went over and did was open up a bunch of new positions that meet our trading strategy criteria, while always being only 1% of the money that we have in our account, which is exactly what we wanted.

[9:12]But our board isn't fully functioning yet because in order for us to run it, we have to actively come over here in the chat interface and say, hey, run the board and see if we should buy, if we should sell, if we should hold, which basically requires active time.

[9:26]But what we can do, and what also Claude code is suggesting us to do is to turn this into a scheduled task, so this automatically runs for us in the background without us needing to come over here in the chat interface and basically trigger it ourselves.

[9:43]Now, to make this into a scheduled task, what we have to do is to simply come over here and say something like, hey great, now what I want you to do is to turn this into a scheduled task so this agent triggers every 5 minutes or so, so we can see if we should buy, sell, or hold in all the positions that we have, and obviously, make sure every time it triggers, it gets all the necessary information that it needs from the API, and it also is able to see the previous moves that the bot did on the previous run, so it knows what the bot previously did, so it has better context of the situation.

[10:17]And I will simply send over this prompt so Claude code can go over and turn this into a scheduled task. But while Claude is doing that, I do want to say that if you want all the prompts that we're going to use to build out this full bot, along with the full code of it, you can find it inside my school community, the link to which I will leave in the description below.

[10:36]Where you can find all the assets from all the videos that I have made along with plenty of more content for building your own AI apps.

[10:43]So again, if you're interested in that, click the top link in the description. And boom, that's exactly what Claude went over and did. It created the scheduled task, and now it is going to run every 5 minutes and check how our trades are doing, and whether we should close a position, open a position, or simply just hold.

[11:01]And currently, this would be a fully finished bot guys, but to take this a step even further, it's time to show you how you can copy or even take significantly into account the trading movements of US politicians and other big names like big CEOs, and basically people that are required to disclose their trades by law.

[11:23]Since these people do tend to have insider information because they are the law makers that could create a law that would benefit a specific type of company, or they are the CEOs that know exactly what is happening inside the companies, which means that they can very accurately buy or sell stocks of their company and make a big profit.

[11:50]Well, to that I have to say, you don't actually need to, because there is this website that is called Capital trades that basically does this for you.

[11:58]It literally takes all the disclosed trades of US politicians and shows them to the public. What this allows us to do is to take the information from this website every time a politician makes a trade, and then we can heavily factor this in when our bot is deciding whether to buy or sell a stock.

[12:15]And as you can see, in this website, they literally have all the US politicians that trade stocks.

[12:20]Over here we can see John Bozman, John Fetterman and all of their stock activity. And if I actually click on the politicians tab, I can also select a specific politician that I want to see what trades they have recently did.

[12:33]So what I want you to do is to simply come over here and copy the URL of this website, which I will also leave a link for in the description below.

[12:40]And then I want you to come back over to Claude code, paste it in and say something like, hey great, but I also want to factor in the trades of US politicians, since they really tend to have insider information a lot of times, since they are the ones that are making the laws.

[12:50]What I want you to do is to have the bot trigger and also analyze the news and see which US politician bought a specific stock and if that US politician bought it, I want this to be heavily factored in to whether we're going to buy or sell a stock.

[13:00]I kind of want to switch the current strategy that we have, and also make the bot heavily factor in the moves of these politicians.

[13:20]But before I send over this prompt, I do want to point one thing out, and that is that these politicians over here and all US politicians have 45 days to disclose their trades, which means that they can make a trade today and they can disclose it literally 40 days after they made it.

[13:37]Which slightly defeats the purpose of us following the trades, but there is a work around that, which Claude also suggested me, and it told me to target longer-term trades, and no single day or a few day trades.

[13:51]Since following a politician in those trades will be basically worthless along with all these other factors, like prioritizing early fillers, also following cluster signals, so when multiple politicians buy the same stock around the same time, then that's a much stronger indicator than just one buying it.

[14:08]Also doing cross reference committee assignments, which means that we can have Claude track which committee a senator sits on, and then watch for trades in the related sectors before disclosure appears, which are some very good ways to work around the 45-day filling period.

[14:26]Plus the fact that they have 45 days to fill in the trades doesn't mean that they will take all the 45 days. Some senators actually fill them in within 1 to 2 days, so it would be better to prioritize those.

[14:38]So what I'm going to do is I will simply come over here and copy this research that I did with Claude for how we can work around this 45-day filling issue.

[14:46]And I will also come over here and paste it in and I will say, also, make sure you take these things into account, because politicians do have 45 days to fill in the trades, so make sure our new strategy also heavily takes this into account.

[15:00]And now what I'm going to do is I will simply send over this prompt so Claude can analyze all the information that we gave it, which is the fact that we wanted to follow the trades of US politicians, but also take these things into account because of the 45-day filling issue, and then update our trading strategy.

[15:16]And as you can see, Claude fully analyzed our prompt and gave us this implementation plan over here, where it is telling us exactly how it is going to go over and update our strategy.

[15:26]So what I'm going to do is I will simply come over here and say, hey, this looks great, go over and update our trading strategy for me.

[15:33]And guys, we actually ran into an error because Claude is not able to access the capital trades website because it has undiscraped blockers.

[15:42]So what it suggested us to do is to sign up to another website, which has an API, which is called financial modelingprep.com, which is exactly what I went over and did, and I will also leave the link for this in the description below so you can sign up for free.

[15:56]And then you're going to be brought over in this page over here, where you have the API quick start along with your own unique API key over here.

[16:05]So all I want you to do is to simply come over here and copy all of this information, and then come back over to Claude code, say something like, hey, I successfully signed up to the financial modeling prep.com website.

[16:21]And this is all the information you will need in order for you to get information about the US politicians, and then simply paste in everything that you just copied and click on enter.

[16:26]And guys, these are things that are going to happen where you're building out bots like this. Just as you saw, we tried to use a website, but we weren't actually able to access it programmatically.

[16:36]But we didn't actually say that I'm queer, we just said, hey, how can we fix this, and Claude code gave us another option that we can use.

[16:44]So what I want you to keep from this is that once you face a difficulty when building something, you shouldn't query it and you should just try another way and make it happen.

[16:52]And as you can see, this time it is telling us that both endpoints work, and we are able to fetch the data from April. And boom, Claude code just finished updating our strategy.

[17:01]And over here, it is telling us exactly how it works. First, it sees whether any politicians have bought any stocks recently, then it takes the stocks and it sees if they match our previous trading strategy criteria, and then if they do, it goes over and makes that trade.

[17:18]But right now we have this as a scheduled task. What that means is that it runs with just code and doesn't actually need to use the Claude model.

[17:27]Now, if we want to use the reasoning of the Claude model to determine whether the specific politician that made the trade is something that we should follow as well or not, then we cannot have this as a scheduled task, and we actually need to turn this into a routine.

[17:41]Because the difference from a scheduled task to a routine is that the scheduled task runs on just code without any AI inside it, while the routine wakes up Claude itself and uses it to execute that task.

[17:55]And since we do want to use Claude as an advisor to figure out whether that is a trade worth taking, what I'm going to do is I will come over here and say, hey, what I want you to do is to turn this into a routine that, let's say, runs every 5 minutes again, because I also want to use the intelligence of Claude to figure out whether the trade that the senator has made is something that we should follow or not, based on information and any news research that Claude can do as well.

[18:22]And I will simply send over this prompt so Claude can go over and update our bot one last time, which is exactly what it is going over and doing.

[18:30]But as you can see, it came up with an interesting strategy because Claude routines firing every 5 minutes will cost us money, $1 to $3 every day, or possibly even $5 to $10 every single day, but I found a better way, which is that it is going to spin up a Claude session only when it actually has to, so it doesn't eat up this much money.

[18:50]Now, obviously, if you're trading with big sums of money, for example, $10,000 plus, this cost over here of $10 per day is literally nothing, like you can easily pay that.

[19:01]But if you're more of a beginner trader, then not spinning up Claude every single time can actually save you some money. And boom, as you can see, it just finished and over here it is telling us exactly how it works right now.

[19:12]It first fetches the data from the politicians, it then does some web searches on these politicians to see if there are any relevant news, and then based on this information, Claude creates a decision and logs it into a file, which then our trading strategy takes and decides if that trade would also match our previous indicators.

[19:34]And if it does, it approves it, and if not, it simply rejects it. And as you can see, it also tells us that this turned it into a routine that will execute every 5 minutes.

[19:43]And if I actually come over here in the routine tab, I am able to see it over here. It is called the politician trade bot. And if I actually click on it, I can see that this routine is active.

[19:53]I can see the instructions to understand exactly how it works, and if I want to, I can also come over here and click on the run now, just to see that it actually is working correctly.

[20:00]And as you can see, here it is currently executing. It is fetching information to see if any politicians performed any trades recently. Then it's going to analyze it, figure out which one of them are worth pursuing, and then it's going to compare it and see if it matches our other trading indicators. And if they do, this will go over and place that trade for us.

[20:22]And boom, this has just finished executing. And over here we can see all the trades that have been placed along with how much money we have managed to make on each trade, and how much money we have lost. As you can see, our portfolio is already up $150, which is a pretty good indicator, to say the least.

[20:40]But right now we have a problem because if we close down our PC, then this routine is not going to be able to trigger, which means that our board is not going to be working for us 24/7.

[20:51]So to fix this issue, what we need to do is to push this bot over to the cloud so it runs over there, and we don't need to have our PC open. We can do this by using the GitHub CLI, so I can connect and then you can transfer all the code over there.

[21:21]And that's very important, guys. Tell it to use the GitHub CLI, so you can connect to your GitHub account, and then you can transfer all your code to a brand new repo, and then simply send over this prompt.

[21:32]Which, as you can see, that's exactly what it is doing. Now, normally here, this is going to pop up an interface where it will prompt you to sign in with your GitHub account. If you haven't done that yet, but since I already have, it doesn't give me this interface.

[21:44]Now, what it is going over and doing is removing all the API keys that we have because we don't want this information to be accessible to GitHub, and it's putting them in a safe .ENV file.

[21:55]And as you can see, it went over and created a private GitHub repo, and now it is pushing all the code of our board over there.

[22:04]And now it is going over and setting the secrets, which are basically all the API keys that we have given it over here in our local environment.

[22:10]And boom, as you can see, this just finished over here, and it is telling us that the bot is fully transferred to our GitHub actions repo, which if we actually click on, we can see it open up in a browser tab.

[22:21]And this is literally our exact bot, which follows the exact trading strategy that we talked about before, but this time, it lives in the cloud and works for us 24/7 without us needing to have our PC open all the time.

[22:35]Now, if you want to get access to this GitHub repo, which includes all the code and rules of our bot, and also all the prompts that we use to build it, you can find it inside my school community.

[22:45]The link to which I have left at the top of this video's description, where inside, you're going to find all the assets from all the videos that I have made along with get access to a lot more content, like how you can build and monetize your own AI app in just 30 days.

[23:02]So if that's a topic that interests you, and you want to build your own AI app, make sure you click the top link in my description, and you can come and check out my school community, and I will be waiting to see you inside.

[23:13]But also get three things you're going to enjoy this video, which I build a crypto trading board using again, Claude code, so make sure you go and watch that next, and I will see you over there.

Need another transcript?

Paste any YouTube URL to get a clean transcript in seconds.

Get a Transcript