From 8ac1c1ad13f3f2b8bf9980099ead3a153a774578 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Sat, 3 Aug 2024 09:43:31 +0200 Subject: [PATCH] bugfixing options flow page --- src/routes/options-flow/+page.svelte | 167 ++++++++++++--------------- 1 file changed, 74 insertions(+), 93 deletions(-) diff --git a/src/routes/options-flow/+page.svelte b/src/routes/options-flow/+page.svelte index 5f9e1fd3..78f1a661 100644 --- a/src/routes/options-flow/+page.svelte +++ b/src/routes/options-flow/+page.svelte @@ -14,6 +14,7 @@ let rawData = []; let filterList = []; + let displayedData =[]; let flowSentiment; let putCallRatio; @@ -35,7 +36,6 @@ let isLoaded = false; let mode = $isOpen === true ? true : false; let showMore = false; - let newIncomingData = false; let optionSymbol; let optionDescription; @@ -140,65 +140,48 @@ function handleViewData(optionData) { socket.addEventListener('message', (event) => { - previousCallVolume = displayCallVolume ?? 0; - if(mode === true) { - try { - const newData = JSON.parse(event.data); - if(rawData?.length !== newData?.length) { - newIncomingData = true; - } + previousCallVolume = displayCallVolume ?? 0; + if(mode === true) { + try { + const newData = JSON.parse(event.data); + if(rawData?.length !== newData?.length) { + newIncomingData = true; + } + rawData = [...newData]; + + // Apply current filters to the new rawData + let filteredData = rawData; - rawData = [...newData]; - + // Apply filterQuery if it exists + if (filterQuery?.length !== 0) { + filteredData = filteredData.filter(item => item?.ticker === filterQuery?.toUpperCase()); + } - // Variables to track filter status - let queryFilteredData = rawData; - let listFilteredData = rawData; + // Apply filterList if it exists + if (filterList?.length !== 0) { + filteredData = filterExpiringSoon(filteredData, Math.max(...filterList)); + } - // Apply filterQuery if it exists - if (filterQuery?.length !== 0) { - queryFilteredData = rawData.filter(item => item?.ticker === filterQuery?.toUpperCase()); - } + if (filteredData.length !== 0) { + notFound = false; + } else { + notFound = true; + } - // Apply filterList if it exists - if (filterList?.length !== 0) { - listFilteredData = filterExpiringSoon(rawData, Math.max(...filterList)); - } + // Update displayedData instead of rawData + displayedData = filteredData; - // Determine the final filtered data based on both filters - if (filterQuery?.length !== 0 && filterList?.length !== 0) { - rawData = queryFilteredData.filter(item => listFilteredData.includes(item)); - } else if (filterQuery?.length !== 0) { - rawData = queryFilteredData; - } else if (filterList?.length !== 0) { - rawData = listFilteredData; - } + calculateStats(displayedData); - if (rawData?.length !== 0 && newIncomingData === true) { - notFound = false; - newIncomingData = false; - } else if (!newIncomingData) { - notFound = false; - newIncomingData = false; - } else { - notFound = true; - newIncomingData = false; - rawData = data?.getOptionsFlowFeed ?? []; - } - - calculateStats(rawData); - if (previousCallVolume !== displayCallVolume && !muted) { - audio?.play(); - } - - - } - catch(e) { - console.log(e) - } - } - - }); + if (previousCallVolume !== displayCallVolume && !muted) { + audio?.play(); + } + } + catch(e) { + console.log(e) + } + } +}); socket.addEventListener('close', (event) => { @@ -211,13 +194,12 @@ function handleViewData(optionData) { } } - - let scrollContainer; - + onMount(async () => { audio = new Audio(notifySound); rawData = data?.getOptionsFlowFeed; + displayedData = rawData; calculateStats(rawData); isLoaded = true; @@ -384,27 +366,26 @@ function calculateStats(data) { } -function handleInput(event) { + function handleInput(event) { filterQuery = event.target.value; - let newData = []; + setTimeout(() => { + let filteredData = rawData; + if (filterQuery?.length !== 0) { - newData = [...rawData?.filter(item => item?.ticker === filterQuery?.toUpperCase())]; - if (newData?.length !== 0) { - rawData = newData; - - - notFound = false; - } else { - notFound = true; - rawData = data?.getOptionsFlowFeed; - } - } else { - notFound = false; - rawData = data?.getOptionsFlowFeed; + filteredData = rawData.filter(item => item?.ticker === filterQuery?.toUpperCase()); } - calculateStats(rawData); + if (filteredData.length !== 0) { + notFound = false; + } else { + notFound = true; + } + + // Update a separate variable for displayed data, not rawData itself + displayedData = filteredData; + + calculateStats(displayedData); }, 200); } @@ -418,7 +399,7 @@ function debounce(fn, delay) { }; } -const debouncedHandleInput = debounce(handleInput, 200); +const debouncedHandleInput = debounce(handleInput, 300); async function handleFilter(newFilter) { @@ -809,7 +790,7 @@ $: {
@@ -827,61 +808,61 @@ $: {
OI
-
handleViewData(rawData[index])} slot="item" let:index let:style {style} class="tr cursor-pointer"> +
handleViewData(displayedData[index])} slot="item" let:index let:style {style} class="tr cursor-pointer">
- {formatTime(rawData[index]?.time)} + {formatTime(displayedData[index]?.time)}
-
assetSelector(rawData[index]?.ticker, rawData[index]?.assetType)} style="justify-content: center;" class="td text-sm text-blue-400 font-normal"> - {rawData[index]?.ticker} +
assetSelector(displayedData[index]?.ticker, displayedData[index]?.assetType)} style="justify-content: center;" class="td text-sm text-blue-400 font-normal"> + {displayedData[index]?.ticker}
- {reformatDate(rawData[index]?.date_expiration)} + {reformatDate(displayedData[index]?.date_expiration)}
- {rawData[index]?.strike_price} + {displayedData[index]?.strike_price}
-
- {rawData[index]?.put_call} +
+ {displayedData[index]?.put_call}
-
- {rawData[index]?.sentiment} +
+ {displayedData[index]?.sentiment}
- {rawData[index]?.underlying_price} + {displayedData[index]?.underlying_price}
- {rawData[index]?.price} + {displayedData[index]?.price}
-
- {abbreviateNumber(rawData[index]?.cost_basis)} +
+ {abbreviateNumber(displayedData[index]?.cost_basis)}
-
- {rawData[index]?.type} +
+ {displayedData[index]?.type}
{new Intl.NumberFormat("en", { minimumFractionDigits: 0, maximumFractionDigits: 0 - }).format(rawData[index]?.volume)} + }).format(displayedData[index]?.volume)}
{new Intl.NumberFormat("en", { minimumFractionDigits: 0, maximumFractionDigits: 0 - }).format(rawData[index]?.open_interest)} + }).format(displayedData[index]?.open_interest)}