Show full company name in most-actives API; ensure name column displays canonical company name
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -1,9 +1,18 @@
|
||||
import type { MostActiveStock } from "../../../types";
|
||||
import Alpaca from "@alpacahq/alpaca-trade-api";
|
||||
|
||||
const ALPACA_API_KEY = process.env.ALPACA_API_KEY!;
|
||||
const ALPACA_SECRET_KEY = process.env.ALPACA_SECRET_KEY!;
|
||||
const ALPACA_DATA_URL = process.env.ALPACA_DATA_URL || "https://data.alpaca.markets";
|
||||
|
||||
const alpaca = new Alpaca({
|
||||
keyId: ALPACA_API_KEY,
|
||||
secretKey: ALPACA_SECRET_KEY,
|
||||
baseUrl: process.env.ALPACA_BASE_URL || "https://paper-api.alpaca.markets",
|
||||
dataBaseUrl: ALPACA_DATA_URL,
|
||||
retryOnError: false,
|
||||
});
|
||||
|
||||
export async function loader() {
|
||||
try {
|
||||
const response = await fetch(`${ALPACA_DATA_URL}/v1beta1/screener/stocks/most-actives`, {
|
||||
@@ -26,6 +35,20 @@ export async function loader() {
|
||||
volume: parseInt(item.volume) || 0,
|
||||
}));
|
||||
|
||||
// If Alpaca's screener returned a symbol as the name, try to fetch the canonical asset name
|
||||
await Promise.all(stocks.map(async (s) => {
|
||||
if (s.name === s.symbol) {
|
||||
try {
|
||||
const asset = await alpaca.getAsset(s.symbol);
|
||||
if (asset && (asset as any).name) {
|
||||
s.name = (asset as any).name;
|
||||
}
|
||||
} catch (err) {
|
||||
// ignore and keep existing name
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
return Response.json(stocks);
|
||||
} catch (error) {
|
||||
console.error("Most active stocks API error:", error);
|
||||
|
||||
Reference in New Issue
Block a user