How to use Perl and Mysql to Track Financial Performance of Equities
2017-09-15
Introduction
Perl's utility can be increased through bolt on software modules, a powerful mechanism for doing this is via the Comprehensive Perl Archive Network (CPAN). This is a repository of over 250,000 software modules for Perl. CPAN modules are managed through an automated installer. CPAN was started in 1995 and has resulted in other languages adopting similar infrastructure.
To gather equity information from Yahoo we are going to use the Finance::Quote and Finance::Yahoo modules. This procedure will teach you how to set-up a portfolio tracking database. The procedure will conclude by showing you some SQL filters to do some data filtering. However, in future procedure I will be teaching how to do the basic math to calculate your own composite index.
Procedure
1. Make sure you have the CPAN modules you need.
shell> perl -MCPAN -e shell
**** Answer no to the configuration question ****
cpan>install Finance::Quote
cpan> install Finance::YahooQuote
2. Now design your portfolio database. I use a two table portfolio database, the first table tracks an equity universe. This table is a list of stocks you want to track, it has additional columns to allow you disactivate equities and add notes describing M&A activity. The second table is a data table that stores financial data at a specified time for each active stock in your list. I am not an active trader so I am not concerned with speed and primarily use this to store equity data on a weekly basis.
The command used to create the list table is below:
(
name VARCHAR(35) NOT NULL,
tckr VARCHAR(5) NOT NULL,
mrkt VARCHAR(6) NOT NULL,
sctr VARCHAR(6) NOT NULL,
mcap DECIMAL(5,1) NULL,
active ENUM('yes','no') NULL,
note VARCHAR(60) NULL
);
The command used to create the list table is below:
(
recid int(10) unsigned not null primary key,
tkr varchar(10) not null,
curdate date not null,
lst numeric(6,2) null,
eps numeric(5,2) null,
pe numeric(4,2) null,
ps numeric(4,2) null,
pb numeric(4,2) null,
ds numeric(4,2) null,
yield numeric(3,2) null,
vol bigint(13) null,
cap bigint(15) null
);
The quickest way to build the tables is to invoke a Mysql command from the shell. You also need to prepare the table create command in a text file.
Ex. Shell Command: mysql -u root -p"password" "database" < ListTableCommands.txt
Note for the above command to work you need to cd into the directory where the file resides. Now use a similar technique to build the second table.
Click here for ListTableCommands.txt Click here for PortfolioTableCommands.txt
Click here for the small list. Click here for the larger list. Now it is easy to dump the equity text file into the list table. Simply log into mysql from the terminal line and type the following command (ensure you are in directory where data resides):
4. Now you list table is populated with equities. I use mysql-workbench to see the list table has loaded data.
5. Now is the hard part you need to use the perl finance modules to extract the equity data from the web and load it into the portfolio. To do this you need to prepare a perl script. Create a text file to store your script and start coding. I won't be breaking the script down further as it is fairly long. Instead, I added a fair amount of comments to describe how the code works.
Click here to download the text file of perl script presented above.
6. Now set the script to executable and script in shell.
7. After typing in current data the database should populate with your desired list of equities. If this checks out in Mysql then congratulations you finished the project.
P.S. 1. Data analysis is not the intent of this procedure, but what good is this data if you don't know how to consume it. Here are some basic sql filters to get you started:
To review your active equities.
To review financial data for a specific date.
select * from portfolio where tkr like '%%' and curdate like '2017-09-18'
Compared to the first to sql statements the 3rd is a fairly complicated. It links the list and portfolio data based on ticker symbol and filters by many different variables. Thus, providing a powerful filter tool that you can further customize to whatever you fancy.
Conclusion
You now have a database with two tables. The list table controls the equities you are currently interested in, and the portfolio table controls the equity data. Use the perl script to import data on a daily or weekly basis and enjoy. Design credit knxops.com in cooperation with Ltimas and co.
create table portfolio
Copyright © 2019 by Gregory Altimas aka Greg Altimas