private static function insertMetricRowsForNewStocks() { $stockCodes = Stock::lists('stock_code'); foreach ($stockCodes as $stockCode) { StockMetrics::updateOrCreate(['stock_code' => $stockCode], []); } }
/** * Execute the console command. * * @return mixed */ public function handle() { $this->info("This is involves downloading and storing several million records. This may take several hours..."); if ($this->confirm('Do you wish to continue?')) { $this->info("Downloading historical financials..."); $numberOfStocks = Stock::count(); foreach (Stock::lists('stock_code') as $key => $stockCode) { if (!Historicals::where(['stock_code' => $stockCode, 'date' => '2015-08-11'])->first()) { $historicalSheetUrl = "http://real-chart.finance.yahoo.com/table.csv?s=" . $stockCode . ".AX&d=7&e=12&f=2015&g=d&a=7&b=11&c=2015&ignore=.csv"; if (get_headers($historicalSheetUrl, 1)[0] == 'HTTP/1.1 200 OK') { file_put_contents('database/files/spreadsheet.txt', trim(str_replace("Date,Open,High,Low,Close,Volume,Adj Close", "", file_get_contents($historicalSheetUrl)))); $spreadSheetFile = fopen('database/files/spreadsheet.txt', 'r'); $dailyTradeRecord = array(); while (!feof($spreadSheetFile)) { $line = fgets($spreadSheetFile); $pieces = explode(',', $line); array_push($dailyTradeRecord, array('stock_code' => $stockCode, 'date' => $pieces[0], 'open' => $pieces[1], 'high' => $pieces[2], 'low' => $pieces[3], 'close' => $pieces[4], 'volume' => $pieces[5], 'adj_close' => $pieces[6], 'created_at' => date("Y-m-d H:i:s"), 'updated_at' => date("Y-m-d H:i:s"))); } //\DB::table('historicals')->where('stock_code', $stockCode)->delete(); \DB::table('historicals')->insert($dailyTradeRecord); } } $this->info("Completed: " . $stockCode . " " . ($key + 1) . "/" . $numberOfStocks . " - " . round(($key + 1) * (100 / $numberOfStocks), 2) . "%"); } $this->info("The historical financials have been downloaded."); } }
/** * Execute the console command. * * @return mixed */ public function handle() { $this->info("Updating " . $this->option('mode') . " sector metrics. This may take several minutes..."); $listOfSectors = Stock::getListOfSectors(); array_push($listOfSectors, 'All'); if ($this->option('testMode') == 'true') { $this->info("Running in test mode. Only Banks and Telcos will be updated."); $listOfSectors = ['Banks', 'Telecommunication Services']; } foreach ($listOfSectors as $sectorName) { $stocksInSector = Stock::where('sector', $sectorName)->lists('stock_code'); if ($sectorName == 'All') { $stocksInSector = Stock::lists('stock_code'); } if (count($stocksInSector) > 0) { $totalSectorMarketCap = SectorHistoricals::getTotalSectorMarketCap($stocksInSector); if (isTradingDay()) { SectorHistoricals::updateOrCreate(['sector' => $sectorName, 'date' => date("Y-m-d")], ['sector' => $sectorName, 'date' => date("Y-m-d"), 'total_sector_market_cap' => $totalSectorMarketCap, 'day_change' => round(SectorHistoricals::getSectorPercentChange($sectorName, $stocksInSector), 2), 'average_sector_market_cap' => $totalSectorMarketCap / count($stocksInSector)]); if ($this->option('mode') == 'full') { foreach ($this->sectorMetrics as $metricName) { SectorHistoricals::updateOrCreate(['sector' => $sectorName, 'date' => date("Y-m-d")], [$metricName => round(StockMetrics::getAverageMetric($metricName, $stocksInSector, $sectorName), 2)]); } } } } } }
/** * Execute the console command. * * @return mixed */ public function handle() { if ($this->option('testMode') == 'true') { $this->info("[Test Mode]"); foreach (['TLS', 'CBA'] as $stockCode) { $priceOneWeekAgo = CalculateStockChangeCommand::getPriceAtDate($stockCode, getDateFromCarbonDate(Carbon::now()->subWeek())); $lastTrade = StockMetrics::where('stock_code', $stockCode)->pluck('last_trade'); $weekChange = CalculateStockChangeCommand::getPercentChange($lastTrade, $priceOneWeekAgo); StockGains::updateOrCreate(['stock_code' => $stockCode], ['stock_code' => $stockCode, 'week_change' => $weekChange, 'updated_at' => date("Y-m-d H:i:s")]); } } else { $this->info("Calculating the stock changes... This may take several minutes."); $stockCodes = Stock::lists('stock_code'); $numberOfStocks = Stock::count(); //For Calculation of YTD gain $firstTradingDateOfYear = CalculateStockChangeCommand::getFirstTradingDateOfYear(); foreach ($stockCodes as $key => $stockCode) { $lastTrade = StockMetrics::where('stock_code', $stockCode)->pluck('last_trade'); $priceOneWeekAgo = CalculateStockChangeCommand::getPriceAtDate($stockCode, getDateFromCarbonDate(Carbon::now()->subWeek())); $weekChange = CalculateStockChangeCommand::getPercentChange($lastTrade, $priceOneWeekAgo); $priceTwoWeeksAgo = CalculateStockChangeCommand::getPriceAtDate($stockCode, getDateFromCarbonDate(Carbon::now()->subWeeks(2))); $twoWeekChange = CalculateStockChangeCommand::getPercentChange($lastTrade, $priceTwoWeeksAgo); $priceOneMonthAgo = CalculateStockChangeCommand::getPriceAtDate($stockCode, getDateFromCarbonDate(Carbon::now()->subMonth())); $oneMonthChange = CalculateStockChangeCommand::getPercentChange($lastTrade, $priceOneMonthAgo); $priceTwoMonthsAgo = CalculateStockChangeCommand::getPriceAtDate($stockCode, getDateFromCarbonDate(Carbon::now()->subMonths(2))); $twoMonthChange = CalculateStockChangeCommand::getPercentChange($lastTrade, $priceTwoMonthsAgo); $priceThreeMonthsAgo = CalculateStockChangeCommand::getPriceAtDate($stockCode, getDateFromCarbonDate(Carbon::now()->subMonths(3))); $threeMonthChange = CalculateStockChangeCommand::getPercentChange($lastTrade, $priceThreeMonthsAgo); $priceSixMonthsAgo = CalculateStockChangeCommand::getPriceAtDate($stockCode, getDateFromCarbonDate(Carbon::now()->subMonths(6))); $sixMonthChange = CalculateStockChangeCommand::getPercentChange($lastTrade, $priceSixMonthsAgo); $priceAtStartOfYear = CalculateStockChangeCommand::getPriceAtDate($stockCode, $firstTradingDateOfYear); $thisYearChange = CalculateStockChangeCommand::getPercentChange($lastTrade, $priceAtStartOfYear); $priceOneYearAgo = CalculateStockChangeCommand::getPriceAtDate($stockCode, getDateFromCarbonDate(Carbon::now()->subYear())); $oneYearChange = CalculateStockChangeCommand::getPercentChange($lastTrade, $priceOneYearAgo); $priceTwoYearsAgo = CalculateStockChangeCommand::getPriceAtDate($stockCode, getDateFromCarbonDate(Carbon::now()->subYears(2))); $twoYearChange = CalculateStockChangeCommand::getPercentChange($lastTrade, $priceTwoYearsAgo); $priceThreeYearsAgo = CalculateStockChangeCommand::getPriceAtDate($stockCode, getDateFromCarbonDate(Carbon::now()->subYears(3))); $threeYearChange = CalculateStockChangeCommand::getPercentChange($lastTrade, $priceThreeYearsAgo); $priceFiveYearsAgo = CalculateStockChangeCommand::getPriceAtDate($stockCode, getDateFromCarbonDate(Carbon::now()->subYears(5))); $fiveYearChange = CalculateStockChangeCommand::getPercentChange($lastTrade, $priceFiveYearsAgo); $priceTenYearsAgo = CalculateStockChangeCommand::getPriceAtDate($stockCode, getDateFromCarbonDate(Carbon::now()->subYears(10))); $tenYearChange = CalculateStockChangeCommand::getPercentChange($lastTrade, $priceTenYearsAgo); if (Historicals::where('stock_code', $stockCode)->first()) { $oldestDate = Historicals::where('stock_code', $stockCode)->orderBy('date', 'asc')->take(1)->lists('date'); $oldestPriceAvailable = CalculateStockChangeCommand::getPriceAtDate($stockCode, substr($oldestDate, 2, -2)); $allTimeChange = CalculateStockChangeCommand::getPercentChange($lastTrade, $oldestPriceAvailable); } else { $allTimeChange = 0; } StockGains::updateOrCreate(['stock_code' => $stockCode], ['stock_code' => $stockCode, 'week_change' => $weekChange, 'two_week_change' => $twoWeekChange, 'month_change' => $oneMonthChange, 'two_month_change' => $twoMonthChange, 'three_month_change' => $threeMonthChange, 'six_month_change' => $sixMonthChange, 'this_year_change' => $thisYearChange, 'year_change' => $oneYearChange, 'two_year_change' => $twoYearChange, 'three_year_change' => $threeYearChange, 'five_year_change' => $fiveYearChange, 'ten_year_change' => $tenYearChange, 'all_time_change' => $allTimeChange, 'updated_at' => date("Y-m-d H:i:s")]); $this->info("Completed: " . $stockCode . " " . ($key + 1) . "/" . $numberOfStocks . " - " . round(($key + 1) * (100 / $numberOfStocks), 2) . "%"); } } }
/** * Execute the console command. * * @return mixed */ public function handle() { $providedDate = $this->ask("Which date would you like to get the sector historicals for? ('yyyy-mm-dd')"); $historicalsAtDate = Historicals::where('date', $providedDate)->get(); $sectors = \DB::table('stocks')->select(\DB::raw('DISTINCT sector'))->lists('sector'); foreach ($sectors as $sector) { $stocksInSector = Stock::where('sector', $sector)->lists('stock_code'); FillSectorHistoricalsCommand::calculateDayGain($stocksInSector, $sector, $providedDate); } $allStockCodes = Stock::lists('stock_code'); FillSectorHistoricalsCommand::calculateDayGain($allStockCodes, "All", $providedDate); $this->info("Sector historicals have been filled for: " . $providedDate); }
/** * Execute the console command. * * @return mixed */ public function handle() { $this->info("Locating and storing company summaries..."); $stocks = Stock::lists('stock_code'); $numberOfStocks = count($stocks); foreach ($stocks as $key => $stockCode) { //Check if there is already a summary if (Stock::where('stock_code', $stockCode)->pluck('business_summary') == null) { $pageContents = file_get_contents("https://au.finance.yahoo.com/q/pr?s=" . $stockCode . ".AX"); if (strpos($pageContents, "</th></tr></table><p>") && strpos($pageContents, '</p><p><a href="/q/ks?s=' . $stockCode . '.AX"><b>')) { $businessSummary = explode('</p><p><a href="/q/ks?s=' . $stockCode . '.AX"><b>', explode("</th></tr></table><p>", $pageContents)[1]); if (strlen($businessSummary[0]) > 0) { $stock = Stock::where('stock_code', $stockCode)->first(); $stock->business_summary = $businessSummary[0]; $stock->save(); } } } $this->info("Completed: " . $stockCode . " " . ($key + 1) . "/" . $numberOfStocks . " - " . round(($key + 1) * (100 / $numberOfStocks), 2) . "%"); } }
public function index(SearchRequest $request) { $stocks = StockMetrics::getMetricsByStockList(Stock::lists('stock_code'), "omit"); return view('pages.stocks')->with(['stockSectors' => Stock::getSectorDropdown(), 'stocks' => $stocks, 'stockSectorName' => $request->stockSector]); }