function refresh_realtime_quotes($s)
{
    global $realtime_quote;
    $allstocks = array();
    // if no symbol supplied, update eod price for all stocks in stock_list
    if (!$s) {
        $query = "SELECT symbol FROM stock_list order by symbol asc ";
        $query = "SELECT name as symbol from trade_db.instrument order by name asc ";
        $result = queryMysql($query);
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            //$return = $row['symbol']."+".$return;
            $str = preg_replace('/\\s+/', '', $row['symbol']);
            array_push($allstocks, $str);
        }
    } else {
        array_push($allstocks, $s);
    }
    $loop_count = 0;
    $batch_stocks = array();
    $batch_list = "";
    for ($x = 0; $x < count($allstocks); $x++) {
        #		for ($x=0; $x<50; $x++) {
        if ($loop_count < 49) {
            array_push($batch_stocks, $allstocks[$x]);
            $batch_list .= $allstocks[$x] . "+";
            $loop_count++;
        } else {
            $allquotes = json_decode(get_yahoo_quote($batch_list), true);
            /*		         
            		         $execute_price = $rt_quotes[0]['open'];
            		         $execute_date = $rt_quotes[0]['last trade date'];
            		         $execute_time = $execute_date." ".$rt_quotes[0]['last trade time'];
            		         $shares_executed = $value['shares'];
            		         $cash_change = -1 * $value['shares'] * $execute_price;		
            */
            for ($i = 0; $i < count($allquotes); $i++) {
                echo "symbol: ", $allquotes[$i]['symbol'], " price:", $allquotes[$i]['last price'], " last trade date: ", $allquotes[$i]['last trade date'], " time: ", $allquotes[$i]['last trade time'], " open:", $allquotes[$i]['last price'], " high: ", $allquotes[$i]['high'], " low: ", $allquotes[$i]['low'], " change: ", $allquotes[$i]['change'], " change in percent: ", $allquotes[$i]['change in percent'], " open: ", $allquotes[$i]['open'], PHP_EOL;
            }
            ## batch insert into crsi_portfolio_performance afterward
            $sql = array();
            foreach ($allquotes as $row) {
                #$sql[] = "('".$allquotes[$i]['symbol']."', '".$allquotes[$i]['last trade date']."', ".$allquotes[$i]['change in percent'].", ".$allquotes[$i]['change'].", ".$allquotes[$i]['high'].", ".$allquotes[$i]['low'].", ". $allquotes[$i]['open'].")";
                $change_in_percent = 1 * $row['change in percent'];
                $daily_change = 1 * $row['change'];
                $sql[] = "('" . $row['symbol'] . "', " . $row['last price'] . ", " . $change_in_percent . ", " . $daily_change . ", " . $row['high'] . ", " . $row['low'] . ", " . $row['open'] . ")";
                $query = "INSERT INTO " . $realtime_quote . " (symbol, last_trade, change_percent, daily_change, high, low, open, last_trade_date, last_trade_time) VALUES (\n\t\t\t\t\t\t\t  '" . $row['symbol'] . "',\n\t\t\t\t\t\t\t  " . $row['last price'] . ", \n\t\t\t\t\t\t\t  " . $change_in_percent . ",\n\t\t\t\t\t\t\t  " . $daily_change . ",\n\t\t\t\t\t\t\t  " . $row['high'] . ",\n\t\t\t\t\t\t\t  " . $row['low'] . ",\n\t\t\t\t\t\t\t  " . $row['open'] . ",\n\t\t\t\t\t\t\t  '" . $row['last trade date'] . "',\n\t\t\t\t\t\t\t  '" . $row['last trade time'] . "' )\n\t\t\t\t\t\t\t  on duplicate key update \n\t\t\t\t\t\t\t  last_trade = " . $row['last price'] . ",\n\t\t\t\t\t\t\t  change_percent = " . $change_in_percent . ",\n\t\t\t\t\t\t\t  daily_change = " . $daily_change . ",\n\t\t\t\t\t\t\t  high = " . $row['high'] . ",\n\t\t\t\t\t\t\t  low = " . $row['low'] . ",\n\t\t\t\t\t\t\t  open = " . $row['open'] . ",\n\t\t\t\t\t\t\t  last_trade_date = '" . $row['last trade date'] . "', \n\t\t\t\t\t\t\t  last_trade_time = '" . $row['last trade time'] . "'\n\t\t\t\t\t\t\t  \n\t\t\t\t\t\t\t  ";
                //echo $query, PHP_EOL;
                queryMysql($query);
            }
            //var_dump($sql);
            //global $dbname;
            //mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
            //mysql_connect("localhost", "root", "") or die(mysql_error());
            //mysql_select_db($dbname) or die (mysql_error());
            //queryMysql ("INSERT INTO realtime_quote (symbol) VALUES ('jimmy')");
            //queryMysql('INSERT INTO '.$realtime_quote.' (symbol, last_trade, change_percent, daily_change, high, low, open) VALUES '.implode(',', $sql));
            //print 'INSERT INTO realtime_quote (symbol, last_trade, change_percent, daily_change, high, low, open) VALUES '.implode(',', $sql);
            /*
            	try {
            		queryMysql('INSERT INTO realtime_quote (symbol, last_trade, change_percent, daily_change, high, low, open) VALUES '.implode(',', $sql));
            		print 'INSERT INTO realtime_quote (symbol, last_trade, change_percent, daily_change, high, low, open) VALUES '.implode(',', $sql);
            	} catch (SQLException $e) {
            		echo "Query: $query \n";
            		echo 'Caught exception: ',  $e->getMessage(), "\n";
            	}
            */
            /*symbol         | varchar(6)  | NO   | PRI | NULL    |       |
            | last_trade     | double      | NO   |     | NULL    |       |
            | bid            | double      | NO   |     | NULL    |       |
            | ask            | double      | NO   |     | NULL    |       |
            | change_percent | double      | NO   |     | NULL    |       |
            | daily_change   | double      | NO   |     | NULL    |       |
            | market_cap     | varchar(10) | NO   |     | NULL    |       |
            | high           | double      | YES  |     | NULL    |       |
            | low            | double      | YES  |     | NULL    |       |
            | open           | double      | YES  |     | NULL    |       |
            */
            $loop_count = 0;
            $batch_stocks = array();
            $batch_list = "";
        }
    }
    /*		$result = queryMysql($query);
    		$return;
    		
    		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    			$return = $row['symbol']."+".$return;
    		}
    		
    		return $return;
    */
}
Esempio n. 2
0
function refresh_realtime_quotes($s)
{
    global $realtime_quote;
    $allstocks = array();
    // if no symbol supplied, update eod price for all stocks in stock_list
    if (!$s) {
        $query = "SELECT symbol FROM stock_list order by symbol asc ";
        $query = "SELECT name as symbol from trade_db.instrument order by name asc ";
        $result = queryMysql($query);
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            //$return = $row['symbol']."+".$return;
            $str = preg_replace('/\\s+/', '', $row['symbol']);
            array_push($allstocks, $str);
        }
    } else {
        array_push($allstocks, $s);
    }
    $loop_count = 0;
    $batch_stocks = array();
    $batch_list = "";
    for ($x = 0; $x < count($allstocks); $x++) {
        #for ($x=0; $x<50; $x++) {
        if ($loop_count < 49) {
            array_push($batch_stocks, $allstocks[$x]);
            $batch_list .= $allstocks[$x] . "+";
            $loop_count++;
        }
        //else {
        if ($loop_count = 49 || $x == count($allstocks) - 1) {
            $allquotes = json_decode(get_yahoo_quote($batch_list), true);
            //var_dump($allquotes);
            ## batch insert into crsi_portfolio_performance afterward
            $sql = array();
            foreach ($allquotes as $row) {
                $change_in_percent = 1 * $row['change in percent'];
                $daily_change = 1 * $row['change'];
                $sql[] = "('" . $row['symbol'] . "', " . $row['last price'] . ", " . $change_in_percent . ", " . $daily_change . ", " . $row['high'] . ", " . $row['low'] . ", " . $row['open'] . ")";
                $query = "INSERT INTO " . $realtime_quote . " (symbol, last_trade, change_percent, daily_change, high, low, open, last_trade_date, last_trade_time) VALUES (\n\t\t\t\t\t\t\t  '" . $row['symbol'] . "',\n\t\t\t\t\t\t\t  " . $row['last price'] . ", \n\t\t\t\t\t\t\t  " . $change_in_percent . ",\n\t\t\t\t\t\t\t  " . $daily_change . ",\n\t\t\t\t\t\t\t  " . $row['high'] . ",\n\t\t\t\t\t\t\t  " . $row['low'] . ",\n\t\t\t\t\t\t\t  " . $row['open'] . ",\n\t\t\t\t\t\t\t  '" . $row['last trade date'] . "',\n\t\t\t\t\t\t\t  '" . $row['last trade time'] . "' )\n\t\t\t\t\t\t\t  on duplicate key update \n\t\t\t\t\t\t\t  last_trade = " . $row['last price'] . ",\n\t\t\t\t\t\t\t  change_percent = " . $change_in_percent . ",\n\t\t\t\t\t\t\t  daily_change = " . $daily_change . ",\n\t\t\t\t\t\t\t  high = " . $row['high'] . ",\n\t\t\t\t\t\t\t  low = " . $row['low'] . ",\n\t\t\t\t\t\t\t  open = " . $row['open'] . ",\n\t\t\t\t\t\t\t  last_trade_date = '" . $row['last trade date'] . "', \n\t\t\t\t\t\t\t  last_trade_time = '" . $row['last trade time'] . "'\n\t\t\t\t\t\t\t  \n\t\t\t\t\t\t\t  ";
                #echo $query, PHP_EOL;
                queryMysql($query);
                $update_quotes = "INSERT INTO quotes (symbol, trade_date, close, pct_change, daily_change, high, low, open, volume, adj_close) VALUES (\n\t\t\t\t\t\t\t  '" . $row['symbol'] . "',\n\t\t\t\t\t\t\t  '" . $row['last trade date'] . "',\n\t\t\t\t\t\t\t  " . $row['last price'] . ", \n\t\t\t\t\t\t\t  " . $change_in_percent . ",\n\t\t\t\t\t\t\t  " . $daily_change . ",\n\t\t\t\t\t\t\t  " . $row['high'] . ",\n\t\t\t\t\t\t\t  " . $row['low'] . ",\n\t\t\t\t\t\t\t  " . $row['open'] . ",\t\t\t\t\t\t\t  \n\t\t\t\t\t\t\t  " . $row['volume'] . ",\n\t\t\t\t\t\t\t  " . $row['last price'] . " )\n\t\t\t\t\t\t\t  on duplicate key update \n\t\t\t\t\t\t\t  close = " . $row['last price'] . ",\n\t\t\t\t\t\t\t  pct_change = " . $change_in_percent . ",\n\t\t\t\t\t\t\t  daily_change = " . $daily_change . ",\n\t\t\t\t\t\t\t  high = " . $row['high'] . ",\n\t\t\t\t\t\t\t  low = " . $row['low'] . ",\n\t\t\t\t\t\t\t  open = " . $row['open'] . ",\n\t\t\t\t\t\t\t  volume = " . $row['volume'] . ",\n\t\t\t\t\t\t\t  adj_close = " . $row['last price'] . "\n\n\t\t\t\t\t\t\t  \n\t\t\t\t\t\t\t  \n\t\t\t\t\t\t\t  ";
                #echo $update_quotes, PHP_EOL;
                queryMysql($update_quotes);
                //					echo "update quotes: ", $update_quotes, PHP_EOL;
            }
            $loop_count = 0;
            $batch_stocks = array();
            $batch_list = "";
        }
    }
    /*		$result = queryMysql($query);
    		$return;
    		
    		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    			$return = $row['symbol']."+".$return;
    		}
    		
    		return $return;
    */
}