learning_ai_invt_trdg/backend/test_stock_baseline.ts

32 lines
1.0 KiB
TypeScript

import Alpaca from '@alpacahq/alpaca-trade-api';
import { config } from '../src/config/index.js';
async function testStock() {
const alpaca = new (Alpaca as any)({
keyId: config.ALPACA_API_KEY,
secretKey: config.ALPACA_API_SECRET,
paper: true,
});
console.log('Testing Stock data (AAPL)...');
try {
const bars = alpaca.getBarsV2('AAPL', {
timeframe: '1Min',
start: new Date(Date.now() - 3600000 * 24).toISOString(),
limit: 5,
feed: 'iex'
});
let count = 0;
for await (const b of bars) {
count++;
console.log(`Got Stock Bar: ${b.Timestamp} | Price: ${b.ClosePrice}`);
}
if (count > 0) console.log('✅ Success! Stock data is working.');
else console.log('❌ No stock bars found. This might be due to market hours or delay.');
} catch (e: any) {
console.error('❌ Stock API Error:', e.message);
}
}
testStock();