HomeVideos

Polymarket Trading Bot Deep Dive (Part 2): SignalLayer, StrategyLayer & PositionLayer

Now Playing

Polymarket Trading Bot Deep Dive (Part 2): SignalLayer, StrategyLayer & PositionLayer

Transcript

345 segments

0:00

Okay, so in the past video I talked

0:04

about the websocket event bus and the

0:07

processing pipeline. That was the part

0:08

one of the poly market trading bot deep

0:11

dive. So that was basically

0:16

all of uh this part of the architecture.

0:20

So from the config layer to the data

0:23

sources, pipeline adapters and then

0:25

processing. And then I talked about the

0:27

event bus. So if you haven't seen that

0:29

video already, I would suggest to see

0:30

that first before you are watching this

0:32

one because in this video I'll be

0:34

explaining some changes that I've done

0:36

to the architecture but also our signal

0:39

layer and then this part over here which

0:42

is also a new kind of a new feature and

0:45

then I'm going to in kind of you know

0:48

big picture stuff explain the strategy

0:50

layer and then the execution layer.

0:52

There's a lot of details in this. I will

0:54

probably make another part three video

0:56

about that. for example, how the orders

0:58

on poly market is handled, you know, and

1:01

and stuff like that.

1:05

So, just to reiterate, we have some data

1:08

sources which is, for example, the

1:10

Bitcoin price and the poly market

1:12

channel that sends us raw data in our uh

1:18

trading bot, right? So, we have the raw

1:19

data that's passed over to some pipeline

1:22

adapters. basically just adapters that

1:25

tags this raw data uh so we can use it

1:29

our in our system. You can see how we

1:31

have different tags, different data that

1:33

we're going to use later in our system,

1:35

but we also have the raw message. Then

1:37

we have the processing unit that takes

1:39

basically the raw data from the tag

1:41

message and then process it. It's to you

1:44

know a uniform uh event that we then

1:46

call processed event that's then going

1:49

to be pushed to the event bus. And the

1:52

event bus basically you know pushes this

1:55

event out to all the different module

1:57

modules that have subscribed to the

1:59

event bus. In uh this video I'll be

2:01

talking about the signal layer. So the

2:02

signal layer can be many different

2:05

signals that we want our strategies to

2:07

subscribe to. So a very classic trading

2:11

you know strategy in in stock trading is

2:14

like looking at the you know moving

2:16

averages and stuff like that. So a

2:17

moving average could be you know the

2:19

average price over a given period

2:21

crosses another moving average over a

2:24

time period. So that's a signal that we

2:26

can create in our signal layer. So we

2:28

then have our signal um subscribing to a

2:33

a data source through the event bus and

2:37

then when it's produced the signal it

2:38

then pushes it over to the event bus

2:40

again and then that distributes the

2:43

signal to other areas of our trading bot

2:46

that has subscribed to it. In our case

2:48

it's going to be the strategy layer. the

2:51

strategy layer we have different

2:52

strategies that you know says all right

2:55

for example strategy one have signal A

2:58

and B so that will then subscribe to

3:00

those signals and the event bus

3:03

basically just manages all of that so

3:04

that's kind of a recap of what we talked

3:06

about so to take a little bit deeper

3:10

talk about the signal layer and the

3:13

strategy layer I have to refer to the

3:16

code this file here is just an example

3:19

file that demonstrate how I kind of put

3:22

all these different layers together. So

3:24

you can see how we uh start by

3:26

initializing the event bus. So that's

3:29

the main parts here, right? The event

3:31

bus. And then we create a signal

3:35

producer. So the signal producer is um

3:39

you can see how we defined it over here.

3:41

In this case, it's the exponential

3:43

moving average signal producer that

3:46

takes a basically takes a base class

3:48

that have different methods that we

3:52

require it to implement. And then you

3:54

can see how we have the event bus and

3:56

then we have the method subscribe and

3:58

then the producer the signal producer

4:00

and then the signal producer has an

4:03

onevent method. So what happens here is

4:06

we create a signal a signal producer

4:09

that we call it. We subscribe that to

4:12

the event bus and then it gets like a

4:14

call back method an onevent call back

4:17

method. So the event bus knows what

4:20

method to call when the signal produces

4:23

a signal basically and then we basically

4:26

do the same with the with other kind of

4:28

signals. So, so we can provision

4:29

different kinds of signals that we want

4:32

to use in in our strategies basically.

4:34

And then a new thing that I have added

4:37

to the architecture is two different

4:40

things. A position tracker and then a

4:44

pending order tracker. And they kind of

4:47

they are very similar but they they

4:49

serve different purposes. The pending

4:51

order tracker it's kind of in the name.

4:54

So when we produce

4:56

an action so you can see here in our

4:59

strategy layer they can produce actions.

5:02

So in the moment

5:04

in the moment it creates an action

5:08

it will actually omit an event to the

5:10

event bus that then create the pending

5:13

order tracker. And this responsibility

5:17

the responsibility of the pending order

5:19

tracker is to basically just monitor the

5:22

state of of the action that we omitted

5:25

because when we do it well we don't know

5:28

it's kind of a void. It's kind of a

5:30

black box. We don't exactly know what

5:31

happens over at poly market or any other

5:33

market for that matter.

5:36

So that that's why this one will have

5:38

the responsibility of basically

5:40

monitoring through the events that we

5:43

get from the market over here. So we

5:47

have the uh the user channel websockets

5:49

and I'll be explaining a little bit more

5:51

about this in this video because this is

5:53

also something I changed um but but more

5:56

on that just in a moment. And then

5:59

when when that happens that it gets

6:02

notifications from the websocket that

6:05

hey now the order has been filled

6:07

it omits a sim similar event that the

6:10

position tracker subscribes to and then

6:12

it this one the position tracker

6:13

actually tracks our actual positions

6:16

that has been filled. So you can see how

6:18

we we do it kind of in a similar we do

6:21

it in a similar way like the signal

6:22

producer. The signal producer produces

6:24

events and then we also have it with the

6:26

uh the position tracker. The position

6:29

tracker um is also subscribe to the

6:32

event bus that you can see here we do it

6:34

and then we also have an onevent method

6:37

and you can see here if we go over to

6:39

the uh this position tracker you can see

6:42

here how we have the onevent

6:44

and then we have a self.handle handle

6:46

trade.

6:48

Okay. And you can see how we look for

6:50

specifically for the trade event

6:53

and then we mark it in our state. So we

6:56

have a kind of a state management of of

6:59

the the the position here. And then you

7:01

can see here later we we basically

7:02

construct a position in our code and

7:05

then we omit their event. You can see

7:08

how we do we do self.eventbus.publish

7:10

publish position.

7:12

And that happens when we actually have

7:14

the trade filled in in this example

7:16

here. Again, it's just a this is just an

7:17

example. It's not the final it's not the

7:20

final code. I'm still working on this

7:21

just to make it robust. And actually now

7:23

I'm in the process of testing this out

7:26

doing live uh trades just to make sure

7:29

that it's actually working as as

7:31

intended because we kind of want to have

7:34

state in our in our butt to be able to

7:37

monitor you know pending orders and then

7:40

also actual orders that we then later

7:42

can sell or redeem or whatever we want

7:44

to do with them. And then I mentioned

7:46

that I've done some changes up here. So

7:48

before I had actually these two being

7:52

like one part now I've actually

7:54

separated it between

7:57

tradable markets

7:59

and then just data sources because I

8:02

think there's a very big distinction

8:05

between this. So for example, a tradable

8:08

market in our case is poly market. But

8:10

what if we want to connect this trading

8:12

bot to Kelshi or opinion or what if we

8:15

want to trade just regular crypto

8:17

markets because this is potentially

8:19

possible in this trading bot so we don't

8:22

like so we don't restrict ourselves just

8:24

to one market and maybe we want to do an

8:27

arbitrage between several different

8:29

markets. Let's say we want to subscribe

8:31

to both Kelsey and Poly market look for

8:34

similar event because now they have

8:35

15minute bitcoin markets and both Kelsey

8:38

and poly market. So there might be some

8:40

you know arbitrage opportunities there.

8:42

So in order to you know capitalize on

8:45

those arbitrage opportunities we need to

8:48

be able to connect several different

8:49

markets. And that's why I've made the

8:50

distinction between the the markets and

8:53

then the data sources. So the way I've

8:55

done it is I've I've um kind of made

8:59

market adapters that defines standards

9:04

methods in our codebase that different

9:06

markets has to conform with. You can see

9:10

how how I have created this market

9:12

adapter

9:14

and this market adapter is what a poly

9:17

market adapter would inherit from and

9:21

this one defines which kind of methods

9:24

that the adapter should basically create

9:28

and that allows rest of the trading bot

9:31

to easily hook into these methods and

9:33

then execute trades for example.

9:36

So for example, we have a discover

9:38

markets

9:39

and we have a get market method and we

9:42

have have also a subscribe order book

9:45

and a subscribe ticker and a subscribe

9:48

trade and so on. And then of course more

9:51

important we have something like a place

9:52

order.

9:54

We have a cancel order. We have a get

9:56

balance

9:58

and we have all other kinds of events

10:02

that we want

10:04

all all the markets to basically

10:06

implement because if they don't

10:08

implement this the trading bot will not

10:09

work and at this stage I'm not sure if

10:13

this is the final version. This is kind

10:15

of a barebones version of of the market

10:18

adapter I think. And and so again as I

10:21

said I'm still in the process of testing

10:23

this out. So I there's probably going to

10:24

be more changes to this.

10:26

And then if we look at the poly market

10:31

adapter, you can see here how it

10:33

inherits from the market adapter that I

10:35

just showed you before

10:37

and then it actually implements the the

10:41

methods to to to do these um um you know

10:46

methods. So for example, the discover

10:47

markets. All right. It then implements

10:50

the discover markets the way that poly

10:52

market expects it. Right? So we we we

10:54

hook into the different API calls and

10:56

stuff like that that they have on poly

10:58

market. And again we do this for all the

11:00

different methods like how do we place

11:02

an order, how do we cancel an order and

11:04

and and so on and so forth. And then we

11:07

also have the uh translate ticker. So

11:09

the the the translate ticker basically

11:11

just takes the poly market data and then

11:14

it transform it over to a uniform event

11:17

type that we can use in our in our bot.

11:19

So for example poly market they use best

11:23

ask best bit side size

11:26

price and asset ID. Well Calia might use

11:31

some other words and opinion might use a

11:34

completely different word. So we need to

11:37

be able to figure this out in these uh

11:40

methods and then omit a standard event.

11:42

And this goes this goes back to the um

11:47

the the basically the processing right

11:49

the processing unit that we have down

11:51

here which kind of is uniform now in

11:54

this here um where we take the data and

11:57

then we make it uniform so we can use it

11:59

in in the rest of our of our bot. But

12:02

besides that the the the pipeline

12:04

adapters and the data sources kind of

12:06

remains uh the same. That's basically it

12:09

for now. So the last remaining thing

12:11

that uh I need to implement uh to make

12:14

this all work is the execution layer

12:16

which is basically what I've done

12:17

already and now I'm just testing it out

12:19

and I'm testing this out together with

12:22

the position tracker and pending order

12:24

tracker to make sure that that actually

12:25

works correctly. and then I'm trying to

12:28

implement a front end so it's going to

12:29

be easier to manage the uh strategies

12:32

through a nice web interface instead of

12:35

using the terminal. So that's kind of

12:36

the next step and um in the next video I

12:40

will be explaining a little bit more

12:42

about the order flow of the of you know

12:46

on poly market how that happens when we

12:48

actually place live orders but I'll get

12:51

more into details about that later. So

12:53

if you haven't already joined our

12:56

discord server, I would highly highly

12:58

suggest you to do so because we already

13:01

have a lot of members. We have already

13:03

now we have you can see here we have

13:06

uh 100 online and then we have uh let's

13:10

see we have

13:13

you can see here we have 492

13:17

offline. So we have more than 600

13:19

members in the discord server. And you

13:21

see how we have members joining all the

13:23

time.

13:24

We have Julian here joined just a few

13:27

minutes ago. So it's like several people

13:29

every hours joining every single day.

13:32

And we only been have this discord up

13:34

and running for like I think it's around

13:36

two days now. And we have like more than

13:38

600 members join. So that's really nice.

13:40

And we have a lot of nice uh

13:43

conversations in uh the general chats

13:46

where we have a lot of people talking

13:48

and we have other channels coming also.

13:50

So it's a really good place to get alpha

13:52

on poly market and prediction markets in

13:54

general and we have a lot of new things

13:57

coming along here the coming weeks. So I

13:59

would highly suggest you to join if you

14:00

are serious about doing poly market

14:03

trading and poly market trading butts

14:04

and also calian and opinion trading but

14:07

everybody is welcome. It's called poly

14:09

traders that you can see here poly

14:12

traders and I have made sure to leave a

14:15

invite link down below in the

14:17

description. So, make sure to check that

14:19

out if you're interested in poly market

14:21

trading bots. So, until next time, see

14:24

you and have a good one.

Interactive Summary

This video is a deep dive into the poly market trading bot, focusing on recent architectural changes and new features. It revisits the core components like data sources, pipeline adapters, processing units, and the event bus, which were detailed in a previous video. The discussion then moves to the signal layer, explaining how signals like moving averages are generated and distributed via the event bus to the strategy layer. New additions to the architecture include a position tracker and a pending order tracker, designed to monitor the state of actions and filled trades, respectively. The video also highlights a significant architectural change: the separation of tradable markets from data sources, enabling the bot to connect with multiple exchanges (like Kelshi or Opyn) and potentially perform arbitrage. This is achieved through market adapters that define standard methods for different market interactions. The presenter also mentions upcoming work on the execution layer, front-end development for easier strategy management, and future videos that will delve into order flow and live trading details. Finally, the video strongly encourages viewers to join the "poly traders" Discord server for community discussions and alpha on prediction markets.

Suggested questions

8 ready-made prompts