bugfixing show correct change percentage

This commit is contained in:
MuslemRahimi 2024-11-05 19:07:23 +01:00
parent f8b27e1c1c
commit 8ed0e34c21

View File

@ -43,7 +43,7 @@
$: previousClose = data?.getStockQuote?.previousClose; $: previousClose = data?.getStockQuote?.previousClose;
//============================================// //============================================//
const intervals = ["1D", "1W", "1M", "1Y", "MAX"]; const intervals = ["1D", "1W", "1M", "6M", "1Y", "MAX"];
let chart = null; let chart = null;
async function checkChart() { async function checkChart() {
@ -60,66 +60,85 @@
$: { $: {
if (output !== null) { if (output !== null) {
let change; let change;
let formattedDate; let currentDataRow;
let safeFormattedDate; let baseClose;
if (displayData === "1D") { // Determine current data row and base close price based on displayData
const length = oneDayPrice?.length; switch (displayData) {
for (let i = length - 1; i >= 0; i--) { case "1D":
if (!isNaN(oneDayPrice[i]?.close)) { const length = oneDayPrice?.length;
currentDataRow = oneDayPrice[i]; for (let i = length - 1; i >= 0; i--) {
break; if (!isNaN(oneDayPrice[i]?.close)) {
currentDataRow = oneDayPrice[i];
break;
}
} }
} baseClose = previousClose;
break;
case "1W":
currentDataRow = oneWeekPrice?.at(-1); // Latest entry for 1 week
baseClose = oneWeekPrice?.[0]?.close;
break;
case "1M":
currentDataRow = oneMonthPrice?.at(-1); // Latest entry for 1 month
baseClose = oneMonthPrice?.[0]?.close;
break;
case "6M":
currentDataRow = sixMonthPrice?.at(-1); // Latest entry for 6 months
baseClose = sixMonthPrice?.[0]?.close;
break;
case "1Y":
currentDataRow = oneYearPrice?.at(-1); // Latest entry for 1 year
baseClose = oneYearPrice?.[0]?.close;
break;
case "MAX":
currentDataRow = maxPrice?.at(-1); // Latest entry for MAX range
baseClose = maxPrice?.[0]?.close;
break;
} }
if ($realtimePrice !== null) { // Calculate percentage change if baseClose and currentDataRow are valid
change = (($realtimePrice / previousClose - 1) * 100).toFixed(2); const closeValue =
} else { displayData === "1D" &&
change = !$isCrosshairMoveActive &&
displayData === "1D" $realtimePrice !== null
? ((currentDataRow?.close ?? currentDataRow?.value) / ? $realtimePrice
previousClose - : (currentDataRow?.close ?? currentDataRow?.value);
1) *
100 if (closeValue && baseClose) {
: ((currentDataRow?.close ?? currentDataRow?.value) / change = ((closeValue / baseClose - 1) * 100).toFixed(2);
displayLastLogicalRangeValue -
1) *
100;
change = change.toFixed(2);
} }
const date = new Date(currentDataRow?.time * 1000); // Format date
const date = new Date(currentDataRow?.time);
const options = { const options = {
day: "2-digit", day: "2-digit",
month: "short", month:
displayData === "1D" || displayData === "1W" || displayData === "1M"
? "short"
: "numeric",
year: "numeric", year: "numeric",
hour: "numeric", hour: "numeric",
minute: "2-digit", minute: "2-digit",
timeZone: "UTC",
}; };
formattedDate = const formattedDate = !isNaN(date)
displayData === "1D" || displayData === "1W" || displayData === "1M" ? date.toLocaleString("en-US", options)
? date.toLocaleString("en-US", options) : convertTimestamp(data?.getStockQuote?.timestamp);
: date.toLocaleDateString("en-US", {
day: "2-digit",
month: "short",
year: "numeric",
});
safeFormattedDate =
formattedDate === "Invalid Date"
? convertTimestamp(data?.getStockQuote?.timestamp)
: formattedDate;
// Set display legend
displayLegend = { displayLegend = {
close: close:
currentDataRow?.value === "-" && currentDataRow?.close === undefined currentDataRow?.value ??
? data?.getStockQuote?.price currentDataRow?.close ??
: (currentDataRow?.close ?? currentDataRow?.value), data?.getStockQuote?.price,
date: safeFormattedDate, date: formattedDate,
change: change, change,
}; };
} }
} }