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() {
    console.log('waiting for open');
    await waitForNextOpen();

    await wait(1000); //wait a miniute
    
    const q = await alpaca.getLatestQuote('TQQQ');
    await wait(1000);

    const q2 = await alpaca.getLatestQuote('TQQQ');

    if (q2.ap - q.ap > 0) {
        //up day
        console.log('up day: ', new Date())
    }
    else {
        //down day
        console.log('down day', new Date());
    }
}
async function main() {
    while(true) {
        await runDay();
        await wait(1000 * 60 * 60);//wait an hour before going and getting the next open
    }
}




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