public static function getAverageMetric($metricName, $listOfStocks, $sectorName)
 {
     $sectorMetrics = array();
     foreach ($listOfStocks as $stock) {
         $sectorMetric = StockMetrics::where('stock_code', $stock)->pluck($metricName);
         array_push($sectorMetrics, $sectorMetric);
     }
     return array_sum($sectorMetrics) / count($sectorMetrics);
 }
 /**
  * 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()
 {
     if ($this->option('testMode') == 'true') {
         $this->info("[Test Mode]");
         StockMetrics::whereIn('stock_code', ['TLS', 'CBA'])->where('day_change', '!=', 0)->update(['day_change' => 0.0]);
         $this->info("Gains reset for TLS and CBA.");
     } else {
         $this->info("Resetting day gain to 0.00% for all stocks.");
         StockMetrics::where('day_change', '!=', 0)->update(['day_change' => 0.0]);
         $this->info("All gains have been reset.");
     }
 }
 private static function getStockCodeParameter($testMode = false)
 {
     if (!$testMode) {
         //Limit of 100 at a time due to yahoo's url length limit
         $stockCodeList = StockMetrics::where('updated_at', '<', Carbon::now()->subSeconds(60))->orderBy('updated_at')->take(100)->lists('stock_code');
         $stockCodeParameter = "";
         foreach ($stockCodeList as $stockCode) {
             $stockCodeParameter .= "+" . $stockCode . ".AX";
         }
         return substr($stockCodeParameter, 1);
     } else {
         return "TLS.AX+CBA.AX";
     }
 }
 public static function getGraphData($stockCode, $timeFrame = 'last_month', $dataType)
 {
     $historicals = Historicals::where(['stock_code' => $stockCode])->dateCondition($timeFrame)->orderBy('date')->get();
     $graphData = array();
     foreach ($historicals as $record) {
         if ($dataType == 'Price') {
             $price = $record->close;
             $fiftyDayMovingAverage = $record->fifty_day_moving_average;
             $twoHundredDayMovingAverage = $record->two_hundred_day_moving_average;
         }
         array_push($graphData, array(getCarbonDateFromDate($record->date)->toFormattedDateString(), $price, $fiftyDayMovingAverage, $twoHundredDayMovingAverage));
     }
     //Add Current day's trade value to graph data
     //10:32am allows time for the metrics to be populated
     if (isTradingDay() && getCurrentTimeIntVal() >= 103200 && !Historicals::where(['stock_code' => $stockCode, 'date' => date('Y-m-d')])->first()) {
         $stockMetric = StockMetrics::where('stock_code', $stockCode)->first();
         $metricDate = explode(" ", $stockMetric->updated_at)[0];
         array_push($graphData, array(getCarbonDateFromDate($metricDate)->toFormattedDateString(), $stockMetric->last_trade));
     }
     return $graphData;
 }
 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)]);
 }
 public static function getSectorTotalChange($stocksInSector)
 {
     $marketCapDayChanges = array();
     foreach ($stocksInSector as $stock) {
         $metric = StockMetrics::where('stock_code', $stock)->first();
         array_push($marketCapDayChanges, $metric->market_cap - $metric->market_cap / ($metric->day_change / 100 + 1));
     }
     return array_sum($marketCapDayChanges);
 }
 public function getDayChange($id)
 {
     return view('layouts.partials.individual-stock-day-change')->with(['dayChange' => StockMetrics::where('stock_code', $id)->pluck('day_change')]);
 }