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; }
public static function getIndividualSectorGraphData($sectorName, $timeFrame = 'last_month', $dataType) { $graphData = array(); $historicals = SectorHistoricals::where(['sector' => htmlspecialchars_decode($sectorName)])->dateCondition($timeFrame)->orderBy('date')->get(); foreach ($historicals as $record) { if ($dataType == 'Market Cap') { $recordValue = $record->total_sector_market_cap; } elseif ($dataType == 'Volume') { $recordValue = $record->average_daily_volume; } array_push($graphData, array(getCarbonDateFromDate($record->date)->toFormattedDateString(), $recordValue)); } return $graphData; }
private static function getPriceAtDate($stockCode, $date) { $price = Historicals::where('stock_code', $stockCode)->where('date', $date)->first(); if (!$price) { $startDate = getDateFromCarbonDate(getCarbonDateFromDate($date)->subDays(3)); return Historicals::where('stock_code', $stockCode)->whereBetween('date', array($startDate, $date))->take(1)->pluck('close'); } return $price->close; }