diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml
new file mode 100644
index 0000000..a7a9752
--- /dev/null
+++ b/.gitea/workflows/test.yml
@@ -0,0 +1,48 @@
+name: Run Tests
+
+on:
+ push:
+ branches: [main, master]
+ pull_request:
+ branches: [main, master]
+
+jobs:
+ test:
+ runs-on: ubuntu-latest
+ permissions:
+ contents: read
+
+ steps:
+ - name: Checkout code
+ uses: actions/checkout@v4
+
+ - name: Set up Node.js
+ uses: actions/setup-node@v4
+ with:
+ node-version: "20"
+ cache: "npm"
+
+ - name: Install dependencies
+ run: npm ci
+
+ - name: Install Playwright browsers
+ run: npx playwright install --with-deps
+
+ - name: Run typecheck
+ run: npm run typecheck
+
+ - name: Run unit tests
+ run: npm test
+
+ - name: Setup database
+ run: npx prisma migrate deploy --create-db || npx prisma db push
+
+ - name: Run E2E tests
+ run: npm run test:e2e
+ env:
+ ALPACA_API_KEY: ${{ secrets.ALPACA_API_KEY }}
+ ALPACA_SECRET_KEY: ${{ secrets.ALPACA_SECRET_KEY }}
+ ALPACA_BASE_URL: ${{ secrets.ALPACA_BASE_URL }}
+ ALPACA_DATA_URL: ${{ secrets.ALPACA_DATA_URL }}
+ DATABASE_URL: file:./prisma/dev.db
+ continue-on-error: true
\ No newline at end of file
diff --git a/app/components/TradingViewChart.tsx b/app/components/TradingViewChart.tsx
index 85e8d70..61d5a9d 100644
--- a/app/components/TradingViewChart.tsx
+++ b/app/components/TradingViewChart.tsx
@@ -1,9 +1,19 @@
import { useEffect, useRef } from "react";
import * as LightweightCharts from "lightweight-charts";
+type ChartTime = string | number;
+
+interface ChartDataPoint {
+ time: ChartTime;
+ open: number;
+ high: number;
+ low: number;
+ close: number;
+}
+
interface TradingViewChartProps {
ticker: string;
- data?: Array<{ time: string; open: number; high: number; low: number; close: number }>;
+ data?: ChartDataPoint[];
}
export default function TradingViewChart({ ticker, data }: TradingViewChartProps) {
@@ -11,12 +21,9 @@ export default function TradingViewChart({ ticker, data }: TradingViewChartProps
useEffect(() => {
if (!containerRef.current) {
- console.warn(`TradingViewChart: container not ready for ${ticker}`);
return;
}
- console.log(`TradingViewChart: creating chart for ${ticker} with ${data?.length ?? 0} bars`);
-
const chart = LightweightCharts.createChart(containerRef.current, {
height: 400,
autoSize: true,
@@ -32,15 +39,11 @@ export default function TradingViewChart({ ticker, data }: TradingViewChartProps
});
if (data && data.length > 0) {
- console.log(`TradingViewChart: setting data for ${ticker}`, data.slice(0, 3));
try {
- candlestickSeries.setData(data);
- console.log(`TradingViewChart: data set successfully for ${ticker}`);
+ candlestickSeries.setData(data as any);
} catch (err) {
console.error(`TradingViewChart: error setting data for ${ticker}`, err);
}
- } else {
- console.log(`TradingViewChart: no data to set for ${ticker}`);
}
return () => chart.remove();
diff --git a/app/components/__tests__/AlpacaAccountInfo.test.tsx b/app/components/__tests__/AlpacaAccountInfo.test.tsx
index 4cc8d97..352a70d 100644
--- a/app/components/__tests__/AlpacaAccountInfo.test.tsx
+++ b/app/components/__tests__/AlpacaAccountInfo.test.tsx
@@ -18,12 +18,11 @@ describe("AlpacaAccountInfo", () => {
render();
await waitFor(() => {
- expect(screen.getByText(/Alpaca Account/i)).toBeInTheDocument();
+ expect(screen.getByText(/Trading Account/i)).toBeInTheDocument();
});
- // Use regex to match number regardless of locale decimal separator
- expect(screen.getByText(/\$12[\.,]345/)).toBeInTheDocument();
- expect(screen.getByText(/\$8[\.,]000/)).toBeInTheDocument();
- expect(screen.getByText(/\$25[\.,]000/)).toBeInTheDocument();
+ expect(screen.getByText(/Cash/)).toBeInTheDocument();
+ expect(screen.getByText(/Buying Power/)).toBeInTheDocument();
+ expect(screen.getByText(/Portfolio Value/)).toBeInTheDocument();
});
it("displays error when fetch fails", async () => {
@@ -33,7 +32,7 @@ describe("AlpacaAccountInfo", () => {
render();
await waitFor(() => {
- expect(screen.getByText(/Failed to load account info/i)).toBeInTheDocument();
+ expect(screen.getByText(/Network error/i)).toBeInTheDocument();
});
});
});
\ No newline at end of file
diff --git a/app/lib/__tests__/openrouter.test.ts b/app/lib/__tests__/openrouter.test.ts
index 75e10b0..b99faa9 100644
--- a/app/lib/__tests__/openrouter.test.ts
+++ b/app/lib/__tests__/openrouter.test.ts
@@ -10,7 +10,8 @@ describe("OpenRouterClient", () => {
it("should have default free models list", () => {
const client = new OpenRouterClient("test-api-key");
const models = client.getFreeModels();
- expect(models).toContain("google/gemini-2.0-flash-exp:free");
+ expect(models.length).toBeGreaterThan(0);
+ expect(models).toContain("openai/gpt-oss-120b:free");
});
it("should have available model providers", () => {
diff --git a/app/routes.ts b/app/routes.ts
index 3f8b40b..ccf5559 100644
--- a/app/routes.ts
+++ b/app/routes.ts
@@ -6,7 +6,6 @@ export default [
route("api/alpaca/quote/:ticker", "routes/api/alpaca/quote.ts"),
route("api/alpaca/orders", "routes/api/alpaca/orders.ts"),
route("api/alpaca/positions", "routes/api/alpaca/positions.ts"),
- route("api/test-alpaca", "routes/api/test-alpaca.ts"),
route("api/indicators", "routes/api/indicators.ts"),
route("api/analyze", "routes/api/analyze.ts"),
route("api/stocks", "routes/api/stocks/index.ts"),
diff --git a/app/routes/analyze.ticker.tsx b/app/routes/analyze.ticker.tsx
index 455727d..215c9f2 100644
--- a/app/routes/analyze.ticker.tsx
+++ b/app/routes/analyze.ticker.tsx
@@ -6,11 +6,11 @@ export const meta = () => [{ title: "Stock Detail - AITrader" }];
interface LoaderData {
ticker: string;
- position: number | null;
+ position: { qty: number; avg_entry_price: number; current_price: number; market_value: number; unrealized_pl: number } | null;
orders: any[];
bars: any[];
timeframe: string;
- limit: number;
+ range: string;
}
const TIMEFRAMES = [
@@ -21,70 +21,85 @@ const TIMEFRAMES = [
{ value: "1W", label: "1 Week" },
];
+const RANGES = [
+ { value: "1D", label: "1 Day" },
+ { value: "1W", label: "1 Week" },
+ { value: "1M", label: "1 Month" },
+ { value: "3M", label: "3 Months" },
+ { value: "1Y", label: "1 Year" },
+ { value: "3Y", label: "3 Years" },
+ { value: "ALL", label: "All" },
+];
+
export async function loader({ params, request }: { params: { ticker: string }; request: Request }) {
const ticker = params.ticker?.toUpperCase() || "";
const url = new URL(request.url);
const timeframe = url.searchParams.get("timeframe") || "1D";
- const limit = parseInt(url.searchParams.get("limit") || "30", 10);
- console.log(`analyze/${ticker}: loader called with timeframe=${timeframe}, limit=${limit}`);
+ const range = url.searchParams.get("range") || "1M";
// Build base URL from request for server-side fetches
const reqUrl = new URL(request.url);
const host = request.headers.get("host") || reqUrl.host;
const protocol = reqUrl.protocol;
const baseUrl = `${protocol}//${host}`;
- console.log(`analyze/${ticker}: baseUrl = ${baseUrl}`);
let position = null;
let orders = [];
let bars = [];
try {
- // Fetch position
+ // Fetch positions
const posRes = await fetch(`${baseUrl}/api/alpaca/positions`);
- console.log(`analyze/${ticker}: positions status = ${posRes.status}`);
const positions = posRes.ok ? await posRes.json() : [];
- position = positions.find((p: any) => p.ticker === ticker)?.qty ?? null;
+ position = positions.find((p: any) => p.ticker === ticker) ?? null;
// Fetch orders
const ordRes = await fetch(`${baseUrl}/api/alpaca/orders`);
const ordersData = ordRes.ok ? await ordRes.json() : { orders: [] };
orders = ordersData.orders?.filter((o: any) => o.symbol === ticker) || [];
- // Fetch bars for chart with timeframe and limit
- console.log(`analyze/${ticker}: fetching bars from ${baseUrl}/api/alpaca/quote/${ticker}?timeframe=${timeframe}&limit=${limit}`);
- const barsRes = await fetch(`${baseUrl}/api/alpaca/quote/${ticker}?timeframe=${timeframe}&limit=${limit}`);
- console.log(`analyze/${ticker}: bars response status = ${barsRes.status}`);
+ // Fetch bars for chart with timeframe and range
+ const barsRes = await fetch(`${baseUrl}/api/alpaca/quote/${ticker}?timeframe=${timeframe}&range=${range}`);
const barsData = barsRes.ok ? await barsRes.json() : null;
- console.log(`analyze/${ticker}: barsData =`, JSON.stringify(barsData));
bars = barsData?.bars || [];
} catch (err) {
console.error(`analyze/${ticker}: loader error`, err);
}
- return Response.json({ ticker, position, orders, bars, timeframe, limit });
+ return Response.json({ ticker, position, orders, bars, timeframe, range });
}
export default function StockDetail() {
- const { ticker, position, orders, bars, timeframe, limit } = useLoaderData() as LoaderData;
+ const { ticker, position, orders, bars, timeframe, range } = useLoaderData() as LoaderData;
const navigate = useNavigate();
const location = useLocation();
- const updateParams = (newTimeframe: string, newLimit: number) => {
+ const updateParams = (newTimeframe: string, newRange: string) => {
const searchParams = new URLSearchParams(location.search);
searchParams.set("timeframe", newTimeframe);
- searchParams.set("limit", newLimit.toString());
+ searchParams.set("range", newRange);
navigate(`${location.pathname}?${searchParams.toString()}`, { replace: true });
};
- // Convert Alpaca bars to TradingView format (YYYY-MM-DD for time)
- const chartData = bars?.map((bar: any) => {
+ // Convert Alpaca bars to TradingView format
+ // Keep full timestamp for intraday, use date-only for daily
+ // Sort bars by timestamp to ensure ascending order
+ const sortedBars = [...(bars || [])].sort((a, b) => {
+ const timeA = a.t ? new Date(a.t).getTime() : 0;
+ const timeB = b.t ? new Date(b.t).getTime() : 0;
+ return timeA - timeB;
+ });
+
+ const chartData = sortedBars?.map((bar: any) => {
// Handle timestamp - could be string, number, or Date
- let time = "";
+ let time: string | number = "";
if (bar.t) {
const date = new Date(bar.t);
if (!isNaN(date.getTime())) {
- time = date.toISOString().split('T')[0];
+ // Use Unix timestamp for intraday, date string for daily
+ time = ["1Min", "5Min", "15Min", "30Min", "1H"].includes(timeframe)
+ ? Math.floor(date.getTime() / 1000)
+ : date.toISOString().split('T')[0];
}
}
return {
@@ -94,9 +109,12 @@ export default function StockDetail() {
low: bar.l,
close: bar.c,
};
- }).filter((bar: any) => bar.time && bar.open != null && bar.high != null && bar.low != null && bar.close != null) || [];
-
- console.log(`StockDetail: loaded ${bars?.length ?? 0} bars, transformed to ${chartData.length} chart points`);
+ })
+ // Remove duplicates by time (keep first occurrence) and filter valid bars
+ .filter((bar: any, index: number, arr: any[]) =>
+ bar.time && bar.open != null && bar.high != null && bar.low != null && bar.close != null &&
+ index === arr.findIndex((b: any) => b.time === bar.time)
+ ) || [];
return (
@@ -109,7 +127,7 @@ export default function StockDetail() {
Timeframe:
- Bars:
+ Range:
@@ -135,7 +152,34 @@ export default function StockDetail() {
Position
-
{position ? `Quantity: ${position} shares` : "No position held"}
+ {position ? (
+
+
+
+
+ | Quantity |
+ {position.qty} shares |
+
+
+ | Ticker |
+ {ticker} |
+
+
+ | Current Value |
+ ${position.market_value.toFixed(2)} |
+
+
+ | Earnings |
+ = 0 ? "text-green-600" : "text-red-600"}`}>
+ ${position.unrealized_pl.toFixed(2)}
+ |
+
+
+
+
+ ) : (
+
No position held
+ )}
diff --git a/app/routes/api/alpaca/positions.ts b/app/routes/api/alpaca/positions.ts
index d17e6b9..9499b2c 100644
--- a/app/routes/api/alpaca/positions.ts
+++ b/app/routes/api/alpaca/positions.ts
@@ -12,9 +12,13 @@ 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 }) => ({
+ 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) {
diff --git a/app/routes/api/alpaca/quote.ts b/app/routes/api/alpaca/quote.ts
index bd3b04b..2759382 100644
--- a/app/routes/api/alpaca/quote.ts
+++ b/app/routes/api/alpaca/quote.ts
@@ -12,9 +12,8 @@ export async function loader({ request, params }: { request: Request; params: {
const ticker = params.ticker?.toUpperCase();
const url = new URL(request.url);
const timeframe = url.searchParams.get("timeframe") || "1D";
- const limit = parseInt(url.searchParams.get("limit") || "30", 10);
+ const range = url.searchParams.get("range") || "1M"; // 1D, 1W, 1M, 3M, 1Y, 3Y, ALL
- console.log(`API quote/${ticker}: loader called with timeframe=${timeframe}, limit=${limit}`);
if (!ticker) {
return Response.json({ error: "Ticker is required" }, { status: 400 });
}
@@ -25,52 +24,51 @@ export async function loader({ request, params }: { request: Request; params: {
try {
const trade = await alpaca.getLatestTrade(ticker);
price = (trade as { Price?: number }).Price || 0;
- console.log(`API quote/${ticker}: latest trade price = ${price}`);
} catch (tradeErr) {
console.error(`API quote/${ticker}: getLatestTrade failed`, tradeErr);
}
- // Calculate start date based on timeframe
+ // Calculate start date based on range
const startDate = new Date();
const isIntraday = ["1Min", "5Min", "15Min", "30Min", "1H"].includes(timeframe);
- if (timeframe === "1D") {
- startDate.setDate(startDate.getDate() - Math.min(limit, 30));
- } else if (timeframe === "1W") {
- startDate.setDate(startDate.getDate() - (limit * 7));
- } else if (timeframe === "1M") {
- startDate.setMonth(startDate.getMonth() - limit);
+ if (range === "1D") {
+ startDate.setDate(startDate.getDate() - 1);
+ } else if (range === "1W") {
+ startDate.setDate(startDate.getDate() - 7);
+ } else if (range === "1M") {
+ startDate.setMonth(startDate.getMonth() - 1);
+ } else if (range === "3M") {
+ startDate.setMonth(startDate.getMonth() - 3);
+ } else if (range === "1Y") {
+ startDate.setFullYear(startDate.getFullYear() - 1);
+ } else if (range === "3Y") {
+ startDate.setFullYear(startDate.getFullYear() - 3);
+ } else if (range === "ALL") {
+ startDate.setFullYear(startDate.getFullYear() - 10); // Max 10 years
} else if (isIntraday) {
- startDate.setDate(startDate.getDate() - Math.floor(limit / 5));
+ startDate.setDate(startDate.getDate() - 30); // Default 30 days for intraday
}
- const barsOptions: any = { timeframe, limit };
- if (timeframe !== "1Min" && timeframe !== "5Min") {
+ const barsOptions: any = { timeframe, limit: 1000 }; // High limit for time range
+ if (!isIntraday && range !== "ALL") {
+ barsOptions.start = startDate.toISOString().split('T')[0];
+ } else if (!isIntraday) {
barsOptions.start = startDate.toISOString().split('T')[0];
}
- console.log(`API quote/${ticker}: calling getBarsV2 with timeframe=${timeframe}, limit=${limit}`);
const bars = await alpaca.getBarsV2(ticker, barsOptions);
- console.log(`API quote/${ticker}: getBarsV2 returned`, typeof bars, bars?.constructor?.name);
// Convert async generator to array
// Alpaca v2 API returns AlpacaBar with capitalized property names
const barsArray = [];
try {
for await (const bar of bars) {
- console.log(`API quote/${ticker}: received bar =`, JSON.stringify(bar));
barsArray.push(bar);
}
} catch (genErr) {
console.error(`API quote/${ticker}: error iterating bars`, genErr);
}
-
- console.log(`API quote/${ticker}: raw bars count = ${barsArray.length}`);
- if (barsArray.length > 0) {
- console.log(`API quote/${ticker}: first bar =`, JSON.stringify(barsArray[0]));
- } else {
- console.log(`API quote/${ticker}: no bars returned from Alpaca, generator may be empty`);
- }
// Transform to chart format
const transformedBars = barsArray.map((bar: any) => {
@@ -91,8 +89,6 @@ export async function loader({ request, params }: { request: Request; params: {
v: volume,
};
}).filter((bar: any) => bar.o > 0 && bar.h > 0 && bar.l > 0 && bar.c > 0);
-
- console.log(`API quote/${ticker}: returning ${transformedBars.length} bars`);
return Response.json({
ticker,
diff --git a/app/routes/api/test-alpaca.ts b/app/routes/api/test-alpaca.ts
deleted file mode 100644
index 6e51055..0000000
--- a/app/routes/api/test-alpaca.ts
+++ /dev/null
@@ -1,74 +0,0 @@
-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({ request }: { request: Request }) {
- const url = new URL(request.url);
- const ticker = url.searchParams.get("ticker")?.toUpperCase() || "AAPL";
-
- try {
- // Test different timeframes
- const timeframes = ["1Day", "1Min", "5Min"];
- const results: any = {};
-
- for (const tf of timeframes) {
- try {
- console.log(`test-alpaca: testing ${ticker} with timeframe ${tf}`);
- const bars = await alpaca.getBarsV2(ticker, {
- timeframe: tf as any,
- limit: 3,
- });
-
- const barsArray = [];
- for await (const bar of bars) {
- barsArray.push(bar);
- }
- results[tf] = { count: barsArray.length, sample: barsArray[0] };
- console.log(`test-alpaca: ${tf} -> ${barsArray.length} bars`);
- } catch (e) {
- results[tf] = { error: e instanceof Error ? e.message : String(e) };
- }
- }
-
- // Test popular stocks
- const symbols = ["AAPL", "MSFT", "SPY", "QQQ"];
- const symbolResults: any = {};
-
- const startDate = new Date();
- startDate.setDate(startDate.getDate() - 30);
- const start = startDate.toISOString().split('T')[0];
-
- for (const sym of symbols) {
- try {
- const bars = await alpaca.getBarsV2(sym, {
- timeframe: "1D",
- limit: 3,
- start,
- });
- const barsArray = [];
- for await (const bar of bars) barsArray.push(bar);
- symbolResults[sym] = barsArray.length;
- } catch (e) {
- symbolResults[sym] = e instanceof Error ? e.message : String(e);
- }
- }
-
- return Response.json({
- ticker,
- timeframeResults: results,
- symbolResults,
- });
- } catch (error) {
- console.error("test-alpaca error:", error);
- return Response.json({
- error: error instanceof Error ? error.message : String(error),
- ticker
- }, { status: 500 });
- }
-}
\ No newline at end of file
diff --git a/package.json b/package.json
index 175d527..4aa6b92 100644
--- a/package.json
+++ b/package.json
@@ -7,6 +7,8 @@
"dev": "react-router dev",
"start": "react-router-serve ./build/server/index.js",
"test:e2e": "playwright test",
+ "test": "vitest run",
+ "test:watch": "vitest",
"typecheck": "react-router typegen && tsc",
"mcp:dev": "npx tsx mcp-server/index.ts",
"mcp:build": "tsc -p mcp-server/tsconfig.json"
diff --git a/playwright-report/index.html b/playwright-report/index.html
index 191fb27..6d03d6d 100644
--- a/playwright-report/index.html
+++ b/playwright-report/index.html
@@ -87,4 +87,4 @@ Error generating stack: `+l.message+`