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

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

    const symbols = ['BTC/USD', 'BTCUSD'];
    const methods = ['getBarsV2', 'getCryptoBars'];

    for (const method of methods) {
        for (const symbol of symbols) {
            console.log(`Checking ${method} for ${symbol}...`);
            try {
                const bars = (alpaca as any)[method](symbol, {
                    timeframe: '1Min',
                    limit: 10,
                    start: new Date(Date.now() - 3600000 * 24).toISOString()
                });
                let count = 0;
                for await (const b of bars) {
                    count++;
                }
                console.log(`Result: ${count} bars found.`);
            } catch (e: any) {
                console.error(`Method ${method} failed for ${symbol}: ${e.message}`);
            }
        }
    }
}

test();
