HomeVideos

WebSocket Event Bus & Processing Pipeline – Polymarket Trading Bot Deep Dive (Part 1)

Now Playing

WebSocket Event Bus & Processing Pipeline – Polymarket Trading Bot Deep Dive (Part 1)

Transcript

415 segments

0:00

Okay, so we are back with another video.

0:02

So in this video I will be going in a

0:05

little bit more details about how I

0:09

handle all the events the you know event

0:12

layer the processing layer and also

0:16

doing uh somewhat of a smaller refactor.

0:18

So I've been looking into the to the

0:20

comments here and

0:23

I specifically

0:26

uh saw this one here.

0:28

Uh he says I think this is the one.

0:33

Yeah, nice architecture. Signal layer

0:36

should publish to the event bus and uh

0:38

strategy layer consumes all signal plus

0:41

position. So this um actually inspired

0:44

me from cool ball cool

0:48

uh to do somewhat of a small refactor

0:52

compared to the previous uh video I did.

0:56

So you can see here it looks a little

0:57

bit different. So we have the event bus

1:00

actually being the more central part of

1:02

the the trading uh bot here. So we still

1:06

have we still have the data sources for

1:09

example the market channel webocket the

1:11

user channel web socket from uh from uh

1:15

poly market here over to the pipeline

1:17

adapters over to the processing unit. So

1:20

what happens here is we have the event

1:22

bus sort of as a central unit that

1:24

basically just orchestrates all the

1:26

different events meaning that for

1:28

example we have the signal layer with

1:30

all the different signals that we use

1:32

for our strategies. This one can

1:34

subscribe um to events from the event

1:38

bus and it actually does that indirectly

1:40

through the strategy layer. So you can

1:42

see how I have errors coming back and

1:43

forth. It means for example with the

1:45

position layer the position layer can

1:48

subscribe to the event bus. For example,

1:50

if we are looking for the events from

1:53

the user channel and then whenever it um

1:57

matches let's say it matches uh one of

1:59

these events and then updates our

2:02

position state that a trade has been

2:05

filled. it can then sub um it can then

2:08

uh actually publish an event to the

2:11

event bus and then other parts of the

2:14

trading bot um can subscribe to that

2:17

event as well. So let's say that our

2:19

strategies relies on uh market fills

2:24

then it can actually subscribe to that

2:27

uh event uh through the event bus to the

2:30

position layer.

2:32

So um that's actually how that works.

2:34

And then for the execution layer I have

2:38

I thought about also doing it through

2:40

the event bus um but um

2:44

I've chosen at the end to keep this

2:47

separate because it's um you know it's

2:50

it's actions and um and and yeah so so

2:54

the strategy layers are still publishing

2:56

these sort of action events over to the

2:59

execution layer. So um yeah, so thank

3:03

you for that cool B and um

3:11

yeah and I saw this comment here as

3:13

well. I just replied to it actually Mr.

3:15

Flipstar I think uh all your comments is

3:18

really great and I just put a comment

3:21

here. So I think so you have uh let me

3:23

see here.

3:26

Yeah. So position sync issue. Yeah. So

3:28

sometimes the connection drops to the to

3:30

the websocket. So you mentioned losing

3:32

websocket for two seconds. I saw this by

3:34

auto locking to subbase.

3:36

So I can recover state and restart. And

3:38

yeah, this is you know it's a good idea.

3:40

But like again like I mentioned here in

3:43

my in my comment like if I run let's say

3:45

I subscribe to market events for just

3:48

one market the 15 minute uh Bitcoin

3:52

market locking data to JSON for just uh

3:57

60 seconds is around 20 megabyte and

4:00

I'll I'll demonstrate this uh in my

4:02

video here actually how much it is. So

4:04

I'm just thinking like if you're locking

4:05

this to superb basease if you are

4:07

dropping all the data after the market

4:09

um resolves or actually are you actually

4:12

keeping it for a long time it seems like

4:14

you have to clean up a lot otherwise it

4:16

could potentially be very um very

4:18

expensive because you if you run this

4:20

you know daily uh 24 hour you know it's

4:23

it's a lot of data.

4:25

All right so let let's let me uh go over

4:27

to the more interesting parts which is

4:30

the coding here. All right. So, I've

4:32

created this um sort of a test file just

4:35

to demonstrate

4:37

uh a few areas of the trading bots. So,

4:40

specifically what I will be showing is

4:44

the the the pipelines, the pipeline

4:46

adapters, the processing units, and then

4:48

the event bus.

4:51

All the other stuff such as the signal

4:53

layer, the position layer, the strategy

4:55

layer, I will show that in a in a later

4:56

video. So for now it's sort of this uh

4:59

area here that I will be uh be showing

5:02

you. So basically we start by

5:04

initializing the event bus and the event

5:06

bus if you go into the code here you can

5:08

see here how it has a publish method. So

5:12

basically this method publishes an event

5:14

to all the subscribers. So again a

5:16

subscriber can for example be the

5:18

position layer subscribes to events

5:22

through the event bus to uh the user

5:25

channel. Okay. So, so that's that method

5:28

that handles all that part here. And

5:30

then we also have a subscribe method in

5:32

the event bus. So again, this position

5:35

layer want to subscribe to you know any

5:38

event or it could be the signal that

5:40

want to subscribe. So we have a

5:41

subscribe method and a publish method. A

5:44

publish method to publish an event to

5:46

sort of the network and then as a a

5:49

subscribe. So, we basically just start

5:51

by initializing the event bus here. And

5:54

then we have a replay mode, which is

5:57

actually something I forgot to mention

5:59

in my previous video. And I'm also going

6:02

to demonstrate this by showing how uh we

6:05

can actually do a replay of um all the

6:09

data that we're collecting. So, I'll be

6:11

showing that here later. Next part is

6:14

the processing. Okay. So the processing

6:18

layer is over here.

6:21

The the processing layer basically has

6:24

the responsibility of taking the tagged

6:27

message

6:28

and constructing

6:30

um you know the final processed event

6:33

that all our subscribers can use. So we

6:37

are kind of building the butt out from

6:39

the from the end you know so from the

6:41

event bus then to the processing unit.

6:46

And then you can see here how the

6:48

processing unit has uh of course it has

6:50

the event bus and then it has a

6:52

processors. So the processors is

6:54

specific to the different data sources.

6:57

So for example we can have a processor

7:00

uh specific for the the market channel

7:03

webocket or it could be you know for the

7:06

BTCUSD. It then process the raw data

7:10

that we have here. So that basically

7:12

what its responsibility is is to process

7:15

the raw data to some data that we can

7:18

actually use to compute our signals or

7:21

or or whatever we needed to.

7:25

So we uh have a method to register that

7:27

processor

7:29

and then we have a method to process it

7:32

using the the processor and then we have

7:36

a helper method here to actually

7:38

register the different processors. So I

7:40

can open this up and you can see how it

7:42

basically just take the processing

7:45

class and then we register the processor

7:47

that I just discussed and um we have for

7:52

example the book message. So we have a

7:53

type here. So that's the uh book event

7:57

from polyark websocket and then process

8:00

book. So that's just different methods

8:02

to handle the data. So you can see how

8:05

that looks like. So we just do some

8:07

validation of the data and um and then

8:10

we uh you know we return the processed

8:13

event.

8:15

So for example the what market it is

8:18

because we can have we can be subscribed

8:20

to many different markets. So in this

8:22

case the processed event also needs to

8:23

know well what market is it what kind of

8:25

event type. So we basically just making

8:27

these events uniform and do some

8:29

validation and stuff like that. You see

8:32

how we have the data, the best bit and

8:34

ask price, the sizes and so on and so

8:38

forth. Time stamp

8:40

where we uh we pass it. For example, we

8:43

could potentially have many different

8:45

kinds of timestamps coming in from

8:47

different events and all use different,

8:50

you know, so maybe we want to unifor uh

8:52

you make that uniform in some way,

8:53

right? So we register it here and then

8:57

we have our polyard websocket adapter.

9:00

Okay. So for for this example, I've

9:02

implemented

9:05

the uh market channel webocket here.

9:13

So that's basically the uh the pipeline

9:17

adapter

9:19

here. Okay. which its responsibility is

9:23

basically to connect with data sources

9:25

and then tag the message tag the message

9:28

with different data that that's relevant

9:30

for our strategies and for our trading

9:33

bot.

9:36

So we uh start by defining the the

9:39

polymer market web socket adapter

9:43

and this is basically where we connect

9:45

you know we we do all the connections

9:47

and and this one could be you know it it

9:50

didn't has have to be the websocket it

9:52

can also be you know creating a you know

9:55

a pipeline for you know the weather in

9:58

in France you know or you know it could

10:00

be the Bitcoin price it could be data

10:04

from from Reddit maybe you some

10:06

sentiment analysis we can feed into our

10:09

trading bot

10:10

and um and then it you know this one

10:13

basically just follows the principles

10:15

from the documentation of poly market

10:18

we have a connect method we have

10:20

disconnect method we have a subscribe

10:24

active markets

10:26

and this is probably a part of it where

10:28

I'm some it's a little bit hardcoded

10:32

here where the way it works now it's it

10:34

basically just connects to the Bitcoin

10:37

15-minute markets, you know, and then

10:39

just auto automatically connect. I will

10:41

do some refactoring here where it's

10:43

going to be easier to fine grain that

10:46

like maybe we don't want to do that.

10:47

Maybe we want to, you know, select a

10:49

specific market or maybe we want to do

10:52

the Ethereum 15 minutes or maybe we want

10:54

to do something completely different.

10:56

So, so I'm going to change that to some

10:58

extent, but for the sake of this

10:59

example, I'll keep it and and and not

11:02

really touch this for a while. And um

11:05

yeah, so so that's basically how that

11:07

works. And then we have a set message

11:10

call back on the adapter. So um and then

11:14

you have the the processing uh method on

11:17

the processing um

11:20

uh uh class over here. So that is

11:23

actually what happens here. So it

11:25

basically

11:26

connects to the websocket

11:29

where we get the raw data that you can

11:31

see here. it runs through the pipeline.

11:36

Okay, so that's that's this the pipeline

11:39

adapter here basically this one which

11:41

tags it. So it becomes a tagged message.

11:43

Okay, where we just give it some data

11:46

and then we have uh the processing. So

11:48

this um message callback will then call

11:51

this method here from the processing

11:53

unit and then construct the processed

11:57

event and the process event is now ready

12:00

for our event bus

12:02

and uh and then the and then yeah to to

12:05

be uh you know transferred to all the

12:07

different areas of our bot that

12:09

subscribes to set event. So let me

12:14

demonstrate this so you can see for

12:16

yourself how this works. And as I

12:19

mentioned

12:21

just before we have a method to record

12:25

data.

12:27

So I will

12:29

try change my screen here a little bit

12:32

so you can see. So if I go

12:35

to uh

12:38

the recordings here you can see we have

12:40

a folder with different recordings. Now

12:42

you can when I start the bot you should

12:44

be able to see a new file coming in. So

12:47

let me run it here. So you saw here we

12:49

have the file now it's empty. So it

12:52

basically uh

12:56

do like this

12:58

it will uh subscribe to the websockets

13:01

and subscribe to the serious. So this is

13:03

kind of hardcoded for now. So this

13:04

specific series that's the bitcoin up

13:06

and down. And then you can see I found

13:08

96 active events. So this is again an

13:11

area where I need to fix it because this

13:13

serious Bitcoin up and down it actually

13:15

have a lot of active markets even though

13:17

they're not really active. Okay. So now

13:19

it's actually going to subscribe to 96

13:21

different events.

13:24

So um and that's not efficient. That's

13:26

why it actually takes so long time to

13:27

begin because it subscribes all of them.

13:29

So I have to fix this. But you'll see

13:31

the data that we actually have come in.

13:34

So now you can see how it starts and how

13:36

it fills up the the files. You can see

13:38

here it's actually the same event. If

13:39

you look at at all the the slots here,

13:42

it's it's basically the same event. So

13:43

it's it's connected with 96 different

13:45

events, but it's only data from from one

13:47

we actually getting in. So that's

13:48

something I need to fix. So yeah, we're

13:51

just getting all this data in. And you

13:52

can see how we actually filling up this

13:55

uh this file here. And again, I will

13:57

demonstrate how much data this actually

14:00

is. You can see how just filling up

14:01

filling up filling up. And uh this these

14:04

are the price updates. And you can see

14:06

here how sometimes you get other events.

14:08

There's for example what a last trade

14:10

event. So that's an event when there's

14:12

actually a trade getting filled.

14:15

Okay. And we have a book message as

14:18

well. Difficult to see because it's

14:20

going so fast. So we have here we have a

14:24

book message here for example.

14:27

And um yeah, we have all the the um the

14:32

raw data here, but we also have other

14:34

data that we basically tag it it with.

14:38

Um so yeah, now it's just running and uh

14:40

we don't have any strategies or

14:42

anything. We just get the data. Um and

14:44

just to demonstrate. So so now it's

14:46

probably been running for maybe a minute

14:48

or something. So let me just uh stop it

14:50

for a while.

14:53

So now I stopped it and let me see if I

14:57

can just open it up. Here we have So

15:00

yeah, so it ran for 129 seconds and we

15:03

got 10,577

15:06

events which is like 81 and a half event

15:11

per second.

15:14

And uh most of these events is the price

15:17

change event. And um and yeah, then it

15:20

records the file that we have here.

15:22

Yeah. So this is the file here. So we

15:24

ran it for what was it?

15:28

129 seconds and it's a 6.41 mgabyte. So

15:33

let's say we have 3 mgaby per minute

15:36

time 60 and then times

15:39

uh

15:41

24. So that's 4.3 gabyte per day, you

15:44

know. It's and we've run it then 30 days

15:48

you know it's it's quickly to fill up.

15:50

So I mean yeah it's it's a lot of data.

15:53

Um

15:55

so let me now demonstrate. Now we have

15:57

this and the reason why I made this is

15:59

if you want to do a a rerun you know

16:03

because it's it's going to collect all

16:04

the data actually. It's not it's not

16:06

only going to collect the websocket

16:09

data. So if we have other signals it's

16:11

it's it's basically collecting data from

16:13

the event bus. So if the signal layer

16:17

if the SEC signal layer produces

16:19

produces an event

16:21

and publishes it to the event bus it

16:24

will be recorded and the same for the

16:25

position layer. So if I run it here and

16:29

um I do let's say we take this one here.

16:34

So I'll just pass in for now and you can

16:36

see how it runs and now it's going to

16:38

run much faster because we don't have

16:39

any limit. Um,

16:43

I actually think I made it so it's 10

16:45

times as fast as before. And uh, so

16:48

yeah, it's basically just run through

16:50

all the data again. And now it stopped.

16:51

It replayed 10,577

16:54

messages, which is, you know, pretty

16:57

fast. And again, it's just for

16:59

simulation. It's just through like back

17:01

testing.

17:02

So it allows us to just record a lot of

17:05

data and then run our strategies on it

17:07

again and again. So that was that was

17:09

basically the idea behind that.

17:14

So yeah, that's it for this video. I um

17:17

next step is actually to do a video

17:21

about um I think it makes sense to do

17:26

uh I actually maybe I'll split it up

17:28

into multiple videos, but I'll do the

17:29

signal layer, position layer,

17:33

and the strategy layer. And and yeah,

17:35

see how it goes. So, so I will uh make

17:37

sure to make that video as soon as

17:39

possible.

17:40

So, yeah, if you enjoyed this video, uh

17:43

yeah, leave a like, subscribe if you

17:45

want to see more, and soon I am ready to

17:48

announce a

17:50

a news on this channel. Something a lot

17:53

of people have been requested that I

17:55

will be launching hopefully uh soon, but

17:59

I will let you know in a separate video

18:01

when that happens. So until next time,

18:04

have a good one and see you.

Interactive Summary

The video details a refactor of a trading bot's event handling architecture, inspired by a user comment. The updated design positions the event bus as a central orchestrator for events flowing from data sources through pipeline adapters to processing units. The speaker demonstrates the implementation of the event bus, processing unit, and a PolyMarket websocket adapter. A key feature introduced is a replay mode, enabling the recording and subsequent replaying of market data for backtesting. The discussion also highlights the significant volume of data generated by logging market events, raising concerns about storage costs. Future videos are planned to cover the signal, position, and strategy layers of the bot.

Suggested questions

7 ready-made prompts