From 68780f84454fc7e1328318c451d1f047460b986a Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Mon, 2 Sep 2024 20:00:12 +0200 Subject: [PATCH] add popular strategies --- .../stock-screener/[strategyId]/+page.svelte | 145 ++++++++++++------ 1 file changed, 98 insertions(+), 47 deletions(-) diff --git a/src/routes/stock-screener/[strategyId]/+page.svelte b/src/routes/stock-screener/[strategyId]/+page.svelte index aeb37da6..2ba7c37a 100644 --- a/src/routes/stock-screener/[strategyId]/+page.svelte +++ b/src/routes/stock-screener/[strategyId]/+page.svelte @@ -14,7 +14,7 @@ $strategyId = data?.getStrategyId; let ruleOfList = data?.getStrategy?.rules ?? []; let displayRules = []; - + let selectedPopularStrategy = ''; const title = data?.getStrategy?.title; let stockScreenerData = data?.getStockScreenerData?.filter(item => @@ -217,7 +217,7 @@ const getStockScreenerData = async (rules) => { fundRows?.sort((a, b) => a.label.localeCompare(b.label)); */ - let ruleName = 'marketCap'; + let ruleName = ''; // Define your default values @@ -308,15 +308,16 @@ const getStockScreenerData = async (rules) => { } - function changeRule(state: string) - { - searchTerm = ''; - ruleName = state; - handleAddRule() +function changeRule(state: string) +{ + searchTerm = ''; + selectedPopularStrategy = ''; + ruleName = state; + handleAddRule() - //const closePopup = document.getElementById("ruleModal"); - //closePopup?.dispatchEvent(new MouseEvent('click')) - } + //const closePopup = document.getElementById("ruleModal"); + //closePopup?.dispatchEvent(new MouseEvent('click')) +} const valueMappings = { revenue: valueRevenue, @@ -536,16 +537,17 @@ async function updateStockScreenerData() { } async function handleResetAll() { + selectedPopularStrategy = ''; ruleOfList = []; ruleOfList = [...ruleOfList]; ruleName = ''; filteredData = []; displayResults = []; await handleSave(false); - } async function handleDeleteRule(state) { + selectedPopularStrategy = ''; for (let i = 0; i < ruleOfList.length; i++) { if (ruleOfList[i].name === state) { ruleOfList.splice(i, 1); // Remove the element at index i from the ruleOfList @@ -1073,40 +1075,74 @@ function handleChangeValue(value) { } -async function preSelectStrategy(state:string) { - //To-do: Piece of shit code needs to be optimized better - if(state === 'dividendGrowth') { +async function popularStrategy(state: string) { + const strategies = { + dividendGrowth: { + name: 'Dividend Growth', + rules: [ + { condition: "over", name: "dividendGrowth", value: 5 }, + { condition: "over", name: "dividendYield", value: 1 }, + { condition: "under", name: "payoutRatio", value: 60 }, + { condition: "over", name: "growthRevenue", value: 5 } + ] + }, + topGainers1Y: { + name: 'Top Gainers 1Y', + rules: [ + { condition: "over", name: "change1Y", value: 50 }, + { condition: "over", name: "marketCap", value: 10 }, + { condition: "over", name: "eps", value: 5 } + ] + }, + topShortedStocks: { + name: 'Top Shorted Stocks', + rules: [ + { condition: "over", name: "shortFloatPercent", value: 20 }, + { condition: "over", name: "shortRatio", value: 1 }, + { condition: "over", name: "shortOutStandingPercent", value: 10 }, + { condition: "over", name: "sharesShort", value: 20 }, + { condition: "over", name: "marketCap", value: 1 } + ] + }, + momentumTAStocks: { + name: 'Momentum TA Stocks', + rules: [ + { condition: "under", name: "rsi", value: 40 }, + { condition: "under", name: "stochRSI", value: 40 }, + { condition: "over", name: "marketCap", value: 1 }, + { condition: "under", name: "mfi", value: 40 } + ] + }, + topAIStocks: { + name: 'Top AI Stocks', + rules: [ + { condition: "over", name: "fundamentalAnalysis", value: 70 }, + { condition: "over", name: "trendAnalysis", value: 60 }, + { condition: "over", name: "marketCap", value: 1 } + ] + }, + underValuedStocks: { + name: 'Undervalued Stocks', + rules: [ + { condition: "under", name: "marketCap", value: 1 }, + { condition: "over", name: "debtEquityRatio", value: 1 }, + { condition: "over", name: "debtRatio", value: 1 }, + { condition: "over", name: "eps", value: 0 } + ] + }, + }; - ruleOfList = [ - { - "condition": "over", - "name": "dividendGrowth", - "value": 5 - }, - { - "condition": "over", - "name": "dividendYield", - "value": 1 - }, - { - "condition": "under", - "name": "payoutRatio", - "value": 60 - }, - { - "condition": "over", - "name": "growthRevenue", - "value": 5 - } - - ]; - ruleOfList?.forEach(row => { - ruleName = row?.name - ruleCondition[ruleName] = row?.condition; - handleChangeValue(row?.value); - }) - await updateStockScreenerData(); - } + const strategy = strategies[state]; + if (strategy) { + selectedPopularStrategy = strategy.name; + ruleOfList = strategy?.rules; + ruleOfList?.forEach(row => { + ruleName = row.name; + ruleCondition[ruleName] = row.condition; + handleChangeValue(row.value); + }); + await updateStockScreenerData(); + } } @@ -1174,7 +1210,7 @@ async function preSelectStrategy(state:string) {