From 20f2ca7f3be2d22e7e19633389121d86c418f1ad Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Fri, 18 Oct 2024 11:35:11 +0200 Subject: [PATCH] feature update: select year and quarter for insider statistics --- .../stocks/[tickerID]/insider/+page.server.ts | 1 - .../stocks/[tickerID]/insider/+page.svelte | 137 ++++++++++++++++-- 2 files changed, 127 insertions(+), 11 deletions(-) diff --git a/src/routes/stocks/[tickerID]/insider/+page.server.ts b/src/routes/stocks/[tickerID]/insider/+page.server.ts index 0217a02c..b5a77d75 100644 --- a/src/routes/stocks/[tickerID]/insider/+page.server.ts +++ b/src/routes/stocks/[tickerID]/insider/+page.server.ts @@ -53,7 +53,6 @@ export const load = async ({ locals, params }) => { return acc; }, []); - return output; }; diff --git a/src/routes/stocks/[tickerID]/insider/+page.svelte b/src/routes/stocks/[tickerID]/insider/+page.svelte index 2b9273ed..aaab4b43 100644 --- a/src/routes/stocks/[tickerID]/insider/+page.svelte +++ b/src/routes/stocks/[tickerID]/insider/+page.svelte @@ -7,18 +7,20 @@ import { formatString, abbreviateNumber } from "$lib/utils"; import UpgradeToPro from "$lib/components/UpgradeToPro.svelte"; import { onMount } from "svelte"; - + import * as DropdownMenu from "$lib/components/shadcn/dropdown-menu/index.js"; + import { Button } from "$lib/components/shadcn/button/index.js"; export let data; let isLoaded = false; let statistics = {}; let buySellRatio = 0; + const now = new Date(); + let year = now.getFullYear(); + let quarter = Math.floor(now.getMonth() / 3) + 1; + let yearRange = []; - function calculateInsiderTradingStatistics(data) { - const now = new Date(); - const year = now.getFullYear(); - const quarter = Math.floor(now.getMonth() / 3) + 1; + function calculateInsiderTradingStatistics(data, year, quarter) { // Helper function to check if the transaction date is within the current quarter const isInCurrentQuarter = (transactionDate) => { const date = new Date(transactionDate); @@ -68,7 +70,6 @@ ); let insiderTradingList = rawData?.slice(0, 50); - function backToTop() { window.scrollTo({ top: 0, @@ -102,11 +103,23 @@ } onMount(() => { - statistics = calculateInsiderTradingStatistics(rawData); + statistics = calculateInsiderTradingStatistics(rawData, year, quarter); buySellRatio = statistics?.soldShares !== 0 - ? (statistics?.buyShares / statistics?.soldShares)?.toFixed(2) + ? statistics?.buyShares / statistics?.soldShares : 0; + + yearRange = Array.from( + new Set( + rawData?.map((item) => + (new Date(item?.transactionDate))?.getFullYear(), + ), + ), + )?.sort((a, b) => b - a); + if (yearRange?.length > 0) { + year = yearRange?.slice(0)?.at(0); + } + isLoaded = true; window.addEventListener("scroll", handleScroll); return () => { @@ -133,6 +146,17 @@ }, "n/a": { text: "n/a", class: "text-gray-300" }, }; + + +$: { + if((year || quarter ) && typeof window !== 'undefined') { + statistics = calculateInsiderTradingStatistics(rawData, year, quarter); + buySellRatio = + statistics?.soldShares !== 0 + ? statistics?.buyShares / statistics?.soldShares + : 0; + } + } @@ -212,6 +236,97 @@ {#if isLoaded} {#if insiderTradingList?.length !== 0} {#if Object?.keys(statistics)?.length !== 0} +
+
+ + + + + + + Select Year + + + + {#each yearRange as index} + (year = index)} + class="cursor-pointer hover:bg-[#27272A]" + > + {index} + + {/each} + + + +
+
+ + + + + + + Select Quarter + + + + {#each [1,2,3,4] as index} + (quarter = index)} + class="cursor-pointer hover:bg-[#27272A]" + > + Q{index} + + {/each} + + + +
+
+

Q{statistics?.quarter} {statistics?.year} Insider Statistics @@ -235,7 +350,9 @@ - {buySellRatio} + {buySellRatio > 0 + ? buySellRatio?.toFixed(1) + : buySellRatio} @@ -276,7 +393,7 @@ > {buySellRatio}%{(buySellRatio * 100)?.toFixed(0)}%