Inspiration

In modern markets, prices don't just move on technicals—they move on narratives. Traditional market making algorithms often get "run over" during high-volatility news events because they are blind to the context driving the flow. We were inspired to build a system that doesn't just look at the order book, but actually reads the news. We wanted to bridge the gap between Natural Language Processing (NLP) and High-Frequency Trading (HFT), creating a bot that acts as a liquidity provider in calm waters and a predatory sniper when news breaks.

What it does

Twice The Trade is a hybrid algorithmic trading system designed for the Optibook exchange, trading dual-listed equities (NVDA, ING, SAN, CSCO, PFE). It functions in two distinct regimes based on a real-time "Fair Value" model:

  1. The Inventory Manager (Quiet Regime): When social media is quiet, the bot acts as a Market Maker, providing liquidity on both sides of the book to capture the spread, while managing inventory risk through skewed quotes.
  2. The Sniper (Active Regime): When our AI model detects significant news, it calculates an "Alpha" signal. If the signal is strong enough to overcome transaction costs, the bot switches modes instantly—pulling quotes and aggressively sweeping the order book (taking liquidity) before the market adjusts to the new information.

How we built it

We architected the system using a Sidecar Pattern to handle the speed mismatch between heavy AI inference and fast tick data:

  • The Brain (AI Sidecar): We built a custom Multi-Head Transformer model (based on finbert-tone). Unlike standard sentiment models, ours is a "3-Headed Dragon" that performs three tasks in a single forward pass:
    • Sentiment Head: Classifies news as Bullish, Bearish, or Neutral.
    • Entity Head: Identifies exactly which stock(s) are targeted (solving the "hallucination" problem where news about Cisco affects Nvidia).
    • Volatility Head: Predicts the magnitude of the expected price move (Regression).
  • The Body (Execution Engine): A Python-based HFT loop (TwiceTheTradeBot) that runs independently. It reads the latest Alpha signals from the sidecar and executes trades using a custom RateLimiter and strictly managed position limits.
  • The Glue: A dynamic pricing formula: $P_{fair} = P_{mid} \cdot (1 + \alpha_{sentiment} + \alpha_{inventory})$.

Challenges we ran into

  • The Latency Mismatch: NLP models take ~50ms to run, while the order book changes in milliseconds. Initially, our bot "froze" while reading tweets. We solved this by decoupling the sentiment engine from the execution loop, allowing the bot to continue quoting while the AI processed data in the background.
  • Over-Aggression: In early tests, the "Sniper" logic was too trigger-happy, leading to massive inventory build-ups. We had to implement "Optimistic Position Updates" in trading_main.py to prevent the bot from double-firing orders before the exchange confirmed the first trade.
  • Data Imbalance: Our training data was noisy. We had to implement a custom loss function with Entity Masking, ensuring the model didn't learn false correlations (e.g., punishing the model for predicting a move in NVDA when the news was clearly about PFE).

Accomplishments that we're proud of

  • The "3-Head Dragon": Successfully training a single BERT model to handle Sentiment, Entity Detection, and Volatility simultaneously. Our validation logs show 100% accuracy on entity detection and sentiment classification on the held-out set.
  • Robust Architecture: Implementing a file-lock mechanism (acquire_bot_lock) and strict rate limiting to ensure the bot remains stable and compliant with exchange rules, even during crash scenarios.
  • Smart Skewing: The math behind our fair value calculation successfully shifts our quotes before the market moves, allowing us to buy low and sell high passively.

What we learned

  • Inventory is King: You can have the best signal in the world, but if you can't manage your inventory, you will blow up. Adding the inventory_skew variable to our pricing model was the turning point for profitability.
  • Not All News is Equal: Predicting the direction isn't enough; you need to predict the magnitude. A small positive news story shouldn't trigger a massive liquidity sweep.
  • Clean Data > Big Models: We got better performance by cleaning our CSVs and properly handling "Zero Return" rows than we did by trying larger Transformer models.

What's next for wavers

  • Dual-Listing Arbitrage: While we currently trade pairs individually, the next step is to implement atomic hedging between the main and dual listings (e.g., Long NVDA / Short NVDA_DUAL) to lock in risk-free profits.
  • Hardware Acceleration: Moving the inference engine to a dedicated GPU or compiled ONNX runtime to drive latency down from 50ms to <5ms.
  • Reinforcement Learning: Replacing our linear scalar parameters (ALPHA_SCALAR) with an RL agent that learns the optimal aggression levels dynamically.

Built With

Share this project:

Updates