|
| 1 | +# Polygon.io Flat Files Stock Trades Analysis Scripts |
| 2 | + |
| 3 | +This repository contains Python scripts for analyzing stock market trading data using Flat Files from Polygon.io. These scripts demonstrate various ways to dissect and visualize trade data for comprehensive market analysis. |
| 4 | + |
| 5 | +Please see the tutorial: [Deep Dive into Trade-Level Data with Flat Files](https://polygon.io/blog/insights-from-trade-level-data) |
| 6 | + |
| 7 | +## Scripts Overview |
| 8 | + |
| 9 | +### **exchange-heatmap.py** |
| 10 | +This script aggregates trades by exchange into 30-minute chunks and creates a heatmap visualization. It highlights the flow of trades and peak activity times across different exchanges, providing insights into how different exchanges operate throughout the day. |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +### **exchanges-seen.py** |
| 15 | +Analyzes the distribution of trades across different exchanges and calculates their respective percentages of total trades. This script helps identify which exchanges handle the most trading volume, offering a perspective on market structure. |
| 16 | + |
| 17 | +``` |
| 18 | +Exchange 4: 25,570,324 trades, 36.32% of total trades |
| 19 | +Exchange 12: 15,147,689 trades, 21.52% of total trades |
| 20 | +Exchange 11: 6,877,306 trades, 9.77% of total trades |
| 21 | +Exchange 19: 5,098,852 trades, 7.24% of total trades |
| 22 | +Exchange 10: 4,006,611 trades, 5.69% of total trades |
| 23 | +Exchange 8: 3,686,168 trades, 5.24% of total trades |
| 24 | +Exchange 15: 2,446,340 trades, 3.47% of total trades |
| 25 | +Exchange 21: 2,173,744 trades, 3.09% of total trades |
| 26 | +Exchange 7: 1,509,083 trades, 2.14% of total trades |
| 27 | +Exchange 20: 1,296,811 trades, 1.84% of total trades |
| 28 | +Exchange 18: 674,553 trades, 0.96% of total trades |
| 29 | +Exchange 13: 527,767 trades, 0.75% of total trades |
| 30 | +Exchange 2: 417,295 trades, 0.59% of total trades |
| 31 | +Exchange 3: 393,919 trades, 0.56% of total trades |
| 32 | +Exchange 17: 230,210 trades, 0.33% of total trades |
| 33 | +Exchange 1: 183,010 trades, 0.26% of total trades |
| 34 | +Exchange 9: 159,020 trades, 0.23% of total trades |
| 35 | +Exchange 14: 1,211 trades, 0.00% of total trades |
| 36 | +``` |
| 37 | + |
| 38 | +### **top-10-tickers.py** |
| 39 | +Identifies the top 10 most traded stocks and calculates their respective percentages of the total trades. This script provides a clear view of the market's most active stocks, highlighting where the most trading activity is concentrated. |
| 40 | + |
| 41 | +``` |
| 42 | +TSLA: 1,549,605 trades, 2.20% of total trades |
| 43 | +NVDA: 788,331 trades, 1.12% of total trades |
| 44 | +SPY: 669,762 trades, 0.95% of total trades |
| 45 | +AMD: 587,140 trades, 0.83% of total trades |
| 46 | +MDIA: 561,698 trades, 0.80% of total trades |
| 47 | +AAPL: 540,870 trades, 0.77% of total trades |
| 48 | +SOXL: 533,511 trades, 0.76% of total trades |
| 49 | +QQQ: 508,822 trades, 0.72% of total trades |
| 50 | +CADL: 466,604 trades, 0.66% of total trades |
| 51 | +AMZN: 465,526 trades, 0.66% of total trades |
| 52 | +``` |
| 53 | + |
| 54 | +### **trades-histogram.py** |
| 55 | +Creates a histogram that aggregates trades into 30-minute intervals throughout the day. This visualization helps understand the distribution of trading volume across different times, including pre-market, regular trading hours, and after-hours. |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +## Download the Data |
| 60 | + |
| 61 | +First, let's download an actual file and explore the data and see what we can learn. We start by downloading the trades for 2024-04-05 via the [File Browser](https://polygon.io/flat-files/stocks-trades/2024/04). The `us_stocks_sip/trades_v1/2024/04/2024-04-05.csv.gz` file is about 1.35GB and is in a compressed gzip format. |
| 62 | + |
| 63 | +``` |
| 64 | +gunzip 2024-04-05.csv.gz |
| 65 | +``` |
| 66 | + |
| 67 | +## Getting Started |
| 68 | + |
| 69 | +To run these scripts, you will need Python 3 and several dependencies installed, including pandas, matplotlib, seaborn, and pytz. Ensure that you have the trading data file available and modify the `file_path` variable in each script to point to your data file location. |
| 70 | + |
| 71 | +``` |
| 72 | +pip install pandas matplotlib seaborn pytz |
| 73 | +``` |
| 74 | + |
| 75 | +## Usage |
| 76 | + |
| 77 | +Each script is designed to be run independently: |
| 78 | + |
| 79 | +```bash |
| 80 | +python exchange-heatmap.py |
| 81 | +python exchanges-seen.py |
| 82 | +python top-10-tickers.py |
| 83 | +python trades-histogram.py |
| 84 | +``` |
| 85 | + |
| 86 | +Adjust the script parameters as necessary to fit your specific analysis needs or to accommodate different datasets. |
0 commit comments