Make music from the motion of stock prices.
This program gathers stock prices into a database. It generates Midi data – mapping price to pitch, and mapping trading volume to velocity and rhythmic density. It uses ancient Web technology: HTML/javascript front-end with a php back-end accessing a mysql database.
Case study: http://zproject.wikispaces.com/stock+market+music
To run this project, you will need a server (preferably linux) with the following capabilities:
- mysql + phpmyadmin
- php (and ability to run php over the web)
- netcat (nc)
- network access
All of this is pretty standard – so I won’t talk about it here. I am running it on Ubuntu Linux. There are many other ways to get the project working, by using the layout described here.
download
https://github.com/tkzic/internet-sensors
folder: stock-market
files
Max
stock_market_music.maxpat
HTML/javascript web client
- newstock3.html: (web page interface)
- selectstock3.js: (front-end)
php server
- getstock3.php (back-end server to get quotes and save them to a database)
- play3.php: (back-end server to retrieve quotes, analyze, and map to Midi sequence to send to Max)
- udp.php: Osc library
database
The selectstock3.php program harvests stock quote data and stores it in a mysql database.
The database name is: stocks – table is: quotes
Table structure:
The table is basic flat representation of a stock quote, indexed by the ticker symbol. It contains price, volume, high/low/change, timestamp, etc., For our purposes, the price, volume and timestamp are essentially all we need.
SQL to create the table:
CREATE TABLE `stocks`.`quotes` ( `ticker` varchar( 12 ) NOT NULL , `price` decimal( 10, 2 ) NOT NULL , `qtime` datetime NOT NULL , `pchange` decimal( 10, 2 ) NOT NULL , `popen` decimal( 10, 2 ) NOT NULL , `phigh` decimal( 10, 2 ) NOT NULL , `plow` decimal( 10, 2 ) NOT NULL , `volume` int( 11 ) NOT NULL , `ttime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP , `id` int( 11 ) NOT NULL AUTO_INCREMENT , `spare` varchar( 30 ) DEFAULT NULL , UNIQUE KEY `id` ( `id` ) , KEY `ticker` ( `ticker` ) ) ENGINE = MyISAM DEFAULT CHARSET = latin1 COMMENT = 'stock quote transactions';
creating the database, user, and table
- Log into phpmyadmin as root
- Create a new database called ‘stocks’
- In privileges, add a user called: ‘webdb1′ with a password of ’34door’ (note you can change the password later)
- In SQL, copy in the above query to create the ‘stocks’ table
Instructions
Web client
The webpage control program allows you to select stocks by ticker symbol, and get either one quote or get quotes at regular time interval. Each quote is inserted into the stock table for later retrieval and analysis.
The web front end is quirky so I will describe it in terms of how you might typically use it:
market is open – and you just want to play music based on current stock prices
- Enter the ticker symbols for your stocks
- Press ‘tracking’ button – so the quotes get saved
- Enter the IP address of the computer running Max
- Press the ‘auto’ button in the upper left corner – it will run and play forever
market is closed – or you want to play historical data you have saved
- Enter the ticker symbols for your stocks
- Enter the IP address of the computer running Max
- set start end end dates
- Press the ‘play’ button to play once or press ‘loop’ to play continuously (using time interval in seconds)
- market is open – you just want to collect stock quote data
- Enter the ticker symbols for your stocks
- Press the tracking button so quotes will get saved
- Press the ‘get quotes’ button to get current quote or press ‘loop’ button (on the same line) to retrieve quotes continuously every 30 seconds.
Instructions
Max patch
- Make sure the IP address is set to the address of your server
- Select the Midi port for output
- Play a few test notes
- Select either ‘one instrument’ mode (piano) or multi instrument mode. Each time you click the multi instrument button it randomly selects a new combination
notes on stock market data
To look at historical trends, you would need access to historical stock data. To use it as a tool for short term analysis, you would need access to real-time quote data in an API. At the time, both of these cost money.
However, it doesn’t cost money to get recent quotes from Yahoo throughout the day and store them in a database – so that’s the approach I took.
If I were to do this project today, I’d look for a free online source of historical data, in machine-readable form – because the historical data provides the most interesting and organic sounds when converted into music. The instant high speeding trading data would probably make interesting sounds as well, but you still need to pay for the data.
notes on local files
- Original files in tkzic/w3 and public_html/w3 on server
- New versions in public_html/stockmarket2014
- For example http://xx.xx.xxx.xx/~tkzic/stockmarket2014/newstock3.html