From b393cf04759fcffff5cd3b5a66785a1592af6f62 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Fri, 11 Apr 2025 23:07:33 +0200 Subject: [PATCH] add adj price --- .../stocks/[tickerID]/history/+page.server.ts | 4 +- .../stocks/[tickerID]/history/+page.svelte | 113 ++++++++++-------- 2 files changed, 64 insertions(+), 53 deletions(-) diff --git a/src/routes/stocks/[tickerID]/history/+page.server.ts b/src/routes/stocks/[tickerID]/history/+page.server.ts index b6f18331..63d36f87 100644 --- a/src/routes/stocks/[tickerID]/history/+page.server.ts +++ b/src/routes/stocks/[tickerID]/history/+page.server.ts @@ -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", diff --git a/src/routes/stocks/[tickerID]/history/+page.svelte b/src/routes/stocks/[tickerID]/history/+page.svelte index 3de0a6d6..7c868ea6 100644 --- a/src/routes/stocks/[tickerID]/history/+page.svelte +++ b/src/routes/stocks/[tickerID]/history/+page.svelte @@ -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 @@
-
+
{item?.open?.toFixed(2)} + + {item?.adjOpen?.toFixed(2)} + {item?.high?.toFixed(2)} + + {item?.adjHigh?.toFixed(2)} + @@ -459,13 +501,20 @@ - {item?.close?.toFixed(2)} + {item?.adjLow?.toFixed(2)} - {item?.change !== null ? item?.change : "n/a"} + {item?.close?.toFixed(2)} + + + {item?.adjClose?.toFixed(2)} + + - - - -