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

const alpaca = new (Alpaca as any)({
    keyId: 'PKTHEUUYVGMCWDLAREMD2YXNOV',
    secretKey: 'eVi9LZQ1rKx4yy9ExbtLt2fCMWAMujJfapyv9RHnixK',
    paper: true,
});

async function run() {
    const symbol = 'BTCUSD';
    console.log(`Testing ${symbol} with getBarsV2...`);
    try {
        const bars = alpaca.getBarsV2(symbol, {
            timeframe: '1Min',
            start: new Date(Date.now() - 3600000 * 24).toISOString(),
            limit: 5
        });
        for await (const b of bars) {
            console.log('Got bar!', b.Timestamp);
        }
        console.log('SUCCESS!');
    } catch (e: any) {
        console.log('FAILED:', e.message);
    }
}
run();
