Thor
Honorary Master
- Joined
- Jun 5, 2014
- Messages
- 44,236
With the new gorgeous design of mybroadband I decided its time to do some renovations of my own.
I have a small Stock ticker that I use, issue is when I have a lot of them then the process becomes quite slow also if the API is down I need a fallback, thus I will have to store the results in a database the website in question does not have a database so I am thinking of going with a flat file however what happens with simultaneous connections, the website averages on about 4 requests per second.
So my question what would be the best approach to take this to a flat file db system.
My thinking was to setup a cronjob to store the infoObject in the flat file and then each user that visits the site simply reads from the flat file so then it should work if 10 users open the site at the exact same time correct?
PHP:
Also rep is welcome if you found this piece of code useful.
I have a small Stock ticker that I use, issue is when I have a lot of them then the process becomes quite slow also if the API is down I need a fallback, thus I will have to store the results in a database the website in question does not have a database so I am thinking of going with a flat file however what happens with simultaneous connections, the website averages on about 4 requests per second.
So my question what would be the best approach to take this to a flat file db system.
My thinking was to setup a cronjob to store the infoObject in the flat file and then each user that visits the site simply reads from the flat file so then it should work if 10 users open the site at the exact same time correct?
PHP:
PHP:
<?php
$marketsArray = array(
"JSE:DBXWD"=>array("name"=>"Db x-trackers Col in Wld"),
"JSE:TAS"=>array("name"=>"Taste Holdings"),
"JSE:STXIND"=>array("name"=>"Satrix Industrial 25"),
"JSE:CCO"=>array("name"=>"Capital & Counties Properties PLC"),
"JSE:WHL"=>array("name"=>"Woolworths Holdings Limited"),
"JSE:SHP"=>array("name"=>"Shoprite Holdings Ltd"),
"JSE:RLF"=>array("name"=>"Rolfes Holdings")
);
//this isn't the most accurate as it's only taking into account 2 data points.
foreach($marketsArray as $key => $val){
//get url for this symbol
$infoUrl = 'http://www.google.com/finance/info?client=ig&q='.$key;
//get contents, remove //
$infoObj = str_replace('//','',file_get_contents($infoUrl));
if($infoObj){
//clean control chars and convert to asc array
$infoObj = json_decode(utf8_encode($infoObj),true);
/*"id": "338568" - internal google security id
,"t" : "CCO" - stock symbol
,"e" : "JSE" - exchange name
,"l" : "19.72" - last trade price
,"l_fix" : "19.72" - last trade ?
,"l_cur" : "19.72" - last trade with currency
,"s": "0" - last trade size
,"ltt":"3:59PM EDT" - last trade time
,"lt" : "December 13, 3:59PM EDT" - last trade date time long
,"lt_dts" : "2016-12-13T15:59:59Z" - last trade date time
,"c" : "-0.31" - change
,"c_fix" : "-0.31" - ?
,"cp" : "-1.55" - ? percentage
,"cp_fix" : "-1.55" - ?
,"ccol" : "chr" - ?
,"pcls_fix" : "20.03" - previous close price
*/
$stock = $infoObj[0]['t'];
$exchange = $infoObj[0]['e'];
$code = $stock.':'.$exchange;
$cPercentage = $infoObj[0]['cp'];//this is the percentage, which is basically just comparing the change between the end of the last day, and the most recent transaction
$cAmount = $infoObj[0]['c'];
$prevClosePrice = floatval(str_replace(',','',$infoObj[0]['pcls_fix']));
$lastTradePrice = floatval(str_replace(',','',$infoObj[0]['l']));
$lastTradePriceCur = $infoObj[0]['l_cur'];
$lastTradeDate = $infoObj[0]['lt'];
//mean
$mean = ($prevClosePrice+$lastTradePrice)/2;
//subtract mean from each, and square
$newPrev = ($prevClosePrice-$mean)*($prevClosePrice-$mean);
$newLast = ($lastTradePrice-$mean)*($lastTradePrice-$mean);
//sum/devide by 1 (so nothing) and sqrt
$final = round(sqrt($newPrev+$newLast),2);
$ticker = "<li><a href=\"https://www.google.com/finance?q=$code\">";
$ticker .= '<b>'.$val['name'].'</b>';
$ticker .= " ";
$ticker .= "$code";
$ticker .= " ";
$ticker .= "$lastTradePriceCur";
$ticker .= " ";
$ticker .= "$cAmount";
$ticker .= " ";
$ticker .= "$cPercentage";
$ticker .= '</a></li>';
echo $ticker;
}
}
?>
Also rep is welcome if you found this piece of code useful.
Last edited:
