Skip to content

tomasmach/polyspike-hunter

Repository files navigation

PolySpike Hunter ๐Ÿน

A high-frequency trading bot for Polymarket designed to capture short-term volatility ("spikes") caused by emotional trading or breaking news. Built with Python and py-clob-client.

License: MIT

โš ๏ธ DISCLAIMER: This software is for educational purposes only. Algorithmic trading involves significant risk. The authors are not responsible for any financial losses incurred while using this bot. Use at your own risk.

๐ŸŽฏ Current Status: Paper Trading Mode

โœ… FULLY IMPLEMENTED - Ready to use!

The bot currently runs in Paper Trading Mode:

  • โœ… Real market data from Polymarket API
  • โœ… Simulated trades with fake balance ($100 starting)
  • โœ… Complete strategy, risk management, and reporting
  • โŒ No real money at risk - pure simulation

โšก How It Works

The bot operates on a Spike Hunter / Mean Reversion strategy:

  1. Monitor: Polls 50 highest-volume Polymarket markets every second
  2. Detect: Identifies price spikes (ยฑ3% deviation from moving average)
  3. Enter: Opens simulated $5 position when spike detected
  4. Exit: Closes position at:
    • Take Profit: +4% gain
    • Stop Loss: -2% loss
  5. Log: Records all trades to JSON and market data to CSV

๐Ÿ› ๏ธ Features

  • ๐Ÿ“Š Paper Trading: Risk-free simulation with fake balance
  • โšก Low Latency: Fully asynchronous architecture using asyncio
  • ๐Ÿ”Œ Real Data: Live market data from Polymarket CLOB API
  • ๐ŸŽฏ Smart Strategy: Spike detection with moving average analysis
  • ๐Ÿ›ก๏ธ Risk Management: Position sizing, max drawdown, cooldowns
  • ๐Ÿ“ˆ Real-time Reporting: CSV/JSON logging of trades and market data
  • ๐Ÿ“ก MQTT Integration: Real-time events pro Discord bot monitoring
  • ๐Ÿ”ง Highly Configurable: All parameters via .env file

๐Ÿ“ก MQTT Integration

Bot publishes real-time events via MQTT for monitoring and Discord notifications:

Events:

  • โœ… Bot status (started/stopped/heartbeat)
  • โœ… Spike detection
  • โœ… Position opened/closed
  • โœ… Trade completed (+ P&L)
  • โœ… Balance updates (every 12h)
  • โœ… Session statistics

MQTT Broker:

  • Localhost (port 1883)
  • No authentication
  • Topic prefix: polyspike/

Documentation: See docs/MQTT_API.md for complete API reference.

Discord Bot: Separate repository (coming soon)

๐Ÿš€ Quick Start

1. Install Dependencies

pip install -r requirements.txt

2. Configure (Optional)

The bot comes with sensible defaults in .env.example. For testing:

cp .env.example .env
# Edit .env if you want to change settings

3. Run the Bot

python run_bot.py

Or:

python main.py

4. Monitor & Stop

  • Watch real-time logs in console
  • Press Ctrl+C to stop
  • Check data/sessions/TIMESTAMP/ for results

๐Ÿ“Š What You'll See

๐Ÿน PolySpike Hunter - Paper Trading Mode
============================================================
๐Ÿ“Š Configuration:
  Initial Balance: $100.0
  Spike Threshold: 3%
  Position Size: $5.0
  Max Markets: 50
============================================================

Starting bot...

2025-12-25T18:10:03Z [info] ๐Ÿš€ Starting PolySpikeHunter...
2025-12-25T18:10:04Z [info] โœ… Bot is now running. Press Ctrl+C to stop.
2025-12-25T18:10:15Z [info] spike_detected direction=down spike_pct=-3.2%
2025-12-25T18:10:15Z [info] โœ… ENTRY EXECUTED price=0.4800 size=$5.00
2025-12-25T18:10:45Z [info] take_profit_triggered pnl_pct=+4.12%
2025-12-25T18:10:45Z [info] โœ… EXIT EXECUTED pnl=$+0.20 balance=$100.20
...

๐Ÿ“ Output Files

After each session, find your results in data/sessions/TIMESTAMP/:

  • trades.json - All completed trades with entry/exit prices, P&L
  • markets.csv - Market snapshots (price, MA, volatility)
  • summary.json - Session statistics and performance metrics

MQTT Events: See docs/MQTT_API.md for real-time event streaming via MQTT.

โš™๏ธ Configuration

Edit .env to customize:

# Paper Trading
INITIAL_BALANCE=100.0          # Starting fake balance
PAPER_TRADING=true             # Always true for now

# Strategy
SPIKE_THRESHOLD=0.03           # 3% spike to trigger entry
POSITION_SIZE=5.0              # $5 per trade
TAKE_PROFIT_PCT=0.04           # 4% take profit
STOP_LOSS_PCT=0.02             # 2% stop loss

# Monitoring
MAX_MONITORED_MARKETS=50       # Number of markets to watch
MONITOR_STRATEGY=volume        # Use highest volume markets
POLL_INTERVAL=1.0              # Poll every second

# Risk Management
MAX_DRAWDOWN=50.0              # Stop if losses reach $50
MAX_OPEN_POSITIONS=10          # Max concurrent positions

# MQTT Integration
MQTT_ENABLED=true                    # Enable MQTT publishing
MQTT_HOST=localhost                  # MQTT broker host
MQTT_PORT=1883                       # MQTT broker port
MQTT_BALANCE_UPDATE_INTERVAL=43200  # Balance updates (12h)
MQTT_HEARTBEAT_INTERVAL=30          # Heartbeat interval

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                   Main Bot Loop                     โ”‚
โ”‚                    (main.py)                        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
               โ”‚
      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
      โ”‚                 โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Market    โ”‚   โ”‚   Strategy    โ”‚
โ”‚  Monitor   โ”‚โ”€โ”€โ–ถโ”‚ Spike Hunter  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
      โ”‚                  โ”‚
      โ”‚           โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
      โ”‚           โ”‚ Paper Trading โ”‚
      โ”‚           โ”‚    Engine     โ”‚
      โ”‚           โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
      โ”‚                  โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”
โ”‚      Risk Manager           โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
              โ”‚
      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
      โ”‚   Reporting    โ”‚
      โ”‚  (CSV/JSON)    โ”‚
      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Core Components:

  • main.py - Main bot orchestrator
  • src/core/market_monitor.py - Async market polling
  • src/core/paper_trading.py - Simulated trading engine
  • src/strategy/spike_hunter.py - Spike detection logic
  • src/core/risk_manager.py - Safety limits and validation
  • src/utils/reporting.py - CSV/JSON logging

๐Ÿงช Testing

Test individual components:

# Test API connection
python test_connection.py

# Test market monitoring
python test_monitor.py

# Test MQTT integration
python test_mqtt.py subscribe   # Listen to all MQTT events
python test_mqtt.py publish     # Send test events

# Run full integration test
python -c "from main import PolySpikeHunter; print('โœ… All imports OK')"

๐Ÿ“š Documentation

๐Ÿ”ฎ Roadmap

Future enhancements:

  • Live trading mode (with real API keys and real money)
  • WebSocket support for lower latency
  • More sophisticated strategies (ML-based?)
  • Backtesting framework
  • Web dashboard for monitoring
  • Multiple strategy support

๐Ÿ“ License

MIT License - See LICENSE for details

โš ๏ธ Final Warning

This is experimental software for educational purposes only.

  • Paper trading mode is completely safe (no real money)
  • Live trading (when implemented) will involve real financial risk
  • Never trade with money you can't afford to lose
  • Cryptocurrency/prediction markets are highly volatile
  • Past performance does not guarantee future results

Use at your own risk!

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages