Chapter 5: The Front-Running TWAP – The Time-Weighted Average Price Trap

The conference room had become their headquarters. Empty coffee cups littered the table, their contents long since drained. Printouts covered every available surface, marked with highlighters and handwritten notes. The whiteboard was a chaotic tapestry of diagrams, timelines, and mathematical formulas, each one representing a piece of the puzzle they were trying to solve.

It was 8:47 PM, and Jaya and Kiran had been working for nearly three hours since the market closed. The exhaustion was beginning to show—Jaya’s eyes were red-rimmed, and Kiran’s usually tidy hair was a disheveled mess. But neither of them was willing to stop. They were too close to understanding the problem, too invested in finding a solution.

“Okay,” Jaya said, breaking the comfortable silence. “Let’s go through this one more time. I want to make sure I understand exactly how the bot is exploiting my TWAP.”

Kiran nodded, pushing aside a stack of papers to clear space on the table. “Good. Understanding the problem is the first step to solving it.”

He pulled up a fresh diagram on his laptop, projecting it onto the large monitor on the wall. The screen showed a detailed timeline of Jaya’s TWAP execution, with annotations marking the bot’s activity.

“Here’s the basic mechanics,” Kiran began. “Your TWAP executes a sell order of 13,333 units every minute, starting at 8:00 AM and continuing until 6:00 PM. The bot observes this pattern and learns your schedule.”

Jaya nodded, following along. “I understand that part. The predictability is the problem.”

“Right,” Kiran confirmed. “But the exploitation itself is more sophisticated than just ‘selling ahead of you.’ Let me show you the full sequence.”

He zoomed in on a specific moment in time—9:47 AM, one of Jaya’s earlier executions. The monitor displayed a frame-by-frame breakdown of the market activity in the seconds surrounding her trade.

“At 9:46:58, the bot places a small sell order for 1,400 units at 42.48, slightly below the current market price,” Kiran said, pointing to the data. “At 9:46:59, it places another sell order for 1,100 units at 42.47. These orders create downward pressure on the price.”

Jaya watched the visualization unfold. “So the bot is pushing the price down before my trade.”

“Exactly,” Kiran said. “At 9:47:00, your TWAP order executes. The market price has been pushed down to 42.46, so your order fills at that lower price. Then, at 9:47:01, the bot cancels its small sell orders, and the price recovers to 42.50.”

Jaya studied the sequence, her mind working through the mechanics. “So the bot sells at a slightly higher price, buys back at the lower price, and captures the spread. But where does it buy back?”

Kiran smiled. “That’s the clever part. The bot doesn’t just sell before you and disappear. It also places buy orders immediately after your trade, capturing the bottom of the dip. Here, look at this.”

He pulled up another visualization, this one showing the full order book activity around the same moment. “At 9:47:01, right after your trade executes, the bot places buy orders at 42.46 and 42.47, filling its position at the bottom. Then the price recovers, and the bot sells its position at the higher price.”

Jaya’s eyes widened. “So the bot is selling ahead of me to push the price down, then buying at the bottom to profit from the recovery. It’s capturing both sides of the trade.”

Kiran nodded approvingly. “Exactly. The bot is creating a temporary price dislocation—a mini-crash, if you will—and profiting from both the downside and the upside. You’re selling at the worst possible moment, and the bot is buying at the best possible moment.”

Jaya sat back, processing the full scope of the exploitation. It wasn’t just front-running—it was a complete market manipulation, executed with surgical precision. The bot wasn’t just profiting from her predictability; it was actively shaping the market to maximize its profits.

“How much is the bot making from each trade?” she asked.

Kiran pulled up a calculation. “Each front-running cycle generates about 0.06 units of profit per token. With your 13,333-unit trade, that’s approximately 800 units of profit per cycle. Over 600 trades in a day, that’s nearly 480,000 units of profit.”

Jaya felt her stomach drop. “Almost half a million units in a single day. And I paid for every bit of it.”

Kiran nodded grimly. “The bot is essentially taking a tax on your position. Every time you trade, it skims a little bit off the top. Over time, that adds up to a significant amount.”

He paused, letting the information sink in. Then he continued, his voice softer. “But here’s the thing—the bot isn’t doing anything illegal. It’s just observing public market data and executing trades based on that data. The pattern was there, and the bot exploited it. That’s how the market works.”

Jaya nodded slowly. “I understand. The bot isn’t cheating. It’s just being smarter than I was.”

“Exactly,” Kiran said. “The market is full of intelligent participants, both human and algorithmic. The ones who succeed are the ones who adapt and evolve. The ones who stay predictable get exploited.”

Jaya was quiet for a moment, absorbing the lesson. Then she leaned forward, a new determination in her eyes. “Okay. I understand the problem. Now let’s talk about the solution.”

Kiran smiled. “That’s the spirit.”

He stood up and walked to the whiteboard, erasing a section to make room for a new diagram. “The core problem is predictability. Your TWAP was predictable because it followed a fixed schedule—the same size, at the same time, every single minute. So the solution is to break that predictability.”

He drew a simple diagram: a line representing time, with dots marking order executions. The first set of dots was evenly spaced, all the same size—Jaya’s TWAP. The second set was irregularly spaced, with varying sizes—the proposed solution.

“The idea is to keep the benefits of the TWAP—splitting a large order into smaller pieces to minimize market impact—while removing the predictability. We do that by randomizing the timing and size of your orders.”

Jaya studied the diagram. “So instead of 13,333 units every minute, I might do 14,200 units in 47 seconds, then 12,800 units in 2 minutes and 13 seconds, then 15,100 units in 1 minute and 5 seconds.”

Kiran nodded enthusiastically. “Exactly. The randomness makes it impossible for the bot to predict when your next order will appear or how large it will be. It can’t front-run you because it doesn’t know when you’re going to trade.”

He turned back to the whiteboard and added more detail to the diagram. “But there’s a catch. The randomness needs to be truly random—not just pseudo-random or deterministic. If the bot can detect a pattern in your randomness, it will adapt and start exploiting you again.”

Jaya frowned. “What do you mean by ‘truly random’?”

Kiran’s eyes lit up. “I’m glad you asked. That’s where the VRF comes in—the Verifiable Random Function I mentioned earlier.”

He pulled up a new screen on his laptop, showing a technical diagram of the VRF system. “A VRF is a cryptographic function that generates random numbers in a way that’s both unpredictable and verifiable. You provide a secret key, and the function generates a random output that no one can predict. But once the output is generated, anyone can verify that it was generated correctly.”

Jaya studied the diagram, her mind working through the concept. “So I can use a VRF to generate random times and sizes for my orders. The bot can’t predict them because it doesn’t know my secret key. But the firm can verify that I’m not just trading randomly.”

“Exactly,” Kiran said. “It’s the best of both worlds—unpredictability for the market, accountability for the firm.”

He typed a few commands on his laptop, and a new interface appeared on the monitor. “I’ve already built a prototype of the VRF-based execution system. It integrates with your trading terminal and generates random orders based on a secret key.”

Jaya leaned forward, studying the interface. It was clean and intuitive, with clear controls for configuring the randomization parameters. “How do I set it up?”

Kiran smiled. “I’ll walk you through it. But first, there’s something else I need to show you.”

He pulled up another window, this one showing a simulation of the randomization system in action. The screen displayed a hypothetical day of trading, with orders appearing at random intervals and random sizes.

“The randomization system is powerful, but it’s not perfect,” Kiran said. “The bot might still find ways to exploit you—for example, by watching the overall rate of your executions and trying to predict your remaining position. That’s why we need to combine randomization with other strategies.”

Jaya nodded. “Like dark pools and alternative execution venues.”

“Exactly,” Kiran confirmed. “But that’s a conversation for another day. For now, let’s focus on getting the randomization system up and running.”

He stood up and walked to Jaya’s laptop, which was open on the table. “May I?”

Jaya nodded, and Kiran began typing, his fingers flying across the keyboard with practiced speed. Within minutes, he had integrated the VRF system into her trading terminal.

“There,” he said, stepping back. “The system is installed. You can activate it whenever you’re ready.”

Jaya studied the interface, a sense of excitement building in her chest. This was the solution she had been looking for—a way to break the predictability that had made her vulnerable to the bot.

“Thank you, Kiran,” she said. “I don’t know how I can ever repay you.”

Kiran waved her gratitude away. “Just beat the bot. That’s all the thanks I need.”


The next morning, Jaya arrived at her workstation early. The trading floor was quiet, with only a few early risers scattered among the desks. The sun was just beginning to rise outside the windows, casting a warm glow across the room.

She settled into her chair and activated the VRF system. The interface appeared on her screen, displaying the configuration options. She set the randomization parameters—order sizes between 10,000 and 16,000 units, order intervals between 30 and 120 seconds—and activated the system.

The first random order appeared at 8:03:47, a sell of 14,200 units. Jaya watched the order book as the trade executed. The price dipped slightly, then recovered. There was no sign of the bot’s manipulation—no small sell orders appearing ahead of her trade, no sudden price drops.

She let out a breath she hadn’t realized she’d been holding. “It worked,” she whispered. “It actually worked.”

The second random order appeared at 8:05:22, a sell of 12,800 units. Again, the execution was clean. The bot was nowhere to be seen.

Jaya continued through the morning, each random order executing without issue. By 10:00 AM, she had sold 500,000 units, and the slippage was minimal—just 0.15%, compared to the 0.7% she had been experiencing with the TWAP.

She made a note in her execution log: “Day 2 – VRF randomization active. 500,000 units sold. Slippage 0.15%. No evidence of front-running. The bot is still present but ineffective.”

The morning continued smoothly, and Jaya began to relax. The randomization system was working exactly as designed. The bot couldn’t predict her orders, so it couldn’t exploit them.

At 11:30 AM, Kiran appeared at her desk, a coffee in each hand. He set one down in front of her and took a sip of the other. “How’s it going?”

Jaya grinned. “Perfect. The randomization is working like a charm. The bot can’t touch me.”

Kiran nodded approvingly. “Good. But don’t get too comfortable. The bot is smart—it will adapt. We need to stay ahead of it.”

Jaya’s smile faded slightly. “What do you mean?”

Kiran pulled up a chair and sat down next to her. “The bot isn’t just a simple algorithm. It’s designed to learn and adapt. It’s probably already analyzing your new pattern, trying to find a way to exploit it.”

Jaya felt a chill run down her spine. “So the randomization might not work forever?”

Kiran shook his head. “It will work for a while—long enough for you to complete your liquidation. But the bot will find new ways to exploit you. It might start watching the overall rate of your executions, or use other data to predict your remaining position.”

Jaya was quiet for a moment, processing his words. Then she nodded. “So we need to keep evolving. Stay one step ahead.”

“Exactly,” Kiran said. “And that’s why I want to show you something else.”

He pulled out his laptop and opened a new program. The screen displayed a detailed analysis of the bot’s behavior, with charts and graphs showing its activity over the past weeks.

“I’ve been tracking the bot’s adaptations,” Kiran said. “Every time a trader changes their strategy, the bot evolves to counter it. It’s a constant arms race.”

He pointed to a chart showing a pattern of activity. “Here’s an example. A trader switched from a TWAP to a VWAP, thinking it would make them less predictable. The bot adapted within two days, identifying the new pattern and exploiting it.”

Jaya studied the chart, her mind working through the implications. “So the bot is constantly evolving. We can’t just find a single solution and expect it to work forever.”

“Exactly,” Kiran said. “We need to keep adapting. We need to use multiple strategies—randomization, dark pools, alternative venues—and we need to keep changing them.”

He paused, his expression growing serious. “But there’s something else. Something I haven’t told you yet.”

Jaya felt her heart sink. “What is it?”

Kiran leaned closer, his voice dropping to a whisper. “I think the bot is targeting you specifically. Not just your TWAP, but you. It’s tracking your activity across the market.”

Jaya stared at him, her blood running cold. “What do you mean?”

Kiran pulled up another screen, this one showing a timeline of Jaya’s trading activity over the past weeks. “I’ve been analyzing the bot’s behavior, and I noticed something interesting. It started targeting you before you even began your TWAP. It was tracking your activity, learning your patterns, preparing for your large order.”

Jaya felt a wave of nausea wash over her. “It was waiting for me. It knew I was going to make a large trade.”

Kiran nodded grimly. “The bot is sophisticated. It doesn’t just react to patterns—it anticipates them. It watches traders’ behavior over time, building profiles of their activity. When it detects a trader who might make a large trade, it positions itself to exploit them.”

Jaya was silent, processing the full scope of the threat. The bot wasn’t just a passive observer—it was an active predator, stalking its prey. And she had been its target.

“What do I do?” she asked, her voice barely above a whisper.

Kiran’s expression softened. “You do what you’ve been doing. You adapt. You evolve. You stay one step ahead.”

He placed a hand on her shoulder, his touch reassuring. “You’re not the victim here, Jaya. You’re the survivor. The bot thought it could exploit you, and you proved it wrong. That’s not weakness—that’s strength.”

Jaya looked at him, feeling a surge of gratitude. He was right. She had been through a trial by fire, and she had emerged stronger. The bot was still out there, still hunting, but she was no longer prey.

“Okay,” she said, her voice firming. “What’s the next step?”

Kiran smiled. “Dark pools. I’ll show you how to use them. The bot can’t see orders that are executed in a dark pool, so it can’t exploit them.”

Jaya nodded, feeling a new sense of purpose. “Let’s do it.”


The afternoon was spent learning about dark pools. Kiran explained how they worked—private trading venues where orders weren’t visible to the public, where large trades could be executed without moving the market, where the bot couldn’t see or exploit her activity.

“It’s like trading in the shadows,” Kiran said. “The bot can’t see you, so it can’t hunt you.”

Jaya absorbed every word, her mind already planning how she would use dark pools in her future trades. By the end of the day, she had configured her terminal to access two different dark pools, giving her additional options for executing her remaining position.

The randomization system continued to work throughout the afternoon, and Jaya’s execution remained clean. By 5:00 PM, she had sold a total of 3.8 million units, with an average slippage of just 0.12%.

She completed the day with a sense of satisfaction. The bot was still active, still hunting, but it couldn’t touch her. She had found a way to fight back, and she had won.

As she packed up her things to leave, Kiran appeared at her desk. “Great work today,” he said. “You handled that like a pro.”

Jaya smiled. “I couldn’t have done it without you.”

Kiran shrugged modestly. “You would have figured it out eventually. You’re a quick learner.”

He paused, his expression growing serious. “But I want you to be careful. The bot is still out there, and it’s still learning. It will find new ways to exploit you.”

Jaya nodded. “I know. And I’ll be ready.”

Kiran smiled. “That’s what I wanted to hear.”


The days that followed were a continuation of the battle. Jaya used the randomization system, the dark pools, and a growing arsenal of other strategies to stay unpredictable. The bot adapted, evolving its tactics, but Jaya adapted faster.

By the end of the week, she had completed her liquidation with minimal slippage. The bot had been beaten—not by a single solution, but by a constant process of adaptation and evolution.

Jaya learned more in that week than in months of training. She learned that the market was a battlefield, and the predators were always watching. She learned that predictability was a liability, and that the only way to survive was to stay one step ahead.

She also learned that she had allies. Kiran had been her guide, her mentor, her partner in the fight against the bot. Without him, she might have never understood the threat, never found the solutions, never beaten the predator.

As she prepared to leave for the weekend, Jaya looked across the trading floor and saw Kiran at his desk. He looked up and caught her eye, a knowing smile on his face.

She returned the smile, feeling a sense of accomplishment. The bot had been a formidable adversary, but she had survived. And she was ready for whatever came next.

Table of contents:
Introduction
Chapter 1: The Large Order
Chapter 2: A Slippage Problem
Chapter 3: The TWAP Solution
Chapter 4: The Predictable Pattern
Chapter 5: The Front-Running TWAP
Chapter 6: The Chunking Attack <<<<<< NEXT
Chapter 7: The Randomization Fix
Chapter 8: The Volume-Weighted Alternative
Chapter 9: The Hidden Liquidity
Chapter 10: Trading Smart, Not Predictable

Loading



Dear reader, love our creation? Support us moving forward