Thumbnail for Building a Polymarket AI Trading Bot From Scratch by All About AI

Building a Polymarket AI Trading Bot From Scratch

All About AI

24m 3s4,189 words~21 min read
AI audio transcription
Transcript source

AI audio transcription

This transcript was generated from the video's audio because no usable YouTube caption track was available. The transcript below is server-rendered so it can be read, searched, cited, and shared without opening the original YouTube player.

Timestamped outline
Pull quotes
[0:00]So we can see, we have done a couple of trades, we made a couple of dollars and if you go to the P&L now, you can see we are two dollars up already, 7%, so that was a great start.
[0:11]Okay, so we put in like a buy and here we get the flash and you can see our balance updated super fast, so I like to see that.
[0:21]So in this video, I wanted to take you with me while we build out a polymarket AI high frequency trading bot.
[0:31]You can basically call it what you want because it's really fun and it's not that hard when we kind of leverage Cloud Code or let's say Codex or any other AI coding agents like Open Code if you want to get into that.
Use this transcript
Related transcript hubs

[0:00]So we can see, we have done a couple of trades, we made a couple of dollars and if you go to the P&L now, you can see we are two dollars up already, 7%, so that was a great start.

[0:11]Okay, so we put in like a buy and here we get the flash and you can see our balance updated super fast, so I like to see that. We did actually go for an up

[0:21]Hello, hope you are doing well. So in this video, I wanted to take you with me while we build out a polymarket AI high frequency trading bot.

[0:31]You can basically call it what you want because it's really fun and it's not that hard when we kind of leverage Cloud Code or let's say Codex or any other AI coding agents like Open Code if you want to get into that.

[0:44]But basically, I wanted to show you how we can set this up and you don't even really need to create an account per se, as you will see soon.

[0:54]So basically, I think we just going to get started, so you can see this is Polymarket and when I say high frequency in the title, right, what I meant, of course, the highest frequency you can do on Polymarket is the Bitcoin up or down.

[1:10]So we bet is the price is going to go up in the next five minutes or is it going to go down, right? That is what we are going to focus on in this video.

[1:18]This is the highest frequency, I think you can get in on Polymarket. So the first thing we really want to do is just get a set up on Cloud Code.

[1:31]So I created like this new thing here, so let's just call it poly high frequency trading, something like that.

[1:41]And let's just go into that, the poly_HFT and we can just go into Claude, right? Let's find that. And I'm going to do the dangerously skip and we should be set up.

[1:54]So let me also find that folder in cursor here, so we can manage our files a bit. poly_HFT. Okay, and let me just adjust this a bit. Get us set up here.

[2:10]I think that should be good, so let's just I want to do a folder, let's call it Docs and we need some information about Polymarket. polymarked.md, so this is going to be our documentation.

[2:22]And I think that is the next thing we can do, we can just go here and we can find the polymarket Quickstart or something, right? Get started and you can see we have some information here and I can zoom in a bit here.

[2:37]So you can see, basically, what we are doing is we are using the Gamma API on Polymarket. So to make it easy, let's just copy this page, right?

[2:51]And let's head back here and just paste that in here, and that should be all we need now to get started.

[3:01]And of course, we have to set up like an account and that will also be done by using Claude Code in this case because we need to put some cryptocurrency, in this case, it's going to be Polygon into our wallet, okay?

[3:19]So let me just do set up that first, I think, so let's just go back to Claude Code and we can say read, uh, I'm on Mac now, of course.

[3:26]So read at what did we call it? At Polymarket and let's just read the documentation.

[3:36]Okay, so now you can see we have read the documentation, so the first thing we need to do is to set up a wallet.

[3:41]So I'm just going to give that instructions to Claude Code here, pretty simple, first thing we need is a wallet, so let's set up a wallet we can trade with on Polymarket, and I'm just going to say move all details into an env file when done, don't print this in this window, so we don't leak our private key, right?

[4:00]So I'm just going to let Claude Code set up my a wallet now and the next thing I'm going to do after that is I'm going to put some money into that wallet so we have some capital to start trading with our bot when we came when we are coming this far.

[4:16]So I'm just going to wait now a bit until we have the wallet set up and this should be pretty fast, I think.

[4:24]And yeah, I will take you back if there's something interesting here. Okay, that took like one minute or something, right?

[4:28]So what I'm going to do now, if you go here, I just removed the private key here, so you can see we have our wallet address, and I'm just going to transfer some funds over there, but first I can just say something like, uh, check the balance of, uh, balance of wallet, right?

[4:49]So we can just see that it should be empty and after that, I'm going to pull up some Polygon we can use on Polymarket.

[4:57]Next, that was done, so you can see we have 0, 0, and there's something called gas, we need to also transfer some Polygon for gas fees on Polymarket and the next the other thing I'm going to transfer is something called USDC.e.

[5:14]And all of this has to be transferred on the Polygon Network. So I'm going to see if I have my MetaMask here and I'm going to show you how simple it is to transfer these funds, and then we can check back again if we have something in our wallet.

[5:26]Okay, so I I didn't have MetaMask on this computer. But I should put up just a couple of screenshots here and you can see I just transferred 30 USDC.e on the Polygon Network and I took like $5 worth of Polygon as gas money.

[5:45]So let's check the balance again now and see if all of this has been transferred and it should be pretty fast, I think.

[5:50]And you can see now we have $30 and we have 57 Polygon, so yeah, we are ready to start trading now.

[6:03]And you can see won't mean to set up the Polymarket client next and place a test order, or fetch some markets first.

[6:09]So now we just going to get a bit into our strategy for today's trading, so if you go back here, and I wanted to do the Bitcoin 5 up and down.

[6:20]But what how should we trade this, right? Because there's a lot of strategies you can do here, bunch of differents bots you can build if you wanted to.

[6:30]But uh, something we can do, we can go to, let's say, since you have leaderboards, right?

[6:38]And we can go to crypto and we can check, let's say, Bonereaper, he's like a big trader here on the 5 up and down markets.

[6:50]So what we can do is, uh, we can just copy his address, right? And we can try to see how he does this trading.

[7:00]This is a strategy you can do. So let me just do a prompt here and we can try to learn from how he because you can see he is a very profitable trader.

[7:08]If you go like one month, that is a pretty steady graph. How much is it? $330 past month in $30,000 in profits.

[7:21]So let's just try to see what he do. So if we go here and let's do a prompt here.

[7:26]So let's just do uh, we are building a trading bot on the 5-minute BTC up and down market.

[7:30]We have a profitable trader here, and we just paste in the address, aka Bonereader. Let's do extensive research to find out how he trades and try to replicate that.

[7:42]Come back when you have a plan, use sub-agents, et cetera, and let's build that profitable bot. So I'm just going to launch this and we're going to see what we can find out from this.

[7:54]Since the blockchain is public, we can try to figure something out how we actually do this. But it's not going to work 100%, right?

[8:05]Because, yeah, that's not it's not that easy. But we could get like an indication and we can try to do some of the things he does.

[8:14]And then we can see how it goes, right? Remember, this is mostly for fun, you will not probably not make a lot of money on this, but uh yeah, we can talk about that later.

[8:26]So let's just execute this research. You can see we launched a couple of background agents that's going to check his trading history, we're going to look at the market mechanics and we have three local agents now and they are running.

[8:40]So let's just wait for this, and then we're going to start building. So after I think it spent like 10 minutes researching, 10-15 minutes.

[8:49]You can see what Bonereaper, that's the correct name, actually does. Strategy, it's a late-window convergence scalping, not directional prediction.

[9:00]So he has made 180,000 trades over the last week, that's crazy. So basically, what he's trying to do, he is not he also holds every position to resolution, he's not selling out because that costs some kind of fear, right?

[9:16]And he is trying to get most of his dollar into prices when they are above .9, right? So the market is pointing to it's going to close on yes.

[9:27]He puts in like a large bet, I will guess, it's probably pretty large, and maybe he puts in like $1,000 and make like 20 and he does that in a insane high volume, right?

[9:39]So you can see his P&L is $20,000 over the last six days, that's pretty good. And basically now we're going to try to replicate that.

[9:47]Reality check against us, we are not going to probably do that, but we just going to try to set it up to have some fun.

[9:54]So we have our proposed plan here, tooling, we're going to set everything up, we're going to do a live size, scaling, and now we have some questions.

[10:02]So do we want to do like a shadow trade first? Probably you should do that, but we just going to go straight to live just to have some fun. Yeah, entry price cap, so yeah, we can just do, uh, 97.

[10:19]It doesn't really matter too much. This means that we will only enter when it's .97 or something like that. Uh or should we just do we can just do 95, it doesn't really matter too much.

[10:32]So that is our setup and hopefully now Claude Code will look at all the data we have collected from the research phase and it will start building out this trading bot here.

[10:44]So when we have the backend set up, we're going to do something fun and that is build like a cool dashboard where we can follow everything in real time and see how our trades go.

[10:54]So let's just wait here now for Claude to kind of build out the backend and then we're going to build out the UI and we're going to try to set it up and do some trading.

[11:03]Okay, so you can see now, Claude Code is actually building out this, you can see we have our little to-do list here.

[11:10]Good, that looks pretty standard. We have the club book, spot price readers. Yeah, order placement with the SDK. Yeah, everything looks as I've seen it before, so that's good.

[11:20]You can see we can even read the book, we could fetch the live market, that is pretty good. So one thing that is not so important with this strategy is latency.

[11:31]But if you're going to do some other strategies like I've done before like arbitrage and stuff like that, latency is very important and my system is just too far off the server.

[11:45]So these high latency trades are very hard. But uh this should work pretty good, I think. It's not going to be a big problem, I think.

[11:53]So yeah, let's just let this run through and then we're going to test it out first and then we're going to build the UI.

[12:00]So you can see now, uh, we are just running this bot on like a dry run just to see that everything works, so this is just going to be like, yeah, we have to wait six minutes because of the five-minute window and stuff like that.

[12:11]So we just going to wait and see if the dry run passes, if we have to do any changes in our setup here.

[12:18]So you can see now the dry bot run has completed. Before going live, I need to check one critical gotcha: USDC.e allowance.

[12:24]When you buy when you place an order on the polymarket, the CTF exchange needs allowance to move your USDC.e. Yeah, this is important.

[12:32]This is something we should have probably have set up before. Okay, so the easiest way to fix this is let's say you don't have an account on Polymarket.

[12:41]Just hit sign up, right? And you can download MetaMask first. I like to use that, so I'm just going to create a new wallet here and I'm just going to do it with my Google account, for example.

[12:52]Okay, so I created my MetaMask wallet. I'm going to open it, right? Uh yeah, just going to agree with that.

[13:00]Okay, so now we can just sign up with MetaMask, requesting signature, and we're going to connect this account. That was done, and we can just call this all about AI or something.

[13:12]Check that off. What's your email? Enable trading, approve tokens spending, enable auto redeem.

[13:21]Okay, so now that we have our account, let's just copy our address here, and let's go back here, and I'm just going to say, this is the Poly account add, and let's just paste that.

[13:35]And from this, we should be able now to set it up so we can start trading. Also see now that we have a portfolio balance here on Polymarket.

[13:44]So now we are running this bot live as a test, just going to run through this first test now, this is just going to take five minutes, and that if that is working, we're going to move on to building the UI, so we can start monitoring our trades more, yeah, interactively and a bit more interesting than just looking at the terminal here.

[14:05]So if we go in here now, we can go to our portfolio, right? And you can see open orders. We don't have anything, we don't have any positions.

[14:14]But I didn't think we closed our position because now our portfolio is over $31, right? But if you go over to history, you can see we placed this here that has a $5 value, and that means that we should be live now.

[14:28]So I can just say, how did we do? So let's check up on that, and the fun part, what I think is the most fun part after we have done the after we have done the, the back end is to build out the front end, so we can kind of track everything in real time.

[14:47]So yeah, hopefully it everything is set up for that now and we can start doing that. So we can see, we have done a couple of trades, we made a couple of dollars, and if you go to the P&L now, you can see we are $2 up already, 7%.

[15:01]So that was a great start. But now we want to move on to our, yeah, we're just going to stop this now. Okay.

[15:09]Okay, so here we're going to do some refinements to our strategy now. I think we just going to implement all of those, we're going to raise the loser floor, we're going to, uh, raise the max press entry.

[15:19]Uh, no, I'm not going to do that. I'm going to tighten the t remaining, so we only going to trade in the last 35 seconds, but we're going to skip the eight seconds at the end.

[15:31]So we only have like a 20-second window to trade on. I think that's pretty smart. And let's just do that, and how to long before the next checkpoint.

[15:42]It doesn't really matter. I'm just going to let it run because we just going to follow this in real time. So now we're going to make those changes and before we fire it up again, I need the the front end.

[15:54]Okay, so I'm just going to make a few adjustments to the I'm just going to keep one position open at a time because we only have like 30 bucks, so I only want to keep a one open.

[16:05]So we are ready to run this live, but before that, like I said, let's think about what kind of UI do we want for this?

[16:17]I was thinking maybe we can do some kind of Bloomberg terminal style, but let me think a few seconds. So the style we're going to go with is now we want a UI dashboard to keep track of our trade, PNL, stats, et cetera, in real time.

[16:26]I prefer a React Vite stack frontend, with low latency, Bloomberg terminal style, 5-second terminal blink when trade is placed, et cetera. Real-time dashboard, please implement this and connect with our backend so we can follow trades in real time.

[16:43]So before we run the bot again now, we are going to build out this, and then we're going to fire it up and I'm going to keep it running for maybe a few hours or something.

[16:54]So we're going to watch together the first kind of trades and then I'm going to come back later today and see how we have done. So yeah, let's just build this out and wait a bit.

[17:04]Okay, so we have our dashboard. So let's just look at it together. So I'm just going to copy, yeah, we're just going to run this local, right?

[17:11]And yeah, how is it looking? All right, that's pretty cool, right? So let me zoom in a bit here.

[17:19]So you can see we have our recent trades, I like that, so we have a two that's filled, uh, yeah, that's fine. Yeah, that's good.

[17:30]And you can see here we have our total equity, I like that. We can see our address, and here is just a decision log live.

[17:37]And here is the window, yeah, this is basically all I wanted. You can see, uh, Bitcoin five minutes. We have the time in my locale, that's good.

[17:45]So now I just think we just going to say, start the bot. Looks great, uh, great, by the way.

[17:55]And let's just do it. So, uh, let me know, start a live, and we should be up and running. And if you can see now, there are only like 2:28 left of this window.

[18:10]And we can see the prices here. Down is now at 94. Hope you can see that. So this should be maybe something we will aim for.

[18:17]The ask is 95. Uh, okay, we are up and running, that's good. So let's just wait and see now if we get into any open positions here.

[18:27]Maybe I can zoom in a bit more. Yeah, that's a bit better for you guys, I guess. Can't be adjusted. This was a big big. We should have maybe adjusted this window a bit.

[18:38]Uh, but it's fine. Uh, yeah, maybe we should do that, but for now let's just keep it like that and we can check if we get into any open positions here.

[18:50]This decision log. Is it updating live? I don't know how frequently we are polling this.

[18:57]Okay, so let's see now. We are closing in on the window again. We are like, yeah, a few seconds away, and we should see the decision log live here starting to, yeah, pull now.

[19:07]And this is a very open, it's 75 at 30 seconds left, so hopefully, we should be able to enter on our position here.

[19:15]You can see we have no open positions at this time. But, uh, we should start. Okay, so we put in like a buy, and here we get the flash, and you can see our balance updated super fast, so I like to see that.

[19:30]We did actually go for an up. Oh, that radically changed. This might hold. Uh, it actually looks like did it hold? Let's see.

[19:42]So we went for up and we bought in at yeah, let's see. I think it actually did resolve at up here.

[19:51]So we made a few cents here, maybe like, we'll see afterwards. But I think we actually, yeah, that went a bit fast at the end here.

[20:02]So maybe we need some way to see the resolution, but actually, the bot worked, and that is only what I wanted to see.

[20:10]So, of course, this is not redeemed, this position. We have an open value of $1.71. So let's just see if we can redeem this over time.

[20:19]But you did see the flashing thing when we bought here, that worked very good, and I was quite happy how this, yeah, we did manage to we actually got up to like 34 here.

[20:30]So I'm not 100% sure what happened, but we might investigate that later. But I'm just going to let this bot run now, right?

[20:38]Okay, so we did adjust down here again. But we made a few cents, so that's good. So I'm just going to let this run now and I'm going to come back, yeah, in like an hour or something.

[20:48]And we can see how we are doing. Maybe I'll do a second update in like a couple of hours. And I'm just going to let this run, but like I have no expectations.

[20:58]It's just going to be for fun and that is what this is. It's pretty fun to just while you're playing some games, you can watch your trading and stuff like that on this dashboard. So just something I do for fun.

[21:09]So let's just let it run for a while and I'll take you back when we, yeah, there's been some time past. Okay, so there's been like an hour or something.

[21:19]Now and I'm just going to call it now for the video because I wanted to post it today. So you can see we are still creeping slowly up. We are above 34 now.

[22:21]I think we were at that before. I can't really remember, but we haven't run into any losses yet. Like all the trades we have done have been profitable.

[22:29]But, uh, don't get baited too hard, this could change any time, right? If you make a miscalculated trade here now, we will probably lose by far the gains we have done so far.

[22:40]But for me, this is just for fun. This is something I like to do. I don't really care about the results. I'm just learning, having fun and playing around with agents. We can build upon this if we wanted to make it even more interesting.

[22:53]We might do that in the future. So yeah, that is what I wanted to share with you today.

[22:58]Uh, is this like a something that's going to make you rich? I wouldn't put a lot of money on this, like at least before you have done a lot of extensive testing over time.

[23:08]But you could play around with these parameters we set up in our config file if you are following the same as I did. But uh, I just like to have it up for fun.

[23:17]I think it's just fun to follow along. I enjoy it more than just doing some random casino thing. It's just more interesting and you can kind of, yeah, uh, affect the results a bit more.

[23:31]If you are interested in, uh, automation, I have created a new Discord, I got my Discord back. So if you want to talk about AI automation, stuff like that, I will be sharing some other projects I'm working on.

[23:42]We already have a few people joined the server, so I'm going to leave a link to the Discord AI Automata server down in the description.

[23:51]If you want to join, talk about AI automations, making money with AI automations, whatever, can be anything, just, uh, if this sounds interesting to you. So yeah, that's what I wanted to share today.

[24:03]Hope you enjoyed it and, yeah, good luck with your trades if you get into this, and I'll see you again very soon.

Need another transcript?

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

Get a Transcript