functions.inc.php

来自「股票监视器是一个简单的实用工具」· PHP 代码 · 共 177 行

PHP
177
字号
<?php/** PHP STOCK TRACKER* -----------------* A multi-user utility for creating and managing stock* watchlists and historical analysis.* Copyright (C) 2005 Joshua Eldridge (joshuae74@hotmail.com)*** This program is free software; you can redistribute it and/or modify* it under the terms of the GNU General Public License as published by* the Free Software Foundation; either version 2 of the License, or* (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA** See the COPYING file for full details.*//* Function Name: get_batch_yahooArguments: symbols_array (should be saved in the users session and contain all the tickersymbols of the stocks contained in their watch list)Returns: An array of current stock information as retrieved from Yahoo!This includes: last trade price, opening price, percent change, day's low and high prices,volume*/function get_batch_yahoo($symbols_array) {/* Yahoo limits to 200 quotes at a time, that's why we chunk the array and   iterate through it */	$multi_array = array_chunk($symbols_array, 200);	foreach($multi_array as $elements) {		$string = implode("&s=", $elements);		$fp = @fopen("http://quote.yahoo.com/d/quotes.csv?s=" . $string . "&f=sl1d1t1c1ohgv&e=.csv", "r");		while (($i = fgetcsv($fp, 1000, ",")) != FALSE) {			if(count($i) > 0) {				$data[] = $i;			}		}		fclose($fp);	}	return $data;}function get_users() {	global $conn;    $sql = 'SELECT id, name, email FROM stocks_users';    $users = $conn->GetAssoc($sql);    return $users;}function check_history($symbol) {	global $conn;	$sql = 'SELECT count(*) FROM stocks_data a, stocks_info b WHERE a.stock_id = b.stock_id AND b.stock_symbol = "' . $symbol . '"';    return $conn->GetOne($sql);}/* Check the watchlists table to see if other usersare watching a stock that is being deleted by someone else.If not, delete the stock -- otherwise keep it*/function check_watchlists($stock_id) {	global $conn;	$sql = 'SELECT count(*) FROM stocks_watchlists WHERE stock_id = ' . $stock_id;    $count = $conn->GetOne($sql);	return $count;}/* Function Name: get_stocks_listArguments: user_id (should be saved in the users session) If the user id isn't passedinto the function, it will pull all the unique stock_ids in all watch lists. This is used wheninitially seeding the database.Returns: An array of stock id, stock symbol and stock full name for everything onthe watch list of the logged in user*/function get_stocks_list($user_id = -1) {    global $conn;    if($user_id == -1) {      $sql = 'SELECT a.stock_id, a.stock_symbol, a.stock_name, a.stock_exchange, a.stock_date_added FROM stocks_info a, stocks_watchlists b WHERE a.stock_id = b.stock_id ORDER BY a.stock_symbol';    } else {      $sql = 'SELECT a.stock_id, a.stock_symbol, a.stock_name, a.stock_exchange, a.stock_date_added FROM stocks_info a, stocks_watchlists b	WHERE a.stock_id=b.stock_id AND b.user_id=' . $user_id .' ORDER BY a.stock_symbol';    }    $stocks = $conn->GetAll($sql);    return $stocks;}/*Function Name: get_batch_highArguments: NoneReturns: A single dimensional array of all the stocks that arebeing watched by all users. This function is used by the cron jobso it knows what to load every night.*/function get_all_stock_ids() {    global $conn;    $sql = 'SELECT stock_id FROM stocks_watchlists GROUP by stock_id';    $data = $conn->GetCol($sql);    return $data;}/* Function Name: get_batch_highArguments: ids_array (should be saved in the users session -- an array of the stock ids contained in the users watchlist), flush directive to renew cached queryReturns: An array of stock id, stock symbol and stock full name for everything onthe watch list of the logged in user*/function get_batch_high($ids_array, $flush = false) {	global $conn;	$date = date("Y-n-j", strtotime("-1 year"));	$ids_string = '(' . implode(",",$ids_array) . ')';	$sql = "SELECT b.stock_symbol, max(a.last_trade), min(a.last_trade) FROM stocks_data a, stocks_info b WHERE b.stock_id IN " . $ids_string . " AND a.trade_date > '" . $date . "' AND a.stock_id=b.stock_id GROUP BY a.stock_id, b.stock_symbol";	if($flush === true) {		$conn->CacheFlush($sql);	}	$time = 3600*24; // cache 24 hours	$highs = cacheQuery($sql, $time);	return $highs;}function get_historical_data($symbol) {  // This will return all the data for a symbol from Jan 1, 2004  $data = array();  $date = getdate();  $month = $date['month'] - 1;  $day = $date['mday'];  if( $fp = @fopen("http://ichart.finance.yahoo.com/table.csv?s=" . $symbol . "&a=00&b=01&c=2004&d=" . $month . "&e=" . $day . "&f=2004&g=d&ignore=.csv", "r")) {    while (($i = fgetcsv($fp, 10000, ",")) != FALSE) {      if(count($i) > 1) {	$data[] = $i;      }	       }    fclose($fp);  }  return $data;}function get_rolling_dates($stock_id, $end_date = -1, $start_date = -1, $order = 'DESC') {	global $conn;	global $ADODB_FETCH_MODE;	// Save The Current Fetch Mode	$SAVE_MODE = $ADODB_FETCH_MODE;	// Temporarily Set The Fetch Mode To NUM	$ADODB_FETCH_MODE = ADODB_FETCH_NUM;		if($end_date == -1) {		$end_date = date('Y-n-j');	}	if($start_date == -1) {		$start_date  = date('Y-n-j', strtotime('-1 month', strtotime($end_date)));	}	    $result = $conn->GetAll("SELECT trade_date, last_trade FROM stocks_data WHERE stock_id = " . $stock_id . " AND trade_date BETWEEN '" . $start_date . "' AND '" . $end_date . "' ORDER BY trade_date " . $order);	// Reset The Old Fetch Mode	$ADODB_FETCH_MODE = $SAVE_MODE;	return $result;}function microtime_float() {   list($usec, $sec) = explode(' ', microtime());   return ((float)$usec + (float)$sec);}?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?