This is a live trading analytics application designed for quantitative traders and researchers. It streams real-time cryptocurrency price data from Binance, stores it locally, calculates statistical trading signals (like spreads and z-scores), and displays everything through an easy-to-use web dashboard.
Think of it as: A mini Bloomberg terminal for crypto pairs that helps you spot trading opportunities in real-time.
- Real-time Data Streaming: Connects to Binance's live market data feed and receives price updates every millisecond
- Data Storage: Saves all price ticks to a local database so you can analyze historical patterns
- Pair Trading Analytics: Compares two cryptocurrencies to find trading opportunities when their prices diverge
- Visual Dashboard: Shows interactive charts with prices, spreads, and statistical indicators
- Smart Alerts: Notifies you when trading signals meet your criteria
- Data Export: Download your analysis results as CSV files
โโโโโโโโโโโโโโโโโโโ
โ Binance API โ โ Live market data (BTC, ETH, etc.)
โโโโโโโโโโฌโโโโโโโโโ
โ
โ
โโโโโโโโโโโโโโโโโโโ
โ WebSocket โ โ backend.py connects and listens
โ Ingestion โ
โโโโโโโโโโฌโโโโโโโโโ
โ
โ
โโโโโโโโโโโโโโโโโโโ
โ SQLite Database โ โ data_store.py saves every tick
โ (ticks.db) โ
โโโโโโโโโโฌโโโโโโโโโ
โ
โ
โโโโโโโโโโโโโโโโโโโ
โ Analytics โ โ analytics.py calculates signals
โ Engine โ (hedge ratio, z-score, ADF test)
โโโโโโโโโโฌโโโโโโโโโ
โ
โ
โโโโโโโโโโโโโโโโโโโ
โ Streamlit โ โ app.py displays interactive charts
โ Dashboard โ
โโโโโโโโโโโโโโโโโโโ
โ
Your Browser (localhost:8501)
Step-by-step flow:
- Ingestion: The app connects to Binance and receives real-time trades (price + quantity)
- Storage: Each trade is stored in
ticks.dbwith timestamp, symbol, price, and quantity - Resampling: Tick data is aggregated into candlesticks (1 second, 1 minute, or 5 minutes)
- Analytics: For any two symbols (e.g., BTC vs ETH), the app calculates:
- Hedge Ratio: How much of asset X you need to hedge asset Y
- Spread: The price difference between the pair
- Z-Score: How many standard deviations the spread is from its mean (indicates overbought/oversold)
- ADF Test: Statistical test to check if the spread is mean-reverting
- Visualization: All results are plotted in real-time on interactive charts
- Alerts: When z-score crosses your threshold, you get notified
- Python 3.8+ installed on your computer (Download here)
- Internet connection (to stream live data from Binance)
- Command Prompt or Terminal access
Save all project files to a folder, for example: C:\Users\YourName\Desktop\qea
- Press
Windows Key + R - Type
cmdand press Enter - Navigate to your project folder:
cd C:\Users\YourName\Desktop\qeaThis keeps your project dependencies isolated:
python -m venv .venv
.venv\Scripts\activateYou'll see (.venv) appear in your command prompt.
pip install -r requirements.txtThis installs all the libraries the project needs (takes 1-2 minutes).
python -m streamlit run app.py- The app will automatically open at
http://localhost:8501 - If it doesn't, manually open your browser and go to that address
- In the dashboard sidebar, click "Start Ingest"
- Wait 10-20 seconds for data to accumulate
- Charts will start updating automatically!
- Streamlit (
streamlit>=1.18)- What it does: Creates the web dashboard without needing HTML/CSS/JavaScript
- Why we use it: Fast prototyping, built-in widgets, auto-refresh capabilities
-
Pandas (
pandas>=1.5)- What it does: Manipulates and analyzes tabular data (like Excel on steroids)
- Why we use it: Resampling ticks to candlesticks, time series operations
-
NumPy (
numpy>=1.23)- What it does: Fast numerical computations and linear algebra
- Why we use it: Calculating hedge ratios via linear regression
-
SQLAlchemy (
sqlalchemy>=1.4)- What it does: Database toolkit (though we use raw SQLite3 for simplicity)
- Why we use it: Provides advanced DB features if needed later
- WebSockets (
websockets>=11.0)- What it does: Maintains persistent connection to Binance for live data
- Why we use it: Much faster than polling; receives updates instantly
- Plotly (
plotly>=5.6)- What it does: Creates interactive charts (zoom, pan, hover tooltips)
- Why we use it: Professional-looking charts with zero effort
- Statsmodels (
statsmodels>=0.13)- What it does: Advanced statistical tests and models
- Why we use it: ADF test for stationarity, time series analysis
qea/
โ
โโโ app.py # Main Streamlit dashboard (START HERE)
โโโ backend.py # WebSocket ingestion from Binance
โโโ data_store.py # SQLite database manager
โโโ analytics.py # Statistical calculations
โโโ requirements.txt # List of Python packages needed
โโโ README.md # This file!
โโโ diagram.drawio # Architecture diagram (editable)
โโโ diagram.svg # Architecture diagram (image)
โ
โโโ ticks.db # SQLite database (created automatically)
โโโ check_db.py # Utility to inspect database contents
โโโ run_ingest.py # Standalone ingestion test script
Choose which cryptocurrencies to track (default: BTCUSDT, ETHUSDT, BNBUSDT)
- 1S: 1-second candlesticks (high-frequency view)
- 1T: 1-minute candlesticks (medium-frequency)
- 5T: 5-minute candlesticks (lower-frequency)
Select exactly 2 symbols to analyze their relationship:
- Hedge Ratio (ฮฒ): From regression Y = ฮฑ + ฮฒX
- Spread: Y - (ฮฑ + ฮฒX) โ measures divergence
- Z-Score: (Spread - Mean) / StdDev โ normalized signal
- ADF Test: Tests if spread is mean-reverting (p-value < 0.05 = stationary)
Upload your own historical OHLC data to test analytics offline
Set a z-score threshold (e.g., 2.0) to get notified when extreme divergences occur
Download all calculated data (prices, spreads, z-scores) as CSV
- โ Zero configuration, single file database
- โ Perfect for local prototypes
- โ For production: Use TimescaleDB (time-series optimized) or InfluxDB
- โ Simple to run (one command)
- โ Easy to debug
- โ For scale: Separate ingestion (Kafka/Redis) from analytics (microservices)
- โ Fast, simple, interpretable
- โ Good baseline for hedge ratio
- โ For production: Add Kalman Filter (adapts to changing relationships) or Robust Regression (handles outliers)
python run_ingest.pyShould print "Starting ingest for 20s..." and collect data.
python check_db.pyShould show tick counts for each symbol (e.g., BTCUSDT: 1123).
python -m streamlit run app.pyOpen browser to http://localhost:8501 and click "Start Ingest".
A single trade event: timestamp, symbol, price, quantity
Open, High, Low, Close prices for a time period (like a candlestick)
The price difference between two assets after adjusting for their correlation
Measures how far the current value is from average (in standard deviations)
- Z > 2: Abnormally high (potential sell signal)
- Z < -2: Abnormally low (potential buy signal)
- Z โ 0: Near average (no signal)
A trading strategy that bets prices will return to their average after extreme moves
- app.py: Run this to start the dashboard
- requirements.txt: Install dependencies from here
- backend.py: Modify to add new data sources (e.g., other exchanges)
- analytics.py: Add new calculations (e.g., Kalman filter, Bollinger bands)
- data_store.py: Change database schema or switch to PostgreSQL
pip install -r requirements.txtMake sure you're in the project folder and virtual environment is activated.
Try:
python -m streamlit run app.pyInstead of just streamlit run app.py.
- Click "Start Ingest" button in the sidebar
- Wait 10-20 seconds for data to accumulate
- Ensure you have internet connection to reach Binance
Close any other Python processes accessing ticks.db:
taskkill /F /IM python.exeOnce you're comfortable with the basics, consider adding:
- Kalman Filter: Dynamic hedge estimation that adapts over time
- Robust Regression: Huber or Theil-Sen for outlier-resistant hedge ratios
- Backtesting Module: Test mean-reversion strategies (z>2 entry, z<0 exit)
- Multi-Exchange Support: Add Coinbase, Kraken data sources
- Message Broker: Use Redis or Kafka to decouple ingestion from analytics
- Advanced Alerts: Email/Telegram notifications, multiple alert rules
- Risk Metrics: VaR, Sharpe ratio, drawdown analysis
This project scaffold and code were generated with assistance from an AI coding assistant (GitHub Copilot using Claude Sonnet 4.5) to speed implementation and produce clear structure.
Prompts used:
- "Create a Streamlit dashboard for real-time crypto pair analytics"
- "Implement Binance WebSocket ingestion with SQLite storage"
- "Add OLS hedge ratio, spread calculation, z-score, and ADF test"
- "Create interactive Plotly charts with controls for timeframe and symbols"
- "Add CSV export and alert functionality"
The AI handled boilerplate code, structure, and integration. Human review ensured correctness, usability, and alignment with quantitative finance best practices.
This prototype is provided as-is for evaluation purposes. Feel free to modify and extend for educational or commercial use.
If you encounter issues:
- Check the Troubleshooting section above
- Review the project structure and ensure all files are present
- Verify Python version (3.8+) and internet connectivity
- Check
ticks.dbexists and has data usingpython check_db.py
Built with โค๏ธ for quantitative traders and researchers