Question

I have a project that I need help with. I want to be able to go to a website of my making, e.g. abc123.com, and have it display the Dow Jones Index. That's it. Just display one number like 12050 on my abc123.com website.

Obviously I have to get the data from somewhere like Yahoo Finance. I don't want to download any .csv files or anything like that. And it doesn't need to be real time data since I know Yahoo's data is 15 minutes delayed. Just want to be able to visit my own site and see what the Dow Jones Index is.

Before asking this question I did research some similar questions on SO, but a lot of them were unanswered or references YQL (something I'm looking into and trying to comprehend at this moment).

Any guidance on the simplest way to do this would be greatly appreciated! I am a beginning programmer in HTML and am looking for the easiest-to-understand-and-implement route.

Was it helpful?

Solution

You can't. Not programmatically, but legally.

Quotes delayed, except where indicated otherwise. Delay times are 15 mins for NASDAQ, NYSE and Amex. See also delay times for other exchanges. Quotes and other information supplied by independent providers identified on the Yahoo! Finance partner page. Quotes are updated automatically, but will be turned off after 25 minutes of inactivity. Quotes are delayed at least 15 minutes. All information provided "as is" for informational purposes only, not intended for trading purposes or advice. Neither Yahoo! nor any of independent providers is liable for any informational errors, incompleteness, or delays, or for any actions taken in reliance on information contained herein. By accessing the Yahoo! site, you agree not to redistribute the information found therein.

All of the major market data providers have some sort of legal stuff like that.

But if you were going to do it anyways, I would probably download the html and parse it in order to find the quote you're looking for.

If you want to do it legally you're going to have to pay a lot of money to get the data from either the exchange itself or a third party entity that relays the information. In that case you're going to have to use whatever API they provide.

Sources: trying to do what you're thinking about doing :)

(EDIT) To those who say use YQL:

Take a look at the Terms of Use.

You can not:

Sell, lease, share, transfer, or sublicense YQL or derive income from the use of YQL in conjunction with Yahoo! APIs or other web services, whether for direct commercial or monetary gain or otherwise, without Yahoo!'s prior, express, written permission

or

Use YQL in a product or service that competes with products or services offered by Yahoo!

Any site that is sharing stock quotes is going to be competing with finance.yahoo.com and therefore disobeying the terms of use of YPL. Furthermore, you won't be able to make any money off of what you're doing with this data. Good luck getting express written consent.

Read under "The Dark Side" here on YQL too: "who owns my code? Technically, they own everything."

YQL isn't a good solution in terms of scalability, profitability, simplicity, or legality. If you don't care about any of these things and your web application isn't going to be popular or make you money, and you don't mind writing a bunch of code to play nicely with Yahoo! then YQL is the way to go.

(EDIT #2)

If you are just trying to display the information for yourself (not sharing it with others), I find it extremely easy to parse html and display it on a website with C#/Visual Basic and ASP.NET rather than php. You can create an http request in C#/VB which will (eventually) return an html file, which you can parse or search through for your Dow Jones Index quote. Do this all in C#/VB as the back end of an ASP web application. I believe you can create these with Visual Studio 2010 Express which is free.

OTHER TIPS

I am not sure whether there's a legal limit for doing this. There are lots of commercial (paid) apps that pull data from Yahoo Finance, and have been working fine for years. I'd say that the trick here is that these apps are installed into the user's device (iOS/Android/Windows devices), and therefore they pull data from Yahoo Finance for the user's own personal use.

Maybe if you pulled the data from Yahoo Finance through Javascript and didn't store it in your servers, the same case would apply to your project. As a matter of fact, I would actually think so. The only difference in this case is that your code would not need to be installed on the user's device, as it is a web page.

Have you tried to contact YQL or Yahoo Finance's support to discuss the legal limits of your projects?

I've written a function to get stock data from Yahoo! Finance API; however, my code downloads .csv file. I hope it's useful.

The full source code and sample results are on my blog.

<?php

/*
Description: Getting Stock Data from Yahoo! Finance
Author URI: http://phpvancouver.ca/
*/

/* 
* More about Yahoo! Finance Tag
* http://www.gummy-stuff.org/Yahoo-data.htm
* http://www.canbike.ca/information-technology/2013/08/10/yahoo-finance-url-download-to-a-csv-file.html
*/

$yahoo_finance_tags = array(
"a" => "Ask",       "a2" => "Average Daily Volume",     "a5" => "Ask Size",
"b" => "Bid",       "b2" => "Ask (Real-time)",      "b3" => "Bid (Real-time)",
"b4" => "Book Value",       "b6" => "Bid Size",     "c" => "Change & Percent Change",
"c1" => "Change",       "c3" => "Commission",       "c6" => "Change (Real-time)",
"c8" => "After Hours Change (Real-time)",       "d" => "Dividend/Share",        "d1" => "Last Trade Date",
"d2" => "Trade Date",       "e" => "Earnings/Share",        "e1" => "Error Indication (returned for symbol changed / invalid)",
"e7" => "EPS Estimate Current Year",        "e8" => "EPS Estimate Next Year",       "e9" => "EPS Estimate Next Quarter",
"f6" => "Float Shares",     "g" => "Day's Low",     "h" => "Day's High",
"j" => "52-week Low",       "k" => "52-week High",      "g1" => "Holdings Gain Percent",
"g3" => "Annualized Gain",      "g4" => "Holdings Gain",        "g5" => "Holdings Gain Percent (Real-time)",
"g6" => "Holdings Gain (Real-time)",        "i" => "More Info",     "i5" => "Order Book (Real-time)",
"j1" => "Market Capitalization",        "j3" => "Market Cap (Real-time)",       "j4" => "EBITDA",
"j5" => "Change From 52-week Low",      "j6" => "Percent Change From 52-week Low",      "k1" => "Last Trade (Real-time) With Time",
"k2" => "Change Percent (Real-time)",       "k3" => "Last Trade Size",      "k4" => "Change From 52-week High",
"k5" => "Percebt Change From 52-week High",     "l" => "Last Trade (With Time)",        "l1" => "Last Trade (Price Only)",
"l2" => "High Limit",       "l3" => "Low Limit",        "m" => "Day's Range",
"m2" => "Day's Range (Real-time)",      "m3" => "50-day Moving Average",        "m4" => "200-day Moving Average",
"m5" => "Change From 200-day Moving Average",       "m6" => "Percent Change From 200-day Moving Average",       "m7" => "Change From 50-day Moving Average",
"m8" => "Percent Change From 50-day Moving Average",        "n" => "Name",      "n4" => "Notes",
"o" => "Open",      "p" => "Previous Close",        "p1" => "Price Paid",
"p2" => "Change in Percent",        "p5" => "Price/Sales",      "p6" => "Price/Book",
"q" => "Ex-Dividend Date",      "r" => "P/E Ratio",     "r1" => "Dividend Pay Date",
"r2" => "P/E Ratio (Real-time)",        "r5" => "PEG Ratio",        "r6" => "Price/EPS Estimate Current Year",
"r7" => "Price/EPS Estimate Next Year",     "s" => "Symbol",        "s1" => "Shares Owned",
"s7" => "Short Ratio",      "t1" => "Last Trade Time",      "t6" => "Trade Links",
"t7" => "Ticker Trend",     "t8" => "1 yr Target Price",        "v" => "Volume",
"v1" => "Holdings Value",       "v7" => "Holdings Value (Real-time)",       "w" => "52-week Range",
"w1" => "Day's Value Change",       "w4" => "Day's Value Change (Real-time)",       "x" => "Stock Exchange",
);

/* This function gets a symbol or an array of symbol as a parameter.
* And it returns an array of the corresponding stock data.
* If an error occurs, the detail of the error is saved in $error_message variable
* which is passed by reference from the parent function
*/

function get_stock_data_from_yahoo_finance_pv($symbol, &$error_message) {

global $yahoo_finance_tags;
$error_message = NULL; // Default value 

$f = ""; // The f parameter in Yahoo! Finance URL
foreach($yahoo_finance_tags as $key => $value)
    $f = $f . $key;

if ( is_array($symbol) ) { // if the symbol is an array

    if ( $symbol == NULL ) { // if the symbol is invalid
        $error_message = "The given symbol is invalid.";
        return -1; // ERROR
    }

    $url = "http://finance.yahoo.com/d/quotes.csv?s=" . implode("+", $symbol) . "&f=" . $f;
    $fp = @fopen($url, "r");
    if ( $fp == FALSE ) { // If the URL can't be opened
        $error_message = "Cannot get data from Yahoo! Finance. The following URL is not accessible, $url";
        return -1; // ERROR
    }

    $arr = array();
    $symbol = explode("+",implode("+", $symbol)); // Eliminate the keys in the symbol array
    $j = 0;
    while ( ($array = @fgetcsv($fp , 4096 , ', ')) !== FALSE ) {
        $i = 0;
        $p = array();
        foreach($yahoo_finance_tags as $key => $value) {
            $p[$key] = $array[$i];
            $i = $i + 1;
        }
        $arr[$symbol[$j]]= $p;
        $j = $j + 1;
    }
    @fclose($fp);
    return $arr;

} else {  // if the symbol is not array 

    if ( strlen($symbol) < 1 || $symbol == NULL ) { // if the symbol is invalid
        $error_message = "The given symbol is invalid.";
        return -1; // ERROR
    }

    $url = "http://finance.yahoo.com/d/quotes.csv?s=" . $symbol . "&f=" . $f;
    $fp = @fopen($url, "r");
    if ( $fp == FALSE ) { // If the URL can't be opened
        $error_message = "Cannot get data from Yahoo! Finance. The following URL is not accessible, $url";
        return -1; // ERROR
    }

    $array = @fgetcsv($fp , 4096 , ', '); 
    $arr = array();
    $i = 0;
    foreach($yahoo_finance_tags as $key => $value) {
        $arr[$key] = $array[$i];
        $i = $i + 1;
    }
    @fclose($fp);       
    return $arr;
}
return -1;
}

Thanks to everyone who contributed because the ideas generated on this thread helped me to find an answer and do EXACTLY what I was looking for. This guy explains how to do it in roughly 10 lines of code :

http://www.seangw.com/wordpress/index.php/2010/01/formatting-stock-data-from-yahoo-finance/

So basically all I did was cut & paste his code and put it in index.html on my site. When visiting my website I get the quotes of tickers I'm interested in. You can modify $stocklist to put in whatever stock you want, and you can modify the response from Yahoo using the tags in $stockFormat. Thanks again SO!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top