HomeVideos

Polymarket Real-Time Market Data: WebSockets Explained

Now Playing

Polymarket Real-Time Market Data: WebSockets Explained

Transcript

298 segments

0:00

All right. So, in this video, I'm going

0:01

to be explaining a little bit about the

0:04

websockets and specifically the Poland

0:06

market websockets that we're going to be

0:08

using in our bots to make trades on and

0:11

and you know implement our our

0:13

strategies. So, I'll be leaving a

0:16

timestamp like if you already know what

0:18

a websocket is, you can just continue to

0:20

the next part of the video where I

0:22

explain about the poly market

0:23

websockets. But if you don't know what a

0:26

websocket is, then stick around because

0:28

I'm just going to do a quick explanation

0:29

of what a web soocket actually is.

0:32

So a websocket is kind of similar to a

0:36

normal like HTTP uh request, but very

0:39

different also. So in a normal let's say

0:42

API application, you do one request to a

0:45

server and then you get something back

0:48

from that server. So you can see here in

0:51

uh in the screenshot here you can see on

0:53

a normal HTTP connection. So this is

0:56

your typical API call you have your

1:00

client. So that can be your program it

1:02

can be your trading but it does a

1:04

request to the server. So let's say in

1:07

our case poly market and then poly

1:09

market returns a response. So let's say

1:12

that you needed in this example to get

1:16

price updates. you would have to

1:18

manually call their servers, you know,

1:21

every second or or faster and then get

1:25

those updates. So that's not really

1:27

efficient because normally um you know,

1:30

you'll be rate limited if you have a if

1:32

you do API calls too much. So you have

1:35

to have to handle that in in some way.

1:38

But then we have a web soocket which is

1:40

different. So again we have the client

1:42

which can in our case be our trading

1:44

bot. it does a request to the server and

1:48

then the server actually sends back data

1:51

in almost real time. So then there's a

1:53

connection between these two um you know

1:56

entities or or servers and then the the

2:00

server that you're requesting just keeps

2:02

sending you data again and again and

2:04

again. So you don't need to to call it

2:05

again and again. You get the data as

2:08

long as you maintain the connection. So

2:11

this is where the poly markets uh

2:15

websockets come in because poly market

2:17

have uh websockets they also have other

2:20

kinds of uh data sources. So they do

2:24

have uh realtime data streams they have

2:27

something they call a gamma structure

2:28

and gamma endpoints and a data API.

2:32

All of these is normal API calls normal

2:37

HTTP requests while the websocket here

2:41

and I think the real time data as well

2:43

maybe I'm not sure I haven't looked into

2:44

that but the websocket here at least is

2:46

a websocket. So what we have is we have

2:50

um basically a subscription. So what you

2:53

do is you make a connection to their

2:55

websocket and then you tell poly market

2:58

what data that you would like to fetch

3:02

or get. They have different um uh

3:05

methods here. So they have uh the

3:07

markets that you can subscribe to which

3:09

is based on the condition ids and you

3:12

also have asset ids. So this is the

3:15

specific token. So maybe you only want

3:17

to be following a specific token, then

3:20

you can do that. Or you can also do

3:22

several different uh markets and so on.

3:27

And it's all explained here in the in

3:29

the in the documentation without digging

3:31

too much into the actual code, but this

3:34

here's an example. And you can actually

3:35

just copy paste this into your own

3:37

trading bot if you'd like. And this kind

3:39

of explains everything. So you have

3:41

different methods in your websocket

3:44

class here. So for example, you have on

3:47

message. So this is the one that's

3:48

actually getting called every time that

3:52

poly market returns data in this web

3:54

soocket. So this is actually can be

3:56

several different messages messages per

3:58

second and then you need to handle those

4:00

in your program. We have other um

4:04

methods here. For example, on error, on

4:06

close

4:07

and uh it has on open

4:11

subscribe to token ids and and so on. So

4:14

different kind of methods to maintain

4:16

and um subscribe to this uh websocket

4:19

here. There are two different websockets

4:21

that I'll be talking about in this

4:22

video. So that's the user channel and

4:24

the market channel. For the sports

4:27

websocket, I'm not really going to go

4:29

into detail about that because frankly I

4:30

haven't actually looked at it yet. But

4:32

the sports websocket can give you

4:34

realtime sports results uh via the the

4:37

websocket. Probably very very good if we

4:40

are doing sports betting. We are not

4:43

doing that right now. Maybe in the

4:44

future. But so for the scope of this

4:46

video, I'll be uh focusing on the user

4:48

channel and the market channel. I also

4:49

going to explain how I'm planning to use

4:51

it in our trading bot.

4:54

So the user channel is is um actually a

4:58

websocket where you you you do need to

5:00

authenticate with an API key and and

5:03

stuff like that contrary to the market

5:05

channel. The market channel you don't

5:06

need to. You can just connect to and you

5:08

get the data. So the reason is because

5:10

here you get data uh specifically for

5:14

your own user and then you get um when

5:17

you then connect you get different

5:19

messages in the web from the web socket

5:22

that you then need to handle in your

5:24

trading bot. So for example for the user

5:28

channel you get a trade message in case

5:30

when a market order is matched when a

5:33

limit order for the user is included in

5:35

a trade or subsequent status changes for

5:39

a trade mind confirmed retrying

5:43

and uh failed

5:46

and then you can see here the structure.

5:47

So, so this is very uh important to

5:51

maintain your you know your portfolio of

5:55

of trades and this is also some

5:57

something that we discussed in the

6:00

previous video looking at the solution

6:02

design. If we go back to the solution

6:05

design here

6:09

you can see how I

6:12

have a a position layer here. So from

6:16

the event bus, the event bus receives

6:20

uh data or events from the data sources.

6:23

So we have a user channel webocket here

6:25

as its own data source. We then handle

6:28

it through our pipelines here and then

6:30

it comes to the position layer and it's

6:33

also going to go over to the strategy

6:35

layer because maybe we have a strategy

6:37

that needs to handle you know their

6:41

trades. They most likely need to, right?

6:43

Maybe we only want to have the trade,

6:46

you know, on the order book for a

6:48

certain amount of of time and then we

6:50

need to cancel it and that's something

6:52

we could potentially do here if that is

6:54

not a possibility through the API

6:56

directly which in some case it is is so

7:00

that's the idea behind that uh for the

7:02

user channel and then we have the market

7:06

channel which is public channel for

7:08

updates related to market updates. Okay,

7:11

so this is actually where you don't need

7:13

to um authenticate. You can just get a

7:17

lot of market updates, public market

7:19

updates and then handle that. And that's

7:21

where we um have the market channel

7:24

webocket.

7:26

This one is um you know then go through

7:29

our pipeline adapters to the processing

7:32

over to the event bus that then uh you

7:34

know

7:36

send it over to our signal layer and

7:39

then this is where we can construct

7:41

different signals again not just these

7:43

two signals. We can have many different

7:44

signals to construct based on the uh

7:48

poly market data that then go over to

7:50

the strategy layer and we then produce

7:52

the actions.

7:55

So again the market channel has

7:58

different uh messages again that we can

8:02

uh work with. We have the book message

8:05

uh price change message. We have a tick

8:08

size change message. We have a last

8:09

trade price message best bit ask message

8:13

new market message market resolve

8:16

message. This is all that we're able to

8:20

subscribe to in the websocket

8:24

and um and I mean you can read all of

8:27

the the details but we can just do some

8:29

examples here. For example, the book

8:30

message is emitted first time when you

8:33

subscribe to a market and then after

8:35

that it will uh be emitted when there's

8:38

a trade that affects the book. So that

8:41

the trading or the order book. Yeah. So

8:43

if you just take a look at the the order

8:46

book here. So whenever there's a trade

8:50

that affects the order book could be

8:52

amount of shares uh or the price you

8:56

know the this is the best ask price and

8:58

the bit bit uh price for buying a yes

9:02

here. Yeah. So then you know we will get

9:05

this message here when when a trade uh

9:07

affects the the book and you know we get

9:10

a payload something like this and with

9:12

for this we can we can actually

9:14

structure our own uh order book in our

9:17

trading bot. So if we have a strategy

9:20

that solely relies on the order book, it

9:25

could it could be like an arbitrage

9:26

strategy or it could be um like we

9:30

talked about before, if you want to

9:32

become part of the rebate program, if

9:34

you remember that if we go to the crypto

9:36

market,

9:39

the 15 here, so we have the mega rebate.

9:43

So we could also have a strategy that

9:45

basically um tries to earn on the mega

9:49

rebate. Let me take a market here that

9:50

has a bigger order book. So so the way

9:53

you could do that is to place uh resting

9:56

orders on the order book. So for

9:58

example, an order on 49 and an order on

10:00

46 and then if it's taken, you actually

10:03

participating in the mega rebate and

10:05

actually collecting

10:07

uh fees from other uh traders that

10:11

places the the market orders. So so for

10:15

for that example, it would be good to

10:18

you know use the web socket to construct

10:20

the um order book through uh these

10:24

messages. It's also possible to get the

10:26

order book through an API call through

10:28

the normal HTTP connection where you

10:30

request and get response back. But if

10:32

you did that, then you would have to

10:35

ping their server, you know, several

10:37

times and and you need to figure out how

10:39

to do the rate limits because there is

10:42

rate limits on the API calls. And then

10:44

the there's a price change message here,

10:47

which is uh from my experience the one

10:49

that has a lot of data and it's going to

10:51

ping you a lot all the time, especially

10:54

if you subscribe to several different

10:56

markets. So So this is um a good one.

11:00

You get a a payload that looks something

11:02

like this. You get the markets and then

11:05

you get price changes for the different

11:07

uh tokens. So you have an asset ID which

11:10

is the token

11:12

um and and here and you have the price

11:15

the size decide buy or sell bit best uh

11:20

bit and ask price for for that specific

11:24

um

11:26

trade.

11:28

Yeah. So many different

11:31

types of messages that you can use in

11:33

your websockets. Yeah. So this that was

11:36

a little bit about the the websockets on

11:39

poly market. My plan is to make my next

11:43

video maybe or at least soon make a

11:45

video about the specific implementation

11:48

of the the websocket. So basically in in

11:52

in our program here I have a polar

11:55

market websocket adapter which um you

11:58

know fits in uh within our our

12:01

architecture. So that's this one over

12:03

here. So we have the pipeline adapter

12:06

the poly market websocket.

12:08

So I haven't updated this to reflect

12:10

these names here. So that's the market

12:12

the market channel the market events

12:14

that we use this um adapter this

12:18

pipeline adapter 2 and this basically

12:20

follows the structure that we saw in the

12:24

example of of polyarket. So we're not

12:26

reinventing the wheel here. we're just

12:28

following along on on on on their

12:30

documentation and then we have improved

12:32

it and and implemented maybe more

12:34

features. Um and then we have another

12:37

pipeline here the poly market user

12:39

events which again like we discussed

12:42

it's the user channel webocket here. So

12:45

we use that in our uh config loader

12:48

basically in our strategies and um my

12:51

plan is to go through this with you and

12:53

explain uh the code how it works and how

12:57

I'm planning to use it in a production

13:00

environment. So stick around and uh I'll

13:04

see you on the next video. Bye.

Interactive Summary

The video introduces websockets, contrasting their real-time, continuous data push mechanism with traditional HTTP requests. It then focuses on PolyMarket's websockets, detailing the User Channel, which requires authentication for user-specific trade data, and the public Market Channel, providing various market updates like order book and price changes. These channels are crucial for developing trading bot strategies such as portfolio management, arbitrage, and participating in rebate programs. The speaker previews a future video on the practical implementation of these websockets.

Suggested questions

5 ready-made prompts