| --- |
| title: skyAlpha |
| emoji: π€οΈ |
| colorFrom: blue |
| colorTo: indigo |
| sdk: gradio |
| sdk_version: 6.19.0 |
| app_file: app.py |
| pinned: false |
| --- |
| |
| # π€οΈ skyAlpha β Multi-Agent Weather Prediction & Polymarket Trading System |
|
|
| [](https://gradio.app) |
| [](https://python.org) |
| [](LICENSE) |
| [](https://huggingface.co/spaces/puravky/skyalpha) |
|
|
| An autonomous AI agent that analyzes live weather forecasts and Polymarket prediction market odds to execute paper trades across 5 global cities using LLM reasoning, the Kelly Criterion, and a Gradio dashboard. |
|
|
| --- |
|
|
| ## β¨ Features |
|
|
| | Feature | Description | |
| |---------|-------------| |
| | **Live Weather** | Real-time forecasts from Open-Meteo (no API key needed) | |
| | **LLM Analysis** | Gemma via OpenRouter compares forecast vs. market probabilities | |
| | **Kelly Sizing** | Optimal position sizing with 25% fractional Kelly and 10% per-trade cap | |
| | **Hedging** | Automatic 20% counter-position hedge on every trade | |
| | **Demo Mode** | Falls back to simulated markets when Polymarket API is unreachable | |
| | **Dashboard** | Real-time Gradio UI with balance, markets, portfolio, and trade history | |
| | **Zero Risk** | Paper trading β no real money involved | |
|
|
| --- |
|
|
| ## ποΈ Architecture |
|
|
| ``` |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| β main.py (Orchestrator) β |
| β β |
| β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β |
| β β data/ β β agent/ β β trading/ β β |
| β β β β β β β β |
| β β weather.py βββΆβ llm.py βββΆβ kelly.py β β |
| β β Open-Meteo β β OpenRouter β β Kelly Calc β β |
| β β β β (Gemma) β β β β |
| β β polymarket β β β β paper_traderβ β |
| β β .py β β β β .py β β |
| β β Gamma API β β β β pm-trader β β |
| β β + Demo β β β β (paper) β β |
| β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β |
| β β |
| β ββββββββββββββββββββββββββββββββββββββββββββββββββββ β |
| β β ui/dashboard.py β Gradio 6 (4 tabs) β β |
| β β π Dashboard β πΌ Portfolio β π History β β |
| β ββββββββββββββββββββββββββββββββββββββββββββββββββββ β |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| ``` |
|
|
| --- |
|
|
| ## π§ How It Works |
|
|
| Each agent cycle executes 5 steps: |
|
|
| ### 1. Fetch Weather |
| Retrieves today's high/low temperatures and conditions for **New York, London, Miami, Tokyo, Los Angeles** from [Open-Meteo](https://open-meteo.com) β a free, no-API-key weather service. |
|
|
| ### 2. Fetch Markets |
| Searches [Polymarket's Gamma API](https://gamma-api.polymarket.com) for active weather prediction markets. **If the API is unreachable** (timeout, rate limit, or network issue), the system falls back to 5 hardcoded demo markets and shows a yellow warning banner in the UI. |
|
|
| ### 3. LLM Analysis |
| Sends the weather data and market prices to **Google Gemma 4 31B** (via [OpenRouter](https://openrouter.ai)) β a free, OpenAI-compatible API. The LLM is prompted to: |
| - Compare forecast-implied probabilities to market prices |
| - Identify trades with >3% edge |
| - Output a structured JSON decision |
|
|
| A robust JSON extractor (`_extract_json`) handles truncation, markdown fences, and malformed responses. |
|
|
| ### 4. Position Sizing |
| Uses the **Kelly Criterion** to calculate optimal bet size: |
|
|
| ``` |
| f* = (b Γ p - q) / b |
| |
| where: |
| p = model probability |
| b = net payout odds |
| q = 1 - p (probability of loss) |
| ``` |
|
|
| We apply **25% fractional Kelly** and cap at **10% of bankroll** per trade for risk management. |
|
|
| ### 5. Paper Trade |
| Executes the trade via `pm-trader` CLI (paper mode). Each trade gets an automatic **20% hedge** on the opposite side to limit downside. In demo mode, trades are logged and skipped. |
|
|
| --- |
|
|
| ## π οΈ Tech Stack |
|
|
| | Layer | Technology | |
| |-------|-----------| |
| | Language | Python 3.11+ | |
| | LLM | Google Gemma 4 31B via OpenRouter | |
| | Weather | Open-Meteo (free, no key) | |
| | Market Data | Polymarket Gamma API + hardcoded demo fallback | |
| | Paper Trading | pm-trader CLI | |
| | UI | Gradio 6 | |
| | Position Sizing | Kelly Criterion (25% fractional) | |
| | Logging | Loguru | |
|
|
| --- |
|
|
| ## π Getting Started |
|
|
| ### Prerequisites |
|
|
| - Python 3.11+ |
| - [OpenRouter API key](https://openrouter.ai/keys) (free credits on signup) |
|
|
| ### Installation |
|
|
| ```bash |
| git clone https://github.com/your-username/skyalpha.git |
| cd skyalpha |
| |
| python -m venv venv |
| source venv/bin/activate # Windows: venv\Scripts\activate |
| |
| pip install -r requirements.txt |
| |
| cp .env.example .env |
| # Edit .env with your OpenRouter key |
| ``` |
|
|
| ### Configuration |
|
|
| | Variable | Required | Default | Description | |
| |----------|----------|---------|-------------| |
| | `OPENROUTER_API_KEY` | β
| β | LLM API key | |
| | `MODEL` | β | `google/gemma-4-31b-it:free` | OpenRouter model ID | |
| | `STARTING_BALANCE` | β | `10000` | Paper trading starting balance | |
| | `APIFY_API_TOKEN` | β | β | Only needed if using Apify weather source | |
|
|
| --- |
|
|
| ## π― Usage |
|
|
| ### Run one cycle |
| ```bash |
| python main.py |
| ``` |
|
|
| ### Launch Gradio dashboard |
| ```bash |
| python main.py --ui |
| # β http://localhost:7860 |
| ``` |
|
|
| ### Run continuously (every 30 min) |
| ```bash |
| python main.py --loop |
| ``` |
|
|
| --- |
|
|
| ## π Deployment (Hugging Face Spaces) |
|
|
| The app is designed to run on [HF Spaces](https://huggingface.co/spaces) with zero configuration: |
|
|
| 1. Create a new Space at https://huggingface.co/new-space |
| 2. Select **Gradio SDK** |
| 3. Push the repo |
| 4. Set these **Secrets** in Space settings: |
|
|
| | Secret | Value | |
| |--------|-------| |
| | `OPENROUTER_API_KEY` | your key | |
| | `MODEL` | `google/gemma-4-31b-it:free` | |
| | `STARTING_BALANCE` | `10000` | |
|
|
| The app auto-detects demo mode when Polymarket's Gamma API is unreachable (which it is from HF's network) and shows live weather + simulated markets. |
|
|
| --- |
|
|
| ## π Project Structure |
|
|
| ``` |
| skyalpha/ |
| βββ agent/ |
| β βββ llm.py # OpenRouter client + JSON extraction |
| βββ data/ |
| β βββ weather.py # Open-Meteo & Apify weather fetchers |
| β βββ polymarket.py # Gamma API + demo market fallback |
| βββ trading/ |
| β βββ kelly.py # Kelly Criterion sizing + hedging |
| β βββ paper_trader.py # pm-trader CLI wrapper |
| βββ ui/ |
| β βββ dashboard.py # Gradio 6 dashboard (4 tabs) |
| βββ results/ # Per-cycle JSON logs |
| βββ app.py # HF Spaces entry point |
| βββ main.py # Orchestrator / CLI entry point |
| βββ requirements.txt |
| βββ .env.example |
| βββ README.md |
| ``` |
|
|
| --- |
|
|
| ## π§ͺ Demo Mode |
|
|
| When Polymarket's Gamma API is unreachable (network restrictions, rate limiting, or timeout), the system: |
|
|
| 1. **Falls back** to 5 hardcoded `DEMO_MARKETS` with realistic prices |
| 2. **Short-circuits** subsequent API calls via `_GAMMA_FAILED` flag (no repeated timeouts) |
| 3. **Skips real trade execution** β logs intended trades as `β οΈ DEMO β would TRADE market for $AMOUNT` |
| 4. **Shows a warning banner** in the dashboard: "β οΈ Demo Mode β Polymarket API unreachable" |
| 5. **Resets to real mode** if the API recovers on the next cycle |
|
|
| Demo markets cover all 5 target cities with plausible temperature thresholds and prices. |
|
|
| --- |
|
|
| ## π Error Handling |
|
|
| | Scenario | Handling | |
| |----------|----------| |
| | Gamma API timeout | Falls back to DEMO_MARKETS after 5s | |
| | LLM JSON parse failure | Multi-strategy `_extract_json()` with truncation repair | |
| | pm-trader unavailable | Graceful `FileNotFoundError` catch β demo defaults | |
| | Missing API key | Returns parse error with clear log message | |
| | Content filter (LLM) | Returns safe default with "Parse error" | |
| |
| --- |
| |
| ## π Roadmap |
| |
| - [ ] **Dynamic demo markets** β generate thresholds based on actual weather data |
| - [ ] **Multi-model ensemble** β average predictions from multiple LLMs |
| - [ ] **Backtesting engine** β replay historical data to validate strategy |
| - [ ] **Real Polymarket CLOB** β swap paper_trader for real on-chain execution |
| - [ ] **Telegram alerts** β notify on trade execution and P&L changes |
| - [ ] **Historical calibration** β EMOS/CRPS for probability calibration |
|
|
| --- |
|
|
| ## π License |
|
|
| MIT β see [LICENSE](LICENSE) for details. |
|
|