Using a Stock Market API

In this post, I’m going to explore the use of integrating with the stock market via PHP and Python code. Yahoo Finance API had a great API but that has been shutdown/disappeared. I have used Markit on Demand before and it worked very well but it’s now returning a “Not Found” on their API page. So I have been looking for a replacement. I did what anyone does when looking for something on the internet, typing into Google “Free Stock Market API”. I got some potential results: 96 Stocks APIs: Bloomberg, NASDAQ and E*TRADE, Alternative to google finance api, etc.

(This failed, jump down below if you want to see the result). I tried to use Quandl. The documentation looked dated but I could sign up, get an API key and they did not ask for a credit card.

A simple request:

https://www.quandl.com/api/v1/datasets/WIKI/AAPL.json?column=4&sort_order=asc&collapse=quarterly&trim_start=2012-01-01&trim_end=2013-12-31&api_key=YOUR_API_KEY

Which can work. But running it with a 2017 date, returns nothing.

Then I checked clicked “Core Financial Data” in their dashboard and found a way to filter by “Free”. The “Wiki EOD Stock Prices” did not work. It just returned dead data. None of the databases had anything.

On to another endpoint:

  • Bloomberg = Enterprise (Not free)
  • Lots of the ones on the 96 list are dead.

Found Something That Worked

After enough Googling, I found this website: https://www.alphavantage.co/. I was able to sign up without a credit card and ran the following request ( demo works but sub your key in.) Run:

https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo

This renders the open, close, high, low, and volume is returned for each date. This is exactly what I’m looking for. Yes! Now it’s time to just make these GET requests with a language of your choosing.

Example data returned:

{
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "MSFT",
"3. Last Refreshed": "2017-10-30 11:34:00",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2017-10-30": {
"1. open": "83.7000",
"2. high": "84.3250",
"3. low": "83.1050",
"4. close": "84.0199",
"5. volume": "10234320"
},
...

Python

For Python, the GET requests will be done with the Requests library. To install Requests, run:

pip install requests

Create Python file with a GET request:

API_KEY = 'YOUR_API_KEY'
r = requests.get('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=' + API_KEY)
if (r.status_code == 200):
print r.json()

r.json() is the same response as you got in your browser. You will have to reference today’s date with:

result = r.json()
dataForAllDays = result['Time Series (Daily)']
dataForSingleDate = dataForAllDays['2017-10-30']
print dataForSingleDate['1. open']
print dataForSingleDate['2. high']
print dataForSingleDate['3. low']
print dataForSingleDate['4. close']
print dataForSingleDate['5. volume']

PHP

For PHP, I’m going to use curl to make a GET request. The curl request looks like this in a PHP file (use a tool like MAMP or server to run the PHP):

$API_KEY = "YOUR_API_KEY";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=" . $API_KEY));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
$result = json_decode($server_output);

And parse the result object to get the current today data.

$dataForAllDays = $result->{'Time Series (Daily)'};
$dataForSingleDate = $dataForAllDays->{'2017-10-30'};
echo $dataForSingleDate->{'1. open'} . '<br/>';
echo $dataForSingleDate->{'2. high'} . '<br/>';
echo $dataForSingleDate->{'3. low'} . '<br/>';
echo $dataForSingleDate->{'4. close'} . '<br/>';
echo $dataForSingleDate->{'5. volume'} . '<br/>';

Conclusion

And that’s the basics of using a Stock Market API. It’s just a matter of finding something that is simple, cheap and some what reliable. I plan to add Node.js to this post in addition. If you have any questions, please ask them below.