add adj price

This commit is contained in:
MuslemRahimi 2025-04-11 23:07:33 +02:00
parent d5638e5e98
commit b393cf0475
2 changed files with 64 additions and 53 deletions

View File

@ -1,10 +1,10 @@
export const load = async ({ locals, params }) => {
const { apiKey, apiURL } = locals;
const getData = async () => {
const postData = { ticker: params.tickerID, timePeriod: 'max' };
const postData = { ticker: params.tickerID };
const response = await fetch(apiURL + "/historical-price", {
const response = await fetch(apiURL + "/historical-adj-price", {
method: "POST",
headers: {
"Content-Type": "application/json",

View File

@ -64,33 +64,51 @@
periodStart,
periodKey,
open: entry.open,
adjOpen: entry.adjOpen,
high: entry.high,
adjHigh: entry.adjHigh,
low: entry.low,
adjLow: entry.adjLow,
close: entry.close,
adjClose: entry.adjClose,
volume: entry.volume,
};
aggregatedData.push(currentPeriod);
} else {
// Update the current period's values
// High values should be the maximum observed so far.
currentPeriod.high = Math.max(currentPeriod.high, entry.high);
currentPeriod.adjHigh = Math.max(
currentPeriod.adjHigh,
entry.adjHigh,
);
// Low values should be the minimum observed so far.
currentPeriod.low = Math.min(currentPeriod.low, entry.low);
currentPeriod.close = entry.close; // Update the close to the most recent in the period
currentPeriod.adjLow = Math.min(currentPeriod.adjLow, entry.adjLow);
// For close values, use the most recent (current) close.
currentPeriod.close = entry.close;
currentPeriod.adjClose = entry.adjClose;
// Sum volumes.
currentPeriod.volume += entry.volume;
}
});
// Replace Daily data with aggregated data
// Replace Daily data with aggregated data including adjusted values
data = aggregatedData.map((period) => ({
time: period.periodStart.toISOString().split("T")[0],
open: period.open,
adjOpen: period.adjOpen,
high: period.high,
adjHigh: period.adjHigh,
low: period.low,
adjLow: period.adjLow,
close: period.close,
adjClose: period.adjClose,
volume: period.volume,
}));
}
// Process the data to add change and changesPercentage
// Process the data to add change and changesPercentage (using non-adjusted close values)
const modifiedData = data?.map((entry, index, arr) => {
if (index === 0) {
return { ...entry, change: null, changesPercentage: null };
@ -102,7 +120,7 @@
previousClose !== 0
? (((currentClose - previousClose) / previousClose) * 100)?.toFixed(2)
: null;
return { ...entry, change, changesPercentage };
return { ...entry, changesPercentage };
});
// Sort the data by "time" from latest to earliest
@ -130,10 +148,13 @@
$: columns = [
{ key: "time", label: "Date", align: "left" },
{ key: "open", label: "Open", align: "right" },
{ key: "adjOpen", label: "Adj Open", align: "right" },
{ key: "high", label: "High", align: "right" },
{ key: "adjHigh", label: "Adj High", align: "right" },
{ key: "low", label: "Low", align: "right" },
{ key: "adjLow", label: "Adj Low", align: "right" },
{ key: "close", label: "Close", align: "right" },
{ key: "change", label: "Change", align: "right" },
{ key: "adjClose", label: "Adj Close", align: "right" },
{ key: "changesPercentage", label: "% Change", align: "right" },
{ key: "volume", label: "Volume", align: "right" },
];
@ -141,10 +162,13 @@
$: sortOrders = {
time: { order: "none", type: "date" },
open: { order: "none", type: "number" },
adjOpen: { order: "none", type: "number" },
high: { order: "none", type: "number" },
adjHigh: { order: "none", type: "number" },
low: { order: "none", type: "number" },
adjLow: { order: "none", type: "number" },
close: { order: "none", type: "number" },
change: { order: "none", type: "number" },
adjClose: { order: "none", type: "number" },
changesPercentage: { order: "none", type: "number" },
volume: { order: "none", type: "number" },
};
@ -212,19 +236,25 @@
({
time,
open,
adjOpen,
high,
adjHigh,
low,
adjLow,
close,
change,
adjClose,
changesPercentage,
volume,
}) => ({
time,
open,
adjOpen,
high,
adjHigh,
low,
adjLow,
close,
change,
adjClose,
changesPercentage,
volume,
}),
@ -233,11 +263,13 @@
const csvRows = [];
// Add headers row
csvRows.push("time,open,high,low,close,change,changesPercentage,volume");
csvRows.push(
"time,open,adjOpen,high,adjHigh,low,adjLow,close,adjClose,changesPercentage,volume",
);
// Add data rows
for (const row of exportList) {
const csvRow = `${row.time},${row.open},${row.high},${row.low},${row.close},${row.change},${row.changesPercentage},${row.volume}`;
const csvRow = `${row.time},${row.open},${row.adjOpen},${row.high},${row.adjHigh},${row.low},${row.adjLow},${row.close},${row.adjClose},${row.changesPercentage},${row.volume}`;
csvRows.push(csvRow);
}
@ -279,7 +311,7 @@
<div
class="relative flex justify-center items-start overflow-hidden w-full"
>
<main class="w-full lg:w-3/4 lg:pr-10">
<main class="w-full">
<div class="sm:pl-7 sm:pb-7 sm:pt-7 w-full m-auto">
<div
class="flex flex-col sm:flex-row items-start w-full sm:justify-between md:space-x-4 md:border-0 w-full mb-3"
@ -446,11 +478,21 @@
>
{item?.open?.toFixed(2)}
</td>
<td
class="text-end text-sm sm:text-[1rem] whitespace-nowrap"
>
{item?.adjOpen?.toFixed(2)}
</td>
<td
class="text-end text-sm sm:text-[1rem] whitespace-nowrap"
>
{item?.high?.toFixed(2)}
</td>
<td
class="text-end text-sm sm:text-[1rem] whitespace-nowrap"
>
{item?.adjHigh?.toFixed(2)}
</td>
<td
class="text-end text-sm sm:text-[1rem] whitespace-nowrap"
>
@ -459,13 +501,20 @@
<td
class="text-end text-sm sm:text-[1rem] whitespace-nowrap"
>
{item?.close?.toFixed(2)}
{item?.adjLow?.toFixed(2)}
</td>
<td
class="text-end text-sm sm:text-[1rem] whitespace-nowrap"
>
{item?.change !== null ? item?.change : "n/a"}
{item?.close?.toFixed(2)}
</td>
<td
class="text-end text-sm sm:text-[1rem] whitespace-nowrap"
>
{item?.adjClose?.toFixed(2)}
</td>
<td
class="text-sm sm:text-[1rem] {item?.changesPercentage >=
0 && item?.changesPercentage !== null
@ -497,44 +546,6 @@
{/if}
</div>
</main>
<aside class="hidden lg:block relative fixed w-1/4 mt-3">
<div
class="w-full border border-gray-300 dark:border-gray-600 rounded-md h-fit pb-4 mt-4 cursor-pointer sm:hover:shadow-lg dark:sm:hover:bg-secondary transition ease-out duration-100"
>
<a
href="/stock-screener"
class="w-auto lg:w-full p-1 flex flex-col m-auto px-2 sm:px-0"
>
<div class="w-full flex justify-between items-center p-3 mt-3">
<h2 class="text-start text-xl font-semibold ml-3">
Stock Screener
</h2>
<ArrowLogo class="w-8 h-8 mr-3 shrink-0 text-gray-400 dark:" />
</div>
<span class="p-3 ml-3 mr-3">
Filter, sort and analyze all stocks to find your next
investment.
</span>
</a>
</div>
<div
class="w-full border border-gray-300 dark:border-gray-600 rounded-md h-fit pb-4 mt-4 cursor-pointer sm:hover:shadow-lg dark:sm:hover:bg-secondary transition ease-out duration-100"
>
<a
href="/watchlist/stocks"
class="w-auto lg:w-full p-1 flex flex-col m-auto px-2 sm:px-0"
>
<div class="w-full flex justify-between items-center p-3 mt-3">
<h2 class="text-start text-xl font-semibold ml-3">Watchlist</h2>
<ArrowLogo class="w-8 h-8 mr-3 shrink-0 text-gray-400 dark:" />
</div>
<span class="p-3 ml-3 mr-3">
Keep track of your favorite stocks in real-time.
</span>
</a>
</div>
</aside>
</div>
</div>
</div>