add realized pnl helpers, reorder methods
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
parent
1804412574
commit
b84b135dae
|
@ -1315,7 +1315,7 @@ export class PerpPosition {
|
||||||
return this.marketIndex !== PerpPosition.PerpMarketIndexUnset;
|
return this.marketIndex !== PerpPosition.PerpMarketIndexUnset;
|
||||||
}
|
}
|
||||||
|
|
||||||
public getBasePositionNative(perpMarket: PerpMarket): I80F48 {
|
public getBasePosition(perpMarket: PerpMarket): I80F48 {
|
||||||
return I80F48.fromI64(this.basePositionLots.mul(perpMarket.baseLotSize));
|
return I80F48.fromI64(this.basePositionLots.mul(perpMarket.baseLotSize));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1378,14 +1378,6 @@ export class PerpPosition {
|
||||||
return toUiDecimalsForQuote(this.getUnsettledFunding(perpMarket));
|
return toUiDecimalsForQuote(this.getUnsettledFunding(perpMarket));
|
||||||
}
|
}
|
||||||
|
|
||||||
public getEquityUi(perpMarket: PerpMarket): number {
|
|
||||||
if (perpMarket.perpMarketIndex !== this.marketIndex) {
|
|
||||||
throw new Error("PerpPosition doesn't belong to the given market!");
|
|
||||||
}
|
|
||||||
|
|
||||||
return toUiDecimalsForQuote(this.getEquity(perpMarket));
|
|
||||||
}
|
|
||||||
|
|
||||||
public getEquity(perpMarket: PerpMarket): I80F48 {
|
public getEquity(perpMarket: PerpMarket): I80F48 {
|
||||||
if (perpMarket.perpMarketIndex !== this.marketIndex) {
|
if (perpMarket.perpMarketIndex !== this.marketIndex) {
|
||||||
throw new Error("PerpPosition doesn't belong to the given market!");
|
throw new Error("PerpPosition doesn't belong to the given market!");
|
||||||
|
@ -1410,6 +1402,14 @@ export class PerpPosition {
|
||||||
return baseLots.mul(lotsToQuote).add(quoteCurrent);
|
return baseLots.mul(lotsToQuote).add(quoteCurrent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getEquityUi(perpMarket: PerpMarket): number {
|
||||||
|
if (perpMarket.perpMarketIndex !== this.marketIndex) {
|
||||||
|
throw new Error("PerpPosition doesn't belong to the given market!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return toUiDecimalsForQuote(this.getEquity(perpMarket));
|
||||||
|
}
|
||||||
|
|
||||||
public hasOpenOrders(): boolean {
|
public hasOpenOrders(): boolean {
|
||||||
const zero = new BN(0);
|
const zero = new BN(0);
|
||||||
return (
|
return (
|
||||||
|
@ -1446,34 +1446,12 @@ export class PerpPosition {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public cumulativePnlOverPositionLifetimeUi(perpMarket: PerpMarket): number {
|
public canSettlePnl(
|
||||||
if (perpMarket.perpMarketIndex !== this.marketIndex) {
|
group: Group,
|
||||||
throw new Error("PerpPosition doesn't belong to the given market!");
|
perpMarket: PerpMarket,
|
||||||
}
|
account: MangoAccount,
|
||||||
|
): boolean {
|
||||||
const priceChange = perpMarket.price.sub(
|
return !this.getSettleablePnl(group, perpMarket, account).eq(ZERO_I80F48());
|
||||||
this.getAverageEntryPrice(perpMarket),
|
|
||||||
);
|
|
||||||
|
|
||||||
return toUiDecimalsForQuote(
|
|
||||||
this.realizedPnlForPositionNative.add(
|
|
||||||
this.getBasePositionNative(perpMarket).mul(priceChange),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public getUnsettledPnl(perpMarket: PerpMarket): I80F48 {
|
|
||||||
if (perpMarket.perpMarketIndex !== this.marketIndex) {
|
|
||||||
throw new Error("PerpPosition doesn't belong to the given market!");
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.quotePositionNative.add(
|
|
||||||
this.getBasePositionNative(perpMarket).mul(perpMarket.price),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public getUnsettledPnlUi(perpMarket: PerpMarket): number {
|
|
||||||
return toUiDecimalsForQuote(this.getUnsettledPnl(perpMarket));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public updateSettleLimit(perpMarket: PerpMarket): void {
|
public updateSettleLimit(perpMarket: PerpMarket): void {
|
||||||
|
@ -1551,6 +1529,20 @@ export class PerpPosition {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getUnsettledPnl(perpMarket: PerpMarket): I80F48 {
|
||||||
|
if (perpMarket.perpMarketIndex !== this.marketIndex) {
|
||||||
|
throw new Error("PerpPosition doesn't belong to the given market!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.quotePositionNative.add(
|
||||||
|
this.getBasePosition(perpMarket).mul(perpMarket.price),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getUnsettledPnlUi(perpMarket: PerpMarket): number {
|
||||||
|
return toUiDecimalsForQuote(this.getUnsettledPnl(perpMarket));
|
||||||
|
}
|
||||||
|
|
||||||
public getSettleablePnl(
|
public getSettleablePnl(
|
||||||
group: Group,
|
group: Group,
|
||||||
perpMarket: PerpMarket,
|
perpMarket: PerpMarket,
|
||||||
|
@ -1571,7 +1563,7 @@ export class PerpPosition {
|
||||||
return limitedUnsettled;
|
return limitedUnsettled;
|
||||||
}
|
}
|
||||||
|
|
||||||
getSettleablePnlUi(
|
public getSettleablePnlUi(
|
||||||
group: Group,
|
group: Group,
|
||||||
perpMarket: PerpMarket,
|
perpMarket: PerpMarket,
|
||||||
account: MangoAccount,
|
account: MangoAccount,
|
||||||
|
@ -1581,12 +1573,38 @@ export class PerpPosition {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public canSettlePnl(
|
public cumulativePnlOverPositionLifetimeUi(perpMarket: PerpMarket): number {
|
||||||
group: Group,
|
if (perpMarket.perpMarketIndex !== this.marketIndex) {
|
||||||
perpMarket: PerpMarket,
|
throw new Error("PerpPosition doesn't belong to the given market!");
|
||||||
account: MangoAccount,
|
}
|
||||||
): boolean {
|
|
||||||
return !this.getSettleablePnl(group, perpMarket, account).eq(ZERO_I80F48());
|
const priceChange = perpMarket.price.sub(
|
||||||
|
this.getAverageEntryPrice(perpMarket),
|
||||||
|
);
|
||||||
|
|
||||||
|
return toUiDecimalsForQuote(
|
||||||
|
this.realizedPnlForPositionNative.add(
|
||||||
|
this.getBasePosition(perpMarket).mul(priceChange),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getUnRealizedPnlUi(perpMarket: PerpMarket): number {
|
||||||
|
if (perpMarket.perpMarketIndex !== this.marketIndex) {
|
||||||
|
throw new Error("PerpPosition doesn't belong to the given market!");
|
||||||
|
}
|
||||||
|
|
||||||
|
const priceChange = perpMarket.price.sub(
|
||||||
|
this.getAverageEntryPrice(perpMarket),
|
||||||
|
);
|
||||||
|
|
||||||
|
return toUiDecimalsForQuote(
|
||||||
|
this.getBasePosition(perpMarket).mul(priceChange),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getRealizedPnlUi(): number {
|
||||||
|
return toUiDecimalsForQuote(this.realizedPnlForPositionNative);
|
||||||
}
|
}
|
||||||
|
|
||||||
toString(perpMarket?: PerpMarket): string {
|
toString(perpMarket?: PerpMarket): string {
|
||||||
|
|
Loading…
Reference in New Issue