From b3228fb1ca0ecc6d38547f8bf456481cd890208b Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Tue, 7 Jan 2025 13:08:49 +0100 Subject: [PATCH] bugfixing --- src/lib/utils.ts | 13 +- .../stocks/[tickerID]/forecast/+page.svelte | 133 ++++++++++-------- 2 files changed, 77 insertions(+), 69 deletions(-) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index db58ea2c..c299b71e 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -1644,18 +1644,7 @@ export const monthNames = [ "Dec", ]; -export const holidays = [ - "2024-01-01", - "2024-01-15", - "2024-02-19", - "2024-03-29", - "2024-05-27", - "2024-06-19", - "2024-07-04", - "2024-09-02", - "2024-11-28", - "2024-12-25", -]; +export const holidays = ['2025-01-01', '2025-01-09','2025-01-20', '2025-02-17', '2025-04-18', '2025-05-26', '2025-06-19', '2025-07-04', '2025-09-01', '2025-11-27', '2025-12-25'] export const getLastTradingDay = () => { const etTimeZone = "America/New_York"; diff --git a/src/routes/stocks/[tickerID]/forecast/+page.svelte b/src/routes/stocks/[tickerID]/forecast/+page.svelte index 426f0a30..7b26f35f 100644 --- a/src/routes/stocks/[tickerID]/forecast/+page.svelte +++ b/src/routes/stocks/[tickerID]/forecast/+page.svelte @@ -52,24 +52,30 @@ const categories = ["Strong Buy", "Buy", "Hold", "Sell", "Strong Sell"]; function findIndex(data) { - const currentYear = new Date().getFullYear(); + let year = new Date().getFullYear() - 1; - // Find the index where the item's date is greater than the current year and revenue is null - const index = data?.findIndex( - (item) => item?.date > currentYear && item?.revenue === null, - ); - - // If index is found and there is at least one item in the data for the current year with non-null revenue - if (index !== -1) { - const hasNonNullRevenue = data?.some( - (item) => item?.date === currentYear && item?.revenue !== null, + while (year > 0) { + // Ensure we don't loop indefinitely + // Find the index where the item's date matches the current year and revenue is null + const index = data?.findIndex( + (item) => item?.date === year && item?.revenue === null, ); - // Add +1 to the index if the condition is met - return hasNonNullRevenue ? index + 1 : index; + // If index is found and there is at least one item in the data for this year with non-null revenue + if (index !== -1) { + const hasNonNullRevenue = data?.some( + (item) => item?.date === year && item?.revenue !== null, + ); + + // Add +1 to the index if the condition is met + return hasNonNullRevenue ? index + 1 : index; + } + + // Decrement the year to search the previous year + year--; } - return index; // Return the index or -1 if not found + return -1; // Return -1 if no matching index is found } function getTotalForDate(index) { @@ -100,7 +106,7 @@ const colors = ["#9E190A", "#D9220E", "#FF9E21", "#31B800", "#008A00"]; // Create a consistent mapping for data - const formattedData = rawAnalystList.map((item) => + const formattedData = rawAnalystList?.map((item) => categories.map((cat) => item[cat] || 0), ); @@ -262,16 +268,14 @@ // Calculate changes using the helper function const estimatedRevenueAvg = - data?.getAnalystEstimate[index - 1]?.estimatedRevenueAvg; - const revenue = data?.getAnalystEstimate[index - 2]?.revenue; - const estimatedRevenueAvgNextYear = data?.getAnalystEstimate[index]?.estimatedRevenueAvg; - - const estimatedEpsAvg = - data?.getAnalystEstimate[index - 1]?.estimatedEpsAvg; - const eps = data?.getAnalystEstimate[index - 2]?.eps; + const revenue = data?.getAnalystEstimate[index - 1]?.revenue; + const estimatedRevenueAvgNextYear = + data?.getAnalystEstimate[index + 1]?.estimatedRevenueAvg; + const estimatedEpsAvg = data?.getAnalystEstimate[index]?.estimatedEpsAvg; + const eps = data?.getAnalystEstimate[index - 1]?.eps; const estimatedEPSAvgNextYear = - data?.getAnalystEstimate[index]?.estimatedEpsAvg; + data?.getAnalystEstimate[index + 1]?.estimatedEpsAvg; // Calculate percentage changes for each metric changeRevenue = calculateChange(estimatedRevenueAvg, revenue); @@ -284,6 +288,8 @@ estimatedEPSAvgNextYear, estimatedEpsAvg, ); + + console.log(changeRevenue, revenue, estimatedRevenueAvg); } function getPriceForecastChart() { @@ -380,7 +386,7 @@ { name: "Historical", type: "line", - data: processedHistorical.map((point) => [point.date, point.value]), + data: processedHistorical?.map((point) => [point.date, point.value]), symbol: "circle", symbolSize: 6, itemStyle: { @@ -393,7 +399,7 @@ { name: "High", type: "line", - data: forecastHigh.map((point) => [point.date, point.value]), + data: forecastHigh?.map((point) => [point.date, point.value]), symbol: "none", lineStyle: { type: "dashed", @@ -403,7 +409,7 @@ { name: "Average", type: "line", - data: forecastAvg.map((point) => [point.date, point.value]), + data: forecastAvg?.map((point) => [point.date, point.value]), symbol: "none", lineStyle: { type: "dashed", @@ -413,7 +419,7 @@ { name: "Low", type: "line", - data: forecastLow.map((point) => [point.date, point.value]), + data: forecastLow?.map((point) => [point.date, point.value]), symbol: "none", lineStyle: { type: "dashed", @@ -741,26 +747,27 @@
- {data?.getAnalystEstimate[index - 1] - ?.estimatedRevenueAvg !== null && - data?.getAnalystEstimate[index - 1]?.estimatedRevenueAvg !== - 0 + {data?.getAnalystEstimate[index]?.estimatedRevenueAvg !== + null && + data?.getAnalystEstimate[index]?.estimatedRevenueAvg !== 0 ? abbreviateNumber( - data?.getAnalystEstimate[index - 1] - ?.estimatedRevenueAvg, + data?.getAnalystEstimate[index]?.estimatedRevenueAvg, ) : "n/a"} - {#if data?.getAnalystEstimate[index - 1]?.estimatedRevenueAvg !== null && data?.getAnalystEstimate[index - 1]?.estimatedRevenueAvg !== 0} + {#if data?.getAnalystEstimate[index]?.estimatedRevenueAvg !== null && data?.getAnalystEstimate[index]?.estimatedRevenueAvg !== 0}
- from {abbreviateNumber( - data?.getAnalystEstimate[index - 2]?.revenue, - )} + from {data?.getAnalystEstimate[index - 1]?.revenue !== + undefined + ? abbreviateNumber( + data?.getAnalystEstimate[index - 1]?.revenue, + ) + : "n/a"}
{/if}
- {#if data?.getAnalystEstimate[index - 1]?.estimatedRevenueAvg !== null && data?.getAnalystEstimate[index - 1]?.estimatedRevenueAvg !== 0} + {#if data?.getAnalystEstimate[index]?.estimatedRevenueAvg !== null && data?.getAnalystEstimate[index]?.estimatedRevenueAvg !== 0} {/if} @@ -810,16 +819,23 @@
- {abbreviateNumber( - data?.getAnalystEstimate[index]?.estimatedRevenueAvg, - )} + {data?.getAnalystEstimate[index + 1] + ?.estimatedRevenueAvg !== undefined + ? abbreviateNumber( + data?.getAnalystEstimate[index + 1] + ?.estimatedRevenueAvg, + ) + : "n/a"}
- from {abbreviateNumber( - data?.getAnalystEstimate[index - 1] - ?.estimatedRevenueAvg, - )} + from {data?.getAnalystEstimate[index] + ?.estimatedRevenueAvg !== undefined + ? abbreviateNumber( + data?.getAnalystEstimate[index] + ?.estimatedRevenueAvg, + ) + : "n/a"}
{abbreviateNumber( - data?.getAnalystEstimate[index - 1]?.estimatedEpsAvg, + data?.getAnalystEstimate[index]?.estimatedEpsAvg, )}
- from {data?.getAnalystEstimate[index - 2]?.eps} + from {data?.getAnalystEstimate[index - 1]?.eps}
{abbreviateNumber( - data?.getAnalystEstimate[index]?.estimatedEpsAvg, + data?.getAnalystEstimate[index + 1]?.estimatedEpsAvg, )}
from {abbreviateNumber( - data?.getAnalystEstimate[index - 1]?.estimatedEpsAvg, + data?.getAnalystEstimate[index]?.estimatedEpsAvg, )}
@@ -962,7 +981,7 @@ class="ml-0.5 mt-1.5 hidden text-sm font-semibold text-white lg:block" > from {abbreviateNumber( - data?.getAnalystEstimate[index - 1]?.estimatedEpsAvg, + data?.getAnalystEstimate[index]?.estimatedEpsAvg, )}