sorted csv

This commit is contained in:
Seth Trowbridge 2026-01-17 14:25:59 -05:00
parent e0af9077ef
commit 207c0067b3

View File

@ -1,5 +1,8 @@
#!/usr/bin/env -S deno run --allow-net --allow-write
//?apikey=1zIRD8Xy8WLOOcTEjT94JnyHBeXDOTcS
// ------------------------------------------------------------
// Linear Regression
// ------------------------------------------------------------
@ -121,17 +124,14 @@ async function throttle<T>(
// ------------------------------------------------------------
async function Dump() {
const spx = await ComputeTicker("^GSPC");
if (!spx) {
console.error("Could not get S&P Index data");
return;
}
const rows: string[] = ["ticker,slope,intercept,r2,growth"];
const results: { ticker: string; model: ReturnType<typeof linearRegression> }[] = [];
const addRow = (ticker: string, model: ReturnType<typeof linearRegression>) => {
rows.push(
`${ticker},${model.slope.toFixed(6)},${model.intercept.toFixed(6)},${model.r2.toFixed(6)},${model?.growth.toFixed(2)}`,
);
results.push({ ticker, model });
};
addRow("SPX", spx);
@ -142,8 +142,7 @@ async function Dump() {
`Finding stocks with slope better than the S&P Index slope (${spx.slope.toFixed(6)})...`,
);
// Throttle to avoid Yahoo soft throttling
const limit = 5; // adjust as needed
const limit = 5;
await throttle(tickers, limit, async (ticker) => {
try {
@ -152,14 +151,22 @@ async function Dump() {
addRow(ticker, model);
console.log(`${ticker}`);
}
else{
//console.log("bad: ", ticker, model?.slope)
}
} catch (e) {
console.log(`Skipping "${ticker}" because: ${e}`);
}
});
// Sort by growth descending
results.sort((a, b) => b.model.growth - a.model.growth);
// Serialize CSV
const rows = ["ticker,slope,intercept,r2,growth"];
for (const { ticker, model } of results) {
rows.push(
`${ticker},${model.growth.toFixed(2)}`
);
}
await Deno.writeTextFile("sp500_regression.csv", rows.join("\n"));
console.log("Dumped output to sp500_regression.csv");
}