Refactor for TDD workflow with Vitest and ESLint
- Move API keys from hardcoded values to .env via dotenv - Extract business logic into src/trading.ts with dependency injection - Add typed AlpacaClient interface, replace `any` on Alpaca class - Add Vitest test suites for trading logic and Alpaca wrapper (14 tests) - Set up ESLint with @typescript-eslint for linting - Fix getAsset to pass symbol string directly to SDK - Fix typo (alpacha -> alpaca), remove unused ws/node-fetch deps - Update .gitignore for node_modules and .env Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
46
src/trading.ts
Normal file
46
src/trading.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { Alpaca } from "./alpaca";
|
||||
|
||||
export function wait(ms: number) {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, ms);
|
||||
});
|
||||
}
|
||||
|
||||
export async function printAsset(alpaca: Alpaca, symbol: string) {
|
||||
const asset = await alpaca.getAsset(symbol);
|
||||
if (asset && asset.fractionable)
|
||||
console.log(symbol + ' is fractional')
|
||||
else
|
||||
console.log(symbol + ' is not fractional')
|
||||
}
|
||||
|
||||
export async function accountBalance(alpaca: Alpaca) {
|
||||
const account = await alpaca.getAccount();
|
||||
return account.cash;
|
||||
}
|
||||
|
||||
export async function waitForNextOpen(alpaca: Alpaca) {
|
||||
const clock = await alpaca.getClock();
|
||||
return wait(new Date(clock.next_open).valueOf() - new Date().valueOf());
|
||||
}
|
||||
|
||||
export async function runDay(alpaca: Alpaca) {
|
||||
console.log('waiting for open');
|
||||
await waitForNextOpen(alpaca);
|
||||
|
||||
await wait(60000); //wait a minute
|
||||
|
||||
const q = await alpaca.getLatestQuote('TQQQ');
|
||||
await wait(1000);
|
||||
|
||||
const q2 = await alpaca.getLatestQuote('TQQQ');
|
||||
|
||||
if (q2.ap - q.ap > 0) {
|
||||
//up day
|
||||
console.log('up day: ', new Date())
|
||||
}
|
||||
else {
|
||||
//down day
|
||||
console.log('down day', new Date());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user