This program is used to build and update a database of stock tickers and their historical price data. It fetches stock ticker information from the Polygon API and retrieves historical price data for each ticker using the same API. The program is written in Python and utilizes SQLite for database management.
Before running the program, make sure you have the following:
- Python 3 installed on your system
- Required Python packages:
dotenv
,sqlite3
,requests
,json
- A valid API key for the Polygon API. You can obtain an API key by signing up on the Polygon website.
-
Clone the repository or download the
Database_Builder.py
andDatabase_Management.py
files. -
Install the required packages by running the following command in your terminal:
pip install python-dotenv
-
Create a file named
.env
in the same directory as the program files. Open the.env
file and add the following line:PolygonKey=YOUR_POLYGON_API_KEY
Replace
YOUR_POLYGON_API_KEY
with your actual Polygon API key.
To build and update the database, follow these steps:
-
Run the
Database_Builder.py
script using Python:python Database_Builder.py
-
The program will check if the database file (
data.db
) exists and is greater than 20,000 bytes. If the file doesn't exist or is empty, it will update the known tickers in the database. -
The program fetches ticker information from the Polygon API and updates the
tickers
table in the database. -
After updating the tickers, the program creates or updates the
app_state
table in the database with the last processed ticker and sets the error status to 0. -
The program proceeds to process each ticker in the
tickers
table. It fetches historical price data for each ticker using the Polygon API and stores it in a separate table in the database. -
During the processing of tickers, the program displays the progress and handles any errors that may occur. It skips tickers with invalid formats or reserved keywords.
-
Once the program finishes processing all tickers, the database will be populated with the latest ticker information and historical price data.
-
To modify the duration of historical price data fetched, you can change the
From
andTo
variables in themake_db
function ofDatabase_Management.py
. By default, it fetches data for the last 5000 days. -
You can adjust the
Multiplier
,Timespan
,Adjusted
,Sort
, andLimit
parameters in thefetch_stock_data
function ofDatabase_Management.py
to customize the API request for historical price data. Refer to the Polygon API documentation for more details. -
The program uses the
data.db
file as the default database. If you want to use a different filename or location, you can modify thedb_filename
anddb_path
variables in bothDatabase_Builder.py
andDatabase_Management.py
to match your desired configuration.
-
The program assumes that you have set up the required tables (
tickers
andapp_state
) in the database. If the tables do not exist, the program will create them automatically. -
Make sure to handle the database file (
data.db
) with care. It contains sensitive financial data and should not be shared publicly or modified manually without proper knowledge.
This program is licensed under the MIT License. You are free to use, modify, and distribute this program in accordance with the terms of the license.
Proper attribution to the original author is required when using or distributing this program. Please provide credit to the original author as specified in the LICENSE file.
By using this program, you acknowledge and agree to comply with the attribution requirements outlined in the MIT License.