@stoqey/ibkr
Version:
NodeJS Interactive Brokers wrapper & utilities using @stoqey/ib
114 lines • 5.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.logTrade = exports.logPosition = exports.logOrder = exports.logBar = void 0;
var interfaces_1 = require("../interfaces");
var instrument_utils_1 = require("../utils/instrument.utils");
var time_utils_1 = require("./time.utils");
var log_1 = require("./log");
var portfolio_utils_1 = require("../utils/portfolio.utils");
var logBar = function (title, bar, warning) {
if (title === void 0) { title = 'MkdMgr.logBar'; }
if (warning === void 0) { warning = false; }
try {
var date = bar.date, instrument = bar.instrument, close = bar.close, volume = bar.volume;
var symbol = (0, instrument_utils_1.getSymbolKey)(instrument);
var msg = "".concat(date ? (0, time_utils_1.formatDateStr)(date) : "", " ").concat(symbol ? symbol : "", " ").concat(close ? "@".concat(close) : "", " ").concat(volume ? "vol=".concat(volume) : "");
if (warning) {
return (0, log_1.warn)(title, msg);
}
;
(0, log_1.log)(title, msg);
}
catch (error) { }
};
exports.logBar = logBar;
// type AnyOrder = Order;
var logOrder = function (title, orderContract, warning) {
if (title === void 0) { title = 'Orders'; }
if (warning === void 0) { warning = false; }
try {
var date = new Date();
var order = orderContract.order;
var contract = orderContract.contract || (order === null || order === void 0 ? void 0 : order.instrument);
if (!order && !contract)
return;
var msg = (0, time_utils_1.formatDateStr)(date);
if (contract) {
var symbol = (0, instrument_utils_1.getSymbolKey)(contract);
msg += ": ".concat(symbol);
}
;
if (order) {
msg += "".concat(order.orderId || order.id ? "id=".concat(("".concat(order.orderId || order.id || "")).slice(0, 4)) : "", " ").concat(order.action, " ").concat((order === null || order === void 0 ? void 0 : order.totalQuantity) || order.quantity, " ").concat(order.auxPrice || order.stopPrice ? "@".concat(order.auxPrice || order.stopPrice) : "", ", ").concat(order.lmtPrice || order.limitPrice ? "LMT=".concat(order.lmtPrice || order.limitPrice) : "", " ").concat(order.orderType || order.type, " ").concat(order.tif || "", " ").concat(order.outsideRth || order.useRth ? "RTH" : "");
}
if (warning) {
return (0, log_1.warn)(title, msg);
}
;
(0, log_1.log)(title, msg);
}
catch (error) { }
};
exports.logOrder = logOrder;
var logPosition = function (title, portfolio, warning) {
if (title === void 0) { title = 'Position'; }
if (warning === void 0) { warning = false; }
try {
var position = (portfolio === null || portfolio === void 0 ? void 0 : portfolio.avgCost) ? (0, interfaces_1.ibkrPositionTossPosition)(portfolio) : portfolio;
var date = new Date();
var contract = position === null || position === void 0 ? void 0 : position.instrument;
if (!position && !contract)
return;
var msg = (0, time_utils_1.formatDateStr)(date);
if (contract) {
var symbol = (0, instrument_utils_1.getSymbolKey)(contract);
msg += ": ".concat(symbol);
}
;
if (position) {
var pnl = (0, portfolio_utils_1.calculatePnl)([position]);
msg += "".concat(position.entryDate ? "entry=".concat((0, time_utils_1.formatDateStr)(position.entryDate)) : "", " ").concat(position.action, " ").concat(position === null || position === void 0 ? void 0 : position.quantity, " @").concat(position.price, " ").concat(position.lastPrice ? "last=".concat(position.lastPrice) : "", " ").concat(pnl ? "pnl=".concat(pnl) : "");
}
if (warning) {
return (0, log_1.warn)(title, msg);
}
;
(0, log_1.log)(title, msg);
}
catch (error) { }
};
exports.logPosition = logPosition;
var logTrade = function (title, trade, warning) {
if (title === void 0) { title = 'Trade'; }
if (warning === void 0) { warning = true; }
try {
var date = new Date();
var contract = trade === null || trade === void 0 ? void 0 : trade.instrument;
if (!trade && !contract)
return;
var msg = (0, time_utils_1.formatDateStr)(date);
if (contract) {
var symbol = (0, instrument_utils_1.getSymbolKey)(contract);
msg += ": ".concat(symbol);
}
;
if (trade) {
var position = {
price: trade.entryPrice || trade.price,
lastPrice: trade.price,
quantity: trade.quantity,
action: trade.action === "BUY" ? "SELL" : "BUY",
};
var pnl = (0, portfolio_utils_1.calculatePnl)([position]);
msg += "id=".concat(trade.id ? trade.id.slice(0, 4) : "", " ").concat(trade.date ? "date=".concat((0, time_utils_1.formatDateStr)(trade.date)) : "", " ").concat(trade.action, " ").concat(trade.type, " ").concat(trade === null || trade === void 0 ? void 0 : trade.quantity, " @").concat(trade.price, " ").concat(pnl ? "pnl=".concat(pnl) : "");
}
if (warning) {
return (0, log_1.warn)(title, msg);
}
;
(0, log_1.log)(title, msg);
}
catch (error) { }
};
exports.logTrade = logTrade;
//# sourceMappingURL=log.utils.js.map