From 5ab3a76afa86442243bbf843bda3ce7e8dffbc6c Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Sun, 8 Sep 2024 20:55:26 +0200 Subject: [PATCH] add chain data --- .../stocks/[tickerID]/options/+page.svelte | 281 ++++++++---------- src/routes/stocks/[tickerID]/options/+page.ts | 39 ++- 2 files changed, 155 insertions(+), 165 deletions(-) diff --git a/src/routes/stocks/[tickerID]/options/+page.svelte b/src/routes/stocks/[tickerID]/options/+page.svelte index e60804fb..bc32a7af 100644 --- a/src/routes/stocks/[tickerID]/options/+page.svelte +++ b/src/routes/stocks/[tickerID]/options/+page.svelte @@ -23,18 +23,12 @@ let displayData = 'volume'; let options; let optionsGEX; - let rawData = data?.getOptionsChain + + let rawData = data?.getOptionsHistoricalData; let optionList = rawData?.slice(0,30); - let flowSentiment = 'n/a'; - let callPercentage; - let putPercentage; - let displayCallVolume; - let displayPutVolume; - let latestPutCallRatio; - let displayOTMRatio; - - + let optionChainList = data?.getOptionsChainData?.at(0)?.chain || []; + let totalVolume //= data?.getOptionsPlotData?.totalVolume; let totalOpenInterest //= data?.getOptionsPlotData?.totalOpenInterest; @@ -109,10 +103,6 @@ function normalizer(value) { return formattedTimeString; } - function changeStatement(event) - { - displayData = event.target.value; - } function changeTimePeriod(event) { @@ -306,55 +296,6 @@ function getGEXPlot() { return option; } - function calculateStats() { - const currentPrice = parseFloat(data?.getStockQuote?.price); - - const { callVolumeSum, putVolumeSum, bullishCount, bearishCount, otmVolume, itmVolume } = rawData?.reduce((acc, item) => { - const volume = parseInt(item?.volume); - const strikePrice = parseFloat(item?.strike_price); - - if (item?.put_call === "Calls") { - acc.callVolumeSum += volume; - if (strikePrice > currentPrice) { - acc.otmVolume += volume; - } else { - acc.itmVolume += volume; - } - } else if (item?.put_call === "Puts") { - acc.putVolumeSum += volume; - if (strikePrice < currentPrice) { - acc.itmVolume += volume; - } else { - acc.otmVolume += volume; - } - } - - if (item?.sentiment === "Bullish") { - acc.bullishCount += 1; - } else if (item?.sentiment === "Bearish") { - acc.bearishCount += 1; - } - - return acc; - }, { callVolumeSum: 0, putVolumeSum: 0, bullishCount: 0, bearishCount: 0, otmVolume: 0, itmVolume: 0 }); - - if (bullishCount > bearishCount) { - flowSentiment = 'Bullish'; - } else if (bullishCount < bearishCount) { - flowSentiment = 'Bearish'; - } else { - flowSentiment = 'Neutral'; - } - - latestPutCallRatio = (putVolumeSum / callVolumeSum); - callPercentage = Math.floor((callVolumeSum) / (callVolumeSum + putVolumeSum) * 100); - putPercentage = (100 - callPercentage); - displayCallVolume = callVolumeSum; - displayPutVolume = putVolumeSum; - - // Calculate OTM/ITM ratio - displayOTMRatio = otmVolume / (itmVolume+otmVolume) ?? 0; - } function filterDate(filteredList, displayTimePeriod) { const now = Date.now(); @@ -469,12 +410,28 @@ function handleViewData(optionData) { optionHistoryList?.forEach((item) => { item.dte = daysLeft(item?.date_expiration); }); - //const openPopup = $screenWidth < 640 ? document.getElementById("optionDetailsMobileModal") : document.getElementById("optionDetailsDesktopModal"); - //openPopup?.dispatchEvent(new MouseEvent('click')) - optionDetailsDesktopModal?.showModal() } - + +function handleMode(i) { + activeIdx = i; + } + + const tabs = [ + { + title: "Historical Data", + }, + { + title: "Chain Data", + }, + ]; + + let activeIdx = 0; + +function changeStatement(event) +{ + optionChainList = (data?.getOptionsChainData?.filter(item => item?.date_expiration === event.target.value))?.at(0)?.chain || []; +} $: { if ((displayTimePeriod || displayData) && optionsPlotData?.length !== 0 && typeof window !== 'undefined') { @@ -611,7 +568,7 @@ $: { - @@ -637,7 +594,7 @@ $: { {#if data?.getOptionsGexData?.length !== 0} -

+

Daily Gamma Exposure (GEX)

@@ -648,21 +605,51 @@ $: { {/if} -

- Historical Option Data +

+ {activeIdx === 0 ? 'Historical Option Data' : 'Option Chain Data'}

- - + {#if optionList?.length !== 0} +
+ {#each tabs as item, i} + + {/each} +
+ + {#if activeIdx === 1} +
+ +
+ {/if} - - - +
- + {#if activeIdx === 0} @@ -680,7 +667,6 @@ $: { {#each (data?.user?.tier === 'Pro' ? optionList : optionList?.slice(0,3)) as item, index} handleViewData(item?.history)} class="cursor-pointer sm:hover:bg-[#245073] sm:hover:bg-opacity-[0.2] odd:bg-[#27272A] border-b-[#09090B] {index+1 === optionList?.slice(0,3)?.length && data?.user?.tier !== 'Pro' ? 'opacity-[0.1]' : ''}"> - @@ -757,6 +743,63 @@ $: {
{formatDate(item?.date)}
+ {:else} + + + + + + + + + + + + + + {#each (data?.user?.tier === 'Pro' ? optionChainList : optionChainList?.slice(0,3)) as item, index} + + + + + + + + + + + + + + + + + + + + + + {/each} + + +
Call PremCall OICall VolumeStrike PricePut VolumePut OIPut Prem
+ {abbreviateNumber(item?.total_premium_call,true)} + + {item?.total_open_interest_call} + + {item?.total_volume_call} + +
+ {item?.strike_price} +
+
+ {item?.total_volume_put} + + {item?.total_open_interest_put} + + {abbreviateNumber(item?.total_premium_put,true)} +
+ {/if}
@@ -913,88 +956,6 @@ $: { - - - -