/**
  * 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)]);
                     }
                 }
             }
         }
     }
 }
 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)]);
 }