DEV Community

Benjamin-Cup
Benjamin-Cup

Posted on

Building a Polymarket TWAP Momentum Trading Bot for 5-Minute Crypto Markets

Short-duration prediction markets move fast.

In a 5-minute crypto market, a trading bot cannot rely only on a simple condition such as:

TWAP > Strike → Buy UP
TWAP < Strike → Buy DOWN
Enter fullscreen mode Exit fullscreen mode

That tells us where the TWAP is, but not where it is going.

polymarket 5min twap momentum trading bot

A better starting point is to combine the current TWAP position with its short-term momentum.

In this tutorial, we'll build the foundation of a Polymarket TWAP Momentum Trading Bot for 5-minute crypto markets.

We'll cover:

  • What TWAP momentum means
  • How to calculate short-term TWAP returns
  • How to generate UP/DOWN signals
  • How to filter weak movements
  • How to structure the strategy in Python
  • How to backtest the strategy
  • Where to take the strategy next

Note: This is an educational trading-system example, not financial advice. Real-world results depend on market conditions, fees, liquidity, latency, and execution quality.

My Polymarket Twap momentum Bot profit Screenshot

1. Understanding the Strategy

Let's start with a simple example.

Suppose the market's strike price is:

$117,000
Enter fullscreen mode Exit fullscreen mode

And the relevant TWAP observations are:

117,050
117,090
117,140
117,190
117,240
Enter fullscreen mode Exit fullscreen mode

The TWAP is above the strike.

More importantly, it is consistently moving upward.

This gives us two pieces of information:

TWAP > Strike
Enter fullscreen mode Exit fullscreen mode

and:

TWAP Momentum > 0
Enter fullscreen mode Exit fullscreen mode

Instead of immediately trading based on the first condition, we require both.

The basic UP setup becomes:

TWAP > Strike
        +
Positive TWAP Momentum
        +
Momentum > Threshold
        ↓
     UP Candidate
Enter fullscreen mode Exit fullscreen mode

For DOWN, we reverse the conditions.

2. Why Momentum Matters

Consider these two situations.

Situation A: Strong upward movement

117,000
117,050
117,120
117,190
117,250
Enter fullscreen mode Exit fullscreen mode

The TWAP is moving higher consistently.

Situation B: Weak upward position

117,250
117,220
117,180
117,140
117,080
Enter fullscreen mode Exit fullscreen mode

The current TWAP may still be above the strike, but the direction is weakening.

A strategy that only checks:

if twap > strike:
    buy_up()
Enter fullscreen mode Exit fullscreen mode

could treat both situations as bullish.

That's a problem.

Momentum gives the bot another dimension:

Where is the TWAP?
+
How quickly is the TWAP moving?
Enter fullscreen mode Exit fullscreen mode

This is the core idea behind our strategy.


3. Calculating TWAP Momentum

A simple way to measure momentum is to calculate the percentage change between the current TWAP and the TWAP from a short time ago.

For example, we'll use a 10-second lookback.

The formula is:

TWAP Return =
(Current TWAP - TWAP 10 Seconds Ago)
/
TWAP 10 Seconds Ago
Enter fullscreen mode Exit fullscreen mode

In Python:

twap_return_10s = (
    twap_now - twap_10s_ago
) / twap_10s_ago
Enter fullscreen mode Exit fullscreen mode

Suppose:

TWAP 10 seconds ago = 117,100
Current TWAP        = 117,250
Enter fullscreen mode Exit fullscreen mode

Then:

(117250 - 117100) / 117100
Enter fullscreen mode Exit fullscreen mode

gives approximately:

0.00128
Enter fullscreen mode Exit fullscreen mode

or:

+0.128%
Enter fullscreen mode Exit fullscreen mode

The important part is not the absolute value.

The important part is the direction and magnitude.

+0.128% → bullish momentum

-0.128% → bearish momentum
Enter fullscreen mode Exit fullscreen mode

4. Creating the UP Signal

Now we can combine the strike relationship with momentum.

A simple UP signal requires:

TWAP > Strike

AND

TWAP Return > Momentum Threshold
Enter fullscreen mode Exit fullscreen mode

For example:

if (
    twap > strike
    and twap_return_10s > momentum_threshold
):
    signal = "UP"
Enter fullscreen mode Exit fullscreen mode

Let's define a threshold:

momentum_threshold = 0.0005
Enter fullscreen mode Exit fullscreen mode

This corresponds to:

0.05%
Enter fullscreen mode Exit fullscreen mode

Now imagine:

TWAP = $117,240
Strike = $117,000

TWAP Return = +0.102%
Enter fullscreen mode Exit fullscreen mode

Since:

117,240 > 117,000
Enter fullscreen mode Exit fullscreen mode

and:

0.102% > 0.05%
Enter fullscreen mode Exit fullscreen mode

the strategy produces:

UP
Enter fullscreen mode Exit fullscreen mode

5. Creating the DOWN Signal

The DOWN signal is the mirror image.

if (
    twap < strike
    and twap_return_10s < -momentum_threshold
):
    signal = "DOWN"
Enter fullscreen mode Exit fullscreen mode

For example:

TWAP = $116,700
Strike = $117,000

TWAP Return = -0.11%
Enter fullscreen mode Exit fullscreen mode

The conditions are satisfied:

TWAP < Strike
Enter fullscreen mode Exit fullscreen mode

and:

TWAP Return < -0.05%
Enter fullscreen mode Exit fullscreen mode

Therefore:

DOWN
Enter fullscreen mode Exit fullscreen mode

6. Don't Trade Weak Signals

One of the most important parts of this strategy is the momentum threshold.

Without a threshold, even extremely small movements can trigger trades.

For example:

TWAP Return = +0.00001%
Enter fullscreen mode Exit fullscreen mode

Technically, momentum is positive.

But this movement may be nothing more than market noise.

So instead of:

twap_return_10s > 0
Enter fullscreen mode Exit fullscreen mode

we use:

twap_return_10s > momentum_threshold
Enter fullscreen mode Exit fullscreen mode

A simple implementation is:

MOMENTUM_THRESHOLD = 0.0005

if twap > strike:
    if twap_return_10s > MOMENTUM_THRESHOLD:
        signal = "UP"
    else:
        signal = "NO_TRADE"

elif twap < strike:
    if twap_return_10s < -MOMENTUM_THRESHOLD:
        signal = "DOWN"
    else:
        signal = "NO_TRADE"
Enter fullscreen mode Exit fullscreen mode

Now the bot has three possible states:

UP
DOWN
NO_TRADE
Enter fullscreen mode Exit fullscreen mode

The third state is extremely important.

A good trading bot should not feel obligated to trade every market update.

Sometimes the best trade is no trade.


7. Implementing the Strategy in Python

Let's turn the idea into a reusable function.

def generate_signal(
    twap,
    strike,
    twap_return_10s,
    momentum_threshold=0.0005
):
    if twap > strike:
        if twap_return_10s > momentum_threshold:
            return "UP"

    elif twap < strike:
        if twap_return_10s < -momentum_threshold:
            return "DOWN"

    return "NO_TRADE"
Enter fullscreen mode Exit fullscreen mode

We can test it:

signal = generate_signal(
    twap=117240,
    strike=117000,
    twap_return_10s=0.00102
)

print(signal)
Enter fullscreen mode Exit fullscreen mode

Output:

UP
Enter fullscreen mode Exit fullscreen mode

And:

signal = generate_signal(
    twap=116700,
    strike=117000,
    twap_return_10s=-0.00110
)

print(signal)
Enter fullscreen mode Exit fullscreen mode

Output:

DOWN
Enter fullscreen mode Exit fullscreen mode

A weak signal:

signal = generate_signal(
    twap=117050,
    strike=117000,
    twap_return_10s=0.00008
)

print(signal)
Enter fullscreen mode Exit fullscreen mode

returns:

NO_TRADE
Enter fullscreen mode Exit fullscreen mode

because the momentum isn't strong enough.


8. Storing Historical TWAP Data

To calculate a 10-second return, the bot needs access to historical TWAP observations.

A simple approach is to maintain a rolling buffer.

from collections import deque

twap_history = deque()
Enter fullscreen mode Exit fullscreen mode

Every time a new TWAP observation arrives:

twap_history.append({
    "timestamp": timestamp,
    "twap": twap
})
Enter fullscreen mode Exit fullscreen mode

We can then find the observation closest to 10 seconds ago.

Conceptually:

Current
   ↓
TWAP History
   │
   ├── now
   ├── -1s
   ├── -2s
   ├── -3s
   ├── ...
   └── -10s
Enter fullscreen mode Exit fullscreen mode

Then:

twap_return_10s = (
    current_twap - historical_twap
) / historical_twap
Enter fullscreen mode Exit fullscreen mode

In a production bot, you should also handle missing observations, stale data, timestamp errors, and reconnects.


9. Adding Time Remaining

Momentum does not necessarily have the same meaning throughout a 5-minute market.

Consider:

4 minutes 40 seconds remaining
Enter fullscreen mode Exit fullscreen mode

versus:

15 seconds remaining
Enter fullscreen mode Exit fullscreen mode

At the beginning of the market, there is plenty of time for the TWAP to move.

Near expiration, there is much less time for the underlying TWAP to change.

Therefore, a production version should track:

time_remaining
Enter fullscreen mode Exit fullscreen mode

For example:

signal_context = {
    "twap": twap,
    "strike": strike,
    "momentum": twap_return_10s,
    "time_remaining": time_remaining,
}
Enter fullscreen mode Exit fullscreen mode

Later, we can use this information to dynamically adjust the required momentum threshold.

For example:

More time remaining
→ require stronger confirmation

Less time remaining
→ use a different threshold
Enter fullscreen mode Exit fullscreen mode

The exact relationship should be determined through testing rather than assumed.


10. Avoiding False Momentum

Momentum doesn't always mean continuation.

Suppose BTC suddenly moves upward:

117,000
117,150
117,300
Enter fullscreen mode Exit fullscreen mode

The TWAP momentum becomes strongly positive.

But a few seconds later:

117,300
117,100
116,950
Enter fullscreen mode Exit fullscreen mode

the move reverses.

This is why momentum should be viewed as a signal, not a guarantee.

The Day 1 strategy deliberately stays simple.

Its purpose is to answer:

Does short-term TWAP momentum provide useful predictive information?

Once we establish that baseline, we can add confirmation filters.


11. Backtesting the Strategy

Before deploying real capital, we need to test the strategy against historical data.

At minimum, record:

timestamp
market_id
TWAP
strike
TWAP return
time remaining
signal
entry price
market outcome
PnL
Enter fullscreen mode Exit fullscreen mode

A simple dataset might look like:

Time TWAP Strike Momentum Signal
12:00:10 117050 117000 +0.01% NO_TRADE
12:00:20 117120 117000 +0.08% UP
12:00:30 117210 117000 +0.11% UP
12:00:40 117180 117000 -0.02% NO_TRADE

Then calculate:

Win Rate
Average PnL
Total PnL
Maximum Drawdown
Average Trade
Number of Trades
Enter fullscreen mode Exit fullscreen mode

But don't stop at aggregate performance.

Break the results into market regimes:

Strong Trend
Choppy Market
High Volatility
Low Volatility
Fast Reversal
Slow Trend
Enter fullscreen mode Exit fullscreen mode

This can reveal where the strategy actually works.


12. The Baseline Strategy

The complete Day 1 logic can be summarized as:

                 Current TWAP
                      │
                      ▼
              Compare to Strike
                /           \
               /             \
       TWAP > Strike      TWAP < Strike
            │                  │
            ▼                  ▼
     Positive Momentum    Negative Momentum
            │                  │
            ▼                  ▼
     Above Threshold      Below Threshold
            │                  │
            ▼                  ▼
           UP                DOWN
Enter fullscreen mode Exit fullscreen mode

In pseudocode:

if twap > strike:
    if twap_return_10s > threshold:
        signal = "UP"

elif twap < strike:
    if twap_return_10s < -threshold:
        signal = "DOWN"

else:
    signal = "NO_TRADE"
Enter fullscreen mode Exit fullscreen mode

This is deliberately simple.

And that's exactly what makes it useful.


13. What We Can Add Next

Once this baseline is working, there are many ways to improve it.

For example:

Multi-Timeframe Momentum

Instead of only using 10-second momentum:

10s momentum
30s momentum
60s momentum
Enter fullscreen mode Exit fullscreen mode

can be combined into a single score.

Order Book Imbalance

We can measure whether buyers or sellers dominate the order book:

OBI =
(Bid Volume - Ask Volume)
/
(Bid Volume + Ask Volume)
Enter fullscreen mode Exit fullscreen mode

Then use order-book pressure to confirm TWAP momentum.

External BTC Price

The bot can compare Polymarket's market state against an external BTC price feed.

This can help detect situations where the underlying crypto market is moving before the prediction-market price fully adjusts.

Volatility Filters

Momentum behaves differently during high-volatility and low-volatility periods.

A volatility filter can help avoid signals generated by insignificant movements.

Probability Models

Eventually, instead of simply returning:

UP
DOWN
Enter fullscreen mode Exit fullscreen mode

the strategy can estimate:

P(UP) = 0.67
Enter fullscreen mode Exit fullscreen mode

and compare that probability against the market price.

That's where the strategy begins moving from simple technical signals toward probability-driven prediction-market trading.


Conclusion

The first version of a Polymarket TWAP Momentum Trading Bot doesn't need dozens of indicators.

It needs a clear hypothesis.

Our hypothesis is simple:

A TWAP that is moving strongly in one direction may provide more useful information than simply knowing whether the TWAP is currently above or below the strike.

The baseline therefore combines:

TWAP Position
+
TWAP Momentum
+
Momentum Threshold
Enter fullscreen mode Exit fullscreen mode

to produce:

UP
DOWN
NO_TRADE
Enter fullscreen mode Exit fullscreen mode

This strategy is not intended to be the final system.

It is the baseline.

Once we have reliable historical results, we can systematically test whether additional information—order-book imbalance, external crypto prices, volatility, time remaining, and probability models—actually improves the strategy.

That's the key principle behind building trading bots:

Start simple. Measure everything. Then add complexity only when the data proves it is useful.

In the next strategy, we'll take the basic TWAP momentum concept and make it more robust by looking at multiple momentum timeframes instead of relying on a single 10-second measurement.

🤝 Collaboration & Contact

If you’re interested in building trading bots, buy trading bots, collaborating, exploring strategy improvements, or discussing about this system, feel free to reach out.

I’m especially open to connecting with:

Quant traders
Engineers building trading infrastructure
Researchers in prediction markets
Investors interested in market inefficiencies

📌 GitHub Repository

This repo has some Polymarket several bots in this system.
You can explore the full implementation, strategy logic, and ongoing updates about 5 min crypto market here:

GitHub logo Benjam1nCup / Polymarket-trading-bot-python-V2

polymarket bot polymarket twap bot polymarket arbitrage bot polymarket trading bot polymarket bot polymarket twap bot polymarket arbitrage bot polymarket trading bot polymarket bot polymarket twap bot polymarket arbitrage bot polymarket trading bot polymarket bot polymarket twap bot polymarket arbitrage bot polymarket trading bot polymarket bot

Polymarket Trading Bot | Polymarket Arbitrage Bot | Polymarket TWAP Trading Bot

An open-source and Strong Strategy collection of Polymarket trading bot and Polymarket arbitrage bot and Polymarket TWAP trading bot in Python for high-performance automated trading on polymarket crypto 5min and 15min markets.

Polymarket-benjamincup-bot-dashboard

Features

  • Explosive growth of Polymarket with surging trading volume and new short-term markets

  • Increasing dominance of automated bots and AI in 5-minute and 15-minute crypto prediction markets

  • Higher profitability potential through advanced arbitrage and market-making strategies

  • Stronger edge for Python-based bots with real-time orderbook intelligence and low-latency execution

  • Continuous evolution of sniper, ladder, stair, momentum, and copy trading strategies

  • Scalable daily profits as prediction markets move toward hundreds of billions in annual volume

  • Full future-proof architecture for new features, contracts, and high-frequency trading environments

Included Trading Bots

Designed for arbitrage, directional strategies, and ultra-short-term markets (including 5-minute and 15-minute rounds), this bot framework provides a robust…




💬 Get in Touch

If you have ideas, questions, or would like to collaborate or want these trading bots, don’t hesitate to reach out directly.
Feedback on your repo (based on your description & strategy)

Contact Info
Telegram
https://t.me/BenjaminCup

tags: polymarket,trading,bot,architecture,tutorial,TWAP

Top comments (0)