public function testArrayMeshAddsCorrectly()
 {
     $array = ['minutes' => 100, 'seconds' => 7000];
     $arrayB = ['hours' => 2, 'minutes' => 20, 'seconds' => 200];
     $expectedResult = ['hours' => 2, 'minutes' => 120, 'seconds' => 7200];
     $result = array_mesh($array, $arrayB);
     assertThat($result, is(equalTo($expectedResult)));
 }
Beispiel #2
0
/**
 * Returns a stock by symbol (case-insensitively) else false if not found.
 */
function lookup($year, $month, $day, $symbol)
{
    // headers for proxy servers
    $headers = ["Accept" => "*/*", "Connection" => "Keep-Alive", "User-Agent" => sprintf("curl/%s", curl_version()["version"])];
    // open connection to Yahoo
    $context = stream_context_create(["http" => ["header" => implode(array_map(function ($value, $key) {
        return sprintf("%s: %s\r\n", $key, $value);
    }, $headers, array_keys($headers))), "method" => "GET"]]);
    //day variables
    $firstday = $day[0];
    $dayTotal = count($day) - 1;
    //month variables
    $firstmonth = $month[0];
    $monthTotal = count($month) - 1;
    //year variables
    $firstyear = $year[0];
    $yearTotal = count($year) - 1;
    //symbol variables
    $symbols = count($symbol);
    //Loop through and download data for each symbol
    for ($i = 0; $i < count($symbol); $i++) {
        $handle = @fopen("http://ichart.yahoo.com/table.csv?s={$symbol[$i]}&a={$firstmonth}&b={$firstday}&c={$firstyear}&d={$month[$monthTotal]}&e={$day[$dayTotal]}&f={$year[$yearTotal]}&g=d&ignore=.csv", "r", false, $context);
        //if symbol does not exist
        if ($handle === false) {
            $notExist[] = $symbol[$i];
            $symbol[$i] = NULL;
            continue;
        }
        // download title of CSV file and throw away
        $data = fgetcsv($handle);
        //loop through data
        while (($data = fgetcsv($handle)) !== FALSE) {
            //date
            $date[$i][] = $data[0];
            //closing price
            $check[$i][] = round($data[4], 2);
        }
        //# of days
        $numberDays[] = count($date[$i]);
    }
    //min and max count of days
    $minDays = min($numberDays);
    $maxDays = max($numberDays);
    foreach ($date as $key => $value) {
        $checks[$date[$key]][] = $check[$key];
        //if only partial data on a stock ticker, set to null
        if (count($date[$key]) !== $maxDays) {
            $notThroughout[] = $symbol[$key];
            $check[$key] = NULL;
            $symbol[$key] = NULL;
        }
    }
    //update stock tickers
    $symbolUpdate = count(array_filter($symbol));
    //call function to sum arrays with identical keys
    if ($check !== NULL) {
        $reverse = array_mesh($check[0], $check[1], $check[2], $check[3], $check[4], $check[5], $check[6], $check[7], $check[8], $check[9], $check[10], $check[11], $check[12], $check[13], $check[14], $check[15], $check[16], $check[17], $check[18], $check[19], $check[20], $check[21], $check[22], $check[23], $check[24], $check[25], $check[26], $check[27], $check[28], $check[29], $check[30], $check[31], $check[32], $check[33], $check[34], $check[35], $check[36], $check[37], $check[38], $check[39], $check[40], $check[41], $check[42], $check[43], $check[44], $check[45], $check[46], $check[47], $check[48], $check[49], $check[50]);
    }
    $dailyAverage = array_reverse($reverse);
    //find average price for all stock from a given day
    foreach ($dailyAverage as &$value) {
        $value = $value / $symbolUpdate;
    }
    $initial = $dailyAverage[0];
    //calculate daily change
    for ($j = 0; $j < count($dailyAverage); $j++) {
        //change from intial average BDs
        $dailyChange[] = $dailyAverage[$j] / $initial * 100;
    }
    foreach ($dailyChange as $k => $value) {
        //used for st.dev calculations
        $difference[] = $dailyChange[$k];
        //inital 12 day average
        if ($k <= 11) {
            $sumarray[] = $dailyChange[$k];
            $intialTwelveDay = array_sum($sumarray) / 12;
            $twelveDayEm[11] = $intialTwelveDay;
        }
        //inital 20 day average
        if ($k <= 19) {
            $sumarray1[] = $dailyChange[$k];
            $intialTwentyDay = array_sum($sumarray1) / 20;
            $twentyDayEm[19] = $intialTwentyDay;
        }
        //inital 26 day average
        if ($k <= 25) {
            $sumarray2[] = $dailyChange[$k];
            $intialTwentySixDay = array_sum($sumarray2) / 26;
            $twentySixDayEm[25] = $intialTwentySixDay;
        }
        //12 day average
        if ($k > 11) {
            $twelveDayEm[] = $dailyChange[$k] * 2 / 13 + $twelveDayEm[$k - 1] * (1 - 2 / 13);
        }
        // 20 day average
        if ($k > 19) {
            $twentyDayEm[] = $dailyChange[$k] * 2 / 21 + $twentyDayEm[$k - 1] * (1 - 2 / 21);
        }
        //26 day average
        if ($k > 25) {
            $twentySixDayEm[] = $dailyChange[$k] * 2 / 27 + $twentySixDayEm[$k - 1] * (1 - 2 / 27);
        }
        //Bollinger Bands
        if ($k >= 19) {
            $lowerbb[$k] = $twentyDayEm[$k] - standard_deviation_population($difference) * 2;
            $upperbb[$k] = $twentyDayEm[$k] + standard_deviation_population($difference) * 2;
            $what[] = array_shift($difference);
        }
    }
    //Twenty Day Charts
    foreach ($twentyDayEm as $key => $value) {
        if ($key == $maxDays - 1) {
            $twentyDay .= "[" . $key . "," . round($value, 2) . "]";
        } else {
            $twentyDay .= "[" . $key . "," . round($value, 2) . "],";
        }
    }
    //Stock Index Chart
    foreach ($dailyChange as $key => $value) {
        if ($key == $maxDays - 1) {
            $stockIndex .= "[" . $key . "," . round($value, 2) . "]";
        } else {
            $stockIndex .= "[" . $key . "," . round($value, 2) . "],";
        }
    }
    //TwelveDay & Index Charts
    foreach ($dailyChange as $key => $value) {
        $key++;
        if ($key >= 11) {
            if ($key == $maxDays - 1) {
                $stockTwelve .= "[" . $key . "," . round($value, 2) . "," . round($twelveDayEm[$key], 2) . "]";
                break;
            } else {
                $stockTwelve .= "[" . $key . "," . round($value, 2) . "," . round($twelveDayEm[$key], 2) . "],";
            }
        }
    }
    //Bollinger Bands & Index Charts
    foreach ($dailyChange as $key => $value) {
        $key++;
        if ($key >= 19) {
            if ($key == $maxDays - 1) {
                $bb .= "[" . $key . "," . round($value, 2) . "," . round($lowerbb[$key], 2) . "," . round($upperbb[$key], 2) . "]";
                break;
            } else {
                $bb .= "[" . $key . "," . round($value, 2) . "," . round($lowerbb[$key], 2) . "," . round($upperbb[$key], 2) . "],";
            }
        }
    }
    //Twelve & TwentySixDay & Index Charts
    foreach ($dailyChange as $key => $value) {
        $key++;
        if ($key >= 25) {
            if ($key == $maxDays - 1) {
                $stockTwelveTwentySix .= "[" . $key . "," . round($value, 2) . "," . round($twelveDayEm[$key], 2) . "," . round($twentySixDayEm[$key], 2) . "]";
                break;
            } else {
                $stockTwelveTwentySix .= "[" . $key . "," . round($value, 2) . "," . round($twelveDayEm[$key], 2) . "," . round($twentySixDayEm[$key], 2) . "],";
            }
        }
    }
    if ($notExist !== NULL) {
        if ($notExist[0] === 0) {
            $notExist == NULL;
        } else {
            if (count($notExist) == 1) {
                $notExist = implode($notExist);
            } else {
                $notExist = implode(",", $notExist);
                $notExist = rtrim($notExist, ",");
            }
        }
    }
    if ($notThroughout !== NULL) {
        if (count($notThroughout) == 1) {
            $notThroughout = implode($notThroughout);
        } else {
            $notThroughout = implode(",", $notThroughout);
            $notThroughout = rtrim($notThroughout, ",");
        }
    }
    //var_dump($notThroughout);
    return ["bb" => $bb, "stockTwelveTwentySix" => $stockTwelveTwentySix, "stockTwelve" => $stockTwelve, "twentyDay" => $twentyDay, "stockIndex" => $stockIndex, "notExist" => $notExist, "notThroughout" => $notThroughout];
    // close connection to Yahoo
    fclose($handle);
}