function codesword_atr($real, $periodLookback = 14) { $tr = codesword_tr($real); //Compute the Average True Range by running WEMA on TR $atr = codesword_wilder_ema($tr, $periodLookback); return $atr; }
function getTrueRange($company, $from = "1900-01-01 00:00:00", $to = null, $dataorg = "json", $host, $db, $user, $pass) { // 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; } $studyPeriod = 14; $smoothingPeriod = 0; $offsetPeriod = $studyPeriod + $smoothingPeriod; if ($dataorg == "highchart") { //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\thigh, low, close\n\t\t\t\t\tFROM {$company} \n\t\t\t\t\tWHERE timestamp >= DATE_ADD('" . $from . "', INTERVAL -{$offsetPeriod} DAY) \n\t\t\t\t\tAND timestamp <= '" . $to . "' ORDER BY timestamp ASC"; } else { $sql = "SELECT DATE_FORMAT(timestamp, '%Y-%m-%d') as timestamp, high, low, close \n\t\t\t\t\tFROM {$company} \n\t\t\t\t\tWHERE timestamp >= DATE_ADD('" . $from . "', INTERVAL -{$offsetPeriod} DAY) \n\t\t\t\t\tAND timestamp <= '" . $to . "' ORDER BY timestamp ASC"; } $result = mysqli_query($con, $sql); $dbreturn = ""; $ctr = 0; $temp; $returnTrueRange; while ($row = mysqli_fetch_array($result)) { if ($dataorg == "json") { $dbreturn[$ctr][0] = $row['timestamp']; $dbreturn[$ctr][1] = floatval($row['high']); $dbreturn[$ctr][2] = floatval($row['low']); $dbreturn[$ctr][3] = floatval($row['close']); } elseif ($dataorg == "highchart") { $dbreturn[$ctr][0] = doubleval($row['timestamp']); $dbreturn[$ctr][1] = floatval($row['high']); $dbreturn[$ctr][2] = floatval($row['low']); $dbreturn[$ctr][3] = floatval($row['close']); } elseif ($dataorg == "array") { //TODO: create code for organizing an array data output } else { $dbreturn[$ctr][0] = $row['timestamp']; $dbreturn[$ctr][1] = floatval($row['high']); $dbreturn[$ctr][2] = floatval($row['low']); $dbreturn[$ctr][3] = floatval($row['close']); } $ctr = $ctr + 1; } if ($dataorg == "json") { $returnTrueRange = codesword_tr($dbreturn); } elseif ($dataorg == "highchart") { $returnTrueRange = codesword_tr($dbreturn); } elseif ($dataorg == "array") { //TODO: create code for organizing an array data output } else { //json $returnTrueRange = codesword_tr($dbreturn); } echo json_encode($returnTrueRange); mysqli_close($con); }