Skip to content

Commit

Permalink
fix(account-info-resolver): some bugs (#913)
Browse files Browse the repository at this point in the history
  • Loading branch information
zccz14 authored Nov 20, 2024
1 parent 19bfd9b commit 202aa1d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
10 changes: 10 additions & 0 deletions common/changes/@yuants/account-info-resolver/2024-11-20-07-24.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@yuants/account-info-resolver",
"comment": "fix bugs",
"type": "patch"
}
],
"packageName": "@yuants/account-info-resolver"
}
9 changes: 6 additions & 3 deletions libraries/account-info-resolver/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class AccountInfoResolver implements IAccountInfoResolver {
(this.mapAccountIdToProductIdToPositions[accountInfo.account_id][position.product_id] ??= []).push(
position,
);
(this.mapProductIdToPositions[position.product_id] ??= new Set()).add(position);
}
}

Expand Down Expand Up @@ -97,6 +98,9 @@ export class AccountInfoResolver implements IAccountInfoResolver {
theAccountInfo.money.used -= thePosition.margin || 0;
theAccountInfo.money.profit -= thePosition.floating_profit || 0;
this.mapProductIdToPositions[product_id].delete(thePosition);
if (this.mapProductIdToPositions[product_id].size === 0) {
delete this.mapProductIdToPositions[product_id];
}
this._positionExit$.next(thePosition);
}

Expand Down Expand Up @@ -141,7 +145,7 @@ export class AccountInfoResolver implements IAccountInfoResolver {
if (order.profit_correction) {
theAccountInfo.money.balance += order.profit_correction;
}
thePosition.updated_at = order.filled_at;
thePosition.updated_at = order.filled_at;

// ISSUE: 假设订单一旦成交即全部成交
if (order.order_direction === 'OPEN_LONG' || order.order_direction === 'OPEN_SHORT') {
Expand Down Expand Up @@ -218,10 +222,9 @@ thePosition.updated_at = order.filled_at;
const quote = this.mapProductIdToQuote.get(product_id);
const product = this.mapProductIdToProduct.get(product_id);
if (!product) throw new Error(`Product not found: ${product_id}`);
if (!quote) throw new Error(`Quote not found: ${product_id}`);

// Position is valid.
const closable_price = position.direction === 'LONG' ? quote.bid : quote.ask;
const closable_price = position.direction === 'LONG' ? quote?.bid || 0 : quote?.ask || 0;
const floating_profit =
getProfit(
product,
Expand Down

0 comments on commit 202aa1d

Please sign in to comment.