Algorithmic Platform | Trading Strategies | OMS | AI | Trading Simulators

Agent-Based Market Simulation: The Chaos of Financial Markets at your fingertips

Agent-Based Market Simulation: The Chaos of Financial Markets at your fingertips

| Categories: Technology, Trading | Tags: trading, infrastructure, high-frequency trading

Dimmed light on my desk. I’m surrounded by screens. The spacecraft-like blue light of the soldering station blinks in the corner. A newly arrived Apple Macintosh Performa 200 waits patiently to be set up on a portable desk. The Queen plays in the background. I’m tired of staring at these monitors, but I can’t leave. Not yet.

I need to finish what I started: an evolutionary computation machine that runs genetic algorithms to “breed” and cherry-pick the best fragments of code. Code that replicates the very chaos of financial markets, the ultimate game that has attracted so many bright minds. Those fragments of code are called “agents.” I call them “exchange agents” to avoid confusion with the ever-expanding universe of AI.

Don’t get me wrong, I am deep in the world of AI. But my final product, a small group of agents, is designed to be simple. Elegantly simple.

I just need a break. I’m exhausted, and the simulation stability issues feel endless. I’m so close, yet so far. A cup of green tea would be nice. A shower. That Atari book I’m craving to read again, and hopefully, a solid sleep to follow. So why am I still here?

I’m here because I feel I can nail it today. I’m here because I want to see the magic. I want to feel that sense of non-instant gratification, that sense of accomplishment. Scratch all that… I just want to see this thing running properly. It was supposed to be working by now, but the complexity of its parts has other ideas. Not yet.

Do you know how hard it is when a creative idea strikes, sometimes at the most inconvenient hour, and you need to test it right here, right now?

Designing trading algorithms is no different. Those sparkly, bubbly thoughts coalesce into concepts, which grow into ideas, and sometimes, truly revolutionary ones.

This quest started a long time ago. When people say good things take time, they aren’t lying, though it has taken far too long for my liking.

It began when I worked for investment banks (a past life I plead guilty to). The best departments in those banks were what I called “talent magnets.” The intricate complexity of the financial markets, coupled with the compensation, attracted some of the brightest minds. It was incredibly fun to find myself among them.

The environment was push, push, push. The bottom line was, and quite rightfully so, of utmost importance. Yet, this often meant that essential, fundamental tooling was left behind. Things like high-quality market simulation became a side project, never the main product. At least, that’s how it used to be.

The apex of Darwinian evolution in most of those places was simply replaying historical stock market quotes into a trading algorithm as if it were the real world. There’s nothing inherently wrong with this approach; it’s the bedrock of casual unit testing, and it works. But it’s restrictive. There’s no life to it. It’s prescriptive, static, and fails to capture the true chaos of a live market.

Some exchanges were kind to fellow programmers, providing test markets (or “simulations”) where we could play, stress-test our strategies, and trial new ideas. This was an absolute game-changer at the time!

The bigger the bank, the more “troublemakers” it had, people like us, pushing the boundaries of these test markets and, frankly, testing the patience of other participants. We would deploy “war” and “wizard” bots designed to wipe the entire order book clean. This was all to test our index arbitrage systems, which would then submit massive “waves” of orders across all the index constituents. We had a ball; it tested the limits of our trading permissions and, less admirably, the other participants’ patience. But it was all war games, and we were the young warriors there to win, right?

Seriously, though, the novelty of sharing a sandbox and stepping on each other’s toes wears off quickly. What we truly needed was our own matching engine—a private, powerful replica of the real exchange. This would allow us to test creative ideas and refine existing algorithms in isolation, without external interference.

While banks sometimes bought into this idea, they never fully committed. It was often seen as a fancy toy for quants and developers, not a hardened, weather-proof fighter pilot simulator. More often than not, building one was a passion project, someone’s initiative squeezed into spare time, rather than a properly funded, strategic endeavor.

We built things. Some were really fancy, with fine-grained tick-data replay (this is where you feed historical quotes into the system and just hope for the best!). Yet, they remained fundamentally passive. They were echoes of the past, far from the dynamic, interactive, and living chaos of a real market.

It was time to leave investment banking.

Those who know me would remember my constant musings about the “Financial Skynet”—about “algos writing algos,” about “motherships controlling the evolution of autonomous alpha-seeking strategies.” I dreamed of delegating trading logic to neural nets, of intelligent sentinels and directional evolutionary processes. The list goes on. My mind would race, rapidly leaping ahead of immediate reality and straight past pesky constraints like time and resources. I can’t help it; it’s who I am.

My obsession has always been the creation of truly realistic markets. Not just replaying old data, but populating a digital ecosystem with agents that mimic the full spectrum of real market participants: HFT market makers, aggressive market takers, patient trend-followers, complex volatility dispersion traders, razor-sharp index arbitrageurs, massive pension funds, nimble scalpers, and… how could I ever forget about Mums and Dads!

This vision demanded a foundation. So, I built a matching engine and a simple exchange, which I joyfully dubbed “the Magic Exchange.” And then came the heart of the simulation: those simple bits of code, the (exchange) agents.

And this is precisely where my journey leads to the future, to Algoteq.

We began with one of our most valuable assets: our dreams! A sequence-based architecture, linearly scalable, powering the best algorithmic trading engine on the planet, all feeding into the simulated market of our dreams. There’s no harm in dreaming, right?

Then came the reality: we had to build it all. We knew it would take years of relentless work.

But what about those magic agents? The ones that could conjure diverse markets with the flick of a finger? Life teaches us, over and over, that you must crawl before you walk. Only then can you hope to sprint.

So, we started with the foundation. I developed a handful of primitive, foundational agents, designed to prove the architecture could scale when the time was right. I brought this prototype technology with me to Algoteq, where their place was secured in the very heart of the exchange just a few bytes away from its core, the matching engine.

Time flowed on, and with its passing, our system matured into a solid product.

Our simulated market became interactive. We fed historical data into the exchange, which produced market data that was happily consumed by our trading algorithms. They eagerly traded on the simulated market, battling against the reconstructed order book and, crucially, against each other.

“But what if I really hammer the market on one side?!” a seasoned trader asked in his baritone, a question echoed by many.

The answer was clear: it leaves a massive dent on one side of the book. We call this phenomenon a temporary market impact. The book would then progressively refill as if the shock never happened. Wait, back to normal? Not even a scar? That absence of a scar is the real mystery, the permanent market impact.

The market wasn’t dead. Quotes ticked, prices moved. We saw surges and falls in activity. But something fundamental was amiss. It still wasn’t interactive enough.

My answer to the trader’s question was always a workaround: “We could use the agent API to code a market impact model to refill that lost liquidity.”

But it felt like half a solution. What we truly needed were the tiny, creepy-crawly creatures—a swarm of agents to create the magic of true interactivity. So, I had to ask: Where are they?

I’ve always believed, perhaps naively, that if you get the representation right, you’ve solved 90% of the problem (I rounded up, of course!).

Attempting to replicate the sheer diversity of the market—HFT market makers, aggressive takers, patient trend-followers, complex volatility traders, razor-sharp arbitrageurs, massive pension funds, nimble scalpers, and let’s not forget Mums and Dads—is a daunting task. No “reality distortion field” (all credit to Steve Jobs) can make that any simpler.

In the wonderful film Short Circuit, when Norton spills coffee on a piece of paper to see if Johnny 5 is alive, Johnny eventually sees a butterfly. So, what do you see when you look at stock charts?

I don’t want to spoil the fun, but I often see chaos. Sometimes I see trends, and how could I miss the range-bound channels (both tilted and horizontal). No butterfly? It makes me wonder, am I alive?

I code these behaviors into our “exchange agents” within a relatively simple parametric space. Meet the team: There’s Walker (a.k.a. “Random Walker”), who lives and breathes chaos. There’s Lemming (don’t worry, there will be many of them), who follows the crowd and leaves trend-like traces behind. And then there are the Contrarians, who want to be different; they fade the trends and are the ultimate opportunists.

And voila! I have a beautiful, yet senseless, market I can happily interact with. Is it happy chaos or pure madness? The judge is still out.

As I run many simulations, I discover gaps in the order books—occasional and rather annoying gaps. What can I do about them? I could push the existing agents, or I can create a new type of agent: the Background (boring name, but straight to the point). Now, just like in a good old 2D computer game, I have my sprites: the chaotic Walker, the hesitant Lemming, and the cheeky Contrarian (I call it “Contra,” just like the game). The relatively stationary tile map, the overall theme and depth of the order book, is maintained by the Background agent. It saved me a lot of computational resources, trust me.

Just four types of agents to rule the world!

Okay, let’s quickly establish where we are. We have our agents operating within a relatively simple parametric space. Have we solved 90% of the problem? They undoubtedly produce highly interactive or sedate markets, configurable to be either fun or boring to trade. But the critical question remains: what about realism?

As the statistician George E.P. Box famously warned, “Remember that all models are wrong; the practical question is how wrong do they have to be to not be useful.”

So, how do we inject realism? I dreamed of a way to massively compress price and volume time series, but how?

The answer lay in a form of computational alchemy: decomposing the historical time series and using the extracted parameters to guide our agents. The goal? To have these simple bits of code produce a market with a statistically similar distribution on the fly. This approach saves gigabytes, even terabytes, of tick data storage and a significant amount of computational capacity.

The decomposition process itself is a vast playground, open to a myriad of techniques: wavelet decomposition, fractal decomposition (a personal favourite), Fourier Transforms (FT or FFT), and many more. For our initial implementation, we used stable parameter estimation. Truly, the sky is the limit.

So, we dissected the market into its constituent parameters. What’s next? The crucial step is to create a “reverse mapping” by translating those distribution parameters back into a configuration for our parametric graph of agents.

The process is elegantly simple in theory: run the decomposition, retrieve the distribution parameters, and map them back to their point of origin, my favourite agents.

But how do we find that perfect mapping? This is precisely where evolutionary computation comes in. Yes, the very system I was so eager to finish that night!

Back in the “Thinking Room” with its dimmed lights, the glow of computer monitors, and Queen playing in the background. A tiredness beyond imagination. Are you still with me? Wow!

One of my favourite personal laws states: “If the manifestation of a problem is plain stupid, so is the cause.” It was time to find that stupid cause.

John Carmack once advised senior developers to spend more time in the debugger than running code in their heads (a habit I’ve always been guilty of). I finally listened to Carmack and dove in. The tail of the vector being sampled contained a bunch of NaNs. Aha! The log-return function, buried deep in the statistics code, was generating them. But where were the negative prices coming from? Ah, the agents were missing a simple boundary check. Don’t get me wrong, I’m a big fan of negative prices (yes, I traded oil futures soon after they famously plunged below zero). I’ll support negative prices in the evolution engine one day, but not tonight.

Okay, exceptions fixed. The simulation finally completes. But where is the inconsistency in the simulation path? I deterministically seed the graph of the pseudo-random number generator and see no path-dependent branching logic getting in the way. What is going on?

Back to my “stupidity law.” Everything looks fine. Where is this annoying sporadicity coming from? Hang on a second… how do I create the original generation? Ah, it was using its own pseudo-random generator. The perils of coding at night! I passed the universal generator through, and at last… consistent, meaningful runs. The regression converges. We have an evolutionary engine, and it flies!

Agent Chromosomes Agent chromosomes: Radix-3 DNA strands defining remarkable market complexity.

The music is off. So are most of the lights. I’m leaving the home office, happy and tired, just like you, probably, after reading this.

Once Upon Atari awaits me, and so does a long sleep. It has been fun. We now have a very sophisticated market at our fingertips.

Thank you for reading!


References

Written by Vlad Shurupov

You Might Also Like

We use cookies to enhance your experience. By continuing to browse, you accept our Corporate Social Responsibility Policy.