STOCK TRADING GAME
http://stockplant.tk is a fun stock market game I have been working on, you can see real time stock trading data and see how stocks are doing, I will be creating a custom graph for it soon instead of the .gif image that is created right now. This game was built on data from Yahoo, expecting yahoo to be a stable form of data retrieval.
The Problem
I had a problem with Yahoo canceling their stock market api without notice, so as a quick solution I used Google sites and ‘scraped’ the data until I could find the solution.
TThe Solution
I did find a solution in https://www.alphavantage.co , this allows for data seizure through a .json method. Basically it returns a ‘multi-dimensional array’ which I can create into a easy to plot chart and graph.
In Conclusion
While it was kind of a startling thing to come to my website, see that it was gathering no data, this experience helped me to learn new methods of gathering data and although it would be nice of the major data provider to give notice of their removal of stock gathering information, I probably wouldn’t have noticed their notes anyway. All in all it helped me to learn how to use multi-dimensional arrays better, even though I already have knowledge of working with arrays, and how to find information from simple searches with scraping
Scraping – to have your website grab information from another site
example of code:
[wpex more=”See Code” less=”Collapse”]$previous_value = libxml_use_internal_errors(TRUE);
//$codeeval = $_GET[‘codeeval’];
$score_url = ‘https://finance.google.com/finance?q=’.$exchange .”:” . $symbol . ‘/’;
$html = file_get_contents($score_url);
$classname = ‘market-data-div’;
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$results = $xpath->query(“//*[@id='” . $classname . “‘]”);
// scrapelibxml_clear_errors();
libxml_use_internal_errors($previous_value);
foreach ($results as $node) {
$full_content = innerHTML($node);$wow = $node->textContent;
}
$stain = preg_split(‘/\s+/’, $wow);
$tops = array();
foreach ($stain as $val){
if (is_numeric($val)) {
array_push($tops,$val);</blockquote>
}
}
$quote = array();
$quote[‘symbol’] = $symbol;
$quote[‘last’] = $tops[0];
$quote[‘date’];
$quote[‘timestamp’];
$quote[‘change’] = $tops[1];
$quote[‘open’] = $tops[7];
$quote[‘high’] = $tops[3];
$quote[‘low’] = $tops[2];
$quote[‘volume’];
$quote[‘previousClose’];
$quote[‘name’] = $fullname;
$quote[‘bid’];
$quote[‘ask’];
$quote[‘eps’] = $tops[10];
$quote[‘YearLow’] = $tops[5];
$quote[‘YearHigh’] = $tops[6];
$quote[‘PE’] = $tops[8];
return $quote;[/wpex]
multi-dimensional array – multiple data points stored into an easy to access array
[wpex more=”See Code” less=”Collapse”]function getQuote($symbol,$exchange,$fullname)
{
$points = array();
$pointdates = array();
// alpha
$link = “https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&outputsize=compact&symbol=”. $symbol .”&apikey=66TWU1D2SCRTXWNB”;$data = file_get_contents($link);
$proc = json_decode($data, true);
//print_r($proc);// multidimensional array
$keys = array_keys($proc);for($i = 0; $i < count($proc); $i++) {
// $nextkey = array_keys($keys[$i]);// echo $keys[$i] . “{
“;
foreach($proc[$keys[$i]] as $key => $value) {
if (!isset($firstdate)){
$firstdate = $key;
$yeardate = strtotime(“+13 months”, strtotime($firstdate));
}
$curdate = $key;
if (is_array ( $value )){
foreach($value as $tea => $plea) {
if ($curdate < $yeardate){
if ($tea == ‘4. close’)
{
array_push($points,$plea);
}
}
// second
if ($curdate )
if ($tea == ‘1. open’ && isset($firstvolume) && !isset($secondopen)){$secondopen = $plea;
}
if ($tea == ‘2. high’ && isset($firsthigh) && !isset($secondhigh)){$secondhigh = $plea;
}
if ($tea == ‘4. close’ && isset($firstclose) && !isset($secondclose)){$secondclose = $plea;
}
if ($tea == ‘3. low’ && isset($firstlow) && !isset($secondlow)){$secondlow = $plea;
}
if ($tea == ‘5. volume’ && isset($firstvolume) && !isset($secondvolume)){$secondvolume = $plea;
}
if ($tea == ‘1. open’ && !isset($firstopen)){$firstopen = $plea;
}
if ($tea == ‘2. high’ && !isset($firsthigh)){$firsthigh = $plea;
}
if ($tea == ‘4. close’ && !isset($firstclose)){$firstclose = $plea;
}
if ($tea == ‘3. low’ && !isset($firstlow)){$firstlow = $plea;
}
if ($tea == ‘5. volume’ && !isset($firstvolume)){$firstvolume = $plea;
}//echo ‘
<div style=”padding-left: 25px;”>’ . $tea . ” : ” . $plea . ‘</div>
‘;}
}}
}
$link = “https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&outputsize=compact&symbol=”. $symbol .”&apikey=66TWU1D2SCRTXWNB”;$data = file_get_contents($link);
$proc = json_decode($data, true);
//print_r($proc);// multidimensional array
$keys = array_keys($proc);for($i = 0; $i < count($proc); $i++) {
// $nextkey = array_keys($keys[$i]);// echo $keys[$i] . “{
“;
foreach($proc[$keys[$i]] as $key => $value) {
if (!isset($firstdate)){
$firstdate = $key;
}
$curdate = $key;
if (is_array ( $value )){
foreach($value as $tea => $plea) {
// second//echo ‘
<div style=”padding-left: 25px;”>’ . $tea . ” : ” . $plea . ‘</div>
‘;}
}}
}
$quote[‘symbol’] = $symbol;
$quote[‘last’] = $firstclose;
$quote[‘date’] = $firstdate;
$quote[‘timestamp’];
$quote[‘change’] = ($firstclose – $secondclose);
$quote[‘open’] = $firstopen;
$quote[‘high’] = $firsthigh;
$quote[‘low’] = $firstlow;
$quote[‘volume’] = $firstvolume;
$quote[‘previousClose’] = $secondclose;
$quote[‘name’] = $fullname;
/* $quote[‘bid’];
$quote[‘ask’];
$quote[‘eps’] = $tops[10]; */
$quote[‘YearLow’] = min($points);
$quote[‘YearHigh’] = max($points);
// $quote[‘PE’] = $tops[8];
return $quote;[/wpex]