A collection of algorithms and tools for analyzing Elon Musk's tweeting patterns and related markets.
# Set up virtual environment
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
Prepares tweet data for analysis:
# Format raw tweet data
python3 src/formating_tweet_data/fixDates.py
# Aggregate tweets by day
python3 src/formating_tweet_data/formatByDay.py
Various algorithms for predicting Elon Musk's tweeting patterns:
See src/algos/README.md
for details on available prediction algorithms.
Tools for analyzing Polymarket data related to Elon Musk's tweeting:
# Get current timeframes and odds from Polymarket
python3 -m src.polymarket.main
# Generate visualization plots
python3 -m src.polymarket.main --generate-plot
# Fetch and display order book data
python3 -m src.polymarket.main --order-frames
--url URL URL of the Polymarket event
--save-data Save data to JSON and CSV files
--generate-plot Generate visualization plots for market probabilities
--order-frames Fetch and display order book frames
--detailed-orders Show detailed buy and sell orders (requires --order-frames)
--save-orders Save order book data to JSON (requires --order-frames)
--visualize-orders Generate visualizations of order books (requires --order-frames)
--compare Compare with previous data (requires --save-data)
--list-events List all previously stored events
Example with custom URL:
python3 -m src.polymarket.main --url "https://polymarket.com/event/your-event-slug" --generate-plot
Advanced utilities for working with Polymarket's Central Limit Order Book (CLOB) API:
# Scan for markets with rewards
python3 -m src.polymarket.market_scanner --rewarded --verbose
# Find the most liquid markets
python3 -m src.polymarket.market_scanner --liquid --top 20
# Analyze a specific market by condition ID
python3 -m src.polymarket.market_scanner --market "your-condition-id" --save
--rewarded Scan for markets with rewards
--liquid Find most liquid markets
--market ID Analyze a specific market by condition ID
--top N Number of top markets to return (default: 10)
--verbose Print detailed information
--save Save analysis results to a file
For detailed information on using the CLOB API, see src/polymarket/CLOB_API_GUIDE.md
.
The OrderBookAnalyzer
class provides comprehensive analysis of Polymarket order books:
# Run the analyzer on a specific event
python3 -m src.polymarket.order_book_analysis --slug "your-event-slug" --visualize
Features include:
- Liquidity metrics calculation
- Price manipulation detection
- Order book depth visualization
- Historical comparison
- Market anomaly detection
Results from the Polymarket analysis are saved in:
- Market data:
src/polymarket/data/{json|csv}
- Order book data:
src/polymarket/data/order_books
- Visualizations:
src/polymarket/plots
- Market analysis:
src/polymarket/data/analysis
The project includes comprehensive utilities for working with Polymarket's APIs:
- Main API Client:
PolymarketAPIClient
class for fetching market data - CLOB Utilities:
ClobUtils
class for working with the CLOB API - Order Book Analysis:
OrderBookAnalyzer
class for analyzing order books - Market Scanner: Command-line utility for scanning markets
For programmatic access to the CLOB API:
from src.polymarket.clob_utils import ClobUtils
# Initialize the client
clob = ClobUtils()
# Get all markets with rewards
rewarded_markets = clob.fetch_all_rewarded_markets()
# Get order book for a specific token
token_id = "your-token-id"
order_book = clob.get_order_book(token_id)
formatted = clob.format_order_book(order_book)
# Print the bid-ask spread
best_bid = max([o.get("price", 0) for o in formatted.get("buy_orders", [])]) if formatted.get("buy_orders") else 0
best_ask = min([o.get("price", 0) for o in formatted.get("sell_orders", [])]) if formatted.get("sell_orders") else 100
spread = best_ask - best_bid if best_bid > 0 and best_ask < 100 else 0
print(f"Spread: {spread:.2f}%")
This repository contains algorithms and tools for analyzing and predicting Polymarket events, with a focus on Elon Musk's tweet patterns.
- Historical tweet data analysis tools
- Prediction algorithms for future tweet counts
- Polymarket event data fetching and visualization
- Order book analysis
For details on using the Polymarket module, see src/polymarket/Readme.md
The repository includes a tool that combines the Polymarket count frames with tweet prediction models to generate probabilities for each frame. This is particularly useful for predicting which bracket of tweet counts is most likely for upcoming Polymarket events.
To use the tweet count frame probability predictor:
# Activate virtual environment
source venv/bin/activate
# Run with default settings (uses April 11-18, 2025 timeframe)
python -m src.polymarket_tweet_predictor
# Run with custom date range
python -m src.polymarket_tweet_predictor --start "2025-04-11 12:00:00" --end "2025-04-18 12:00:00"
# Use custom data file and disable trend adjustment
python -m src.polymarket_tweet_predictor --file /path/to/tweets.csv --no-trend
# Increase simulation accuracy
python -m src.polymarket_tweet_predictor --sims 5000
The output includes:
- The most likely tweet count frames with probabilities
- Detailed breakdowns across all possible frames
- Insight into the distribution of possible outcomes
For details on using the Elon Tweet Predictor, see src/algos/elon_tweet_predictor/README.md
The Elon Musk Tweet Fetcher allows you to fetch tweets from Elon Musk's Twitter account using the Apify Twitter scraper API.
To fetch the latest tweets from Elon Musk and add them to the database:
# Activate virtual environment
source venv/bin/activate
# Fetch 40 tweets using the client method and add to database
python -m src.apify.get_elon_tweets --max-tweets 40 --use-client --add-to-db
- Fetching up to 40 tweets costs approximately 3.2 cents per request using the Apify API
- The
--use-client
flag ensures better reliability and faster processing - For more details on the Tweet Fetcher, see src/apify/README.md
# Get tweets since the latest one in the database
python -m src.apify.get_elon_tweets --max-tweets 40 --use-client --latest --add-to-db
# Debug mode (prints more details and doesn't save to database)
python -m src.apify.get_elon_tweets --max-tweets 40 --use-client --debug
# Clone the repository
git clone <repository-url>
cd polymarket_algo
# Create and activate virtual environment
python -m venv venv
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the terms of the MIT license.
This project includes an automated scheduler that can periodically fetch tweets and run predictions at configurable intervals.
# Activate virtual environment
source venv/bin/activate
# Run with default settings (every 20 minutes)
python -m src.scheduler
# Run with custom interval (every 5 minutes)
python -m src.scheduler --interval 5
# Only fetch tweets, don't run predictions
python -m src.scheduler --tweets-only
# Only run predictions, don't fetch tweets
python -m src.scheduler --predictions-only
# Run jobs once and exit (don't keep running)
python -m src.scheduler --run-once
Use the provided shell scripts to run the scheduler as a background service:
# Start the scheduler with default settings
./src/scheduler/run_scheduler.sh
# Start with custom settings
./src/scheduler/run_scheduler.sh --interval 15 --max-tweets 50 --quiet
# Stop the scheduler
./src/scheduler/stop_scheduler.sh
For more details, see the Scheduler README.