Code for "PHP Stock Market Analyzer" Course

+3 David Refaeli · May 4, 2016
Couldn't find it anywhere, so I'm posting mine.

I ran it on XAMPP - make sure to open http://localhost/phpmyadmin/ and create a database called "youtube" (or anything else, as long a you change it in the code as well), before running the code. 

connect.php

<?php

$connect = mysql_connect('localhost','USER','*****'); //put your own user and password.
//If using XAMPP for first time, it will probably be USER='root', with no password

if(!$connect) { 
die('could not connect');
} else {echo "connected! <br>";
}
mysql_select_db("youtube", $connect)

?>




stockDownloader.php



<?php

include("connect.php"); //my connect.php is in the same directory, so no need Buckys path "include/connect.php"

//this function gets the url of a any ticker company provided, for the current date
function createURL($ticker){
$currentMonth = date("n");
$currentMonth -= 1;
$currentDay = date("j");
$currentYear = date("Y");
// echo "http://real-chart.finance.yahoo.com/table.csv?s=$ticker&d=$currentMonth&e=$currentDay&f=$currentYear&g=d&a=7&b=19&c=2014&ignore=.csv";
// was done for testing reasons
return "http://real-chart.finance.yahoo.com/table.csv?s=$ticker&d=$currentMonth&e=$currentDay&f=$currentYear&g=d&a=3&b=15&c=2016&ignore=.csv"; //I did it for 2 weeks
}

//this function gets the content of the url provided and saves the content as a string, without a header, on an output filepath provided
function getCSVfile($url , $outputFile){
$content = file_get_contents($url);
$content = str_replace("Date,Open,High,Low,Close,Volume,Adj Close","",$content);
$content = trim($content);
//echo $content;  - was done for testing reasons
file_put_contents($outputFile, $content);
}

//this function takes the data from the csv file and saves it into sql
function fileToDatabase($txtFile, $tableName){
$file = fopen($txtFile, "r");
while(!feof($file)){
$line = fgets($file);
$pieces = explode(",",$line);
echo implode(" :: ",$pieces);
echo " new line <br>";

$Date = $pieces[0];
//echo $Date."<br>"; - was done for testing reasons

$Open = $pieces[1];
//echo $Open."<br>"; - was done for testing reasons

$High = $pieces[2];
$Low = $pieces[3];
$Close = $pieces[4];
$Volume = $pieces[5];

$amount_change = $Close-$Open;
$percentage_change = 100*($amount_change/$Open);

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

if(!$result){
$sql2="CREATE TABLE $tableName (Date DATE, PRIMARY KEY(Date), Open FLOAT, High FLOAT, Low FLOAT, Close FLOAT, Volume INT, amount_change FLOAT, percentage_change FLOAT)";
mysql_query($sql2);
}

$sql3 = "INSERT INTO $tableName (Date, Open, High, Low, Close, Volume, amount_change, percentage_change) VALUES ('$Date','$Open','$High','$Low','$Close','$Volume','$amount_change','$percentage_change')";
mysql_query($sql3);

}
fclose($file);
}


function main(){

$mainTickerFile = fopen("TickerMaster.txt",'r');

while(! feof($mainTickerFile)){
$companyTicker = fgets($mainTickerFile);
$companyTicker = trim($companyTicker);
$fileURL = createURL($companyTicker);
$companyTextFile = "txtFiles/".$companyTicker.".txt";
getCSVfile($fileURL, $companyTextFile);
fileToDatabase($companyTextFile, $companyTicker);
}
fclose($mainTickerFile);
}

main();

?>

Post a Reply

Replies

Oldest  Newest  Rating
0 David Refaeli · May 6, 2016
the 2nd part, where the actual analysis is taken place (in a file I called analysis_a.php):


<?php

include ('connect.php'); //my connect is on the same work-folder

function masterLoop(){
$mainTickerFile = fopen("tickerMaster.txt","r");
while(! feof($mainTickerFile)){
$companyTicker = fgets($mainTickerFile);
$companyTicker = trim($companyTicker);

$nextDayInc = 0;
$nextDayDec = 0;
$nextDayNoC = 0;
$total = 0;

$sumOfInc = 0;
$sumOfDec = 0;

$sqlSel = "SELECT Date, percentage_change FROM $companyTicker WHERE percentage_change < '0' ORDER BY Date ASC"; //
$resultSel = mysql_query($sqlSel);


if($resultSel){
while($row = mysql_fetch_array($resultSel)){
$date1 = $row['Date'];
$per_change = $row['percentage_change'];


$sql2 = "SELECT Date, percentage_change FROM $companyTicker WHERE Date > '$date1' ORDER BY Date ASC LIMIT 1";
$resultSel2 = mysql_query($sql2);
$numOfRows = mysql_num_rows($resultSel2);
$row2 = mysql_fetch_array($resultSel2);
$date2 = $row2['Date'];
$per_change2 = $row2['percentage_change'];


if($numOfRows==1){


if($per_change2 > 0){
$nextDayInc++;
$sumOfInc += $per_change2;
$total++;
}else if($per_change2 < 0){
$nextDayDec++;
$sumOfDec += $per_change2;
$total++;
}else{
$nextDayNoC++;
$total++;

}else if ($numOfRows==0){
//no data after today
}else{
echo "You have an error";
}
}
}else{
echo "unable to select $companyTicker <br />";
}

$nextDayIncPer = ($nextDayInc/$total)*100;
$nextDayDecPer = ($nextDayDec/$total)*100;
$avgIncPer = $sumOfInc/$nextDayInc;
$avgDecPer = $sumOfDec/$nextDayDec;


insIntoTable($companyTicker, $nextDayInc, $nextDayIncPer, $avgIncPer, $nextDayDec, $nextDayDecPer, $avgDecPer);
}
}

function insIntoTable($companyTicker, $nextDayInc, $nextDayIncPer, $avgIncPer, $nextDayDec, $nextDayDecPer, $avgDecPer){

$buckyBuy = $nextDayIncPer * $avgIncPer;
$buckySell = $nextDayDecPer * $avgDecPer;


$queryS = "SELECT * FROM analysisA WHERE ticker = '$companyTicker' ";
$result3 = mysql_query($queryS);
$numOfRows2 = mysql_num_rows($result3);

if($numOfRows2==1){
$queryUpd = "UPDATE analysisA SET ticker='$companyTicker',daysInc='$nextDayInc',pctOfDaysInc='$nextDayIncPer',avgIncPer='$avgIncPer',daysDec='$nextDayDec',pctOfDaysDec='$nextDayDecPer',avgDecPer='$avgDecPer', buy='$buckyBuy', sell='$buckySell' ";
mysql_query($queryUpd);
}else{
$queryIns = "INSERT INTO analysisA (ticker, daysInc, pctOfDaysInc, avgIncPer, daysDec, pctOfDaysDec, avgDecPer, buy, sell) VALUES('$companyTicker','$nextDayInc','$nextDayIncPer','$avgIncPer','$nextDayDec','$nextDayDecPer','$avgDecPer','$buckyBuy','$buckySell')";
mysql_query($queryIns);

}

$queryC = "CREATE TABLE analysisA (ticker VARCHAR(8), PRIMARY KEY(ticker), daysInc INT, pctOfDaysInc FLOAT, avgIncPer FLOAT, daysDec INT, pctOfDaysDec FLOAT, avgDecPer FLOAT, buy FLOAT, sell FLOAT)";
mysql_query($queryC);
masterLoop();

?>
0 Mr Meesta · September 3, 2016
did you get it to work? problem is all the commands starting with 'mysql_query' have been deprecated

would be good if bucky or someone else could revise with mySQLi standards
0 Md. Ariful Islam Talukder · April 23, 2017
Thanks a lot brother ,,,,,it helped a lot..
  • 1

PHP

131,225 followers
About

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

Links
Moderators