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