HomeVideos

Designing a Polymarket Trading Bot – System Architecture

Now Playing

Designing a Polymarket Trading Bot – System Architecture

Transcript

281 segments

0:00

All right. So, in my past videos, I have

0:03

been mostly focused on analyzing, you

0:06

know, other traders. I've been focused

0:08

on the actual strategies and kind of

0:11

just been testing and vibe coding and

0:14

trying to figure out, you know, and

0:16

learning about poly markets. But now

0:18

it's time to take things on a little bit

0:21

more big picture stuff. we actually need

0:24

to figure out the overall solution

0:28

design and and architecture of the the

0:31

bots and I think I I don't think I'm

0:34

sure that this will make like the whole

0:36

software the whole bot much more robust

0:39

and actually better performing uh in the

0:42

future. So in this video I have designed

0:46

the complete uh architecture in like a

0:49

big picture way. So with these diagrams,

0:51

so I'm going to go through it and of

0:53

course you can copy this or you can use

0:55

this for inspiration in your own but of

0:58

course I encourage you if you have any

1:00

ideas or if you see any flaws in this

1:02

design to you know leave a comment down

1:04

below and then I can refine uh this this

1:07

bot here. So let me start from the from

1:10

the top from the from the in the

1:12

beginning of the bot. Of course we have

1:14

what I call the the config loader and

1:16

this is basically just all the settings

1:17

that we're going to have in our bot. The

1:19

settings can be different things such as

1:21

the different strategies, what kind of

1:24

data that we need and uh different you

1:27

know settings. A relatively easy and

1:29

simple way to configure the bot before

1:32

we click play basically. And then we

1:36

have the different data sources that I

1:38

will be uh referring to as pipelines.

1:42

So this can be for example most

1:45

important is the market channel

1:47

websocket from poly markets and we have

1:49

the user channel websocket and other you

1:52

know channels. The idea behind this is

1:54

we that we are able to connect several

1:56

different data sources into our bot and

1:59

then make you know actions based on

2:01

those uh data sources. It could also be

2:04

for example the weather in Canada or it

2:07

could be you know how many times you

2:10

know Elon Musk tweet tweets per day and

2:12

so and you know stuff like that. So any

2:14

kind of data uh we get that into the

2:17

data sources.

2:21

So when we get the data it's just the

2:23

raw data that we receive into our butt

2:28

and then we have these pipeline

2:30

adapters.

2:32

So, so what they do is they basically

2:34

just take the data and then they tag it

2:38

and you know the specific details I'm

2:40

not sure about yet. So this is just an

2:43

an overview and sort of an idea of how

2:45

to go about this. So it could be

2:47

something like the the the a timestamp

2:50

of when it's received what kind of

2:51

source is it the poly markets websocket

2:54

and then what kind of a event it is in

2:57

this case the event type price change as

2:59

an example. So this is what we call the

3:02

tagged message

3:05

that's then passed over to our

3:07

processing unit.

3:10

The processing unit has again different

3:13

uh methods to process the actual data.

3:17

So you can see here in the tag message

3:18

we still have the raw data from our

3:21

pipelines from before. So we haven't

3:23

touched that yet. Now the processing

3:26

units uh task is basically to take this

3:30

data the raw data from the event. For

3:32

example, it could be a price change or

3:35

it could be you know a change to the

3:37

order book. It's going to take that data

3:39

and then convert it to you know a format

3:43

that we can use in our bot because you

3:45

have to consider that let's say we have

3:47

10 different data sources that all

3:50

return let's say a price of some kind.

3:53

Well, we need to make sure that all the

3:56

fields and so on and so forth has the

3:58

correct format. And so basically just

4:01

unifying the the format so we can use

4:03

that in in the rest of of our our

4:07

program.

4:08

So so that's what I call the processed

4:11

event. Okay. So we get the raw data in,

4:14

we tag the data and then we process it.

4:19

We then pass it over to our event bus.

4:21

So the task of the event bus is

4:24

basically just to to orchestrate

4:28

the different events that we have to the

4:30

different signals. So for example, let's

4:32

say that we have a simple moving average

4:35

strategy. Let's say a crossover of 14

4:39

SMA over you know 28 SMA. That's a

4:43

signal that we register in our bot. And

4:46

this signal then subscribes to the

4:48

specific events that it needs. Let's say

4:50

we have uh 10 different sources, but

4:53

this one we only need this one only need

4:55

to subscribe to let's say the BTCUSD

4:58

uh data source.

5:01

So that so that's uh this event bus's um

5:05

you know responsibility to basically

5:08

forward the processed event over to our

5:12

signal signal layer that has the

5:14

different signals. Okay.

5:18

Now the signal layer's responsibility is

5:21

then producing the signal. So for

5:23

example, it could be the exponential

5:25

moving average. Okay. So it then takes

5:29

the event and then it it computes the

5:31

signal.

5:34

In this case, it's the exponential

5:35

moving average, different kind of

5:37

periods and so on.

5:39

It passes that data

5:42

over to our strategy layer.

5:45

So, this is kind of where I'm not sure

5:48

if this makes this spot a little bit

5:50

different, but the bot will be able to

5:52

actually have several different

5:54

strategies in one. So, it can subscribe

5:58

to several different markets, but also

5:59

have different strategies. So, we have

6:02

different strategies that also subscribe

6:05

to different signals. So, you see the

6:08

strategies subscribe to signals, the

6:10

signals describe subscribe to events and

6:12

so on.

6:15

And the strategy is then you know that's

6:17

where we formulate the actual strategy.

6:19

For example, the moving average

6:21

crossover or whatever strategy that we

6:24

you know figure out and that produces

6:26

actions.

6:28

Again the details here is not clear yet.

6:30

It's just an overview and it produces an

6:33

action payload. For example, it can be

6:36

the buy action to buy, you know, uh

6:41

whatever for an amount and then a reason

6:44

and other data that we deem fit for for

6:47

the action um payloads here.

6:52

It then moves over to the execution

6:56

layer. So this is where the you know

6:59

this is where we actually make the

7:00

orders. So, we have some validation and

7:02

maybe some locking. Again, a little bit

7:04

fluffy about the details here. And then

7:07

we have a a risk manager. So, um I'm not

7:11

sure exactly how I'm going to go about

7:13

this yet because I'm also thinking about

7:15

implementing methods to control the

7:18

risks uh within the strategies. For

7:22

example, if we have let's say we have

7:25

three or four different strategies

7:27

actually the same strategy

7:29

but maybe with different risk profiles

7:32

and we want to you know test that

7:35

then we need some kind of a risk uh

7:38

module on the strategy layer and uh and

7:43

so this risk manager is potentially just

7:45

a portfolio risk manager like an overall

7:50

uh risk manager. So I need to think

7:52

about that a little bit more. And then

7:54

we have the execution. So the actual you

7:57

know do we take the action go through

8:00

all these steps and then we execute it.

8:03

After we execute the trade this is kind

8:05

of this is what I've just been kind of

8:08

thinking about is well how do we

8:10

actually manage it then? How do we

8:12

because the strategies probably also

8:14

need to know when to exit. For example

8:17

let's say that all right we place a a

8:19

buy. First of all, we need to monitor is

8:22

it getting filled and if not then what

8:24

are we going to do unless the poly

8:27

market client handles handles it for us

8:31

but in case it doesn't and in case we

8:32

have to manage that manually well we

8:35

have to figure that out and I do have a

8:37

solution for that I'm going to explain

8:38

and then we also have to manage let's

8:40

say we need to close

8:42

um positions maybe we need to sell

8:45

positions based on the definition in the

8:48

strategy Okay.

8:50

So

8:52

this is where if we go back to the top

8:57

uh to the data sources, this is where

8:59

the user channel websocket comes in and

9:03

potentially other methods. So we

9:07

actually take that data

9:10

um the the order data for the specific

9:13

user in our case you know our you know

9:16

portfolio because we can subscribe to

9:18

websockets as well. So for example when

9:20

an order is filled we get that in as an

9:23

event from the websocket. We manage it

9:26

just like we did before and we process

9:30

it and so on and so forth. But the

9:32

difference is it doesn't go to the

9:33

signal layer. instead it goes to our

9:36

position layer. So we need a a way let's

9:40

see if you can see it. So we need um a

9:43

place in our our bot to uh handle state

9:47

of the the positions. So, so that will

9:52

receive uh events from the use event

9:54

pipeline and then it will keep local

9:56

state and I'm thinking also a way to

9:58

synchronize. Let's say that let's say

10:01

that we lose connection of of the

10:03

websocket for 2 seconds and in that

10:06

period of time we had a trade execute.

10:08

then we need a way to uh synchronize and

10:10

then we can utilize

10:12

another

10:14

pipeline which could just be an you know

10:17

a an API call that just polling every I

10:21

don't know 5 seconds every second or

10:23

whatever the rate limit allows us to

10:25

pull.

10:29

So the position layer then enables us to

10:33

um yeah just to keep a track of the

10:35

positions and so on. And we can also

10:37

have our

10:40

we can also have our strategies

10:42

subscribe to

10:45

the for example the user channel web

10:47

socket. So let's say we subscribe to the

10:49

market channel which gets all the you

10:53

know price updates for the market but

10:54

also the user channel. So let's say we

10:57

subscribe to the market channel. We

10:59

figure out all right now we want to buy.

11:00

We then make an action to buy

11:03

and then when it's filled, we get the

11:06

event from the the user channel here all

11:10

the way down through here through the

11:12

event bus and then to the signal layer.

11:14

Now,

11:17

this is where I'm not 100% sure yet

11:21

because we probably don't want to have a

11:23

signal specifically for

11:26

uh like all right now this order has

11:28

been filled. So we potentially need

11:32

this event bus to also just relay it

11:35

directly to uh the strategy layer.

11:41

Not sure yet. So this is need to think

11:43

about this a little bit more. So of

11:44

course if you have some suggestions how

11:46

to solve that or if you have some ideas

11:49

um how to improve this then please let

11:52

me know down in the comments below. And

11:55

um the next step will then be to

11:57

implement all these different modules.

12:00

And I've already begun working on the

12:03

the the pipelines here, the pipeline

12:06

adapters, the processing unit,

12:09

and then part of the event bus. I'll be

12:11

making a video about that when the code

12:14

looks good and when it's um you know,

12:17

when it's in a good order. So, I expect

12:19

that uh video to be ready hopefully um

12:23

hopefully tomorrow. So, I hope you check

12:25

in uh tomorrow and subscribe to receive

12:27

notification next time I post a video.

12:29

So, see you on the next video. Have a

12:31

good one. Bye.

Interactive Summary

The video details the comprehensive architectural design of a robust trading bot, shifting focus from individual strategies to a complete system. It describes components such as the config loader, various data sources (pipelines) processed and tagged by pipeline adapters and processing units for uniform formatting, an event bus to orchestrate events to specific signals, and a strategy layer to generate actions. An execution layer handles order placement and risk, while a position layer tracks post-execution trade states using user channel websockets and synchronization, ensuring reliable trade management. The speaker invites feedback on specific design aspects and outlines future implementation steps.

Suggested questions

5 ready-made prompts