import Alpaca from '@alpacahq/alpaca-trade-api';
import * as dotenv from 'dotenv';
dotenv.config();

const alpaca = new (Alpaca as any)({
    keyId: process.env.ALPACA_API_KEY,
    secretKey: process.env.ALPACA_API_SECRET,
    paper: true,
});

async function run() {
    const symbol = 'BTC/USD';
    console.log(`Testing ${symbol} with getCryptoBars...`);
    try {
        const bars = alpaca.getCryptoBars(symbol, {
            timeframe: '1Min',
            start: new Date(Date.now() - 3600000).toISOString(),
            limit: 5
        });
        let found = false;
        for await (const b of bars) {
            console.log('Got bar!');
            found = true;
        }
        if (found) console.log('SUCCESS!');
        else console.log('No bars found but no error.');
    } catch (e: any) {
        console.log('FAILED:', e.message);
    }
}
run();
