Last active
July 26, 2022 18:54
-
-
Save zz85/69765a775c19a4a8ee9caa99ddcf1c41 to your computer and use it in GitHub Desktop.
Portfolio Positions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Ib = require('ib'); | |
var ib = new Ib() | |
function getPositions(ib, cb) { | |
ib.on('position', onPositions) | |
ib.reqPositions() | |
var positions = []; | |
var pending = null; | |
function onPositions(account, contract, pos, avgCost) { | |
var symbol = contract.symbol; | |
var position = pos; | |
var avgCost = avgCost; | |
positions.push({ | |
symbol, position, avgCost | |
}); | |
clearTimeout(pending); | |
pending = setTimeout(() => { | |
ib.removeListener('position', onPositions) | |
cb(positions); | |
}) | |
} | |
} | |
ib.connect() | |
getPositions(ib, (positions) => { | |
console.log('symbol\tpos\tavgCost\ttotalCost') | |
positions.forEach(({ | |
symbol, position, avgCost | |
}) => { | |
var totalCost = position * avgCost; | |
console.log(`${symbol}\t${position}\t${avgCost.toFixed(3)}\t${totalCost.toFixed(3)}`) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment