modifying the PHP Stock Market Analyzer code.

0 tony ward · May 9, 2015
Hi, BR,

I am now trying to modify your code a bit so it can suitable for other stock markets in the world.
This is because I found the code cannot be applied in other stock market because they use numbers rather than alphabet characters in US. 

I am trying to add a external text file say "csvurl.txt" to redirect the data file downloaded in gg spreadsheet. say.
H0001, URL1
H0003, URL3

In the createURL file, I am trying to crate the url by splitting the string in csvurl.txt file but I find it will return the url three times, say, URL1URL3URL1URL3URL1URL3, etc. Secondly, I find the text file created, say H0001, H0003, are identical which is out of my expected. I am newbie in PHP but I really want to create a database myself.

There is another problem, the arrangement of the information in gg spreadsheet is different from yahoo. I am trying to solve it myself. Especially, the volume is shown as "1,999,999" (with " " in text file) rather than 1999999. I hope BR or other programmer can help me to sort out the problem. Thanks.

The code is as follows:


include ("includes/connect.php");

function createURL($ticker){
    $c1 = file('csvurl.txt');
    $l = count($c1);
    for ($i = 0; $i < $l; $i++) {
        list($t, $url) = explode(',', $c1[$i]);
    // making array $red['H001'] => 'URL1"
        $red[trim($t)] = trim($url);
// If rule exists
    return isset($red[$ticker]) ? return($red[$ticker]) : false;

function getCSVFile($url, $outputFile){
$content = file_get_contents($url);
$content = str_replace("Date,Open,High,Low,Close,Volume,Adj Close", "", $content);
$content = trim($content);
file_put_contents($outputFile, $content);

function fileToDatabase($txtFile, $tableName){
$file = fopen($txtFile, "r");
$line = fgets($file);
$pieces = explode(",", $line);

$date = $pieces[0];
$open = $pieces[1];
$high = $pieces[2];
$low = $pieces[3];
$close = $pieces[4];
$volume = $pieces[5];
$amount_change = $close-$open;
$percent_change = ($amount_change/$open)*100;

$sql = "SELECT * FROM $tableName";
$result = mysql_query($sql);

$sql2 = "CREATE TABLE $tableName (date DATE, PRIMARY KEY(date), open FLOAT, high FLOAT, low FLOAT, close FLOAT, volume INT, amount_change FLOAT, percent_change FLOAT)";

$sql3 = "INSERT INTO $tableName (date, open, high, low, close, volume, amount_change, percent_change) VALUES ('$date','$open','$high','$low','$close','$volume', '$amount_change', '$percent_change')";

function main(){
$mainTickerFile = fopen("tickerMaster.txt", "r");
$companyTicker = fgets($mainTickerFile);
$companyTicker = trim($companyTicker);
$fileURL = createURL($companyTicker);
$companyTxtFile = "txtFiles/".$companyTicker.".txt";
getCSVFile($fileURL, $companyTxtFile);
fileToDatabase($companyTxtFile, $companyTicker);



Post a Reply


Oldest  Newest  Rating
0 Pere Garau Burguera · May 9, 2015
The code doesn't show up for some reason. 
0 tony ward · May 11, 2015
Hi, Pere, 

I have attached the code.
Thanks for your prompt reply. :)
  • 1



Server-side, HTML embedded scripting language used to create dynamic Web pages.