Skip to content

Commit

Permalink
getMaxTradeSize(), getLiquidationPrice() and uPnL overhaul (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipzeta authored Jul 28, 2023
1 parent 457c2cc commit 86e9972
Show file tree
Hide file tree
Showing 9 changed files with 413 additions and 62 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ Version changes are pinned to SDK releases.

## Unreleased

## [1.6.0] 2023-07-25

- Add getMaxTradeSize() and getLiquidationPrice() to risk calcs. ([#252](https://github.com/zetamarkets/sdk/pull/252))
- Reformat pnl into estimateRealizedPnl() and calculateUnrealizedPnl(), using function overloads ([#252](https://github.com/zetamarkets/sdk/pull/252))
- Add checkLiquidity() util function to find the best price in the orderbook for a given size. ([#252](https://github.com/zetamarkets/sdk/pull/252))

## [1.5.0] 2023-07-25

- Clean up some risk calculations under the hood. ([#255](https://github.com/zetamarkets/sdk/pull/255))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zetamarkets/sdk",
"repository": "https://github.com/zetamarkets/sdk/",
"version": "1.5.0",
"version": "1.6.0",
"description": "Zeta SDK",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
10 changes: 2 additions & 8 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,15 +681,9 @@ export class Client {
}
}

public getMarginAccountState(
asset: Asset,
pnlExecutionPrice: number = undefined,
pnlAddTakerFees: boolean = false
): types.MarginAccountState {
public getMarginAccountState(asset: Asset): types.MarginAccountState {
return Exchange.riskCalculator.getMarginAccountState(
this.getSubClient(asset).marginAccount,
pnlExecutionPrice,
pnlAddTakerFees
this.getSubClient(asset).marginAccount
);
}

Expand Down
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ export const CRANK_ACCOUNT_LIMIT = 12;
export const CRANK_PERP_ACCOUNT_LIMIT = 10;
export const MAX_MARKETS_TO_FETCH = 50;

export const MIN_LOT_SIZE = 0.001;
export const PERP_MARKET_ORDER_SPOT_SLIPPAGE = 0.02;

// This is the most we can load per iteration without
// hitting the rate limit.
export const MARKET_LOAD_LIMIT = 12;
Expand Down
11 changes: 2 additions & 9 deletions src/cross-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -838,15 +838,8 @@ export class CrossClient {
);
}

public getAccountState(
pnlExecutionPrice: number = undefined,
pnlAddTakerFees: boolean = false
): types.CrossMarginAccountState {
return Exchange.riskCalculator.getCrossMarginAccountState(
this._account,
pnlExecutionPrice,
pnlAddTakerFees
);
public getAccountState(): types.CrossMarginAccountState {
return Exchange.riskCalculator.getCrossMarginAccountState(this._account);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/risk-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function collectRiskMaps(
imMap: Map<Asset, Number>,
imSCMap: Map<Asset, Number>,
mmMap: Map<Asset, Number>,
mmioMap: Map<Asset, Number>,
upnlMap: Map<Asset, Number>,
unpaidFundingMap: Map<Asset, Number>
): Map<Asset, types.AssetRiskState> {
Expand All @@ -23,6 +24,7 @@ export function collectRiskMaps(
initialMargin: imMap.get(a),
initialMarginSkipConcession: imSCMap.get(a),
maintenanceMargin: mmMap.get(a),
maintenanceMarginIncludingOrders: mmioMap.get(a),
unrealizedPnl: upnlMap.get(a),
unpaidFunding: unpaidFundingMap.get(a),
});
Expand Down Expand Up @@ -149,7 +151,8 @@ export function checkMarginAccountMarginRequirement(
);
let totalMaintenanceMargin =
Exchange.riskCalculator.calculateTotalMaintenanceMargin(
marginAccount
marginAccount,
types.ProgramAccountType.MarginAccount
) as number;
let buffer = marginAccount.balance.toNumber() + pnl - totalMaintenanceMargin;
return buffer > 0;
Expand Down
Loading

0 comments on commit 86e9972

Please sign in to comment.