if (isset($_GET['chart']) || $isCmd) { $chartDataType = isset($_GET['chart']) ? $_GET['chart'] : $cmdChart; //$chartDataType = $_GET['chart']; debug_print("Date range: from {$fromDate} to {$toDate}<Br>"); switch ($chartDataType) { case 'close': getClose($company, $fromDate, $toDate, $dataorg, $mysql_host, $mysql_database, $mysql_user, $mysql_password); break; case 'current': getCurrentDayPrices($company, $dataorg, $mysql_host, $mysql_database, $mysql_user, $mysql_password); break; case 'volume': getVolume($company, $fromDate, $toDate, $dataorg, $mysql_host, $mysql_database, $mysql_user, $mysql_password); break; case 'ohlcur': getOHLCurrent($company, $dataorg, $mysql_host, $mysql_database, $mysql_user, $mysql_password); break; case 'ohlc': getOHLC($company, $fromDate, $toDate, $dataorg, $mysql_host, $mysql_database, $mysql_user, $mysql_password); break; case 'ohlcha': getOHLCHA($company, $fromDate, $toDate, $dataorg, $mysql_host, $mysql_database, $mysql_user, $mysql_password); break; case 'sohlcavv': $targetDate = null; if (isset($_GET['targetdate'])) { $targetDate = isset($_GET['targetdate']) ? $_GET['targetdate'] : null; } getSingleOHLCAVV($company, $targetDate, $dataorg, $mysql_host, $mysql_database, $mysql_user, $mysql_password); break; case 'macd':
function getOHLC($company, $from = "1900-01-01 00:00:00", $to = null, $dataorg = "json", $host, $db, $user, $pass) { $ohlcur = []; //ohl-current candlestick $accessOHLCur = false; //don't read the ohlcurrent by default // Create connection $con = mysqli_connect($host, $user, $pass, $db); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); return; } if ($dataorg == "highchart" || $dataorg == "array2") { //Added 8 hours to timestamp because of the Philippine Timezone WRT GMT (+8:00) $sql = "SELECT (UNIX_TIMESTAMP(DATE_ADD(timestamp, INTERVAL 8 HOUR)) * 1000) as timestamp, \n\t\t\t\t\topen, high, low, close, volume \n\t\t\t\t\tFROM {$company} \n\t\t\t\t\tWHERE timestamp >= '" . $from . "' AND timestamp <= '" . $to . "' ORDER BY timestamp ASC"; } else { $sql = "SELECT DATE_FORMAT(timestamp, '%Y-%m-%d') as timestamp, open, high, low, close, volume \n\t\t\t\t\tFROM {$company} \n\t\t\t\t\tWHERE timestamp >= '" . $from . "' AND timestamp <= '" . $to . "' ORDER BY timestamp ASC"; } $result = mysqli_query($con, $sql); $dbreturn = ""; $ctr = 0; $temp; while ($row = mysqli_fetch_array($result)) { if ($dataorg == "json") { $dbreturn[$ctr]['timestamp'] = $row['timestamp']; $dbreturn[$ctr]['open'] = $row['open']; $dbreturn[$ctr]['high'] = $row['high']; $dbreturn[$ctr]['low'] = $row['low']; $dbreturn[$ctr]['close'] = $row['close']; $dbreturn[$ctr]['volume'] = $row['volume']; } elseif ($dataorg == "highchart") { $dbreturn[$ctr][0] = doubleval($row['timestamp']); $dbreturn[$ctr][1] = floatval($row['open']); $dbreturn[$ctr][2] = floatval($row['high']); $dbreturn[$ctr][3] = floatval($row['low']); $dbreturn[$ctr][4] = floatval($row['close']); $dbreturn[$ctr][5] = intval($row['volume']); } elseif ($dataorg == "array") { $dbreturn[$ctr][0] = $row['timestamp']; $dbreturn[$ctr][1] = floatval($row['open']); $dbreturn[$ctr][2] = floatval($row['high']); $dbreturn[$ctr][3] = floatval($row['low']); $dbreturn[$ctr][4] = floatval($row['close']); $dbreturn[$ctr][5] = intval($row['volume']); } elseif ($dataorg == "array2") { $dbreturn[$ctr][0] = doubleval($row['timestamp']); $dbreturn[$ctr][1] = floatval($row['open']); $dbreturn[$ctr][2] = floatval($row['high']); $dbreturn[$ctr][3] = floatval($row['low']); $dbreturn[$ctr][4] = floatval($row['close']); $dbreturn[$ctr][5] = intval($row['volume']); } else { $dbreturn[$ctr]['timestamp'] = $row['timestamp']; $dbreturn[$ctr]['open'] = $row['open']; $dbreturn[$ctr]['high'] = $row['high']; $dbreturn[$ctr]['low'] = $row['low']; $dbreturn[$ctr]['close'] = $row['close']; $dbreturn[$ctr]['volume'] = $row['volume']; } $ctr = $ctr + 1; } if ($ctr <= 1) { //no data return 0; } $dateToday = date('Y-m-d'); $latestTS = 0; switch ($dataorg) { case 'json': $latestTS = $dbreturn[$ctr - 1]['timestamp']; break; case 'highchart': $latestTS = $dbreturn[$ctr - 1][0]; $dateToday = date('U') * 1000; break; case 'array': $latestTS = $dbreturn[$ctr - 1][0]; break; case 'array2': $latestTS = $dbreturn[$ctr - 1][0]; $dateToday = date('U') * 1000; break; default: $latestTS = $dbreturn[$ctr - 1]['timestamp']; break; } //echo "after switch. Date Today: $dateToday, Latest: $latestTS<Br>"; if ($dateToday > $latestTS && !isWeekend($dateToday)) { $accessOHLCur = true; //get the candlestick for today OHLCurrent $ohlcur = getOHLCurrent($company, $dataorg, $host, $db, $user, $pass); //echo json_encode($ohlcur); if ($ohlcur == "") { $accessOHLCur = false; //echo "Returned OHLCurrent is empty. "; } } mysqli_close($con); if ($dataorg == "json") { if ($accessOHLCur) { $ts = date('Y-m-d', strtotime($ohlcur[0]['timestamp'])); //echo "json ts new: " . $ts . "<Br>"; if ($ts > $dbreturn[$ctr - 1]['timestamp']) { //echo "json OHLCurrent > latestDB TS<Br>"; $dbreturn[$ctr]['timestamp'] = $ts; $dbreturn[$ctr]['open'] = $ohlcur[0]['open']; $dbreturn[$ctr]['high'] = $ohlcur[0]['high']; $dbreturn[$ctr]['low'] = $ohlcur[0]['low']; $dbreturn[$ctr]['close'] = $ohlcur[0]['close']; $dbreturn[$ctr]['volume'] = $ohlcur[0]['volume']; } } echo json_encode($dbreturn); } elseif ($dataorg == "highchart") { //echo "highchart<Br>"; if ($accessOHLCur) { $ts = $ohlcur[0][0] / 1000; $ts = ($ts - $ts % 86400) * 1000; //echo "highchart ts: $ts<Br>"; if ($ts > $dbreturn[$ctr - 1][0]) { //echo "highchart OHLCurrent > latestDB TS<Br>"; $dbreturn[$ctr][0] = $ts; $dbreturn[$ctr][1] = $ohlcur[0][1]; $dbreturn[$ctr][2] = $ohlcur[0][2]; $dbreturn[$ctr][3] = $ohlcur[0][3]; $dbreturn[$ctr][4] = $ohlcur[0][4]; $dbreturn[$ctr][5] = $ohlcur[0][5]; } } echo json_encode($dbreturn); } elseif ($dataorg == "array") { if ($accessOHLCur) { //echo "company: $company, "; $ts = date('Y-m-d', strtotime($ohlcur[0][0])); //echo "array ts new: " . $ts . "<Br>"; if ($ts > $dbreturn[$ctr - 1][0]) { //echo "array OHLCurrent > latestDB TS<Br>"; $dbreturn[$ctr][0] = $ts; $dbreturn[$ctr][1] = $ohlcur[0][1]; $dbreturn[$ctr][2] = $ohlcur[0][2]; $dbreturn[$ctr][3] = $ohlcur[0][3]; $dbreturn[$ctr][4] = $ohlcur[0][4]; $dbreturn[$ctr][5] = $ohlcur[0][5]; } } //echo json_encode($dbreturn); return $dbreturn; } elseif ($dataorg == "array2") { if ($accessOHLCur) { $ts = $ohlcur[0][0] / 1000; $ts = ($ts - $ts % 86400) * 1000; //echo "array2 ts: $ts<Br>"; if ($ts > $dbreturn[$ctr - 1][0]) { //echo "array2 OHLCurrent > latestDB TS<Br>"; $dbreturn[$ctr][0] = $ts; $dbreturn[$ctr][1] = $ohlcur[0][1]; $dbreturn[$ctr][2] = $ohlcur[0][2]; $dbreturn[$ctr][3] = $ohlcur[0][3]; $dbreturn[$ctr][4] = $ohlcur[0][4]; $dbreturn[$ctr][5] = $ohlcur[0][5]; } } //echo json_encode($dbreturn); return $dbreturn; } else { //json $dbreturn[$ctr]['timestamp'] = $ohlcur['timestamp']; $dbreturn[$ctr]['open'] = $ohlcur['open']; $dbreturn[$ctr]['high'] = $ohlcur['high']; $dbreturn[$ctr]['low'] = $ohlcur['low']; $dbreturn[$ctr]['close'] = $ohlcur['close']; $dbreturn[$ctr]['volume'] = $ohlcur['volume']; echo json_encode($dbreturn); } }