Designing a Polymarket Trading Bot – System Architecture
281 segments
All right. So, in my past videos, I have
been mostly focused on analyzing, you
know, other traders. I've been focused
on the actual strategies and kind of
just been testing and vibe coding and
trying to figure out, you know, and
learning about poly markets. But now
it's time to take things on a little bit
more big picture stuff. we actually need
to figure out the overall solution
design and and architecture of the the
bots and I think I I don't think I'm
sure that this will make like the whole
software the whole bot much more robust
and actually better performing uh in the
future. So in this video I have designed
the complete uh architecture in like a
big picture way. So with these diagrams,
so I'm going to go through it and of
course you can copy this or you can use
this for inspiration in your own but of
course I encourage you if you have any
ideas or if you see any flaws in this
design to you know leave a comment down
below and then I can refine uh this this
bot here. So let me start from the from
the top from the from the in the
beginning of the bot. Of course we have
what I call the the config loader and
this is basically just all the settings
that we're going to have in our bot. The
settings can be different things such as
the different strategies, what kind of
data that we need and uh different you
know settings. A relatively easy and
simple way to configure the bot before
we click play basically. And then we
have the different data sources that I
will be uh referring to as pipelines.
So this can be for example most
important is the market channel
websocket from poly markets and we have
the user channel websocket and other you
know channels. The idea behind this is
we that we are able to connect several
different data sources into our bot and
then make you know actions based on
those uh data sources. It could also be
for example the weather in Canada or it
could be you know how many times you
know Elon Musk tweet tweets per day and
so and you know stuff like that. So any
kind of data uh we get that into the
data sources.
So when we get the data it's just the
raw data that we receive into our butt
and then we have these pipeline
adapters.
So, so what they do is they basically
just take the data and then they tag it
and you know the specific details I'm
not sure about yet. So this is just an
an overview and sort of an idea of how
to go about this. So it could be
something like the the the a timestamp
of when it's received what kind of
source is it the poly markets websocket
and then what kind of a event it is in
this case the event type price change as
an example. So this is what we call the
tagged message
that's then passed over to our
processing unit.
The processing unit has again different
uh methods to process the actual data.
So you can see here in the tag message
we still have the raw data from our
pipelines from before. So we haven't
touched that yet. Now the processing
units uh task is basically to take this
data the raw data from the event. For
example, it could be a price change or
it could be you know a change to the
order book. It's going to take that data
and then convert it to you know a format
that we can use in our bot because you
have to consider that let's say we have
10 different data sources that all
return let's say a price of some kind.
Well, we need to make sure that all the
fields and so on and so forth has the
correct format. And so basically just
unifying the the format so we can use
that in in the rest of of our our
program.
So so that's what I call the processed
event. Okay. So we get the raw data in,
we tag the data and then we process it.
We then pass it over to our event bus.
So the task of the event bus is
basically just to to orchestrate
the different events that we have to the
different signals. So for example, let's
say that we have a simple moving average
strategy. Let's say a crossover of 14
SMA over you know 28 SMA. That's a
signal that we register in our bot. And
this signal then subscribes to the
specific events that it needs. Let's say
we have uh 10 different sources, but
this one we only need this one only need
to subscribe to let's say the BTCUSD
uh data source.
So that so that's uh this event bus's um
you know responsibility to basically
forward the processed event over to our
signal signal layer that has the
different signals. Okay.
Now the signal layer's responsibility is
then producing the signal. So for
example, it could be the exponential
moving average. Okay. So it then takes
the event and then it it computes the
signal.
In this case, it's the exponential
moving average, different kind of
periods and so on.
It passes that data
over to our strategy layer.
So, this is kind of where I'm not sure
if this makes this spot a little bit
different, but the bot will be able to
actually have several different
strategies in one. So, it can subscribe
to several different markets, but also
have different strategies. So, we have
different strategies that also subscribe
to different signals. So, you see the
strategies subscribe to signals, the
signals describe subscribe to events and
so on.
And the strategy is then you know that's
where we formulate the actual strategy.
For example, the moving average
crossover or whatever strategy that we
you know figure out and that produces
actions.
Again the details here is not clear yet.
It's just an overview and it produces an
action payload. For example, it can be
the buy action to buy, you know, uh
whatever for an amount and then a reason
and other data that we deem fit for for
the action um payloads here.
It then moves over to the execution
layer. So this is where the you know
this is where we actually make the
orders. So, we have some validation and
maybe some locking. Again, a little bit
fluffy about the details here. And then
we have a a risk manager. So, um I'm not
sure exactly how I'm going to go about
this yet because I'm also thinking about
implementing methods to control the
risks uh within the strategies. For
example, if we have let's say we have
three or four different strategies
actually the same strategy
but maybe with different risk profiles
and we want to you know test that
then we need some kind of a risk uh
module on the strategy layer and uh and
so this risk manager is potentially just
a portfolio risk manager like an overall
uh risk manager. So I need to think
about that a little bit more. And then
we have the execution. So the actual you
know do we take the action go through
all these steps and then we execute it.
After we execute the trade this is kind
of this is what I've just been kind of
thinking about is well how do we
actually manage it then? How do we
because the strategies probably also
need to know when to exit. For example
let's say that all right we place a a
buy. First of all, we need to monitor is
it getting filled and if not then what
are we going to do unless the poly
market client handles handles it for us
but in case it doesn't and in case we
have to manage that manually well we
have to figure that out and I do have a
solution for that I'm going to explain
and then we also have to manage let's
say we need to close
um positions maybe we need to sell
positions based on the definition in the
strategy Okay.
So
this is where if we go back to the top
uh to the data sources, this is where
the user channel websocket comes in and
potentially other methods. So we
actually take that data
um the the order data for the specific
user in our case you know our you know
portfolio because we can subscribe to
websockets as well. So for example when
an order is filled we get that in as an
event from the websocket. We manage it
just like we did before and we process
it and so on and so forth. But the
difference is it doesn't go to the
signal layer. instead it goes to our
position layer. So we need a a way let's
see if you can see it. So we need um a
place in our our bot to uh handle state
of the the positions. So, so that will
receive uh events from the use event
pipeline and then it will keep local
state and I'm thinking also a way to
synchronize. Let's say that let's say
that we lose connection of of the
websocket for 2 seconds and in that
period of time we had a trade execute.
then we need a way to uh synchronize and
then we can utilize
another
pipeline which could just be an you know
a an API call that just polling every I
don't know 5 seconds every second or
whatever the rate limit allows us to
pull.
So the position layer then enables us to
um yeah just to keep a track of the
positions and so on. And we can also
have our
we can also have our strategies
subscribe to
the for example the user channel web
socket. So let's say we subscribe to the
market channel which gets all the you
know price updates for the market but
also the user channel. So let's say we
subscribe to the market channel. We
figure out all right now we want to buy.
We then make an action to buy
and then when it's filled, we get the
event from the the user channel here all
the way down through here through the
event bus and then to the signal layer.
Now,
this is where I'm not 100% sure yet
because we probably don't want to have a
signal specifically for
uh like all right now this order has
been filled. So we potentially need
this event bus to also just relay it
directly to uh the strategy layer.
Not sure yet. So this is need to think
about this a little bit more. So of
course if you have some suggestions how
to solve that or if you have some ideas
um how to improve this then please let
me know down in the comments below. And
um the next step will then be to
implement all these different modules.
And I've already begun working on the
the the pipelines here, the pipeline
adapters, the processing unit,
and then part of the event bus. I'll be
making a video about that when the code
looks good and when it's um you know,
when it's in a good order. So, I expect
that uh video to be ready hopefully um
hopefully tomorrow. So, I hope you check
in uh tomorrow and subscribe to receive
notification next time I post a video.
So, see you on the next video. Have a
good one. Bye.
Ask follow-up questions or revisit key timestamps.
The video details the comprehensive architectural design of a robust trading bot, shifting focus from individual strategies to a complete system. It describes components such as the config loader, various data sources (pipelines) processed and tagged by pipeline adapters and processing units for uniform formatting, an event bus to orchestrate events to specific signals, and a strategy layer to generate actions. An execution layer handles order placement and risk, while a position layer tracks post-execution trade states using user channel websockets and synchronization, ensuring reliable trade management. The speaker invites feedback on specific design aspects and outlines future implementation steps.
Videos recently processed by our community