@stoqey/ibkr
Version:
NodeJS Interactive Brokers wrapper & utilities using @stoqey/ib
35 lines • 1.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculatePnl = exports.getTotalProfitAmount = exports.getPercentageGain = void 0;
var interfaces_1 = require("../interfaces");
/**
* Get Profit percentage gained
* https://www.investopedia.com/ask/answers/how-do-you-calculate-percentage-gain-or-loss-investment/
* @param startPrice
* @param endPrice
*/
var getPercentageGain = function (startPrice, endPrice) {
return (endPrice - startPrice) / startPrice * 100;
};
exports.getPercentageGain = getPercentageGain;
/**
* GetTotalProfitAmount, from start and end
* @param start
* @param end
* @param capital
*/
var getTotalProfitAmount = function (start, end, capital, sell) {
if (sell === void 0) { sell = false; }
var profit = sell ? start - end : end - start;
return (profit / start) * capital;
};
exports.getTotalProfitAmount = getTotalProfitAmount;
var calculatePnl = function (positions) {
return positions.reduce(function (acc, position) {
var amount = position.quantity * position.price;
var pnl = position.lastPrice ? (0, exports.getTotalProfitAmount)(position.price, position.lastPrice, amount, position.action === interfaces_1.OrderAction.SELL) : 0;
return acc + pnl;
}, 0);
};
exports.calculatePnl = calculatePnl;
//# sourceMappingURL=portfolio.utils.js.map