fix(candles-chart): ignore candles from before market open date (#5678)
This commit is contained in:
parent
6d2f367987
commit
efb746f373
@ -20,6 +20,9 @@ query Candles($marketId: ID!, $interval: Interval!, $since: String!) {
|
||||
code
|
||||
}
|
||||
}
|
||||
marketTimestamps {
|
||||
open
|
||||
}
|
||||
candlesConnection(
|
||||
interval: $interval
|
||||
since: $since
|
||||
|
@ -12,7 +12,7 @@ export type CandlesQueryVariables = Types.Exact<{
|
||||
}>;
|
||||
|
||||
|
||||
export type CandlesQuery = { __typename?: 'Query', market?: { __typename?: 'Market', id: string, decimalPlaces: number, positionDecimalPlaces: number, tradableInstrument: { __typename?: 'TradableInstrument', instrument: { __typename?: 'Instrument', id: string, name: string, code: string } }, candlesConnection?: { __typename?: 'CandleDataConnection', edges?: Array<{ __typename?: 'CandleEdge', node: { __typename?: 'Candle', periodStart: any, lastUpdateInPeriod: any, high: string, low: string, open: string, close: string, volume: string } } | null> | null } | null } | null };
|
||||
export type CandlesQuery = { __typename?: 'Query', market?: { __typename?: 'Market', id: string, decimalPlaces: number, positionDecimalPlaces: number, tradableInstrument: { __typename?: 'TradableInstrument', instrument: { __typename?: 'Instrument', id: string, name: string, code: string } }, marketTimestamps: { __typename?: 'MarketTimestamps', open: any }, candlesConnection?: { __typename?: 'CandleDataConnection', edges?: Array<{ __typename?: 'CandleEdge', node: { __typename?: 'Candle', periodStart: any, lastUpdateInPeriod: any, high: string, low: string, open: string, close: string, volume: string } } | null> | null } | null } | null };
|
||||
|
||||
export type CandlesEventsSubscriptionVariables = Types.Exact<{
|
||||
marketId: Types.Scalars['ID'];
|
||||
@ -46,6 +46,9 @@ export const CandlesDocument = gql`
|
||||
code
|
||||
}
|
||||
}
|
||||
marketTimestamps {
|
||||
open
|
||||
}
|
||||
candlesConnection(interval: $interval, since: $since, pagination: {last: 5000}) {
|
||||
edges {
|
||||
node {
|
||||
|
@ -15,6 +15,10 @@ export const candlesQuery = (
|
||||
id: 'market-0',
|
||||
decimalPlaces: 5,
|
||||
positionDecimalPlaces: 0,
|
||||
marketTimestamps: {
|
||||
__typename: 'MarketTimestamps',
|
||||
open: '2022-04-06T09:15:00Z',
|
||||
},
|
||||
tradableInstrument: {
|
||||
instrument: {
|
||||
id: '',
|
||||
|
@ -13,6 +13,9 @@ const returnDataMocks = (nodes: CandleFieldsFragment[]): CandlesQuery => {
|
||||
market: {
|
||||
decimalPlaces: 1,
|
||||
positionDecimalPlaces: 1,
|
||||
marketTimestamps: {
|
||||
open: '2022-05-10T11:00:00Z',
|
||||
},
|
||||
candlesConnection: {
|
||||
edges: nodes.map((node) => ({ node })),
|
||||
},
|
||||
|
@ -172,13 +172,30 @@ export class VegaDataSource implements DataSource {
|
||||
},
|
||||
fetchPolicy: 'no-cache',
|
||||
});
|
||||
|
||||
if (data?.market?.candlesConnection?.edges) {
|
||||
const decimalPlaces = data.market.decimalPlaces;
|
||||
const positionDecimalPlaces = data.market.positionDecimalPlaces;
|
||||
|
||||
const openSince =
|
||||
typeof data.market.marketTimestamps.open === 'string' &&
|
||||
data.market.marketTimestamps.open.length > 0
|
||||
? new Date(data.market.marketTimestamps.open)
|
||||
: // this should never happen, but just in case let's have it as
|
||||
// Date(0) if the market data is incomplete for some reason
|
||||
new Date(0);
|
||||
|
||||
if (this.from < openSince) {
|
||||
// overwrite `from` if requested value is before the market's open date
|
||||
this.from = openSince;
|
||||
}
|
||||
|
||||
const candles = data.market.candlesConnection.edges
|
||||
.map((edge) => edge?.node)
|
||||
.filter((node): node is CandleFieldsFragment => !!node)
|
||||
.filter(
|
||||
(node) => sinceMarketOpen(node, openSince) && !emptyCandle(node)
|
||||
)
|
||||
.map((node) =>
|
||||
parseCandle(node, decimalPlaces, positionDecimalPlaces)
|
||||
)
|
||||
@ -326,3 +343,9 @@ function parseCandle(
|
||||
volume: Number(addDecimal(candle.volume, positionDecimalPlaces)),
|
||||
};
|
||||
}
|
||||
|
||||
const sinceMarketOpen = (candle: CandleFieldsFragment, openSince: Date) =>
|
||||
new Date(candle.periodStart) >= openSince;
|
||||
|
||||
const emptyCandle = (candle: CandleFieldsFragment) =>
|
||||
candle.high === '' && candle.low === '';
|
||||
|
Loading…
Reference in New Issue
Block a user