function codesword_utr($real, $periodLookback = 14) { $atr = codesword_atr($real, $periodLookback); for ($i = 0; $i < count($atr); $i++) { //multiply by period $atr[$i][1] = $atr[$i][1] * $periodLookback; } return $atr; }
function getAverageTrueRange($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; $returnATR; 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") { $returnATR = codesword_atr($dbreturn); } elseif ($dataorg == "highchart") { $returnATR = codesword_atr($dbreturn); } elseif ($dataorg == "array") { //TODO: create code for organizing an array data output } else { //json $returnATR = codesword_atr($dbreturn); } echo json_encode($returnATR); mysqli_close($con); }
function getSMAentryATRstop($company, $from = "1900-01-01 00:00:00", $to = null, $dataorg = "json", $samplePeriod = 15, $enSignals = false, $enJsonEncode = "false", $enProfitComputation = true, $host, $db, $user, $pass) { // TODO: Get OHLC data $intervalPeriod = $samplePeriod * 1.5; //from date has to be adjusted for the bollinger bands $date = date_create($from); date_add($date, date_interval_create_from_date_string("-{$intervalPeriod} days")); $fromAdjusted = date_format($date, "Y-m-d"); $dataOhlc = []; //OHLC data format [timestamp,open,high,low,close,volume] if ($dataorg == "highchart") { $dataOhlc = getOHLC($company, $fromAdjusted, $to, "array2", $host, $db, $user, $pass); } else { $dataOhlc = getOHLC($company, $fromAdjusted, $to, "array", $host, $db, $user, $pass); } //Return if $dataOhlc is null if ($dataOhlc == [] || $dataOhlc == 0) { return 0; } //Tailor data for the SMA input //Input for SMA functions should be [timestamp,close] $ctr = 0; $dataOhlcForSMA = []; foreach ((array) $dataOhlc as $ohlc) { $dataOhlcForSMA[$ctr][0] = $ohlc[0]; //timestamp $dataOhlcForSMA[$ctr++][1] = $ohlc[4]; //close } //Get SMA Signal $sma = codesword_sma($dataOhlcForSMA, $samplePeriod); //Get SMA Buy Signals $smaBuy = []; $ctrBuy = 0; $buysellSignals = codesword_smaBuySellSignal($dataOhlcForSMA, $sma); foreach ($buysellSignals as $signal) { if ($signal[1] == "buy") { $smaBuy[$ctrBuy++] = $signal; } } // echo json_encode($smaBuy); //Tailor data for the ATR input $ctr = 0; $dataOhlcForATR = []; foreach ((array) $dataOhlc as $ohlc) { $dataOhlcForATR[$ctr][0] = $ohlc[0]; //timestamp $dataOhlcForATR[$ctr][1] = $ohlc[2]; //high $dataOhlcForATR[$ctr][2] = $ohlc[3]; //low $dataOhlcForATR[$ctr][3] = $ohlc[4]; //close $ctr = $ctr + 1; } // echo json_encode($dataOhlcForATR); // Compute the ATR $atr = codesword_atr($dataOhlcForATR); // echo json_encode($atr); // TODO: Get ATR Stop/Sell Signals // the stop signal is based on the entry price that we'll get from // the SMA Buy Signal // Inputs: // 1. Data OHLC for ATR (ts,open,high,low,close) // 2. SMA Buy Signals (ts, buy) // 3. ATR Values (ts, atr) $smaEntryAtrStopSignals = codesword_smaEntryATRstopTradeDetector($company, $dataOhlc, $smaBuy, $atr, 2, 4, $enProfitComputation); }