POLY TRADER

2 Squirrels AI

Technology Stack

Multi-strategy AI trading bot for Polymarket prediction markets. Combines algorithmic trading with ML and news sentiment analysis on the Polygon blockchain with a $200 USD budget constraint.

LLMClaude Haiku
SentimentFinBERT
MLRandom Forest
Tradingpy-clob-client
BlockchainPolygon
DatabaseSQLite

Workflow 1: Real-Time Arbitrage Detection

%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#C17852', 'primaryTextColor': '#F0F6FC', 'primaryBorderColor': '#4A5E32', 'lineColor': '#E6C98F', 'secondaryColor': '#161B22', 'tertiaryColor': '#0D1117'}}}%%
flowchart LR
    subgraph Data["📊 MARKET DATA"]
        WS["🔌 WebSocket"]
        REST["🌐 REST API"]
        BOOK["📈 Orderbooks"]
    end
    subgraph Analysis["🔍 ANALYSIS"]
        CALC["🧮 YES + NO < $0.97?"]
        SPREAD["📉 Spread > 3%?"]
        LIQ["💧 Liquidity Check"]
    end
    subgraph Execute["⚡ EXECUTION"]
        SIZE["📐 Position Sizing"]
        FOK["🎯 Fill-or-Kill Orders"]
        PARALLEL["⚡ Parallel Legs"]
    end
    subgraph Output["📤 RESULT"]
        PNL["💰 Guaranteed P&L"]
        LOG[("📄 SQLite Log")]
        DISCORD["📢 Discord Alert"]
    end
    WS --> BOOK
    REST --> BOOK
    BOOK --> CALC
    CALC --> SPREAD
    SPREAD --> LIQ
    LIQ --> SIZE
    SIZE --> FOK
    FOK --> PARALLEL
    PARALLEL --> PNL
    PNL --> LOG
    PNL --> DISCORD
                

Workflow 2: News-Driven Sentiment Trading

%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#C17852', 'primaryTextColor': '#F0F6FC', 'primaryBorderColor': '#4A5E32', 'lineColor': '#E6C98F', 'secondaryColor': '#161B22', 'tertiaryColor': '#0D1117'}}}%%
flowchart TB
    subgraph News["📰 NEWS COLLECTION"]
        API["🌐 NewsAPI"]
        ARTICLES["📄 24h Lookback"]
    end
    subgraph Claude["🤖 CLAUDE ANALYSIS"]
        PROMPT["📝 Article + Question"]
        ENTITIES["🏷️ Extract Entities"]
        SENTIMENT["😊 Sentiment -1 to +1"]
        CONFIDENCE["📊 Confidence 0-1"]
    end
    subgraph Fallback["🔄 FALLBACK"]
        FINBERT["🧠 FinBERT Batch"]
    end
    subgraph Signal["📈 SIGNAL GENERATION"]
        IMPLIED["📊 Implied Probability"]
        DIVERGE["📉 Divergence > 15%?"]
        COOLDOWN["⏰ 4hr Cooldown"]
    end
    subgraph Trade["💹 TRADE"]
        GTC["📋 GTC Order"]
        LOG[("📄 Trade Log")]
    end
    API --> ARTICLES
    ARTICLES --> PROMPT
    PROMPT --> ENTITIES
    ENTITIES --> SENTIMENT
    SENTIMENT --> CONFIDENCE
    CONFIDENCE -->|"< 0.7"| FINBERT
    CONFIDENCE -->|"> 0.7"| IMPLIED
    FINBERT --> IMPLIED
    IMPLIED --> DIVERGE
    DIVERGE --> COOLDOWN
    COOLDOWN --> GTC
    GTC --> LOG
                

Workflow 3: Random Forest Price Prediction

%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#C17852', 'primaryTextColor': '#F0F6FC', 'primaryBorderColor': '#4A5E32', 'lineColor': '#E6C98F', 'secondaryColor': '#161B22', 'tertiaryColor': '#0D1117'}}}%%
flowchart TB
    subgraph Data["📊 HISTORICAL DATA"]
        MARKETS["📈 20 Markets × 7 Days"]
        HOURLY["⏰ Hourly Samples"]
    end
    subgraph Features["🔧 FEATURE ENGINEERING"]
        PRICE["💰 Price Features
Returns, SMA, RSI"] VOL["📊 Volume Features
Momentum, Spread"] SENT["😊 Sentiment Features
FinBERT Score"] STRUCT["📐 Structure Features
Days to Resolution"] end subgraph Train["🧠 TRAINING"] SPLIT["✂️ TimeSeriesSplit
5 Folds"] RF["🌲 Random Forest
200 Trees, depth=10"] CV["📊 Cross-Validation"] end subgraph Predict["🎯 PREDICTION"] PROB["📈 Price > 5% in 4hrs?"] CONF["📊 Confidence Score"] FEAT["🔍 Feature Importance"] end MARKETS --> HOURLY HOURLY --> PRICE HOURLY --> VOL HOURLY --> SENT HOURLY --> STRUCT PRICE --> SPLIT VOL --> SPLIT SENT --> SPLIT STRUCT --> SPLIT SPLIT --> RF RF --> CV CV --> PROB PROB --> CONF CONF --> FEAT

Workflow 4: Asymmetric Position Building

%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#C17852', 'primaryTextColor': '#F0F6FC', 'primaryBorderColor': '#4A5E32', 'lineColor': '#E6C98F', 'secondaryColor': '#161B22', 'tertiaryColor': '#0D1117'}}}%%
flowchart LR
    subgraph Monitor["📊 MONITORING"]
        YES["📈 YES Price"]
        NO["📉 NO Price"]
    end
    subgraph Entry["📥 ENTRY LOGIC"]
        THRESH["💰 Price < $0.45?"]
        PROJ["🧮 New Avg Cost"]
        PROFIT["📊 Combined < $0.97?"]
    end
    subgraph Build["🏗️ POSITION BUILD"]
        SIZE["📐 $5 Increments"]
        GTC["📋 GTC Order"]
        TRACK["📊 Cost Basis Tracker"]
    end
    subgraph Exit["📤 EXIT LOGIC"]
        SPIKE["📈 Price > $0.70?"]
        NET["💰 Net Profit > 10%?"]
        SELL["💵 Execute Exit"]
    end
    YES --> THRESH
    NO --> THRESH
    THRESH --> PROJ
    PROJ --> PROFIT
    PROFIT --> SIZE
    SIZE --> GTC
    GTC --> TRACK
    TRACK --> SPIKE
    SPIKE --> NET
    NET --> SELL
                

Workflow 5: FinBERT Sentiment Pipeline

%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#C17852', 'primaryTextColor': '#F0F6FC', 'primaryBorderColor': '#4A5E32', 'lineColor': '#E6C98F', 'secondaryColor': '#161B22', 'tertiaryColor': '#0D1117'}}}%%
flowchart TB
    subgraph Input["📥 TEXT INPUT"]
        ARTICLES["📰 News Articles"]
        BATCH["📦 Batch Processing"]
    end
    subgraph Model["🧠 FINBERT"]
        LOAD["📥 Lazy Load Model"]
        TOKEN["🔤 Tokenize
Max 512 tokens"] INFER["⚡ Forward Pass"] end subgraph Score["📊 SCORING"] SOFTMAX["📈 Softmax Outputs"] CALC["🧮 positive - negative"] LABEL["🏷️ pos/neg/neutral"] end subgraph Aggregate["🔗 AGGREGATION"] DECAY["⏰ Time Decay
6hr Half-life"] WEIGHT["⚖️ Weighted Average"] SUMMARY["📊 Market Sentiment"] end ARTICLES --> BATCH BATCH --> LOAD LOAD --> TOKEN TOKEN --> INFER INFER --> SOFTMAX SOFTMAX --> CALC CALC --> LABEL LABEL --> DECAY DECAY --> WEIGHT WEIGHT --> SUMMARY

Workflow 6: Risk Management Circuit Breaker

%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#C17852', 'primaryTextColor': '#F0F6FC', 'primaryBorderColor': '#4A5E32', 'lineColor': '#E6C98F', 'secondaryColor': '#161B22', 'tertiaryColor': '#0D1117'}}}%%
flowchart TB
    subgraph Monitor["📊 DAILY MONITORING"]
        PNL["💰 Daily P&L"]
        LOSS["📉 Max Loss: $10 (5%)"]
    end
    subgraph PreTrade["✅ PRE-TRADE CHECKS"]
        POS["📐 Position ≤ $10"]
        MARKET["📊 Market Exp ≤ $20"]
        TOTAL["💼 Portfolio ≤ $160"]
        CASH["💵 Cash Reserve ≥ 20%"]
    end
    subgraph Errors["⚠️ ERROR HANDLING"]
        API["🔌 API Failures"]
        COUNT["🔢 3 Consecutive?"]
        PAUSE["⏸️ Pause 5 min"]
    end
    subgraph Circuit["🔴 CIRCUIT BREAKER"]
        HALT["🛑 Halt Trading"]
        WAIT["⏰ 24hr Cooldown"]
        ALERT["📢 Discord Alert"]
    end
    subgraph Report["📋 STATUS"]
        STATUS["📊 Cash, Positions, P&L"]
        RESET["🔄 Daily Reset"]
    end
    PNL --> LOSS
    LOSS -->|"Exceeded"| HALT
    HALT --> WAIT
    WAIT --> ALERT
    POS --> MARKET
    MARKET --> TOTAL
    TOTAL --> CASH
    API --> COUNT
    COUNT --> PAUSE
    STATUS --> RESET