import { Alpaca } from "./alpaca";

const alpaca = new Alpaca(false);

async function printAsset(symbol: string) {
    const asset = await alpaca.getAsset(symbol);
    if (asset && asset.fractionable)
        console.log(symbol + ' is fractional')
    else 
        console.log(symbol + ' is not fractional')
}
async function accountBalance() {
    const account = await alpaca.getAccount();
    return account.cash;

}
async function waitForNextOpen() {
    const clock = await alpaca.getClock();
    return wait(new Date(clock.next_open).valueOf() - new Date().valueOf());
}
function wait(ms :number) {
    return new Promise((resolve) => {
        console.log('would wait: ' + ms + 'ms');
        resolve('')
        //setTimeout(resolve, ms);
    });

}
async function runDay() {
    await waitForNextOpen();
    console.log('run the day');
    await printAsset('AAPL');
    await printAsset('TQQQ');
    await printAsset('SQQQ');

    console.log(await accountBalance());
    let stocks = [
        'AAPL',
        'TQQQ',
        'SQQQ'
    ]
    console.log(await alpaca.getLatestTrades(stocks))
    //console.log(await alpaca.getLatestQuote('AAPL'));
}
async function main() {
    await runDay();
}




//run main
main().then(
    () => {
        console.log("done")
    }
).catch(
    (e) => console.log('Error: ', e)
).finally(
    () => process.exit(0)
);