diff --git a/src/routes/stocks/[tickerID]/insider/+page.server.ts b/src/routes/stocks/[tickerID]/insider/+page.server.ts index 21f15870..0217a02c 100644 --- a/src/routes/stocks/[tickerID]/insider/+page.server.ts +++ b/src/routes/stocks/[tickerID]/insider/+page.server.ts @@ -57,6 +57,7 @@ export const load = async ({ locals, params }) => { return output; }; + /* const getInsiderTradingStatistics = async () => { const postData = { ticker: params.tickerID, @@ -76,10 +77,11 @@ export const load = async ({ locals, params }) => { return output; }; + */ // Make sure to return a promise return { getInsiderTrading: await getInsiderTrading(), - getInsiderTradingStatistics: await getInsiderTradingStatistics(), + //getInsiderTradingStatistics: await getInsiderTradingStatistics(), }; }; diff --git a/src/routes/stocks/[tickerID]/insider/+page.svelte b/src/routes/stocks/[tickerID]/insider/+page.svelte index b30858a9..d69b9054 100644 --- a/src/routes/stocks/[tickerID]/insider/+page.svelte +++ b/src/routes/stocks/[tickerID]/insider/+page.svelte @@ -8,20 +8,58 @@ import { onMount } from 'svelte'; export let data; let isLoaded = false; - let statistics = data?.getInsiderTradingStatistics ?? {}; + - let buySellRatio = statistics?.totalBought/statistics?.totalSold - let buyShares = statistics?.totalBought - let soldShares = statistics?.totalSold - let buySharesPercentage = Math.floor(buyShares/(buyShares+soldShares)*100); - let soldSharesPercentage = 100 - buySharesPercentage; + let statistics = {}; + function calculateInsiderTradingStatistics(data) { + const now = new Date(); + const year = now.getFullYear(); + const quarter = Math.floor(now.getMonth() / 3) + 1; + + // Helper function to check if the transaction date is within the current quarter + const isInCurrentQuarter = (transactionDate) => { + const date = new Date(transactionDate); + return date.getFullYear() === year && Math.floor(date.getMonth() / 3) + 1 === quarter; + }; + + // Initialize statistics object + let statistics = { + soldShares: 0, + buyShares: 0, + buySharesPercentage: 0, + soldSharesPercentage: 0, + quarter: quarter, + year: year, + }; + + // Summing up bought and sold shares efficiently in a single loop + data.forEach(({ transactionType, securitiesTransacted, price, transactionDate }) => { + if (price > 0 && isInCurrentQuarter(transactionDate)) { + if (transactionType === "Sold") { + statistics.soldShares += securitiesTransacted; + } else if (transactionType === "Bought") { + statistics.buyShares += securitiesTransacted; + } + } + }); + + const totalShares = statistics.buyShares + statistics.soldShares; + + if (totalShares > 0) { + statistics.buySharesPercentage = Math.floor((statistics.buyShares / totalShares) * 100); + statistics.soldSharesPercentage = 100 - statistics.buySharesPercentage; + } + + return statistics; +} + let rawData = data?.getInsiderTrading?.sort( (a, b) => new Date(b?.transactionDate) - new Date(a?.transactionDate) ); - let insiderTradingList = rawData?.slice(0,20) + let insiderTradingList = rawData?.slice(0,50) function backToTop() { @@ -61,6 +99,9 @@ async function handleScroll() { onMount(() => { + + statistics = calculateInsiderTradingStatistics(rawData); + isLoaded = true; window.addEventListener('scroll', handleScroll); return () => { @@ -148,7 +189,7 @@ const transactionStyles = {