Skip to content

Commit

Permalink
reuse average price function
Browse files Browse the repository at this point in the history
  • Loading branch information
tinaszheng committed Aug 16, 2024
1 parent 898e65c commit 3de43bc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
1 change: 0 additions & 1 deletion src/constants/candles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export const RESOLUTION_TO_INTERVAL_MS = {
'1D': timeUnits.day,
} as Record<ResolutionString, number>;


/**
* @description ResolutionStrings used with TradingView's charting library mapped to CandleResolution
*/
Expand Down
12 changes: 12 additions & 0 deletions src/lib/orders.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { OrderSide } from '@dydxprotocol/v4-client-js';
import BigNumber from 'bignumber.js';

import {
AbacusOrderStatus,
AbacusOrderType,
AbacusOrderTypes,
KotlinIrEnumValues,
Nullable,
SubaccountFills,
TRADE_TYPES,
type Asset,
type OrderStatus,
Expand Down Expand Up @@ -140,3 +142,13 @@ export const getHydratedTradingData = <

export const getTradeType = (orderType: string) =>
TRADE_TYPES[orderType as KotlinIrEnumValues<typeof AbacusOrderType>];

export const getAverageFillPrice = (fills: SubaccountFills) => {
let total = BigNumber(0);
let totalSize = BigNumber(0);
fills.forEach((fill) => {
total = total.plus(BigNumber(fill.price).times(fill.size));
totalSize = totalSize.plus(fill.size);
});
return totalSize.gt(0) ? total.div(totalSize) : null;
};
22 changes: 11 additions & 11 deletions src/lib/tradingView/dydxfeed/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { formatNumberOutput, OutputType } from '@/components/Output';
import { RootStore } from '@/state/_store';
import { getFillDetails } from '@/state/accountSelectors';

import { getAverageFillPrice } from '@/lib/orders';

/**
* @description Converts times in ms to the appropriate bar time (in seconds)
* For example, if the starting time = 5000ms, interval = 10,000ms, a value of 26,000ms would
Expand All @@ -37,12 +39,6 @@ export function getBarTime(
.toNumber();
}

function averageFillPrice(fills: SubaccountFills) {
const totalSize = sum(fills.map((fill) => fill.size * fill.price));
const size = sum(fills.map((fill) => fill.size));
return totalSize / size;
}

export const getMarkForOrderFills = (
store: RootStore,
orderFills: SubaccountFills,
Expand All @@ -54,11 +50,15 @@ export const getMarkForOrderFills = (
selectedLocale: SupportedLocales,
theme: ThemeColorBase
): Mark => {
const formattedAveragePrice = formatNumberOutput(averageFillPrice(orderFills), OutputType.Fiat, {
decimalSeparator: localeSeparators.decimal,
groupSeparator: localeSeparators.group,
selectedLocale,
});
const formattedAveragePrice = formatNumberOutput(
getAverageFillPrice(orderFills),
OutputType.Fiat,
{
decimalSeparator: localeSeparators.decimal,
groupSeparator: localeSeparators.group,
selectedLocale,
}
);
const formattedSize = formatNumberOutput(
sum(orderFills.map((fill) => fill.size)),
OutputType.Asset,
Expand Down
12 changes: 2 additions & 10 deletions src/state/accountSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
import { NUM_PARENT_SUBACCOUNTS, OnboardingState } from '@/constants/account';
import { LEVERAGE_DECIMALS } from '@/constants/numbers';

import { MustBigNumber } from '@/lib/numbers';
import {
getAverageFillPrice,
getHydratedTradingData,
isOrderStatusClearable,
isOrderStatusOpen,
Expand Down Expand Up @@ -476,15 +476,7 @@ const getFillsForOrderId = createAppSelector(
* @returns the average price the order is filled at
*/
export const getAverageFillPriceForOrder = () =>
createAppSelector([(s, orderId) => getFillsForOrderId(s, orderId)], (fillsForOrderId) => {
let total = MustBigNumber(0);
let totalSize = MustBigNumber(0);
fillsForOrderId.forEach((fill) => {
total = total.plus(MustBigNumber(fill.price).times(fill.size));
totalSize = totalSize.plus(fill.size);
});
return totalSize.gt(0) ? total.div(totalSize) : null;
});
createAppSelector([(s, orderId) => getFillsForOrderId(s, orderId)], getAverageFillPrice);

/**
* @param state
Expand Down

0 comments on commit 3de43bc

Please sign in to comment.