Capturing Stock Market Index Prices into a Local Database
Get link
Facebook
X
Pinterest
Email
Other Apps
-
Goal
I’m in what will be a very long process of building my own personal database of
stock prices and related data. One of the first points of data I aim to collect
is daily price data of some of the major indices, i.e., Dow Jones, Nasdaq etc.
This post will go over the first steps on how i'm capturing that data. The API service that is being used, the SQL Server table structure and the loading of data from the API call into the database using python.
The data provider that I’ve chosen for capturing the market
data is from Rapid API. Unfortunately, a lot of stock price providers don't contain index data.
The call i'm going to be using is the PriceCustomDate method call. It takes a symbol, start date, and end date for a date range to select from. What I want to do is create a table in the SQL Database that will store the data from the API Call.
[] 82 items
"Success"
200
[] 82 items
▶
{} 7 keys
180.9597320557
182.0099945068
1641168000000
182.8800048828
177.7100067139
177.8300018311
104487900
Database Table Structure
For the purposes of simply loading the data the table consists
of all strings for column types for the data points being uploaded. The other
columns added are meta data used for traceability.
CREATE TABLE [CODE_STORM].[Daily_Price_Load]
(
DailyPriceId BIGINT IDENTITY(1,1) NOT NULL PRIMARY KEY,
[index] NVARCHAR(250),
Ticker NVARCHAR(250),
Adjusted_Close NVARCHAR(250),
Close_ NVARCHAR(250),
Date_ NVARCHAR(250),
High_ NVARCHAR(250),
Low_ NVARCHAR(250),
Open_ NVARCHAR(250),
Volume_ NVARCHAR(250),
Price_Date NVARCHAR(250),
EntityId BIGINT NOT NULL,
Load_Date DATE NOT NULL,
Load_Time DATETIME2 NOT NULL,
Source_ NVARCHAR(500) NOT NULL,
Source_Dataset NVARCHAR(255) NOT NULL,
Source_API_Call NVARCHAR(2000) NOT NULL
)
Some things to take a note of.
The schema CODE_STORM is created because that is the name of service the data is coming from putting grouping all tables and procedures used in association with that service under the same schema allows for easy convention structure to quickly know about those database objects.
All the incoming data from the API Call is stored as strings so that it reduces the chances of running into an error when loading in the data into the database.
This is only the first step of loading in the data. It will be converted into the actual data types like Dates and Decimals in another step.
The other columns that are for keeping meta data about the data being loaded.
Loading Data With Python:
Using Python is a quick and easy way to set up a script to pull data from the API Call and load into a SQL Database.
First thing I created a Python script to handle the connection to the database. This is in its own file.
importsqlalchemyassa
importurllibasur
defget_sql_engine(db):
conn_str = ""
ifdb == "StockTradingConfig":
conn_str = "DRIVER={ODBC Driver 17 for SQL Server};"
frame.to_sql(name = 'Daily_Price_Load', con = engine,
schema = 'CODE_STORM', if_exists = 'append')
returnframe, response
There is one method 'get_market_index_frame' that is not covered as part of this post. Before I created the database for the stock price data, I created a database for ticker and other configuration information. In a future series I will go over that. For the moment in the 'load_daily_market_index_data' method, instead of iterating over the index frame it can be changed to an array of tickers and the entity_id can be dropped for the moment. The entity_id is part of the configuration database and will be gone over later.
Testing Logic with a Jupyter Lab Notebook
Jupyter lab is a fantastic tool for data science. It's also great just for testing Python scripts and classes to get them working.
First a section is created for loading in the libraries.
importimportlibasimp
importpandasaspd
importCS_Daily_Price_Loadasdl
importdatetime
fromdatetimeimportdatetimeasdate
imp.reload(dl)
Next create the variables needed to pass into the method.
Next, a .bat file needs to be created to do the execution of the python script above. Notepad can be used just and saved as a .bat file. It needs the full file path of where the Python.exe is and the full path of the python file that is to be run.
Next in windows, the Task Scheduler is used for the automation.
I've set the schedule to daily.
Then the time it runs.
Next the action, "Start a program" is selected.
Finally, the Program/Script just need to be the full file path of the .bat file.
Summary
This is one of the first steps into loading stock market
index data into a local database, that can be used for personal research, my goal eventually is to tie many different sources of data together. This is purely uploading the raw data; in the next post I will go over the transformation into the actual data types for example the strings of prices will be converted into decimals. There are a few reasons for splitting up the steps, either for making it easier to single out where an error is occurring or creating smaller manageable pieces of logic instead of trying to do everything in big logic files.
Goal My aim is to load daily stock prices into a local database. First, I plan to automatically set up the load into the SQL database. This part will be the step that loads the data as strings into the database. This post will go over the steps on how I'm capturing that data. The API service that is being used, the SQL Server table structure and the loading of data from the API call into the database using python.
Goal In the first part located here stock index data was uploaded into a SQL Server database. All the incoming data that was captured from the API call was stored as strings. In this update that data will be transformed into its proper data types.
Comments
Post a Comment