What I Built (In Plain English)

On prediction markets like Polymarket, prices update from an external oracle that lags behind primary crypto exchanges (like Binance) by about 2.7 seconds. When Bitcoin price spikes on Binance, Polymarket's price stays frozen for a brief moment.

I built an automated bot in C++20 that watches both markets simultaneously, spots that 2.7-second delay, calculates the true fair price, and executes profitable trades before the oracle catches up.

HFT Arbitrage Bot Architecture

⚠️ Tech Ahead: The sections below dive into zero-allocation memory constraints, lock-free circular buffers, sigmoid pricing math, and deterministic Polygon EVM execution.


Technical Architecture

[Binance WebSocket Tick] ──► [Lock-Free Circular Buffer] ──► [Fair Value Sigmoid]
                                                                     │
                                                                     ▼
[Polygon RPC Execution] ◄── [Adaptive Kelly Sizer] ◄── [5-Stage Filter Matrix]

1. Zero-Allocation Hot Path

Signal detection and fair-value evaluation execute without dynamic heap allocation (new/malloc) on the critical path. All state structures, socket buffers, and order templates are pre-allocated during engine boot.

2. Lock-Free Circular History Buffer

The engine maintains a contiguous ring buffer storing the last 100 seconds of microsecond-stamped price ticks. This enables O(1) constant-time lookups to query the exact historical price from T - 2.7s:

struct PriceTick {
    uint64_t timestamp_us;
    double price;
};

// Lock-free circular buffer for deterministic history queries
template <size_t N>
class CircularTickBuffer {
    std::array<PriceTick, N> buffer_;
    std::atomic<size_t> head_{0};
public:
    void push(double price, uint64_t ts) noexcept;
    double get_price_at(uint64_t target_ts) const noexcept;
};

5-Stage Deterministic Gate Matrix

Every tick must pass five sequential boolean gates before dispatching an order to the Polygon blockchain:

Gate Condition Purpose
1. Min Price Move |P(t) - P(t - 2.7s)| > delta_min Ignores low-volatility market noise.
2. Entry Zone 0.38 <= Token_Price <= 0.62 Avoids unfavorable tail payouts.
3. Fair Value Strength |Fair_Value - 0.50| >= 0.05 Confirms statistical edge over random walk.
4. Minimum Edge Fair_Value - Token_Price >= 0.05 Covers gas and DEX slippage costs.
5. Timing Window Elapsed_Time < 80% of window Prevents late fills near resolution cutoff.

Dual Arbitrage Engines

  1. Latency Arb: Front-runs oracle resolution updates using real-time Binance WebSocket price velocity.
  2. Dump Hedge: Scans for structural mispricings where P(YES) + P(NO) < 1.00, locking in risk-free parity arbitrage.
  3. Adaptive Kelly Sizing: Dynamically computes position size based on current portfolio bankroll and rolling win-rate probability.
— [ DISPATCH TRANSMISSION COMPLETE // END OF FILE ] —
NR

Nuno Ribeiro

Head of Cloud, Full Stack developer (C++, Rust, TypeScript), and digital artist. Writing on distributed architectures, low-latency execution pipelines, and automated intelligence.