public function sectorCapsPieChart($numberOfSectors)
 {
     $graphData = SectorHistoricals::getAllSectorGraphData($numberOfSectors);
     $sectorCaps = \Lava::DataTable();
     $sectorCaps->addStringColumn('Sector Name')->addNumberColumn('Percent')->addRows($graphData);
     return $sectorCaps->toJson();
 }
 /**
  * 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)]);
                     }
                 }
             }
         }
     }
 }
 public function testSectorGraph()
 {
     $mostRecentDateInGraph = SectorHistoricals::getMostRecentSectorHistoricalsDate();
     $mostRecentMonth = jdmonthname(explode("-", $mostRecentDateInGraph)[1], 2);
     $mostRecentYear = explode("-", $mostRecentDateInGraph)[0];
     $this->visit('/sectorGraph/Banks/last_month/Market%20Cap')->see('Date')->see('Market Cap')->see($mostRecentMonth)->see($mostRecentYear);
     $this->visit('/sectorGraph/Banks/last_6_months/Market%20Cap')->see('Date')->see('Market Cap')->see($mostRecentMonth)->see($mostRecentYear);
 }
 public function show(SearchRequest $request)
 {
     $stocks = StockMetrics::getMetricsByStockList($this->search->getSearchResults($request), $request->omitCondition);
     if ($request->viewType == 'partial') {
         if ($request->section == 'sectorDayGain' || $request->section == 'sectorDayLoss') {
             return view('layouts.partials.sector-day-change-display')->with(['sectorChanges' => SectorHistoricals::getSectorDayChanges($request->section), 'title' => SectorHistoricals::getSectorDayChangeTitle($request->section)]);
         }
         return view('layouts.partials.stock-list-display')->with(['stocks' => $stocks]);
     }
     return view('pages.stocks')->with(['stocks' => $stocks, 'stockSectors' => Stock::getSectorDropdown(), 'stockSectorName' => $request->stockSector]);
 }
 public function index()
 {
     //Line graph for market cap
     $totalMarketCapGraphData = SectorHistoricals::getIndividualSectorGraphData('All', 'last_month', 'Market Cap');
     $marketCaps = \Lava::DataTable();
     $marketCaps->addStringColumn('Date')->addNumberColumn('Market Cap')->addRows($totalMarketCapGraphData);
     $marketCapsLava = \Lava::AreaChart('MarketCaps')->dataTable($marketCaps)->setOptions(['width' => 725, 'height' => 360, 'title' => 'ASX Market Cap (Billions)']);
     //PieChart for Sectors' Market Caps
     $individualSectorCapsGraphData = SectorHistoricals::getAllSectorGraphData('top_5');
     $sectorCaps = \Lava::DataTable();
     $sectorCaps->addStringColumn('Sector Name')->addNumberColumn('Percent')->addRows($individualSectorCapsGraphData);
     $sectorCapsLava = \Lava::PieChart('Sectors')->dataTable($sectorCaps)->customize(['tooltip' => ['text' => 'percentage']])->setOptions(['width' => 725, 'height' => 360, 'title' => 'Sector Caps (Billions)', 'pieSliceText' => 'label']);
     return view('pages.home')->with(['marketCapsLava' => $marketCapsLava, 'sectorDayGains' => SectorHistoricals::getSectorDayChanges('sectorDayGain'), 'sectorDayLosses' => SectorHistoricals::getSectorDayChanges('sectorDayLoss'), 'sectorDayGainTitle' => SectorHistoricals::getSectorDayChangeTitle('sectorDayGain'), 'sectorDayLossTitle' => SectorHistoricals::getSectorDayChangeTitle('sectorDayLoss')]);
 }
 /**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function show(Request $request, $id)
 {
     if ($request->stockCodeFind) {
         return redirect('stocks/' . $request->stockCodeFind);
     }
     if (Stock::where('stock_code', $id)->first()) {
         $priceGraphData = Stock::getGraphData($id, 'last_month', 'Price');
         $prices = \Lava::DataTable();
         $prices->addStringColumn('Date')->addNumberColumn('Price')->addNumberColumn('50 Day Moving Average')->addNumberColumn('200 Day Moving Average')->addRows($priceGraphData);
         $stockPriceLava = \Lava::LineChart('StockPrice')->dataTable($prices)->customize(['explorer' => ['actions' => ['dragToZoom', 'rightClickToReset']]])->setOptions(['width' => 620, 'height' => 360, 'title' => 'Price of ' . strtoupper($id)]);
         $sector = Stock::where('stock_code', $id)->pluck('sector');
         $motRecentSectorHistoricalsDate = SectorHistoricals::getMostRecentSectorHistoricalsDate();
         return view('pages.individualstock')->with(['stockPriceLava' => $stockPriceLava, 'stock' => Stock::where('stock_code', $id)->first(), 'relatedStocks' => StockMetrics::getMetricsByStockList(Stock::getRelatedStocks($id), 'omit'), 'metrics' => StockMetrics::where('stock_code', $id)->first(), 'mostRecentStockHistoricals' => Historicals::where('stock_code', $id)->orderBy('date', 'DESC')->limit(1)->first(), 'sectorAverage' => SectorHistoricals::where(['sector' => $sector, 'date' => $motRecentSectorHistoricalsDate])->first()]);
     }
     return redirect('/');
 }
 private static function calculateDayGain($listOfStocks, $sectorName, $providedDate)
 {
     $marketCaps = array();
     $marketCapDayChanges = array();
     foreach ($listOfStocks as $stock) {
         $marketCap = StockMetrics::where('stock_code', $stock)->pluck('market_cap');
         $open = Historicals::where('stock_code', $stock)->where('date', $providedDate)->pluck('open');
         $close = Historicals::where('stock_code', $stock)->where('date', $providedDate)->pluck('close');
         $dayChange = FillSectorHistoricalsCommand::getDayChange($open, $close);
         array_push($marketCaps, $marketCap);
         array_push($marketCapDayChanges, $marketCap - $marketCap / ($dayChange / 100 + 1));
     }
     $totalSectorMarketCaps = array_sum($marketCaps);
     $totalSectorDayChange = array_sum($marketCapDayChanges);
     if ($totalSectorMarketCaps > 0) {
         $percentChange = 100 / $totalSectorMarketCaps * $totalSectorDayChange;
     } else {
         $percentChange = 0;
     }
     SectorHistoricals::updateOrCreate(['sector' => $sectorName, 'date' => $providedDate], ['sector' => $sectorName, 'date' => $providedDate, 'day_change' => round($percentChange, 2)]);
 }
 private static function sectorLimitToNumber($sectorLimit)
 {
     if ($sectorLimit == 'all') {
         $mostRecentDate = SectorHistoricals::getMostRecentSectorHistoricalsDate();
         return SectorHistoricals::where('date', $mostRecentDate)->where('sector', '!=', 'All')->lists('sector')->count();
     } else {
         $explodedSectorLimit = explode('_', $sectorLimit);
         return end($explodedSectorLimit);
     }
 }
 public function sectorDayChanges($sectorName)
 {
     return view('layouts.partials.all-sectors-day-change-display')->with(['selectedSector' => $sectorName, 'sectors' => SectorHistoricals::getSectorDayChanges("sectorDayGain", 30), 'sectorWeekDay' => SectorHistoricals::getSectorWeekDay()]);
 }
 public function change()
 {
     return view('layouts.partials.market-change-display')->with(['marketChangeMessage' => SectorHistoricals::getMarketChangeMessage(), 'marketChange' => SectorHistoricals::getMarketChange()]);
 }