# The Swing Log prompt

Paste everything below the line into your coding agent, in the folder of your cloned repository.
Do the setup in [README.md](README.md) first. Change the stack, the names, or the design to suit you.

---

Build me a personal trading journal called "Swing Log" and put it online. It is for me alone. Do the whole job in this one session. Stop only when a PR is ready to merge.

## Step 1: the journal

Swing Log is a trading journal for my swing trades. Each row is one trade. I log the entry, then close it later with the exit. The journal shows what I made, what I risked, and whether I followed my plan.

Scope, and nothing more:

- Only I can use it. Sign in with Clerk, and make Convex reject every account except mine (`OWNER_EMAIL`).
- Log a trade: symbol, asset type (stock or crypto), side (long or short), quantity, entry price, entry date, fees, the dollars at risk, a setup tag, and a note. Only the symbol, side, quantity, entry price, and entry date are required. The asset type is the only switch. It picks where the live price comes from, and it keeps a stock and a coin with the same ticker apart.
- Close a trade with an exit price, an exit date, and exit fees. Store the realized P&L after fees.
- A trade table with open and closed tabs, sorting, and a search box. Edit and delete from each row.
- Stats for closed trades: net P&L, win rate, average win, average loss, profit factor, and average R (P&L divided by the dollars at risk). Show R only for trades that have a risk.
- An equity curve of cumulative closed P&L.
- A dev seed with 25 sample trades, open and closed, longs and shorts, stocks and crypto, so the stats and the curve have data on localhost. Seed nothing in production.

Stack: TanStack Start, Convex, Clerk, Tailwind, shadcn/ui on Base UI (never Radix), Vite, pnpm, TypeScript, and zod. This folder is the clone of my private GitHub repository `swing-log`. It has only a README and a `.gitignore` on `main`.

### Setup is done

I set up every account by hand before this prompt. Do not ask me for keys, and never print one.

- `.env.local` holds `VITE_CLERK_PUBLISHABLE_KEY`, `CLERK_SECRET_KEY`, `CLERK_FRONTEND_API_URL`, `ALPACA_API_KEY_ID`, `ALPACA_API_SECRET_KEY`, `RESEND_API_KEY`, and `OWNER_EMAIL`. Check that Git ignores it.
- Convex: link this folder to my existing project `swing-log` with `npx convex dev`. The CLI is signed in. Copy the server values into the dev deployment with `npx convex env set`, reading each one from the file.
- Clerk: a development instance, with the Convex integration on.
- Vercel: the project `swing-log` is connected to the repository, and `CONVEX_DEPLOY_KEY` is set for production only.
- Use `gh`, `npx convex`, and `vercel` for everything else. All three are signed in. Never open a dashboard in my browser: print the URL and I open it.
- Check each key with one real call before you build on it: a sign-in, one stock price and one crypto price, and one test email to me. If a key fails, stop and tell me which one and what the service said.
- Write `docs/setup.md`: what each account holds and each variable, so I can do the setup again later.

## Design

Match this mock: https://c4089l5fd2zx.postplan.dev ("Instrument Deck"). Open it in a browser and copy its layout. Leave out its account column. There is no account field.

- Colors: canvas `#0B0E13`, surface `#12161E`, elevated `#181D27`, border `#232A36`, accent `#F59E0B`, ink `#F1F5F9`, profit `#34D399`, loss `#FB7185`. Dark only.
- Fonts: Plus Jakarta Sans for text, JetBrains Mono for numbers. Radius 6, 8, and 12 px. Spacing on a 4 px base.
- Layout: the header has the wordmark, the alerts bell with its unread count, the user menu, and an amber "Log trade" button. Below it, a large equity card with net P&L and the curve sits beside six stat tiles. The trade table comes last. Each open row shows the stop and the target with the distance to each, and a meter for where the live price sits between them.
- Rules:
  1. Every ticker, price, P&L, R value, quantity, and date is in JetBrains Mono with tabular numbers.
  2. Profit and loss always have a + or − sign as well as the color.
  3. Amber is only for the primary action, focus rings, and unread alerts.

## Step 2: stop loss and take profit alerts

Open a second PR after I merge the first.

- A trade gets an optional stop loss and an optional take profit. Check that the stop and the target sit on the right sides of the entry for a long and for a short. When I set a stop, show the dollars at risk and use them for the risk field until I type my own. When I set both, show the reward in R.
- A Convex cron checks open trades with a level once an hour. These are swing trades, so hourly is enough. Get one price per symbol, never one per trade, and make no price calls when no trade has a level.
- All prices come from Alpaca's market data API. Stocks need my paper account key pair. Crypto needs no key. Some coins have no Alpaca price: show "no live price" for them and skip their alerts.
- The price feed fails sometimes. Plan for it now, so an old price never looks live:
  - Stocks and crypto fail on their own. A failed stock request must not stop crypto prices.
  - Store Alpaca's trade time with each price, and show the price's age. A price from the last close must look old.
  - Record each failed check and show it in the app: what failed, when, and when the next check runs.
  - On a 401 or 403, say that Alpaca rejected the key. Do not retry it before the next check.
  - On a 429, wait until the time in `X-Ratelimit-Reset` and try once more. Never fail without a word.
  - Add a "Check now" button with a cooldown, so it cannot spam Alpaca.
- A long hits its stop at or below the stop and its target at or above the target. A short is the mirror image. Each level alerts once. Moving a level arms it again.
- An alert never closes a trade. It tells me, and I close the trade myself.
- Three channels: in the app (always on), a browser notification through Web Push and a service worker (turned on per device), and email (turned on in settings). Make the VAPID keys yourself. Send email through Resend with a plain `fetch`, no new package, from `onboarding@resend.dev` to `OWNER_EMAIL`. That needs no domain, but Resend then delivers only to the email of my Resend account, so `OWNER_EMAIL` must be that address.
- A bell in the header with an unread count opens the alerts. Each alert has "Close at <level>", which opens the close dialog with that price filled in, "Edit levels", and "Dismiss".
- Focused tests for the hit rule, firing once, re-arming, one price call per symbol, and each price feed failure above.

## Step 3: put it online

Open a third PR after I merge the alerts. Set up real CI/CD, so a merge to `main` deploys everything with no manual step.

- Host it on Vercel on its free `.vercel.app` address. The project `swing-log` exists and is connected to the repository.
- Use a Convex production deployment. A production build runs `npx convex deploy` and then builds the site, so each merge to `main` deploys the Convex functions and the site together.
- Vercel cannot detect TanStack Start in this repository, so a plain import serves a 404. Add the Nitro Vite plugin (`nitro/vite`) so the build writes Vercel output. In `vercel.json`, set `"framework": "tanstack-start"` and a build command that runs `npx convex deploy --cmd 'pnpm build' --cmd-url-env-var-name VITE_CONVEX_URL` when `VERCEL_ENV` is `production`, and only `pnpm build` otherwise.
- Preview builds for pull requests must not fail. They use the dev Convex deployment and never deploy Convex.
- `CONVEX_DEPLOY_KEY` is already in Vercel for production only. Do not change it.
- Keep the Clerk development instance. A Clerk production instance needs a domain I own, and this app does not need one.
- Set every other production value with the Vercel and Convex CLIs, reading each one from `.env.local`. Ask me before the first production deploy.
- Prove the pipeline. This PR gets a working preview. When I merge it, show the build log in the terminal with `vercel inspect --logs`, where Convex deploys, then the live URL.
- Check it on the live URL: I sign in, log a trade, set a stop that the next check hits, and get the alert in the app, in the browser, and by email. A second account must be refused.

## Rules

- Do not touch any other project on this computer or in my accounts.
- Never show or log real trades, balances, or P&L from any other app or account.
- Never print a token or put one in a command argument.
- When you finish, report the live URL, the elapsed time from the first start to the last merge, and the number of PRs.
