update stock screener
This commit is contained in:
parent
b7eaef761d
commit
9afc5bf2c4
@ -1,7 +1,7 @@
|
|||||||
<script lang='ts'>
|
<script lang='ts'>
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import { goto} from '$app/navigation';
|
import { goto} from '$app/navigation';
|
||||||
import { userRegion, screenWidth, strategyId, numberOfUnreadNotification } from '$lib/store';
|
import { screenWidth, strategyId, numberOfUnreadNotification, getCache, setCache} from '$lib/store';
|
||||||
import toast from 'svelte-french-toast';
|
import toast from 'svelte-french-toast';
|
||||||
import { abbreviateNumber, formatRuleValue } from '$lib/utils';
|
import { abbreviateNumber, formatRuleValue } from '$lib/utils';
|
||||||
|
|
||||||
@ -11,18 +11,6 @@
|
|||||||
export let data;
|
export let data;
|
||||||
export let form;
|
export let form;
|
||||||
|
|
||||||
const usRegion = ['cle1','iad1','pdx1','sfo1'];
|
|
||||||
let fastifyURL;
|
|
||||||
userRegion.subscribe(value => {
|
|
||||||
|
|
||||||
if (usRegion.includes(value)) {
|
|
||||||
fastifyURL = import.meta.env.VITE_USEAST_FASTIFY_URL;
|
|
||||||
} else {
|
|
||||||
fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
$strategyId = data?.getStrategyId;
|
$strategyId = data?.getStrategyId;
|
||||||
let ruleOfList = data?.getStrategy?.rules ?? [];
|
let ruleOfList = data?.getStrategy?.rules ?? [];
|
||||||
|
|
||||||
@ -40,6 +28,33 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
const getStockScreenerData = async (rules) => {
|
||||||
|
const ruleNames = rules?.map(rule => rule?.name)?.sort()?.join(',');
|
||||||
|
const cachedData = getCache(ruleNames, 'getStockScreenerData');
|
||||||
|
|
||||||
|
if (cachedData) {
|
||||||
|
console.log('Using cached data');
|
||||||
|
return cachedData;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Fetching new data from API');
|
||||||
|
const postData = { ruleOfList: rules?.map(rule => rule.name) };
|
||||||
|
const response = await fetch(data?.apiURL + '/stock-screener-data', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"X-API-KEY": data?.apiKey
|
||||||
|
},
|
||||||
|
body: JSON.stringify(postData)
|
||||||
|
});
|
||||||
|
const output = await response.json();
|
||||||
|
|
||||||
|
// Cache the new data
|
||||||
|
setCache(ruleNames, output, 'getStockScreenerData');
|
||||||
|
|
||||||
|
return output;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
let filteredData = [];
|
let filteredData = [];
|
||||||
let displayResults = [];
|
let displayResults = [];
|
||||||
@ -101,7 +116,6 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let allRows = [
|
let allRows = [
|
||||||
{ rule: 'avgVolume', label: 'Average Volume',category: 'fund' },
|
{ rule: 'avgVolume', label: 'Average Volume',category: 'fund' },
|
||||||
{ rule: 'rsi', label: 'Relative Strength Index (RSI)',category: 'ta' },
|
{ rule: 'rsi', label: 'Relative Strength Index (RSI)',category: 'ta' },
|
||||||
@ -391,7 +405,7 @@ function handleAddRule() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
function handleRule(newRule) {
|
async function handleRule(newRule) {
|
||||||
const existingRuleIndex = ruleOfList.findIndex(rule => rule.name === ruleName);
|
const existingRuleIndex = ruleOfList.findIndex(rule => rule.name === ruleName);
|
||||||
if (existingRuleIndex !== -1) {
|
if (existingRuleIndex !== -1) {
|
||||||
const existingRule = ruleOfList[existingRuleIndex];
|
const existingRule = ruleOfList[existingRuleIndex];
|
||||||
@ -401,17 +415,37 @@ function handleAddRule() {
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
ruleOfList[existingRuleIndex] = newRule;
|
ruleOfList[existingRuleIndex] = newRule;
|
||||||
|
ruleOfList = [...ruleOfList]; // Trigger reactivity
|
||||||
toast.success('Rule updated', {
|
toast.success('Rule updated', {
|
||||||
style: 'border-radius: 200px; background: #333; color: #fff;'
|
style: 'border-radius: 200px; background: #333; color: #fff;'
|
||||||
});
|
});
|
||||||
//ruleName = '';
|
await updateStockScreenerData();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ruleOfList = [...ruleOfList, newRule];
|
ruleOfList = [...ruleOfList, newRule];
|
||||||
toast.success('Rule added', {
|
toast.success('Rule added', {
|
||||||
style: 'border-radius: 200px; background: #333; color: #fff;'
|
style: 'border-radius: 200px; background: #333; color: #fff;'
|
||||||
});
|
});
|
||||||
//ruleName = '';
|
await updateStockScreenerData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function updateStockScreenerData() {
|
||||||
|
try {
|
||||||
|
const newData = await getStockScreenerData(ruleOfList);
|
||||||
|
stockScreenerData = newData?.filter(item => {
|
||||||
|
const ratingRecommendationExists = item?.ratingRecommendation !== null;
|
||||||
|
const trendAnalysisAccuracyExists = item?.trendAnalysis?.accuracy !== null;
|
||||||
|
const fundamentalAnalysisAccuracyExists = item?.fundamentalAnalysis?.accuracy !== null;
|
||||||
|
return ratingRecommendationExists && trendAnalysisAccuracyExists && fundamentalAnalysisAccuracyExists;
|
||||||
|
});
|
||||||
|
filteredData = filterStockScreenerData();
|
||||||
|
displayResults = filteredData?.slice(0, 10);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching new stock screener data:', error);
|
||||||
|
toast.error('Failed to update stock data. Please try again.', {
|
||||||
|
style: 'border-radius: 200px; background: #333; color: #fff;'
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -490,7 +524,7 @@ async function handleSave(state:string) {
|
|||||||
{
|
{
|
||||||
const postData = {'strategyId': $strategyId, 'rules': ruleOfList}
|
const postData = {'strategyId': $strategyId, 'rules': ruleOfList}
|
||||||
|
|
||||||
const response = await fetch(fastifyURL+'/save-strategy', {
|
const response = await fetch(data?.fastifyURL+'/save-strategy', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
|
|||||||
@ -50,25 +50,29 @@ export const load = async ({params}) => {
|
|||||||
|
|
||||||
const getStockScreenerData = async () => {
|
const getStockScreenerData = async () => {
|
||||||
let output;
|
let output;
|
||||||
|
const strategy = await getStrategy();
|
||||||
|
const ruleOfList = strategy?.rules?.map(item => item?.name) || [];
|
||||||
|
const ruleNames = ruleOfList.sort().join(',');
|
||||||
// Get cached data for the specific tickerID
|
// Get cached data for the specific tickerID
|
||||||
const cachedData = getCache('', 'getStockScreenerData');
|
const cachedData = getCache(ruleNames, 'getStockScreenerData');
|
||||||
if (cachedData) {
|
if (cachedData) {
|
||||||
output = cachedData;
|
output = cachedData;
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
const postData = {'ruleOfList': ruleOfList}
|
||||||
// make the POST request to the endpoint
|
// make the POST request to the endpoint
|
||||||
const response = await fetch(apiURL + '/stock-screener-data', {
|
const response = await fetch(apiURL + '/stock-screener-data', {
|
||||||
method: 'GET',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json", "X-API-KEY": apiKey
|
"Content-Type": "application/json", "X-API-KEY": apiKey
|
||||||
},
|
},
|
||||||
|
body: JSON.stringify(postData)
|
||||||
});
|
});
|
||||||
|
|
||||||
output = await response.json();
|
output = await response.json();
|
||||||
|
|
||||||
// Cache the data for this specific tickerID with a specific name 'getStockScreenerData'
|
// Cache the data for this specific tickerID with a specific name 'getStockScreenerData'
|
||||||
setCache('', output, 'getStockScreenerData');
|
setCache(ruleNames, output, 'getStockScreenerData');
|
||||||
}
|
}
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
@ -78,6 +82,9 @@ export const load = async ({params}) => {
|
|||||||
return {
|
return {
|
||||||
getStockScreenerData: await getStockScreenerData(),
|
getStockScreenerData: await getStockScreenerData(),
|
||||||
getStrategy: await getStrategy(),
|
getStrategy: await getStrategy(),
|
||||||
getStrategyId: await getStrategyId()
|
getStrategyId: await getStrategyId(),
|
||||||
|
apiURL,
|
||||||
|
fastifyURL,
|
||||||
|
apiKey,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user