From 949f5e0bc10ec856a974090302239614ee47b17b Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Thu, 12 Sep 2024 15:35:25 +0200 Subject: [PATCH] add advanced filter to options flow --- src/routes/options-flow/+page.svelte | 130 ++++-------------- .../options-flow/workers/filterWorker.ts | 55 +++++++- 2 files changed, 77 insertions(+), 108 deletions(-) diff --git a/src/routes/options-flow/+page.svelte b/src/routes/options-flow/+page.svelte index 7f56e79c..eb45685d 100644 --- a/src/routes/options-flow/+page.svelte +++ b/src/routes/options-flow/+page.svelte @@ -25,8 +25,9 @@ const allRules = { cost_basis: { label: 'Premium', step: ['10M','5M','1M','500K','100K','50K','10K','5K'], defaultCondition: 'over', defaultValue: '50K' }, put_call: { label: 'Contract Type', step: ["Calls", "Puts"], defaultValue: 'any' }, sentiment: { label: 'Sentiment', step: ["Bullish","Neutral", "Bearish"], defaultValue: 'any' }, - execution_estimate: { label: 'Execution', step: ["At Ask","At Bid"], defaultValue: 'any' }, + execution_estimate: { label: 'Execution', step: ["At Ask","At Bid", "Below Ask", "Below Bid"], defaultValue: 'any' }, option_activity_type: { label: 'Option Type', step: ["Sweep","Trade"], defaultValue: 'any' }, + date_expiration: { label: 'Date Expiration', step: ["1 day","1 Week","2 Weeks","1 Month","3 Months","6 Months","1 Year","3 Years"], defaultValue: 'any' }, }; @@ -80,8 +81,13 @@ async function handleResetAll() { ruleOfList = []; ruleOfList = [...ruleOfList]; ruleName = ''; + checkedItems = new Set(); + Object.keys(allRules).forEach(ruleName => { + ruleCondition[ruleName] = allRules[ruleName].defaultCondition; + valueMappings[ruleName] = allRules[ruleName].defaultValue; + }); displayRules = allRows?.filter(row => ruleOfList.some(rule => rule.name === row.rule)); - + displayedData = rawData; } function changeRule(state: string) @@ -107,8 +113,9 @@ function handleAddRule() { switch (ruleName) { case "put_call": case "sentiment": - case "execution_estimate","option_activity_type": + case "execution_estimate": case "option_activity_type": + case "date_expiration": newRule = { name: ruleName, value: Array.isArray(valueMappings[ruleName]) ? valueMappings[ruleName] : [valueMappings[ruleName]] }; // Ensure value is an array break; default: @@ -152,12 +159,13 @@ async function handleRule(newRule) { const loadWorker = async () => { syncWorker.postMessage({ rawData, ruleOfList }); - console.log(ruleOfList) }; const handleMessage = (event) => { displayRules = allRows?.filter(row => ruleOfList.some(rule => rule.name === row.rule)); displayedData = event.data?.filteredData ?? []; + calculateStats(displayedData); + //console.log(displayedData) }; @@ -181,7 +189,7 @@ async function handleChangeValue(value) { } else { checkedItems.add(value); } - if (["put_call","sentiment","execution_estimate","option_activity_type"]?.includes(ruleName)) { + if (["put_call","sentiment","execution_estimate","option_activity_type","date_expiration"]?.includes(ruleName)) { // Ensure valueMappings[ruleName] is initialized as an array if (!Array.isArray(valueMappings[ruleName])) { valueMappings[ruleName] = []; // Initialize as an empty array if not already @@ -292,36 +300,9 @@ const nyseDate = new Date(data?.getOptionsFlowFeed?.at(0)?.date ?? null)?.toLoca let optionOpenInterest; let optionSentiment; let optionPrice; - let optionTradeCount; let optionexecution_estimate; - let optionExchange; - /* - async function infiniteHandler({ detail: { loaded, complete, error } }) { - try { - const lastId = rawData?.at(-1)?.id; - const postData = {'lastId': lastId}; - const response = await fetch(data?.apiURL + '/options-flow-feed', { - method: 'POST', - headers: { - "Content-Type": "application/json", "X-API-KEY": data?.apiKey - }, - body: JSON.stringify(postData), - }); - const newData = await response.json(); - if(newData?.length === 0) { - complete(); - } - else { - rawData = [...rawData,...newData]; - loaded(); - } - } catch (e) { - error(); - } - } - */ function toggleMode() { if ($isOpen) { @@ -366,9 +347,9 @@ function handleViewData(optionData) { optionOpenInterest = new Intl.NumberFormat("en", {minimumFractionDigits: 0, maximumFractionDigits: 0}).format(optionData?.open_interest); optionSentiment = optionData?.sentiment; optionPrice = optionData?.price; - optionTradeCount = optionData?.tradeCount; + //optionTradeCount = optionData?.tradeCount; optionexecution_estimate = optionData?.execution_estimate; - optionExchange = optionData?.exchange; + //optionExchange = optionData?.exchange; const openPopup = $screenWidth < 640 ? document.getElementById("optionDetailsMobileModal") : document.getElementById("optionDetailsDesktopModal"); openPopup?.dispatchEvent(new MouseEvent('click')) @@ -639,6 +620,7 @@ function calculateStats(data) { // Update a separate variable for displayed data, not rawData itself displayedData = filteredData; + calculateStats(displayedData); }, 200); } @@ -657,40 +639,6 @@ const debouncedHandleInput = debounce(handleInput, 300); -// Function to filter elements with date_expiration within a given number of days -const filterExpiringSoon = (data, days) => { - const currentDate = new Date(); // Get today's date - return data.filter(item => { - const expirationDate = new Date(item?.date_expiration); - const timeDiff = expirationDate - currentDate; // Time difference in milliseconds - const daysDiff = timeDiff / (1000 * 60 * 60 * 24); // Convert to days - return daysDiff <= days && daysDiff >= 0; // Ensure it's within the specified number of days and not in the past - }); -}; - - -$: { - if(filterList && typeof window !== 'undefined' && mode === false) - { - if(filterList?.length !== 0) - { - const newData = filterExpiringSoon(rawData, Math.max(...filterList)); - if (newData?.length !== 0) { - rawData = newData; - notFound = false; - } else { - notFound = true; - rawData = data?.getOptionsFlowFeed; - } - - } - else if (filterQuery?.length === 0) { - rawData = data?.getOptionsFlowFeed; - - } - calculateStats(rawData); - } - } @@ -858,7 +806,7 @@ $: { - {#if !['put_call',"sentiment", "execution_estimate","option_activity_type"]?.includes(row?.rule)} + {#if !['put_call',"sentiment", "execution_estimate","option_activity_type","date_expiration"]?.includes(row?.rule)}
@@ -882,7 +830,7 @@ $: {
{/if} - {#if !['put_call',"sentiment", "execution_estimate","option_activity_type"]?.includes(row?.rule)} + {#if !['put_call',"sentiment", "execution_estimate","option_activity_type","date_expiration"]?.includes(row?.rule)} {#each row?.step as newValue} @@ -891,7 +839,7 @@ $: { {/each} - {:else if ['put_call',"sentiment", "execution_estimate","option_activity_type"]?.includes(row?.rule)} + {:else if ['put_call',"sentiment", "execution_estimate","option_activity_type","date_expiration"]?.includes(row?.rule)} {#each row?.step as item}
event.preventDefault()}> @@ -1254,18 +1202,7 @@ $: { Add Filters
- -
e?.key === 'Enter' ? e.preventDefault() : '' }> - -
-
- -
-
-
- +
@@ -1306,35 +1243,18 @@ $: {
-