Untitled
unknown
plain_text
2 years ago
3.5 kB
5
Indexable
public static function fetchExchangeMarketCap(Exchange $exchange)
{
$response = [];
$companyIds = self::getTotalCompaniesInExchange($exchange);
$companyCount = count($companyIds['all_companies']);
$exchangeCorpCount = count($companyIds['exchange_companies']);
// Chart Start Date
$date1YAgo = Carbon::now()->subDays(366)->format('Y-m-d');
// Chart 1: exchange
$response['exchangeMarketCap'] = $exchange->exchangeMarketCaps()
->where('current_date', '>', $date1YAgo)
->orderBy('current_date')->get();
// Chart 2: exchange Fetch the average market cap per day for all companies on Eucaps platform
$rawAvgAllCorpMarketCap = MarketCap::query()
->whereIn('company_id', $companyIds['all_companies'])
->selectRaw('DATE(market_caps.current_date) as date, ROUND(AVG(market_cap), 6) as average_market_cap, count(*) as total')
->where('current_date', '>', $date1YAgo)
->groupBy('current_date')
->orderBy('current_date')
->get();
// Sanitizing Raw Chart Data For Better stability
function chartNormalizer($rawAvgAllCorpMarketCap, $companyCount)
{
$capPercentage = 20; //100 => no_cap
$companyCap = intval($companyCount - ($companyCount * ($capPercentage / 100.0)));
$normalizedChartData = []; //main data object
$avgMarketCaps = $rawAvgAllCorpMarketCap->pluck('average_market_cap')->toArray();
// Calculate mean and variance
$mean = array_sum($avgMarketCaps) / count($avgMarketCaps);
$variance = array_sum(array_map(function ($value) use ($mean) {
return pow($value - $mean, 2);
}, $avgMarketCaps)) / count($avgMarketCaps);
//Standard Deviation
$stdDev = sqrt($variance);
// Set a threshold for anomaly detection (e.g., z-score > 2 or < -2)
$threshold = 2; // reference: normal distribution chart
foreach ($rawAvgAllCorpMarketCap as $dayItem) {
$value = $dayItem->average_market_cap ?? null;
$zScore = ($value - $mean) / $stdDev;
//Triggers if item is within normalizing threshold & avg_based_on_companies not an out-liner
if (!(abs($zScore) > $threshold) && ($dayItem->total > $companyCap)) {
$normalizedChartData[] = $dayItem;
}
}
return $normalizedChartData;
}
// $response['averageAllCompaniesMarketCap'] = $rawAvgAllCorpMarketCap;
$response['averageAllCompaniesMarketCap'] = chartNormalizer($rawAvgAllCorpMarketCap, $companyCount);
// Chart 3: Fetch the average market cap per day for companies under the exchange
$rawAvgExchangeCorpMarketCap = MarketCap::query()
->whereIn('company_id', $companyIds['exchange_companies'])
->selectRaw('DATE(market_caps.current_date) as date, ROUND(AVG(market_cap), 6) as average_market_cap, count(*) as total')
->where('current_date', '>', $date1YAgo)
->groupBy('current_date')
->orderBy('current_date')
->get();
// $response['averageExchangeCompaniesMarketCap'] = $rawAvgExchangeCorpMarketCap;
$response['averageExchangeCompaniesMarketCap'] = chartNormalizer($rawAvgExchangeCorpMarketCap, $exchangeCorpCount);
return $response;
}Editor is loading...
Leave a Comment