diff --git a/src/routes/stock-screener/[strategyId]/+page.svelte b/src/routes/stock-screener/[strategyId]/+page.svelte index 829313de..745209b5 100644 --- a/src/routes/stock-screener/[strategyId]/+page.svelte +++ b/src/routes/stock-screener/[strategyId]/+page.svelte @@ -28,6 +28,7 @@ // Define all possible rules and their properties const allRules = { avgVolume: { label: 'Avgerage Volume', step: ['100M','10M','1M','100K','10K','1K','0'], category: 'fund', defaultCondition: 'over', defaultValue: 'any' }, + volume: { label: 'Volume', step: ['100M','10M','1M','100K','10K','1K','0'], category: 'fund', defaultCondition: 'over', defaultValue: 'any' }, rsi: { label: 'RSI', step: [90,80,70,60,50,40,30,20], category: 'ta', defaultCondition: 'over', defaultValue: 'any' }, stochRSI: { label: 'Stoch RSI Fast', step: [90,80,70,60,50,40,30,20], category: 'ta', defaultCondition: 'over', defaultValue: 'any' }, mfi: { label: 'MFI', step: [90,80,70,60,50,40,30,20], category: 'ta', defaultCondition: 'over', defaultValue: 'any' }, @@ -79,7 +80,7 @@ const allRules = { var: { label: 'Value-at-Risk', step: ['-1%','-5%','-10%','-15%','-20%'], category: 'fund', defaultCondition: 'over', defaultValue: 'any' }, trendAnalysis: { label: 'AI Trend Analysis (Bullish)', step: ['90%','80%','70%','60%','50%'], category: 'ai', defaultCondition: 'over', defaultValue: 'any' }, fundamentalAnalysis: { label: 'AI Fundamental Analysis (Bullish)', step: ['90%','80%','70%','60%','50%'], category: 'ai', defaultCondition: 'over', defaultValue: 'any' }, - analystRating: { label: 'Analyst Rating', step: ['Buy', 'Hold', 'Sell'], category: 'fund', defaultCondition: '', defaultValue: 'any' }, + analystRating: { label: 'Analyst Rating', step: ['Buy', 'Hold', 'Sell'], category: 'fund', defaultCondition: '', defaultValue: 'Buy' }, currentRatio: { label: 'Current Ratio', step: [50,40,30,20,10,5,1], category: 'fund', defaultCondition: 'over', defaultValue: 'any' }, quickRatio: { label: 'Quick Ratio', step: [50,40,30,20,10,5,1], category: 'fund', defaultCondition: 'over', defaultValue: 'any' }, debtEquityRatio: { label: 'Debt / Equity', step: [50,40,30,20,10,5,1], category: 'fund', defaultCondition: 'over', defaultValue: 'any' }, @@ -492,63 +493,51 @@ function filterStockScreenerData() { } -let order = 'highToLow'; -let sortBy = ''; // Default sorting by change percentage - -function changeOrder(state:string) { - if (state === 'highToLow') - { - order = 'lowToHigh'; - } - else { - order = 'highToLow'; - } - } - -const sortByHighestChange = (tickerList) => { - return tickerList?.sort(function(a, b) { - if(order === 'highToLow') - { - return b?.changesPercentage - a?.changesPercentage; - } - else { - return a?.changesPercentage - b?.changesPercentage; - } - - }); +enum Order { + HighToLow = 'highToLow', + LowToHigh = 'lowToHigh' } -const sortByMarketCap = (tickerList) => { - return tickerList?.sort(function(a, b) { - if(order === 'highToLow') - { - return b?.marketCap - a?.marketCap; - } - else { - return a?.marketCap - b?.marketCap; - } +enum SortBy { + Change = 'change', + MarketCap = 'marketCap', + PE = 'pe', // Add new sorting criteria here + Volume = 'volume' // Add new sorting criteria here + +} + +// Mapping of SortBy enum to actual data keys +const sortByKeys: Record = { + [SortBy.Change]: 'changesPercentage', + [SortBy.MarketCap]: 'marketCap', + [SortBy.PE]: 'pe', + [SortBy.Volume]: 'volume', // Add new key here + +}; + +let order = Order.HighToLow; +let sortBy = SortBy.Change; // Default sorting by change percentage + +function changeOrder(state: Order) { + order = state === Order.HighToLow ? Order.LowToHigh : Order.HighToLow; +} + +const sortItems = (tickerList: any[], key: string) => { + return tickerList?.sort((a, b) => { + const aValue = a[key] ?? 0; + const bValue = b[key] ?? 0; + return order === Order.HighToLow ? bValue - aValue : aValue - bValue; }); } - - $: { - if(order) - { - if(sortBy === 'change') - { - displayResults = sortByHighestChange(filteredData)?.slice(0,50); - } - else if(sortBy === 'marketCap') - { - displayResults = sortByMarketCap(filteredData)?.slice(0,50); - } - + if (order) { + const key = sortByKeys[sortBy]; // Use the mapping to get the key + displayResults = sortItems(filteredData, key)?.slice(0, 50); } } - $: { if(searchTerm) { @@ -940,13 +929,10 @@ async function popularStrategy(state: string) { - -
- - + -
+
{ruleOfList?.length !== 0 ? filteredData?.length : 0} Matches Found