fix(stock-price): use previous-close for change calculation (#4674)
This commit is contained in:
@@ -24,12 +24,16 @@ export const fetchStockPriceHandler = createCachedWidgetRequestHandler({
|
|||||||
if (!firstResult) {
|
if (!firstResult) {
|
||||||
throw new Error("Received invalid data");
|
throw new Error("Received invalid data");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const priceHistory =
|
||||||
|
firstResult.indicators.quote[0]?.close.filter(
|
||||||
|
// Filter out null values from price arrays (Yahoo Finance returns null for missing data points)
|
||||||
|
(value) => value !== null && value !== undefined,
|
||||||
|
) ?? [];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
priceHistory:
|
priceHistory,
|
||||||
firstResult.indicators.quote[0]?.close.filter(
|
previousClose: firstResult.meta.previousClose ?? priceHistory[0] ?? 1,
|
||||||
// Filter out null values from price arrays (Yahoo Finance returns null for missing data points)
|
|
||||||
(value) => value !== null && value !== undefined,
|
|
||||||
) ?? [],
|
|
||||||
symbol: firstResult.meta.symbol,
|
symbol: firstResult.meta.symbol,
|
||||||
shortName: firstResult.meta.shortName,
|
shortName: firstResult.meta.shortName,
|
||||||
};
|
};
|
||||||
@@ -58,6 +62,7 @@ const dataSchema = z
|
|||||||
meta: z.object({
|
meta: z.object({
|
||||||
symbol: z.string(),
|
symbol: z.string(),
|
||||||
shortName: z.string(),
|
shortName: z.string(),
|
||||||
|
previousClose: z.number().optional(),
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -13,12 +13,12 @@ function round(value: number) {
|
|||||||
return Math.round(value * 100) / 100;
|
return Math.round(value * 100) / 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
function calculateChange(valueA: number, valueB: number) {
|
function calculateChange(currentPrice: number, previousClose: number) {
|
||||||
return valueA - valueB;
|
return currentPrice - previousClose;
|
||||||
}
|
}
|
||||||
|
|
||||||
function calculateChangePercentage(valueA: number, valueB: number) {
|
function calculateChangePercentage(currentPrice: number, previousClose: number) {
|
||||||
return 100 * ((valueA - valueB) / valueA);
|
return 100 * ((currentPrice - previousClose) / previousClose);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function StockPriceWidget({ options, width, height }: WidgetComponentProps<"stockPrice">) {
|
export default function StockPriceWidget({ options, width, height }: WidgetComponentProps<"stockPrice">) {
|
||||||
@@ -26,9 +26,9 @@ export default function StockPriceWidget({ options, width, height }: WidgetCompo
|
|||||||
const theme = useMantineTheme();
|
const theme = useMantineTheme();
|
||||||
const [{ data }] = clientApi.widget.stockPrice.getPriceHistory.useSuspenseQuery(options);
|
const [{ data }] = clientApi.widget.stockPrice.getPriceHistory.useSuspenseQuery(options);
|
||||||
|
|
||||||
const stockValuesChange = round(calculateChange(data.priceHistory.at(-1) ?? 0, data.priceHistory[0] ?? 0));
|
const stockValuesChange = round(calculateChange(data.priceHistory.at(-1) ?? 0, data.previousClose));
|
||||||
const stockValuesChangePercentage = round(
|
const stockValuesChangePercentage = round(
|
||||||
calculateChangePercentage(data.priceHistory.at(-1) ?? 0, data.priceHistory[0] ?? 0),
|
calculateChangePercentage(data.priceHistory.at(-1) ?? 0, data.previousClose),
|
||||||
);
|
);
|
||||||
|
|
||||||
const stockValuesMin = Math.min(...data.priceHistory);
|
const stockValuesMin = Math.min(...data.priceHistory);
|
||||||
|
|||||||
Reference in New Issue
Block a user