Hello, I’m trying to understand how to check current position and use that variable outside of the function. I use this code:
function rebalance() {
try {
alpaca.getPosition(stock).then((resp) => {
position = resp.qty;
console.log(“Position quantity from rebalance function”, position);
});
} catch (err) {
// console.log(err);
errors = err.error[code
];
if (errors == 40410000
) {
position = 0;
console.log("Position: ", position);
}
}
}
//…
rebalance();
if (position == 0 ) { //Execute trade}
if( position >0) || (position <0) { // Trade is already executed}
How to get position variable outside of that function?