Building a Polymarket TWAP State Machine for Trading Bots

A short-duration trading bot can receive every price update correctly and still make a structurally wrong decision. The reason is simple: a stream of prices is not the same thing as a market state. For TWAP-resolved Polymarket markets, the strategy is not merely predicting where an exchange price will move next. It is attempting to estimate whether an oracle-defined measurement will satisfy a specific resolution rule. Current Polymarket markets can explicitly define a Chainlink TWAP stream as the resolution source rather than another exchange's spot price. That changes the architecture.

Understanding the Problem: Price Streams vs. Market State

Traditional trading bots often operate by reacting to incoming price data. They might execute trades based on thresholds, moving averages, or other technical indicators derived directly from spot prices. However, when dealing with markets that resolve based on a Time-Weighted Average Price (TWAP) oracle, this approach becomes insufficient. A TWAP is not a single point in time but an aggregate over a period, typically 30 minutes, an hour, or longer. This means that the 'price' relevant to resolution is not the instantaneous price a bot sees, but the average price over a specific window. Simply chasing the latest price update can lead to misinterpretations of the market's actual resolution condition.

Consider a scenario where a bot is designed to buy if the price crosses a certain threshold. If the market resolves based on a 1-hour TWAP, the bot needs to understand how its current trades and the observed prices will contribute to that 1-hour average. A sudden spike or dip in the spot price might be irrelevant if it occurs outside the TWAP window or is counterbalanced by prices during the rest of the window.

The core issue is that bots need to reason about the state of the market relative to its resolution mechanism, not just the ephemeral price data. This requires a more sophisticated model that explicitly accounts for the temporal and observational aspects of TWAP resolution.

The Polymarket TWAP State Machine Architecture

To address this, a state machine provides a robust framework. It breaks down the complex process of interacting with a TWAP-resolved Polymarket into distinct, manageable states. This approach ensures that the bot's decision-making logic is aligned with the market's resolution rules. The proposed state machine models four critical components:

1. Resolution State

This state represents the bot's understanding of the market's final resolution outcome. It's not about predicting the outcome, but about tracking what information is available and how it relates to the potential final resolution. This could involve tracking whether the TWAP period has concluded, whether the oracle has reported a final value, and what that value is. For a bot, this state is crucial for determining when it can no longer influence the outcome and when it should cease active trading or prepare for settlement.

2. Oracle Observations State

This state is concerned with the actual data being fed into the market's resolution mechanism. For a TWAP market, this means continuously monitoring the Chainlink TWAP oracle (or whichever oracle is specified). The bot needs to record the observed prices and timestamps that constitute the TWAP. This isn't just about fetching the latest value; it's about collecting the underlying data points that will be used to calculate the average. This state might involve storing a history of observations, calculating intermediate TWAP values, and understanding the window over which these calculations are performed.

Diagram illustrating the flow of oracle data into the TWAP calculation state.

3. Market Time State

This state manages the temporal aspects of the market. It tracks the current time relative to the TWAP calculation window and the market's overall lifespan. Key events include the start and end of the TWAP period, the market's opening and closing times, and any other time-sensitive conditions defined by Polymarket. A bot operating on a short-duration market needs precise timing. For instance, it must know exactly when the TWAP period begins and ends to understand which price observations are relevant for resolution.

4. Execution State

This state governs the bot's actual trading actions. Based on the information gathered in the other states (resolution status, oracle observations, and market time), the execution state determines when and how to place orders. This could involve executing trades to hedge positions, to try and influence the TWAP within acceptable risk parameters, or to exit positions as resolution approaches. The decisions made here are directly informed by the bot's understanding of the market state, not just raw price feeds.

Benefits of the State Machine Approach

Implementing a state machine for TWAP-resolved Polymarket bots offers several advantages:

  • Clarity and Robustness: Explicitly defining states makes the bot's logic easier to understand, debug, and maintain. It forces developers to consider all relevant scenarios and transitions.
  • Accuracy: By modeling the market's resolution mechanism, the bot can make decisions that are more aligned with the actual outcome, reducing the risk of structurally wrong trades.
  • Adaptability: This modular design makes it easier to adapt the bot to different TWAP durations, different oracle types, or even different market resolution mechanisms in the future.
  • Risk Management: A clear understanding of the market state, particularly the temporal and observational aspects, allows for more sophisticated risk management. The bot can identify periods where trading is futile or excessively risky.

Implications for Bot Development

Developers building trading bots for platforms like Polymarket, especially those dealing with oracle-based resolutions, need to move beyond simple price-following algorithms. The state machine paradigm offers a structured way to incorporate market-specific logic. It's less about predicting the price and more about understanding the rules of the game – in this case, the rules of TWAP-based resolution.

This architectural shift requires a deeper understanding of smart contract resolution mechanisms, oracle data feeds, and temporal logic. It means treating the bot not just as a price execution engine, but as an intelligent agent that reasons about a dynamic, rule-governed environment. The success of such bots hinges on their ability to accurately model and react to the market's state, as defined by its resolution parameters.