/**
  * 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 testUpdateSectorChange()
 {
     $this->artisan("stocks:updateSectorMetrics", ['--testMode' => true]);
     if (isTradingDay()) {
         $this->seeInDatabase('sector_historicals', ['sector' => 'Telecommunication Services', 'date' => date("Y-m-d")]);
         $this->seeInDatabase('sector_historicals', ['sector' => 'Banks', 'date' => date("Y-m-d")]);
     }
 }
function getMarketStatus()
{
    if (getCurrentTimeIntVal() >= 100000 && getCurrentTimeIntVal() <= 160000) {
        if (isTradingDay()) {
            if (date("Y-m-d") == "2016-12-23" || date("Y-m-d") == "2016-12-30" || date("Y-m-d") == "2015-12-31") {
                if (getCurrentTimeIntVal() <= 141000) {
                    return "Market Open";
                } else {
                    return "Market Closed";
                }
            } else {
                return "Market Open";
            }
        }
    }
    return "Market Closed";
}
 /**
  * Define the application's command schedule.
  *
  * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
  * @return void
  */
 protected function schedule(Schedule $schedule)
 {
     if (isTradingDay()) {
         $schedule->command('stocks:calculateStockChange')->weekdays()->dailyAt('03:35');
         $schedule->command('stocks:resetDayChange')->dailyAt('04:00');
         //Full sector metric update once before and once after each trading day
         $schedule->command('stocks:updateSectorMetrics', ['--mode' => 'full'])->weekdays()->dailyAt('10:28');
         $schedule->command('stocks:updateSectorMetrics', ['--mode' => 'full'])->weekdays()->dailyAt('16:25');
         $schedule->command('stocks:getDailyFinancials')->weekdays()->dailyAt('16:30');
     }
     //Only run these between 10:30 and 17:00 Sydney Time
     if (getCurrentTimeIntVal() >= 103000 && getCurrentTimeIntVal() <= 170000 && isTradingDay()) {
         $schedule->command('stocks:updateStockMetrics')->cron("*/2 * * * *");
         $schedule->command('stocks:updateSectorMetrics', ['--mode' => 'partial'])->cron("*/2 * * * *");
     }
     $schedule->command('stocks:updateStockList')->dailyAt('02:00');
     $schedule->command('stocks:getCompanySummaries')->dailyAt('02:01');
 }
 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;
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $this->info("This process can take several minutes...");
     $this->info("Getting daily financials...");
     $stockCodes = Stock::all()->lists('stock_code');
     $numberOfStocks = count($stockCodes);
     $iterationNumber = 1;
     $maxIterations = ceil($numberOfStocks / 100);
     if ($this->option('testMode') == 'true') {
         $maxIterations = 1;
         $this->info("[Test Mode]");
     }
     while ($iterationNumber <= $maxIterations) {
         $stockCodeParameter = GetDailyFinancialsCommand::getStockCodeParameter($this->option('testMode'));
         $financialsURL = "http://finance.yahoo.com/d/quotes.csv?s=" . $stockCodeParameter . "&f=sohgl1v";
         $dailyRecords = explode("\n", file_get_contents($financialsURL));
         foreach ($dailyRecords as $record) {
             if ($record != null) {
                 $individualRecord = explode(',', $record);
                 $stockCode = substr(explode('.', $individualRecord[0])[0], 1);
                 if (isTradingDay()) {
                     Historicals::updateOrCreate(['stock_code' => $stockCode, 'date' => date("Y-m-d")], ["stock_code" => $stockCode, "date" => date("Y-m-d"), "open" => $individualRecord[1], "high" => $individualRecord[2], "low" => $individualRecord[3], "close" => $individualRecord[4], "volume" => $individualRecord[5], "adj_close" => $individualRecord[4], "fifty_day_moving_average" => Historicals::getMovingAverage($stockCode, 50), "two_hundred_day_moving_average" => Historicals::getMovingAverage($stockCode, 200), "updated_at" => date("Y-m-d H:i:s")]);
                 }
             }
         }
         $this->info("Updating... " . round($iterationNumber * (100 / $maxIterations), 2) . "%");
         $iterationNumber++;
     }
     if (isTradingDay() && $this->option('testMode') != 'true') {
         $this->info("Removing existing stock_code index in historicals");
         \DB::statement("DROP INDEX `stock_code` ON historicals");
         $this->info("Reapplying index to historicals table");
         \DB::statement("ALTER TABLE `historicals` ADD INDEX (`stock_code`)");
         $this->info("Finished getting daily financials for " . $numberOfStocks . " stocks.");
     }
 }