Polymarket Real-Time Market Data: WebSockets Explained
298 segments
All right. So, in this video, I'm going
to be explaining a little bit about the
websockets and specifically the Poland
market websockets that we're going to be
using in our bots to make trades on and
and you know implement our our
strategies. So, I'll be leaving a
timestamp like if you already know what
a websocket is, you can just continue to
the next part of the video where I
explain about the poly market
websockets. But if you don't know what a
websocket is, then stick around because
I'm just going to do a quick explanation
of what a web soocket actually is.
So a websocket is kind of similar to a
normal like HTTP uh request, but very
different also. So in a normal let's say
API application, you do one request to a
server and then you get something back
from that server. So you can see here in
uh in the screenshot here you can see on
a normal HTTP connection. So this is
your typical API call you have your
client. So that can be your program it
can be your trading but it does a
request to the server. So let's say in
our case poly market and then poly
market returns a response. So let's say
that you needed in this example to get
price updates. you would have to
manually call their servers, you know,
every second or or faster and then get
those updates. So that's not really
efficient because normally um you know,
you'll be rate limited if you have a if
you do API calls too much. So you have
to have to handle that in in some way.
But then we have a web soocket which is
different. So again we have the client
which can in our case be our trading
bot. it does a request to the server and
then the server actually sends back data
in almost real time. So then there's a
connection between these two um you know
entities or or servers and then the the
server that you're requesting just keeps
sending you data again and again and
again. So you don't need to to call it
again and again. You get the data as
long as you maintain the connection. So
this is where the poly markets uh
websockets come in because poly market
have uh websockets they also have other
kinds of uh data sources. So they do
have uh realtime data streams they have
something they call a gamma structure
and gamma endpoints and a data API.
All of these is normal API calls normal
HTTP requests while the websocket here
and I think the real time data as well
maybe I'm not sure I haven't looked into
that but the websocket here at least is
a websocket. So what we have is we have
um basically a subscription. So what you
do is you make a connection to their
websocket and then you tell poly market
what data that you would like to fetch
or get. They have different um uh
methods here. So they have uh the
markets that you can subscribe to which
is based on the condition ids and you
also have asset ids. So this is the
specific token. So maybe you only want
to be following a specific token, then
you can do that. Or you can also do
several different uh markets and so on.
And it's all explained here in the in
the in the documentation without digging
too much into the actual code, but this
here's an example. And you can actually
just copy paste this into your own
trading bot if you'd like. And this kind
of explains everything. So you have
different methods in your websocket
class here. So for example, you have on
message. So this is the one that's
actually getting called every time that
poly market returns data in this web
soocket. So this is actually can be
several different messages messages per
second and then you need to handle those
in your program. We have other um
methods here. For example, on error, on
close
and uh it has on open
subscribe to token ids and and so on. So
different kind of methods to maintain
and um subscribe to this uh websocket
here. There are two different websockets
that I'll be talking about in this
video. So that's the user channel and
the market channel. For the sports
websocket, I'm not really going to go
into detail about that because frankly I
haven't actually looked at it yet. But
the sports websocket can give you
realtime sports results uh via the the
websocket. Probably very very good if we
are doing sports betting. We are not
doing that right now. Maybe in the
future. But so for the scope of this
video, I'll be uh focusing on the user
channel and the market channel. I also
going to explain how I'm planning to use
it in our trading bot.
So the user channel is is um actually a
websocket where you you you do need to
authenticate with an API key and and
stuff like that contrary to the market
channel. The market channel you don't
need to. You can just connect to and you
get the data. So the reason is because
here you get data uh specifically for
your own user and then you get um when
you then connect you get different
messages in the web from the web socket
that you then need to handle in your
trading bot. So for example for the user
channel you get a trade message in case
when a market order is matched when a
limit order for the user is included in
a trade or subsequent status changes for
a trade mind confirmed retrying
and uh failed
and then you can see here the structure.
So, so this is very uh important to
maintain your you know your portfolio of
of trades and this is also some
something that we discussed in the
previous video looking at the solution
design. If we go back to the solution
design here
you can see how I
have a a position layer here. So from
the event bus, the event bus receives
uh data or events from the data sources.
So we have a user channel webocket here
as its own data source. We then handle
it through our pipelines here and then
it comes to the position layer and it's
also going to go over to the strategy
layer because maybe we have a strategy
that needs to handle you know their
trades. They most likely need to, right?
Maybe we only want to have the trade,
you know, on the order book for a
certain amount of of time and then we
need to cancel it and that's something
we could potentially do here if that is
not a possibility through the API
directly which in some case it is is so
that's the idea behind that uh for the
user channel and then we have the market
channel which is public channel for
updates related to market updates. Okay,
so this is actually where you don't need
to um authenticate. You can just get a
lot of market updates, public market
updates and then handle that. And that's
where we um have the market channel
webocket.
This one is um you know then go through
our pipeline adapters to the processing
over to the event bus that then uh you
know
send it over to our signal layer and
then this is where we can construct
different signals again not just these
two signals. We can have many different
signals to construct based on the uh
poly market data that then go over to
the strategy layer and we then produce
the actions.
So again the market channel has
different uh messages again that we can
uh work with. We have the book message
uh price change message. We have a tick
size change message. We have a last
trade price message best bit ask message
new market message market resolve
message. This is all that we're able to
subscribe to in the websocket
and um and I mean you can read all of
the the details but we can just do some
examples here. For example, the book
message is emitted first time when you
subscribe to a market and then after
that it will uh be emitted when there's
a trade that affects the book. So that
the trading or the order book. Yeah. So
if you just take a look at the the order
book here. So whenever there's a trade
that affects the order book could be
amount of shares uh or the price you
know the this is the best ask price and
the bit bit uh price for buying a yes
here. Yeah. So then you know we will get
this message here when when a trade uh
affects the the book and you know we get
a payload something like this and with
for this we can we can actually
structure our own uh order book in our
trading bot. So if we have a strategy
that solely relies on the order book, it
could it could be like an arbitrage
strategy or it could be um like we
talked about before, if you want to
become part of the rebate program, if
you remember that if we go to the crypto
market,
the 15 here, so we have the mega rebate.
So we could also have a strategy that
basically um tries to earn on the mega
rebate. Let me take a market here that
has a bigger order book. So so the way
you could do that is to place uh resting
orders on the order book. So for
example, an order on 49 and an order on
46 and then if it's taken, you actually
participating in the mega rebate and
actually collecting
uh fees from other uh traders that
places the the market orders. So so for
for that example, it would be good to
you know use the web socket to construct
the um order book through uh these
messages. It's also possible to get the
order book through an API call through
the normal HTTP connection where you
request and get response back. But if
you did that, then you would have to
ping their server, you know, several
times and and you need to figure out how
to do the rate limits because there is
rate limits on the API calls. And then
the there's a price change message here,
which is uh from my experience the one
that has a lot of data and it's going to
ping you a lot all the time, especially
if you subscribe to several different
markets. So So this is um a good one.
You get a a payload that looks something
like this. You get the markets and then
you get price changes for the different
uh tokens. So you have an asset ID which
is the token
um and and here and you have the price
the size decide buy or sell bit best uh
bit and ask price for for that specific
um
trade.
Yeah. So many different
types of messages that you can use in
your websockets. Yeah. So this that was
a little bit about the the websockets on
poly market. My plan is to make my next
video maybe or at least soon make a
video about the specific implementation
of the the websocket. So basically in in
in our program here I have a polar
market websocket adapter which um you
know fits in uh within our our
architecture. So that's this one over
here. So we have the pipeline adapter
the poly market websocket.
So I haven't updated this to reflect
these names here. So that's the market
the market channel the market events
that we use this um adapter this
pipeline adapter 2 and this basically
follows the structure that we saw in the
example of of polyarket. So we're not
reinventing the wheel here. we're just
following along on on on on their
documentation and then we have improved
it and and implemented maybe more
features. Um and then we have another
pipeline here the poly market user
events which again like we discussed
it's the user channel webocket here. So
we use that in our uh config loader
basically in our strategies and um my
plan is to go through this with you and
explain uh the code how it works and how
I'm planning to use it in a production
environment. So stick around and uh I'll
see you on the next video. Bye.
Ask follow-up questions or revisit key timestamps.
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.
Videos recently processed by our community