feat(trading): use notional for candle calcs (#6119)
This commit is contained in:
parent
64fc075f7a
commit
840ca63464
@ -61,8 +61,8 @@ export const MarketHeaderStats = ({ market }: MarketHeaderStatsProps) => {
|
||||
<HeaderStat heading={t('Volume (24h)')} testId="market-volume">
|
||||
<Last24hVolume
|
||||
marketId={market.id}
|
||||
positionDecimalPlaces={market.positionDecimalPlaces}
|
||||
marketDecimals={market.decimalPlaces}
|
||||
positionDecimalPlaces={market.positionDecimalPlaces}
|
||||
quoteUnit={quoteUnit}
|
||||
/>
|
||||
</HeaderStat>
|
||||
|
@ -15,7 +15,6 @@ import {
|
||||
import { ButtonLink, Tooltip } from '@vegaprotocol/ui-toolkit';
|
||||
import { useAssetDetailsDialogStore } from '@vegaprotocol/assets';
|
||||
import type {
|
||||
MarketFieldsFragment,
|
||||
MarketMaybeWithData,
|
||||
MarketMaybeWithDataAndCandles,
|
||||
} from '@vegaprotocol/markets';
|
||||
@ -166,16 +165,12 @@ export const useMarketsColumnDefs = () => {
|
||||
valueFormatter: ({
|
||||
data,
|
||||
}: ValueFormatterParams<MarketMaybeWithDataAndCandles, 'candles'>) => {
|
||||
const candles = data?.candles;
|
||||
if (!data) return '-';
|
||||
const candles = data.candles;
|
||||
const vol = candles ? calcCandleVolume(candles) : '0';
|
||||
const quoteName = getQuoteName(data as MarketFieldsFragment);
|
||||
const quoteName = getQuoteName(data);
|
||||
const volPrice =
|
||||
candles &&
|
||||
calcCandleVolumePrice(
|
||||
candles,
|
||||
data.decimalPlaces,
|
||||
data.positionDecimalPlaces
|
||||
);
|
||||
candles && calcCandleVolumePrice(candles, data.decimalPlaces);
|
||||
|
||||
const volume =
|
||||
data && vol && vol !== '0'
|
||||
|
@ -115,6 +115,7 @@ describe('MarketSettledBanner', () => {
|
||||
open: '100',
|
||||
close: '100',
|
||||
volume: '100',
|
||||
notional: '10000',
|
||||
periodStart: subHours(new Date(now), 1).toISOString(),
|
||||
},
|
||||
},
|
||||
@ -125,6 +126,7 @@ describe('MarketSettledBanner', () => {
|
||||
open: '100',
|
||||
close: '200',
|
||||
volume: '100',
|
||||
notional: '10000',
|
||||
periodStart: subHours(new Date(now), 2).toISOString(),
|
||||
},
|
||||
},
|
||||
@ -161,6 +163,7 @@ describe('MarketSettledBanner', () => {
|
||||
open: '100',
|
||||
close: '100',
|
||||
volume: '100',
|
||||
notional: '10000',
|
||||
periodStart: '2020-01-01T00:00:00',
|
||||
},
|
||||
},
|
||||
|
@ -79,6 +79,7 @@ describe('MarketSelectorItem', () => {
|
||||
high: '5',
|
||||
low: '5',
|
||||
volume: '50',
|
||||
notional: '10000',
|
||||
periodStart: yesterday.toISOString(),
|
||||
},
|
||||
{
|
||||
@ -87,6 +88,7 @@ describe('MarketSelectorItem', () => {
|
||||
high: '10',
|
||||
low: '10',
|
||||
volume: '50',
|
||||
notional: '10000',
|
||||
periodStart: yesterday.toISOString(),
|
||||
},
|
||||
];
|
||||
|
@ -3,7 +3,7 @@ import * as Types from '@vegaprotocol/types';
|
||||
import { gql } from '@apollo/client';
|
||||
import * as Apollo from '@apollo/client';
|
||||
const defaultOptions = {} as const;
|
||||
export type MarketCandlesFieldsFragment = { __typename?: 'Candle', high: string, low: string, open: string, close: string, volume: string, periodStart: any };
|
||||
export type MarketCandlesFieldsFragment = { __typename?: 'Candle', high: string, low: string, open: string, close: string, volume: string, notional: string, periodStart: any };
|
||||
|
||||
export type MarketCandlesQueryVariables = Types.Exact<{
|
||||
interval: Types.Interval;
|
||||
@ -12,7 +12,7 @@ export type MarketCandlesQueryVariables = Types.Exact<{
|
||||
}>;
|
||||
|
||||
|
||||
export type MarketCandlesQuery = { __typename?: 'Query', marketsConnection?: { __typename?: 'MarketConnection', edges: Array<{ __typename?: 'MarketEdge', node: { __typename?: 'Market', candlesConnection?: { __typename?: 'CandleDataConnection', edges?: Array<{ __typename?: 'CandleEdge', node: { __typename?: 'Candle', high: string, low: string, open: string, close: string, volume: string, periodStart: any } } | null> | null } | null } }> } | null };
|
||||
export type MarketCandlesQuery = { __typename?: 'Query', marketsConnection?: { __typename?: 'MarketConnection', edges: Array<{ __typename?: 'MarketEdge', node: { __typename?: 'Market', candlesConnection?: { __typename?: 'CandleDataConnection', edges?: Array<{ __typename?: 'CandleEdge', node: { __typename?: 'Candle', high: string, low: string, open: string, close: string, volume: string, notional: string, periodStart: any } } | null> | null } | null } }> } | null };
|
||||
|
||||
export type MarketCandlesUpdateSubscriptionVariables = Types.Exact<{
|
||||
marketId: Types.Scalars['ID'];
|
||||
@ -20,7 +20,7 @@ export type MarketCandlesUpdateSubscriptionVariables = Types.Exact<{
|
||||
}>;
|
||||
|
||||
|
||||
export type MarketCandlesUpdateSubscription = { __typename?: 'Subscription', candles: { __typename?: 'Candle', high: string, low: string, open: string, close: string, volume: string, periodStart: any } };
|
||||
export type MarketCandlesUpdateSubscription = { __typename?: 'Subscription', candles: { __typename?: 'Candle', high: string, low: string, open: string, close: string, volume: string, notional: string, periodStart: any } };
|
||||
|
||||
export const MarketCandlesFieldsFragmentDoc = gql`
|
||||
fragment MarketCandlesFields on Candle {
|
||||
@ -29,6 +29,7 @@ export const MarketCandlesFieldsFragmentDoc = gql`
|
||||
open
|
||||
close
|
||||
volume
|
||||
notional
|
||||
periodStart
|
||||
}
|
||||
`;
|
||||
|
@ -10,7 +10,7 @@ export type MarketsCandlesQueryVariables = Types.Exact<{
|
||||
}>;
|
||||
|
||||
|
||||
export type MarketsCandlesQuery = { __typename?: 'Query', marketsConnection?: { __typename?: 'MarketConnection', edges: Array<{ __typename?: 'MarketEdge', node: { __typename?: 'Market', id: string, candlesConnection?: { __typename?: 'CandleDataConnection', edges?: Array<{ __typename?: 'CandleEdge', node: { __typename?: 'Candle', high: string, low: string, open: string, close: string, volume: string, periodStart: any } } | null> | null } | null } }> } | null };
|
||||
export type MarketsCandlesQuery = { __typename?: 'Query', marketsConnection?: { __typename?: 'MarketConnection', edges: Array<{ __typename?: 'MarketEdge', node: { __typename?: 'Market', id: string, candlesConnection?: { __typename?: 'CandleDataConnection', edges?: Array<{ __typename?: 'CandleEdge', node: { __typename?: 'Candle', high: string, low: string, open: string, close: string, volume: string, notional: string, periodStart: any } } | null> | null } | null } }> } | null };
|
||||
|
||||
|
||||
export const MarketsCandlesDocument = gql`
|
||||
|
@ -13,7 +13,7 @@ interface Props {
|
||||
positionDecimalPlaces?: number;
|
||||
formatDecimals?: number;
|
||||
initialValue?: string;
|
||||
marketDecimals?: number;
|
||||
marketDecimals: number;
|
||||
quoteUnit?: string;
|
||||
}
|
||||
|
||||
@ -41,11 +41,7 @@ export const Last24hVolume = ({
|
||||
: initialValue;
|
||||
|
||||
const candleVolumePrice = oneDayCandles
|
||||
? calcCandleVolumePrice(
|
||||
oneDayCandles,
|
||||
marketDecimals,
|
||||
positionDecimalPlaces
|
||||
)
|
||||
? calcCandleVolumePrice(oneDayCandles, marketDecimals)
|
||||
: initialValue;
|
||||
|
||||
return (
|
||||
|
@ -205,8 +205,8 @@ export const MarketVolumeInfoPanel = ({ market }: MarketInfoProps) => {
|
||||
'24hourVolume': (
|
||||
<Last24hVolume
|
||||
marketId={market.id}
|
||||
positionDecimalPlaces={market.positionDecimalPlaces}
|
||||
marketDecimals={market.decimalPlaces}
|
||||
positionDecimalPlaces={market.positionDecimalPlaces}
|
||||
quoteUnit={getQuoteName(market)}
|
||||
/>
|
||||
),
|
||||
|
@ -1,12 +1,15 @@
|
||||
import { update } from './market-candles-provider';
|
||||
|
||||
describe('market candles provider update', () => {
|
||||
const price = 153350000;
|
||||
const volume = 50;
|
||||
const data = [
|
||||
{
|
||||
high: '153350000',
|
||||
low: '153350000',
|
||||
open: '153350000',
|
||||
close: '153350000',
|
||||
high: price.toString(),
|
||||
low: price.toString(),
|
||||
open: price.toString(),
|
||||
close: price.toString(),
|
||||
notional: (price * volume).toString(),
|
||||
volume: '50',
|
||||
periodStart: '2022-11-01T15:49:00Z',
|
||||
},
|
||||
|
@ -4,6 +4,7 @@ fragment MarketCandlesFields on Candle {
|
||||
open
|
||||
close
|
||||
volume
|
||||
notional
|
||||
periodStart
|
||||
}
|
||||
|
||||
|
@ -40,5 +40,6 @@ const marketCandlesFieldsFragment: MarketCandlesFieldsFragment = {
|
||||
high: '110',
|
||||
low: '90',
|
||||
volume: '1',
|
||||
notional: '100',
|
||||
periodStart: '2022-11-01T15:49:00Z',
|
||||
};
|
||||
|
@ -164,6 +164,7 @@ describe('calcCandleVolumePrice', () => {
|
||||
low: '10',
|
||||
open: '15',
|
||||
close: '90',
|
||||
notional: '100',
|
||||
periodStart: '2022-05-18T13:08:27.693537312Z',
|
||||
},
|
||||
{
|
||||
@ -172,13 +173,11 @@ describe('calcCandleVolumePrice', () => {
|
||||
low: '10',
|
||||
open: '15',
|
||||
close: '90',
|
||||
notional: '100',
|
||||
periodStart: '2022-05-18T14:08:27.693537312Z',
|
||||
},
|
||||
];
|
||||
const marketDecimals = 3;
|
||||
const positionDecimalPlaces = 2;
|
||||
expect(
|
||||
calcCandleVolumePrice(candles, marketDecimals, positionDecimalPlaces)
|
||||
).toEqual('2');
|
||||
const decimalPlaces = 2;
|
||||
expect(calcCandleVolumePrice(candles, decimalPlaces)).toEqual('2');
|
||||
});
|
||||
});
|
||||
|
@ -1,8 +1,4 @@
|
||||
import {
|
||||
addDecimal,
|
||||
formatNumberPercentage,
|
||||
toBigNum,
|
||||
} from '@vegaprotocol/utils';
|
||||
import { formatNumberPercentage, toBigNum } from '@vegaprotocol/utils';
|
||||
import { MarketState, MarketTradingMode } from '@vegaprotocol/types';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import orderBy from 'lodash/orderBy';
|
||||
@ -182,19 +178,12 @@ export const calcCandleVolume = (candles: Candle[]): string | undefined =>
|
||||
*/
|
||||
export const calcCandleVolumePrice = (
|
||||
candles: Candle[],
|
||||
marketDecimals: number = 1,
|
||||
positionDecimalPlaces: number = 1
|
||||
decimalPlaces: number
|
||||
): string | undefined =>
|
||||
candles &&
|
||||
candles.reduce(
|
||||
(acc, c) =>
|
||||
new BigNumber(acc)
|
||||
.plus(
|
||||
BigNumber(addDecimal(c.volume, positionDecimalPlaces)).times(
|
||||
addDecimal(c.high, marketDecimals)
|
||||
)
|
||||
)
|
||||
.toString(),
|
||||
new BigNumber(acc).plus(toBigNum(c.notional, decimalPlaces)).toString(),
|
||||
'0'
|
||||
);
|
||||
|
||||
|
@ -50,6 +50,7 @@ const createCandle = (
|
||||
high: '110',
|
||||
low: '90',
|
||||
volume: '1',
|
||||
notional: '100',
|
||||
periodStart: '2022-11-01T15:49:00Z',
|
||||
};
|
||||
return merge(defaultCandle, override);
|
||||
|
Loading…
Reference in New Issue
Block a user