Skip to content

Commit

Permalink
Fix some statistics calculations crash for a frozen position
Browse files Browse the repository at this point in the history
  • Loading branch information
miohtama committed Sep 21, 2023
1 parent 2ffcf69 commit 470e2c0
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/lib/trade-executor/state/position-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ export interface TradingPositionInfo {
valueAtOpen?: USDollarValue;
quantityAtOpen?: TokenUnits;

portfolioWeightAtOpen: Percent;
// How much of total portfolio this position was at open
//
// Might not be available if the opening trade failed.
//
portfolioWeightAtOpen?: Percent;

//
candleTimeBucket: TimeBucket;
Expand All @@ -55,11 +59,16 @@ export interface TradingPositionInfo {

estimatedMaximumRisk?: Percent;

marketMidPriceAtOpen: USDollarPrice;
// What was the "market price" when the position was opened.
//
// Might not be available if the opening trade failed and the position is frozen
//
marketMidPriceAtOpen?: USDollarPrice;

// Is stop loss used with this position
stopLossable: boolean;

// How much stop loss was from the mid price when opening
stopLossPercentOpen?: USDollarValue;
stopLossPriceLast?: USDollarValue;
stopLossPercent?: Percent;
Expand Down Expand Up @@ -211,12 +220,18 @@ export function extractPositionInfo(position: TradingPosition): TradingPositionI
let currentPrice: USDollarPrice | undefined = undefined;
let durationSeconds: UnixTimestamp;

const marketMidPriceAtOpen = firstTrade.price_structure.mid_price;
const marketMidPriceAtOpen: number | undefined = firstTrade?.price_structure?.mid_price;
const stopLossable = position.stop_loss !== null;

const stopLossPriceOpen = getFirstStopLossPrice(position);
const trailingStopLossPercent = position.trailing_stop_loss_pct;
let stopLossPercentOpen = stopLossPriceOpen && stopLossPriceOpen / marketMidPriceAtOpen;
let stopLossPercentOpen;

if(marketMidPriceAtOpen) {
stopLossPercentOpen = stopLossPriceOpen && stopLossPriceOpen / marketMidPriceAtOpen;
} else {
stopLossPercentOpen = undefined;
}

let stopLossTriggered = false;

Expand Down

0 comments on commit 470e2c0

Please sign in to comment.