Yahoo Stock Market retrieval system

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Yahoo Stock Market retrieval system

Postby phice » Fri Oct 24, 2003 9:38 pm

Small class-based php program to grab the latest stocks (specified in the url [stock.php?symbols=YHOO,MSFT,etc...]). I forget where I got the original source from, but I edited it to enhance a small section of my website. Enjoy :)

Syntax: [ Download ] [ Hide ]
<?php

// Code by:   Don Wilson

// Edited:     October 24, 2003

class stock

{

        function getLastTrade($symbol)

        {

                $file = fopen("http://quote.yahoo.com/d/quotes.csv?s={$symbol}&f=sl1d1t1c1ohgv&e=.csv", "r");

                        $read = fread($file, 2000);

                        fclose($file);

                $read = str_replace(""", "", $read);

                $read = explode("
,", $read);

                return $read[1];

        }



        function getChange($symbol)

        {

                $file = fopen("
http://quote.yahoo.com/d/quotes.csv?s={$symbol}&f=sl1d1t1c1ohgv&e=.csv", "r");

                        $read = fread($file, 2000);

                        fclose($file);

                $read = str_replace(""", "", $read);

                $read = explode("
,", $read);

                return $read[4];

        }



        function getDateClosed($symbol)

        {

                $file = fopen("
http://quote.yahoo.com/d/quotes.csv?s={$symbol}&f=sl1d1t1c1ohgv&e=.csv", "r");

                        $read = fread($file, 2000);

                        fclose($file);

                $read = str_replace(""", "", $read);

                $read = explode("
,", $read);

                return $read[2] . "
" . $read[3];

        }



}



$stock = new Stock();



echo "
<pre>\n";

if(isset($symbols))

{

        $symbols = explode("
,", $symbols);

        foreach($symbols as $symbol)

        {

        $change = $stock->getChange($symbol);

        if($change < 0)

          $change = "
<font color=red>{$change}</font>";

        elseif($change > 0)

          $change = "
<font color=green>{$change}</font>";

        else

          $change = "
0.00";

         

                echo "
Symbol:                   " . $symbol                                                                             . "\n";

                echo "
Last Trade:               " . $stock->getLastTrade($symbol)               . "\n";

                echo "
Change:                   " . $change                     . "\n";

                echo "
Date Closed:              " . $stock->getDateClosed($symbol)      . "\n";

                echo "
\n";

        }

}

echo "
</pre>\n";

?>
User avatar
phice
Moderator
 
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX

Postby Dale » Tue Jun 22, 2004 11:13 am

Sounds silly... but where could i get all the stock exchange symbols?
Dale
Forum Contributor
 
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Postby d3ad1ysp0rk » Tue Jun 22, 2004 10:19 pm

Phice, do you know how legal it would be to use these stocks/prices in a game that makes profit?
d3ad1ysp0rk
Forum Donator
 
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Postby feyd » Tue Jun 22, 2004 10:28 pm

you can use them as entropy data for seeding randoms pretty easy.. however using them straight.. I dunno.. need to talk to a lawyer, but I'd guess it's "okay"
User avatar
feyd
Neighborhood Spidermoddy
 
Posts: 31559
Joined: Mon Mar 29, 2004 4:24 pm
Location: Bothell, Washington, USA

Postby computingfuture » Thu Jun 24, 2004 1:02 pm

LiLpunkSkateR wrote:Phice, do you know how legal it would be to use these stocks/prices in a game that makes profit?



Now thats an idea. But how you you use it to make profit?
computingfuture
Forum Newbie
 
Posts: 6
Joined: Thu Jun 24, 2004 12:49 pm
Location: Seaton Easy Devon UK

Postby phice » Fri Jun 25, 2004 4:59 am

computingfuture.net wrote:
LiLpunkSkateR wrote:Phice, do you know how legal it would be to use these stocks/prices in a game that makes profit?



Now thats an idea. But how you you use it to make profit?


I'm guessing he meant a game that sells account activation, etc that uses the data for it's game contents..?
User avatar
phice
Moderator
 
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX

Re: Yahoo Stock Market retrieval system

Postby mybelle » Thu Jun 10, 2010 4:18 pm

Thank you so much for this. I am a real amateur .. I found a stock ticker online, for a client who wanted it on a website I am building - here is the code:


It works great, but is missing the code to get the actual stock info. Can I use your script to modify this? If so, could I trouble you to tell me where to add it into this code? Thank you so much!
Last edited by mybelle on Thu Jun 10, 2010 5:25 pm, edited 2 times in total.
mybelle
Forum Newbie
 
Posts: 2
Joined: Thu Jun 10, 2010 3:23 pm

Re: Yahoo Stock Market retrieval system

Postby mybelle » Thu Jun 10, 2010 5:24 pm

Thank you so much for this. I am a real amateur .. I found a stock ticker online, for a client who wanted it on a website I am building - here is the code:
<?php
///// Replace with Yahoo Finance stock quotes of your choice //////
$raw_stock_data = array('AAPL 200 205',
'BIDU 500 510','RIMM 150 155', 'SNDK 70 72',
'GOOG 500 510','MSFT 30 32','AMD 9 10');
sort($raw_stock_data);
//////////////////////////////////////////////////////////////////////////
?>
<div align="center">
<marquee bgcolor="#e0e1e1" direction="left" loop="20" width="100%%">
<?php
foreach ($raw_stock_data as $value) {
$results = explode(' ',$value);

$ticker = $results[0];
$buy = $results[1];
$sell = $results[2];

echo "<font color=\"#ffffff\">$ticker </font>";
echo "<font color=\"#00ff00\">$buy </font>";
echo "<font color=\"#ff0000\">$sell </font>";
echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
}
?>
It works great, but is missing the code to get the actual stock info. Can I use your script to modify this? If so, could I trouble you to tell me where to add it into this code? Thank you so much!
mybelle
Forum Newbie
 
Posts: 2
Joined: Thu Jun 10, 2010 3:23 pm

Re: Yahoo Stock Market retrieval system

Postby mikecampbell » Wed Oct 27, 2010 1:02 pm

I put it all together into a ticker with live quotes. You can see (and run) the code here:

http://www.exorithm.com/algorithm/view/stock_ticker
mikecampbell
Forum Commoner
 
Posts: 38
Joined: Tue Oct 12, 2010 7:26 pm

Re: Yahoo Stock Market retrieval system

Postby uriljames » Fri Mar 04, 2011 12:00 am

I like your information very much about yahoo stock market retrieval system. Thanks for sharing this useful info.
uriljames
Forum Newbie
 
Posts: 1
Joined: Thu Mar 03, 2011 11:53 pm


Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 1 guest