From 2dd9a0601c72de9686e191f3608f35baeb1a6726 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Mon, 30 Sep 2024 02:32:10 +0200 Subject: [PATCH] update insider page --- .../stocks/[tickerID]/insider/+page.server.ts | 4 +- .../stocks/[tickerID]/insider/+page.svelte | 75 +++++++++++++++---- .../insider/transcripts/+page.svelte | 19 +---- 3 files changed, 64 insertions(+), 34 deletions(-) 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 = {
Buy/Sell - {buySellRatio?.toFixed(3) } + {(statistics?.buyShares/statistics?.soldShares)?.toFixed(2) }
@@ -158,12 +199,12 @@ const transactionStyles = { - = 1 ? 0 : 100-(buySellRatio*100)?.toFixed(2)}> + = 1 ? 0 : 100-(statistics?.buyShares/statistics?.soldShares*100)?.toFixed(2)}>
- {buySellRatio?.toFixed(2)} + {(statistics?.buyShares/statistics?.soldShares)?.toFixed(2)}
@@ -178,7 +219,7 @@ const transactionStyles = { {new Intl.NumberFormat("en", { minimumFractionDigits: 0, maximumFractionDigits: 0 - }).format(buyShares)} + }).format(statistics?.buyShares)} @@ -188,12 +229,12 @@ const transactionStyles = { - +
- {buySharesPercentage}% + {statistics?.buySharesPercentage}%
@@ -207,7 +248,7 @@ const transactionStyles = { {new Intl.NumberFormat("en", { minimumFractionDigits: 0, maximumFractionDigits: 0 - }).format(soldShares)} + }).format(statistics?.soldShares)} @@ -217,12 +258,12 @@ const transactionStyles = { - +
- {soldSharesPercentage}% + {statistics?.soldSharesPercentage}%
@@ -258,6 +299,7 @@ const transactionStyles = { {#each (data?.user?.tier === 'Pro' ? insiderTradingList : insiderTradingList?.slice(0,3)) as item, index} + {#if item?.price > 0} @@ -291,6 +333,7 @@ const transactionStyles = { + {/if} {/each} diff --git a/src/routes/stocks/[tickerID]/insider/transcripts/+page.svelte b/src/routes/stocks/[tickerID]/insider/transcripts/+page.svelte index ff9bf6fd..b9f12704 100644 --- a/src/routes/stocks/[tickerID]/insider/transcripts/+page.svelte +++ b/src/routes/stocks/[tickerID]/insider/transcripts/+page.svelte @@ -1,15 +1,11 @@