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:
@@ -8,6 +8,7 @@ interface LoaderData {
|
||||
ticker: string;
|
||||
position: number | null;
|
||||
orders: any[];
|
||||
bars: any[];
|
||||
}
|
||||
|
||||
export async function loader({ params, request }: { params: { ticker: string }; request: Request }) {
|
||||
@@ -27,11 +28,25 @@ export async function loader({ params, request }: { params: { ticker: string };
|
||||
const ordersData = ordRes.ok ? await ordRes.json() : { orders: [] };
|
||||
const orders = ordersData.orders?.filter((o: any) => o.symbol === ticker) || [];
|
||||
|
||||
// Fetch bars for chart
|
||||
const barsRes = await fetch(`${baseUrl}/api/alpaca/quote/${ticker}`);
|
||||
const barsData = barsRes.ok ? await barsRes.json() : null;
|
||||
const bars = barsData?.bars || [];
|
||||
|
||||
return Response.json({ ticker, position, orders });
|
||||
}
|
||||
|
||||
export default function StockDetail() {
|
||||
const { ticker, position, orders } = useLoaderData() as LoaderData;
|
||||
const { ticker, position, orders, bars } = useLoaderData() as LoaderData;
|
||||
|
||||
// Convert Alpaca bars to TradingView format
|
||||
const chartData = bars?.map((bar: any) => ({
|
||||
time: bar.t,
|
||||
open: bar.o,
|
||||
high: bar.h,
|
||||
low: bar.l,
|
||||
close: bar.c,
|
||||
})) || [];
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gradient-to-br from-gray-50 to-blue-50">
|
||||
@@ -39,7 +54,7 @@ export default function StockDetail() {
|
||||
<div className="mx-auto max-w-7xl px-6 py-8">
|
||||
<h1 className="text-3xl font-bold text-gray-900 mb-6">{ticker} Detail</h1>
|
||||
|
||||
<TradingViewChart ticker={ticker} />
|
||||
<TradingViewChart ticker={ticker} data={chartData} />
|
||||
|
||||
<div className="mt-6 bg-white rounded-xl shadow-lg p-6 border border-gray-200">
|
||||
<h2 className="text-xl font-bold text-gray-900 mb-4">Position</h2>
|
||||
|
||||
Reference in New Issue
Block a user