function codesword_dx($real, $periodLookback = 14) { //compute the True Range Undivided Average for $periodLookback days $TR14 = codesword_utr($real, $periodLookback); //compute the Directional Movement for $periodLookback days $DM14 = codesword_dm_wema($real, $periodLookback); //compute the Directional Indicators and the Directional Index $DI14 = []; $DI14_diff = []; $DI14_sum = []; $DX = []; for ($i = 0; $i < count($DM14); $i++) { //get timestamp $DI14[$i][0] = $DM14[$i][0]; //+DI14 = +DM14 / TR14 $DI14[$i][1] = $DM14[$i][1] * 100 / $TR14[$i][1]; //-DI14 = -DM14 / TR14 $DI14[$i][2] = $DM14[$i][2] * 100 / $TR14[$i][1]; //calculate the difference and sum of the DI14 $DI14_diff[$i] = abs($DI14[$i][1] - $DI14[$i][2]); $DI14_sum[$i] = abs($DI14[$i][1] + $DI14[$i][2]); //calculate the directional index //DX = (DI14 diff / DI14 sum) * 100 $DX[$i][0] = $DM14[$i][0]; $DX[$i][1] = $DI14_diff[$i] / $DI14_sum[$i] * 100; } return $DX; }
function getUndividedTrueRange($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; $returnUTR; 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") { $returnUTR = codesword_utr($dbreturn); } elseif ($dataorg == "highchart") { $returnUTR = codesword_utr($dbreturn); } elseif ($dataorg == "array") { //TODO: create code for organizing an array data output } else { //json $returnUTR = codesword_utr($dbreturn); } echo json_encode($returnUTR); mysqli_close($con); }