Polymarket Trading Bot Deep Dive (Part 2): SignalLayer, StrategyLayer & PositionLayer
345 segments
Okay, so in the past video I talked
about the websocket event bus and the
processing pipeline. That was the part
one of the poly market trading bot deep
dive. So that was basically
all of uh this part of the architecture.
So from the config layer to the data
sources, pipeline adapters and then
processing. And then I talked about the
event bus. So if you haven't seen that
video already, I would suggest to see
that first before you are watching this
one because in this video I'll be
explaining some changes that I've done
to the architecture but also our signal
layer and then this part over here which
is also a new kind of a new feature and
then I'm going to in kind of you know
big picture stuff explain the strategy
layer and then the execution layer.
There's a lot of details in this. I will
probably make another part three video
about that. for example, how the orders
on poly market is handled, you know, and
and stuff like that.
So, just to reiterate, we have some data
sources which is, for example, the
Bitcoin price and the poly market
channel that sends us raw data in our uh
trading bot, right? So, we have the raw
data that's passed over to some pipeline
adapters. basically just adapters that
tags this raw data uh so we can use it
our in our system. You can see how we
have different tags, different data that
we're going to use later in our system,
but we also have the raw message. Then
we have the processing unit that takes
basically the raw data from the tag
message and then process it. It's to you
know a uniform uh event that we then
call processed event that's then going
to be pushed to the event bus. And the
event bus basically you know pushes this
event out to all the different module
modules that have subscribed to the
event bus. In uh this video I'll be
talking about the signal layer. So the
signal layer can be many different
signals that we want our strategies to
subscribe to. So a very classic trading
you know strategy in in stock trading is
like looking at the you know moving
averages and stuff like that. So a
moving average could be you know the
average price over a given period
crosses another moving average over a
time period. So that's a signal that we
can create in our signal layer. So we
then have our signal um subscribing to a
a data source through the event bus and
then when it's produced the signal it
then pushes it over to the event bus
again and then that distributes the
signal to other areas of our trading bot
that has subscribed to it. In our case
it's going to be the strategy layer. the
strategy layer we have different
strategies that you know says all right
for example strategy one have signal A
and B so that will then subscribe to
those signals and the event bus
basically just manages all of that so
that's kind of a recap of what we talked
about so to take a little bit deeper
talk about the signal layer and the
strategy layer I have to refer to the
code this file here is just an example
file that demonstrate how I kind of put
all these different layers together. So
you can see how we uh start by
initializing the event bus. So that's
the main parts here, right? The event
bus. And then we create a signal
producer. So the signal producer is um
you can see how we defined it over here.
In this case, it's the exponential
moving average signal producer that
takes a basically takes a base class
that have different methods that we
require it to implement. And then you
can see how we have the event bus and
then we have the method subscribe and
then the producer the signal producer
and then the signal producer has an
onevent method. So what happens here is
we create a signal a signal producer
that we call it. We subscribe that to
the event bus and then it gets like a
call back method an onevent call back
method. So the event bus knows what
method to call when the signal produces
a signal basically and then we basically
do the same with the with other kind of
signals. So, so we can provision
different kinds of signals that we want
to use in in our strategies basically.
And then a new thing that I have added
to the architecture is two different
things. A position tracker and then a
pending order tracker. And they kind of
they are very similar but they they
serve different purposes. The pending
order tracker it's kind of in the name.
So when we produce
an action so you can see here in our
strategy layer they can produce actions.
So in the moment
in the moment it creates an action
it will actually omit an event to the
event bus that then create the pending
order tracker. And this responsibility
the responsibility of the pending order
tracker is to basically just monitor the
state of of the action that we omitted
because when we do it well we don't know
it's kind of a void. It's kind of a
black box. We don't exactly know what
happens over at poly market or any other
market for that matter.
So that that's why this one will have
the responsibility of basically
monitoring through the events that we
get from the market over here. So we
have the uh the user channel websockets
and I'll be explaining a little bit more
about this in this video because this is
also something I changed um but but more
on that just in a moment. And then
when when that happens that it gets
notifications from the websocket that
hey now the order has been filled
it omits a sim similar event that the
position tracker subscribes to and then
it this one the position tracker
actually tracks our actual positions
that has been filled. So you can see how
we we do it kind of in a similar we do
it in a similar way like the signal
producer. The signal producer produces
events and then we also have it with the
uh the position tracker. The position
tracker um is also subscribe to the
event bus that you can see here we do it
and then we also have an onevent method
and you can see here if we go over to
the uh this position tracker you can see
here how we have the onevent
and then we have a self.handle handle
trade.
Okay. And you can see how we look for
specifically for the trade event
and then we mark it in our state. So we
have a kind of a state management of of
the the the position here. And then you
can see here later we we basically
construct a position in our code and
then we omit their event. You can see
how we do we do self.eventbus.publish
publish position.
And that happens when we actually have
the trade filled in in this example
here. Again, it's just a this is just an
example. It's not the final it's not the
final code. I'm still working on this
just to make it robust. And actually now
I'm in the process of testing this out
doing live uh trades just to make sure
that it's actually working as as
intended because we kind of want to have
state in our in our butt to be able to
monitor you know pending orders and then
also actual orders that we then later
can sell or redeem or whatever we want
to do with them. And then I mentioned
that I've done some changes up here. So
before I had actually these two being
like one part now I've actually
separated it between
tradable markets
and then just data sources because I
think there's a very big distinction
between this. So for example, a tradable
market in our case is poly market. But
what if we want to connect this trading
bot to Kelshi or opinion or what if we
want to trade just regular crypto
markets because this is potentially
possible in this trading bot so we don't
like so we don't restrict ourselves just
to one market and maybe we want to do an
arbitrage between several different
markets. Let's say we want to subscribe
to both Kelsey and Poly market look for
similar event because now they have
15minute bitcoin markets and both Kelsey
and poly market. So there might be some
you know arbitrage opportunities there.
So in order to you know capitalize on
those arbitrage opportunities we need to
be able to connect several different
markets. And that's why I've made the
distinction between the the markets and
then the data sources. So the way I've
done it is I've I've um kind of made
market adapters that defines standards
methods in our codebase that different
markets has to conform with. You can see
how how I have created this market
adapter
and this market adapter is what a poly
market adapter would inherit from and
this one defines which kind of methods
that the adapter should basically create
and that allows rest of the trading bot
to easily hook into these methods and
then execute trades for example.
So for example, we have a discover
markets
and we have a get market method and we
have have also a subscribe order book
and a subscribe ticker and a subscribe
trade and so on. And then of course more
important we have something like a place
order.
We have a cancel order. We have a get
balance
and we have all other kinds of events
that we want
all all the markets to basically
implement because if they don't
implement this the trading bot will not
work and at this stage I'm not sure if
this is the final version. This is kind
of a barebones version of of the market
adapter I think. And and so again as I
said I'm still in the process of testing
this out. So I there's probably going to
be more changes to this.
And then if we look at the poly market
adapter, you can see here how it
inherits from the market adapter that I
just showed you before
and then it actually implements the the
methods to to to do these um um you know
methods. So for example, the discover
markets. All right. It then implements
the discover markets the way that poly
market expects it. Right? So we we we
hook into the different API calls and
stuff like that that they have on poly
market. And again we do this for all the
different methods like how do we place
an order, how do we cancel an order and
and and so on and so forth. And then we
also have the uh translate ticker. So
the the the translate ticker basically
just takes the poly market data and then
it transform it over to a uniform event
type that we can use in our in our bot.
So for example poly market they use best
ask best bit side size
price and asset ID. Well Calia might use
some other words and opinion might use a
completely different word. So we need to
be able to figure this out in these uh
methods and then omit a standard event.
And this goes this goes back to the um
the the basically the processing right
the processing unit that we have down
here which kind of is uniform now in
this here um where we take the data and
then we make it uniform so we can use it
in in the rest of our of our bot. But
besides that the the the pipeline
adapters and the data sources kind of
remains uh the same. That's basically it
for now. So the last remaining thing
that uh I need to implement uh to make
this all work is the execution layer
which is basically what I've done
already and now I'm just testing it out
and I'm testing this out together with
the position tracker and pending order
tracker to make sure that that actually
works correctly. and then I'm trying to
implement a front end so it's going to
be easier to manage the uh strategies
through a nice web interface instead of
using the terminal. So that's kind of
the next step and um in the next video I
will be explaining a little bit more
about the order flow of the of you know
on poly market how that happens when we
actually place live orders but I'll get
more into details about that later. So
if you haven't already joined our
discord server, I would highly highly
suggest you to do so because we already
have a lot of members. We have already
now we have you can see here we have
uh 100 online and then we have uh let's
see we have
you can see here we have 492
offline. So we have more than 600
members in the discord server. And you
see how we have members joining all the
time.
We have Julian here joined just a few
minutes ago. So it's like several people
every hours joining every single day.
And we only been have this discord up
and running for like I think it's around
two days now. And we have like more than
600 members join. So that's really nice.
And we have a lot of nice uh
conversations in uh the general chats
where we have a lot of people talking
and we have other channels coming also.
So it's a really good place to get alpha
on poly market and prediction markets in
general and we have a lot of new things
coming along here the coming weeks. So I
would highly suggest you to join if you
are serious about doing poly market
trading and poly market trading butts
and also calian and opinion trading but
everybody is welcome. It's called poly
traders that you can see here poly
traders and I have made sure to leave a
invite link down below in the
description. So, make sure to check that
out if you're interested in poly market
trading bots. So, until next time, see
you and have a good one.
Ask follow-up questions or revisit key timestamps.
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.
Videos recently processed by our community