From 1ae1b4b6747974e758d77b897a5ac3708fb794f9 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Mon, 9 Dec 2024 21:48:51 +0100 Subject: [PATCH] add size / oi ratio --- src/routes/options-flow/+page.svelte | 8 +- .../options-flow/workers/filterWorker.ts | 74 +++++++++++++++---- 2 files changed, 67 insertions(+), 15 deletions(-) diff --git a/src/routes/options-flow/+page.svelte b/src/routes/options-flow/+page.svelte index 63f6fd36..340a169a 100644 --- a/src/routes/options-flow/+page.svelte +++ b/src/routes/options-flow/+page.svelte @@ -43,7 +43,7 @@ const allRules = { size: { label: "Size", - step: ["100K", "50K", "20K", "10K", "5K", "2K", "1K", "100", "0"], + step: ["50K", "20K", "10K", "5K", "2K", "1K", "100", "0"], defaultCondition: "over", defaultValue: "any", }, @@ -65,6 +65,12 @@ defaultCondition: "over", defaultValue: "any", }, + sizeOIRatio: { + label: "Size / Open Interest", + step: ["100%", "80%", "60%", "50%", "30%", "15%", "10%", "5%"], + defaultCondition: "over", + defaultValue: "any", + }, cost_basis: { label: "Premium", step: [ diff --git a/src/routes/options-flow/workers/filterWorker.ts b/src/routes/options-flow/workers/filterWorker.ts index 571d0eb3..eb37b9b7 100644 --- a/src/routes/options-flow/workers/filterWorker.ts +++ b/src/routes/options-flow/workers/filterWorker.ts @@ -70,22 +70,66 @@ function createRuleCheck(rule, ruleName, ruleValue) { const now = new Date(new Date().toLocaleString("en-US", { timeZone: "America/New_York" })); - if (ruleName === 'volumeoiratio') { - return (item) => { - const volume = parseFloat(item.volume); - const openInterest = parseFloat(item.open_interest); +if (ruleName === 'volumeoiratio') { + return (item) => { + const volume = parseFloat(item.volume); + const openInterest = parseFloat(item.open_interest); - if (isNaN(volume) || isNaN(openInterest) || openInterest === 0) { - return false; - } + if (isNaN(volume) || isNaN(openInterest) || openInterest === 0) { + return false; + } - const ratio = (volume / openInterest) * 100; + const ratio = (volume / openInterest) * 100; + + // Handle 'between' condition for volume to open interest ratio + if (rule.condition === 'between' && Array.isArray(ruleValue)) { + const [minRatio, maxRatio] = ruleValue.map(convertUnitToValue); // Convert ruleValue to numeric values + + if (minRatio === null && maxRatio === null) return true; + if (minRatio === null) return ratio <= maxRatio; + if (maxRatio === null) return ratio >= minRatio; + + return ratio >= minRatio && ratio <= maxRatio; + } + + // Existing conditions for 'over' and 'under' + if (rule.condition === 'over' && ratio <= ruleValue) return false; + if (rule.condition === 'under' && ratio >= ruleValue) return false; + + return true; + }; +} + + +if (ruleName === 'sizeoiratio') { + return (item) => { + const size = parseFloat(item?.size); + const openInterest = parseFloat(item?.open_interest); + if (isNaN(size) || isNaN(openInterest) || openInterest === 0) { + return false; + } + + const ratio = (size / openInterest) * 100; + + // Handle 'between' condition for size to open interest ratio + if (rule.condition === 'between' && Array.isArray(ruleValue)) { + const [minRatio, maxRatio] = ruleValue?.map(convertUnitToValue); // Convert ruleValue to numeric values + + if (minRatio === null && maxRatio === null) return true; + if (minRatio === null) return ratio <= maxRatio; + if (maxRatio === null) return ratio >= minRatio; + + return ratio >= minRatio && ratio <= maxRatio; + } + + // Existing conditions for 'over' and 'under' + if (rule.condition === 'over' && ratio <= ruleValue) return false; + if (rule.condition === 'under' && ratio >= ruleValue) return false; + + return true; + }; +} - if (rule.condition === 'over' && ratio <= ruleValue) return false; - if (rule.condition === 'under' && ratio >= ruleValue) return false; - return true; - }; - } if (ruleName === 'date_expiration') { // If ruleValue is empty, undefined, "any", or an array containing only "any", return a function that always returns true @@ -93,6 +137,7 @@ function createRuleCheck(rule, ruleName, ruleValue) { return () => true; } + return (item) => { const expirationDate = new Date(item[rule.name]); if (isNaN(expirationDate)) return false; // Handle invalid dates @@ -242,7 +287,8 @@ onmessage = async (event: MessageEvent) => { const { rawData, ruleOfList, filterQuery } = event.data || {}; // Filter the data let filteredData = await filterRawData(rawData, ruleOfList, filterQuery); - // Remove duplicates based on id + + console.log(ruleOfList) filteredData = Array.from( new Map(filteredData?.map((item) => [item?.id, item]))?.values() );