function download_stock_data_history_by_symbol($stock_symbol) { $year = date('Y'); $day = intval(date('d')); $month = intval(date('m')) - 1; $historical_stock_data_link_format = 'http://real-chart.finance.yahoo.com/table.csv?s=%s&a=%s&b=%s&c=2013&d=%s&e=%s&f=%s&g=d&ignore=.csv'; $link = sprintf($historical_stock_data_link_format, $stock_symbol, $day, $month, $day, $month, $year); $historical_stock_data = file_get_contents($link); // Header row of file // Date,Open,High,Low,Close,Volume,Adj Close,Symbol $symbol_included_historical_stock_data = str_replace("\n", ",{$stock_symbol}\n", $historical_stock_data); $historical_stock_data_directory = get_data_subdirectory('historical_data'); ensure_directory_exists($historical_stock_data_directory); $historical_stock_data_filename = $stock_symbol . '.csv'; $filepath = $historical_stock_data_directory . $historical_stock_data_filename; file_put_contents($filepath, $symbol_included_historical_stock_data); }
/** * Created by PhpStorm. * User: cloudera * Date: 1/19/16 * Time: 3:04 PM */ function get_price_delta_since_date($since_date, $symbol) { $file_path = get_data_subdirectory('historical_data') . '/' . $symbol . '.csv'; $stock_data = file_get_contents($file_path); $stock_data_as_array = explode("\n", $stock_data); $latest_date_data = $stock_data_as_array[1]; $latest_stock_day_data = explode(',', $latest_date_data); $latest_price = $latest_stock_day_data[4]; $since_price = 0; foreach ($stock_data_as_array as $stock_day_data_as_string) { $stock_day_data = explode(',', $stock_day_data_as_string); $date = $stock_day_data[0]; if ($date == $since_date) { $since_price = $stock_day_data[4]; break; } } return floatval($latest_price) - floatval($since_price); }
<?php include './utility.php'; ensure_data_directory_exists(); $nyse_download_link = "http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NYSE&render=download"; $nasdaq_download_link = "http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NASDAQ&render=download"; $listings_dir = get_data_subdirectory('listings'); ensure_directory_exists($listings_dir); $data = file_get_contents($nyse_download_link); file_put_contents($listings_dir . "nyse.csv", $data); $data = file_get_contents($nasdaq_download_link); file_put_contents($listings_dir . "nasdaq.csv", $data);