Files
AITrader/app/routes/api/alpaca/positions.ts
T
henry 15e49cb0f9
Run Tests / test (push) Failing after 8s
feat(tests): update Alpaca API tests to include range parameters and improve stock database cleanup
- Modified Alpaca Historical Bars tests to include range parameters in API requests.
- Updated test descriptions for clarity.
- Added cleanup step to delete test ticker after verification in stock database tests.
- Adjusted Vitest configuration to exclude test files from coverage.
2026-05-14 16:46:28 +02:00

28 lines
1.0 KiB
TypeScript

import Alpaca from "@alpacahq/alpaca-trade-api";
const alpaca = new Alpaca({
keyId: process.env.ALPACA_API_KEY!,
secretKey: process.env.ALPACA_SECRET_KEY!,
baseUrl: process.env.ALPACA_BASE_URL || "https://paper-api.alpaca.markets",
dataBaseUrl: process.env.ALPACA_DATA_URL || "https://data.alpaca.markets",
retryOnError: false,
});
export async function loader() {
try {
const positions = await alpaca.getPositions();
return Response.json(
positions.map((p: { symbol: string; qty: string; avg_entry_price: string; current_price: string; market_value: string; unrealized_pl: string }) => ({
ticker: p.symbol,
qty: parseFloat(p.qty),
avg_entry_price: parseFloat(p.avg_entry_price),
current_price: parseFloat(p.current_price),
market_value: parseFloat(p.market_value),
unrealized_pl: parseFloat(p.unrealized_pl),
}))
);
} catch (error) {
console.error("Alpaca positions error:", error);
return Response.json({ error: "Failed to fetch positions" }, { status: 500 });
}
}