Skip to content

Commit

Permalink
Packages upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
rylorin committed Sep 26, 2024
1 parent 8cdb023 commit 853e6cf
Show file tree
Hide file tree
Showing 6 changed files with 721 additions and 482 deletions.
52 changes: 26 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"doc:dev": "typedoc --out ./doc-dev",
"postinstall": "patch-package && yarn build:ib",
"lint": "eslint ./src",
"prepare": "husky install",
"prepare": "husky",
"prepublishOnly": "yarn build",
"qc": "yarn lint && tsc --noEmit && prettier --check . && stylelint \"**/*.css\"",
"release": "yarn lint && yarn qc && yarn doc && yarn build && yarn run storybook:build",
Expand All @@ -41,15 +41,15 @@
"@emotion/react": "^11.13.3",
"@emotion/styled": "^11.13.0",
"@stoqey/ib": "stoqey/ib#7b0d422920ff88f56bb27b4ca3f8bd9b1ddb6dc5",
"@tanstack/react-table": "^8.19.2",
"@tanstack/react-table": "^8.20.5",
"chakra-react-select": "^4.9.2",
"chart.js": "^4.4.4",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.20.0",
"express": "^4.21.0",
"fast-xml-parser": "^4.5.0",
"formik": "^2.4.6",
"framer-motion": "^11.5.4",
"framer-motion": "^11.7.0",
"json-stringify-safe": "^5.0.1",
"react": "^18.3.1",
"react-chartjs-2": "^5.2.0",
Expand All @@ -60,51 +60,51 @@
"sequelize-typescript": "^2.1.6",
"sqlite3": "^5.1.7",
"winston": "^3.14.2",
"yahoo-finance2": "^2.11.3"
"yahoo-finance2": "^2.12.4"
},
"devDependencies": {
"@babel/core": "^7.25.2",
"@chakra-ui/storybook-addon": "^5.1.0",
"@eslint/compat": "^1.1.1",
"@storybook/addon-actions": "^8.2.9",
"@storybook/addon-essentials": "^8.2.9",
"@storybook/addon-links": "^8.2.9",
"@storybook/cli": "^8.2.9",
"@storybook/components": "^8.2.9",
"@storybook/react": "^8.2.9",
"@storybook/react-vite": "^8.2.9",
"@storybook/addon-actions": "^8.3.3",
"@storybook/addon-essentials": "^8.3.3",
"@storybook/addon-links": "^8.3.3",
"@storybook/cli": "^8.3.3",
"@storybook/components": "^8.3.3",
"@storybook/react": "^8.3.3",
"@storybook/react-vite": "^8.3.3",
"@storybook/testing-library": "^0.2.2",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/express": "^5.0.0",
"@types/json-stringify-safe": "^5.0.3",
"@types/node": "^20.16.5",
"@types/react": "^18.3.5",
"@types/node": "^20.16.9",
"@types/react": "^18.3.9",
"@types/react-dom": "^18.3.0",
"@types/react-router-dom": "^5.3.3",
"@types/validator": "^13.12.1",
"@types/validator": "^13.12.2",
"@vitejs/plugin-react": "^4.3.1",
"babel-loader": "^9.1.3",
"concurrently": "^9.0.0",
"eslint": "^9.10.0",
"babel-loader": "^9.2.1",
"concurrently": "^9.0.1",
"eslint": "^9.11.1",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-react": "^7.35.2",
"eslint-plugin-react": "^7.36.1",
"eslint-plugin-rxjs": "^5.0.3",
"eslint-plugin-storybook": "^0.8.0",
"eslint-plugin-storybook": "^0.9.0",
"http-proxy-middleware": "^3.0.2",
"husky": "^9.1.5",
"husky": "^9.1.6",
"lint-staged": "^15.2.10",
"patch-package": "^8.0.0",
"prettier": "^3.3.3",
"rxjs": "^7.8.1",
"storybook": "^8.2.9",
"storybook": "^8.3.3",
"stylelint": "^16.9.0",
"stylelint-config-standard": "^36.0.1",
"ts-node-dev": "^2.0.0",
"typescript": "^5.6.2",
"typescript-eslint": "^8.5.0",
"vite": "^5.4.3",
"webpack": "^5.94.0"
"typescript-eslint": "^8.7.0",
"vite": "^5.4.8",
"webpack": "^5.95.0"
},
"engines": {
"node": "^20.12.2"
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/Portfolio/Trade/TradeShow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ const TradeShow: FunctionComponent<Props> = ({ ..._rest }): React.ReactNode => {
APY:
</Text>
<Text w="200px" textAlign="right">
<Number value={item.apy} isPercent />
<Number value={item.apy} isPercent decimals={1} />
</Text>
</Flex>
<Flex justifyContent="center" gap="2">
Expand Down
40 changes: 21 additions & 19 deletions src/bots/importer.bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,25 +639,27 @@ export class ImporterBot extends ITradingBot {
break;
case "Bond Interest Paid":
case "Bond Interest Received":
// if ((element.description as string).includes("ACCRUED")) {
// await Statement.findOne({
// where: { transactionId: element.transactionID },
// }).then((statement) => {
// if (statement)
// return BondStatement.findByPk(statement.id).then((bond_statement) => {
// if (bond_statement)
// return bond_statement.update("accruedInterests", element.amount).then(() => statement);
// else {
// this.warn(`Bond statement #${statement.id} not found!`);
// return Promise.resolve(null as null);
// }
// });
// else {
// this.warn(`statement not found for transactionId: ${element.transactionID}`);
// return Promise.resolve(null as null);
// }
// });
// }
if ((element.description as string).includes("ACCRUED")) {
return Statement.findOne({
where: { transactionId: element.transactionID },
}).then(async (statement) => {
if (statement)
return BondStatement.findByPk(statement.id).then(async (bond_statement) => {
if (bond_statement)
return bond_statement.update({ accruedInterests: element.amount }).then(() => null as null);
// .catch((error) => console.error(error));
else {
this.warn(`Bond statement #${statement.id} not found!`);
return null as null;
}
});
else {
this.warn(`statement not found for transactionId: ${element.transactionID}`);
return null as null;
}
});
// .catch((error) => console.error(error));
}
statementType = StatementTypes.InterestStatement;
break;
case "Deposits/Withdrawals":
Expand Down
5 changes: 3 additions & 2 deletions src/models/trade.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ export class Trade extends Model<
@Column({ type: DataType.DATEONLY, field: "expiryDate" })
declare expectedExpiry: string | null; // YYYY-MM-DD

/** expected Pnl on expiration date */
/** Unrealized Pnl. Additional PnL that we will pocket on expiration */
@Column({ type: DataType.FLOAT, defaultValue: 0 })
declare expiryPnl?: number | null;

@Column({ type: DataType.SMALLINT, defaultValue: 0 })
declare strategy: TradeStrategy;

@Column({ type: DataType.FLOAT, field: "pn_l" })
/** Realized PnL */
@Column({ type: DataType.FLOAT })
declare PnL?: number;

@Column
Expand Down
41 changes: 31 additions & 10 deletions src/routers/trades.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const initVirtualFromStatement = (item: StatementEntry): VirtualPositionEntry =>
let contract: StatementUnderlyingEntry | StatementUnderlyingOption;
switch (item.statementType) {
case StatementTypes.EquityStatement:
case StatementTypes.BondStatement:
case StatementTypes.CorporateStatement:
id = item.underlying!.id;
contract = item.underlying!;
break;
Expand Down Expand Up @@ -64,6 +66,8 @@ const updateVirtuals = (
let virtual;
switch (statement_entry.statementType) {
case StatementTypes.EquityStatement:
case StatementTypes.BondStatement:
case StatementTypes.CorporateStatement:
if (statement_entry.underlying) {
id = statement_entry.underlying.id;
if (!virtuals[id]) virtuals[id] = initVirtualFromStatement(statement_entry);
Expand Down Expand Up @@ -136,7 +140,8 @@ const updateTradeExpiry = (
if (
statements[i].statementType == StatementTypes.EquityStatement ||
statements[i].statementType == StatementTypes.OptionStatement ||
statements[i].statementType == StatementTypes.BondStatement
statements[i].statementType == StatementTypes.BondStatement ||
statements[i].statementType == StatementTypes.CorporateStatement
) {
// console.log(items[i]);
if (statements[i].date > closingDate) closingDate = statements[i].date;
Expand Down Expand Up @@ -181,6 +186,14 @@ export const tradeModelToTradeEntry = async (
},
): Promise<TradeEntry> => {
// Init TradeEntry
console.log(
"tradeModelToTradeEntry",
thisTrade.id,
thisTrade.risk,
thisTrade.PnL,
thisTrade.expectedDuration,
thisTrade.expiryPnl,
);
return Promise.resolve({
id: thisTrade.id,
portfolioId: thisTrade.portfolio_id,
Expand All @@ -199,7 +212,7 @@ export const tradeModelToTradeEntry = async (
pnlInBase: thisTrade.pnlInBase,
apy:
thisTrade.risk && thisTrade.expectedDuration
? -((thisTrade.PnL! + thisTrade.expiryPnl!) / thisTrade.risk) * (360 / thisTrade.expectedDuration)
? ((thisTrade.PnL! + thisTrade.expiryPnl!) / -thisTrade.risk) * (360 / thisTrade.expectedDuration)
: undefined,
comment: thisTrade.comment,
statements: undefined,
Expand Down Expand Up @@ -341,8 +354,10 @@ const computeComboRisk = (thisTrade: Trade, virtuals: Record<number, VirtualPosi
Object.values(virtuals).forEach((item) => {
cash_risk -= item.cost;
switch (item.contract.secType) {
case ContractType.Stock:
case ContractType.Bond:
stocks[item.contract.id] = { contract: item.contract, cost: item.cost, quantity: item.quantity / 1000 };
break;
case ContractType.Stock:
if (item.quantity) limits.push(item.cost / item.quantity);
stocks[item.contract.id] = { contract: item.contract, cost: item.cost, quantity: item.quantity };
break;
Expand Down Expand Up @@ -448,6 +463,7 @@ const countContratTypes = (virtuals: Record<number, VirtualPositionEntry>): numb
let short_put = 0;
let long_call = 0;
let short_call = 0;
let long_bond = 0;
Object.values(virtuals).forEach((item) => {
switch (item.contract.secType) {
case ContractType.Stock:
Expand All @@ -467,23 +483,29 @@ const countContratTypes = (virtuals: Record<number, VirtualPositionEntry>): numb
break;
}
break;
case ContractType.Bond:
if (item.quantity > 0) long_bond += 1;
break;
default:
logger.log(
LogLevel.Error,
MODULE + ".computeTradeStrategy",
MODULE + ".countContratTypes",
undefined,
"Unimplemented SecType: " + item.contract.secType,
);
}
});
return [long_stock, short_stock, long_call, short_call, long_put, short_put];
return [long_stock, short_stock, long_call, short_call, long_put, short_put, long_bond];
};

const computeTradeStrategy = (thisTrade: Trade, virtuals: Record<number, VirtualPositionEntry>): void => {
if (!thisTrade.strategy) {
let strategy: TradeStrategy = TradeStrategy.undefined;
const [long_stock, short_stock, long_call, short_call, long_put, short_put] = countContratTypes(virtuals);
if (long_stock > 0) {
const [long_stock, short_stock, long_call, short_call, long_put, short_put, long_bond] =
countContratTypes(virtuals);
if (long_bond > 0) {
strategy = TradeStrategy["long bond"];
} else if (long_stock > 0) {
// Longs stock strategies
if (short_call > 0) {
const stocks_cost = Object.values(virtuals)
Expand Down Expand Up @@ -580,9 +602,8 @@ const computeRisk_Generic = (thisTrade: Trade, statements: StatementEntry[]): Re
}
}

// Sum costs of open positions to compute expiry P&L + already realized PnL
thisTrade.expiryPnl =
Object.values(virtuals).reduce((p, item) => (item.quantity ? p - item.cost : p), 0) + thisTrade.PnL;
// Sum costs of open positions to compute expiry P&L
thisTrade.expiryPnl = Object.values(virtuals).reduce((p, item) => (item.quantity ? p - item.cost : p), 0);

// All done
return virtuals;
Expand Down
Loading

0 comments on commit 853e6cf

Please sign in to comment.