fix: add bars data for TradingView chart from Alpaca
- Modify quote.ts to fetch historical bars for chart data - Update analyze.ticker.tsx to pass bars data to TradingViewChart - Chart now displays candlestick data from Alpaca API
This commit is contained in:
@@ -14,17 +14,36 @@ export async function loader({ params }: { params: { ticker: string } }) {
|
||||
}
|
||||
|
||||
try {
|
||||
// Use latest trade instead of quote for real transaction price
|
||||
// Get latest trade for current price
|
||||
const trade = await alpaca.getLatestTrade(ticker);
|
||||
// trade has: Price, Size, Exchange, Timestamp, etc.
|
||||
const price = (trade as { Price?: number }).Price || 0;
|
||||
|
||||
// Get historical bars for chart (last 30 days, daily)
|
||||
const bars = await alpaca.getBarsV2(ticker, {
|
||||
timeframe: "1Day",
|
||||
limit: 30,
|
||||
});
|
||||
|
||||
// Convert async generator to array
|
||||
const barsArray = [];
|
||||
for await (const bar of bars) {
|
||||
barsArray.push({
|
||||
t: (bar as any).Timestamp || (bar as any).t,
|
||||
o: (bar as any).Open || (bar as any).o,
|
||||
h: (bar as any).High || (bar as any).h,
|
||||
l: (bar as any).Low || (bar as any).l,
|
||||
c: (bar as any).Close || (bar as any).c,
|
||||
v: (bar as any).Volume || (bar as any).v,
|
||||
});
|
||||
}
|
||||
|
||||
return Response.json({
|
||||
ticker,
|
||||
price,
|
||||
timestamp: (trade as { Timestamp?: string }).Timestamp,
|
||||
bars: barsArray,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Alpaca trade error:", error);
|
||||
return Response.json({ error: "Failed to fetch trade" }, { status: 500 });
|
||||
console.error("Alpaca data error:", error);
|
||||
return Response.json({ error: "Failed to fetch data" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user