Skip to content

Commit

Permalink
feat: enable order book candles using feature flag (#1351)
Browse files Browse the repository at this point in the history
  • Loading branch information
tinaszheng authored Dec 4, 2024
1 parent e6b0bd8 commit 7b65342
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 123 deletions.
2 changes: 0 additions & 2 deletions src/constants/tooltips/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { newMarketsTooltips } from './newMarkets';
import { portfolioTooltips } from './portfolio';
import { stakeTooltips } from './stake';
import { tradeTooltips } from './trade';
import { tradeChartTooltips } from './tradeChart';
import { triggersTooltips } from './triggers';
import { vaultTooltips } from './vault';
import { withdrawTooltips } from './withdraw';
Expand All @@ -19,7 +18,6 @@ export const tooltipStrings = {
...portfolioTooltips,
...stakeTooltips,
...tradeTooltips,
...tradeChartTooltips,
...triggersTooltips,
...withdrawTooltips,
...vaultTooltips,
Expand Down
8 changes: 0 additions & 8 deletions src/constants/tooltips/tradeChart.ts

This file was deleted.

40 changes: 0 additions & 40 deletions src/hooks/tradingView/useOrderbookCandles.ts

This file was deleted.

43 changes: 6 additions & 37 deletions src/hooks/tradingView/useTradingView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { DEFAULT_RESOLUTION } from '@/constants/candles';
import { TOGGLE_ACTIVE_CLASS_NAME } from '@/constants/charts';
import { STRING_KEYS, SUPPORTED_LOCALE_MAP } from '@/constants/localization';
import { StatsigFlags } from '@/constants/statsig';
import { tooltipStrings } from '@/constants/tooltips';
import type { TvWidget } from '@/constants/tvchart';

import { store } from '@/state/_store';
Expand All @@ -25,15 +24,15 @@ import { getCurrentMarketConfig, getCurrentMarketId } from '@/state/perpetualsSe
import { updateChartConfig } from '@/state/tradingView';
import { getTvChartConfig } from '@/state/tradingViewSelectors';

import abacusStateManager from '@/lib/abacus';
import { getDydxDatafeed } from '@/lib/tradingView/dydxfeed';
import { getSavedResolution, getWidgetOptions, getWidgetOverrides } from '@/lib/tradingView/utils';
import { orEmptyObj } from '@/lib/typeUtils';

import { useDydxClient } from '../useDydxClient';
import { useLocaleSeparators } from '../useLocaleSeparators';
import { useAllStatsigGateValues, useStatsigGateValue } from '../useStatsig';
import { useStatsigGateValue } from '../useStatsig';
import { useStringGetter } from '../useStringGetter';
import { useURLConfigs } from '../useURLConfigs';
import { useTradingViewLimitOrder } from './useTradingViewLimitOrder';

/**
Expand All @@ -45,9 +44,6 @@ export const useTradingView = ({
orderLineToggleRef,
orderLinesToggleOn,
setOrderLinesToggleOn,
orderbookCandlesToggleRef,
orderbookCandlesToggleOn,
setOrderbookCandlesToggleOn,
buySellMarksToggleRef,
buySellMarksToggleOn,
setBuySellMarksToggleOn,
Expand All @@ -57,16 +53,11 @@ export const useTradingView = ({
orderLineToggleRef: React.MutableRefObject<HTMLElement | null>;
orderLinesToggleOn: boolean;
setOrderLinesToggleOn: Dispatch<SetStateAction<boolean>>;
orderbookCandlesToggleRef: React.MutableRefObject<HTMLElement | null>;
orderbookCandlesToggleOn: boolean;
setOrderbookCandlesToggleOn: Dispatch<SetStateAction<boolean>>;
buySellMarksToggleRef: React.MutableRefObject<HTMLElement | null>;
buySellMarksToggleOn: boolean;
setBuySellMarksToggleOn: Dispatch<SetStateAction<boolean>>;
}) => {
const stringGetter = useStringGetter();
const urlConfigs = useURLConfigs();
const featureFlags = useAllStatsigGateValues();
const dispatch = useAppDispatch();

const { group, decimal } = useLocaleSeparators();
Expand All @@ -82,6 +73,9 @@ export const useTradingView = ({

const savedTvChartConfig = useAppSelector(getTvChartConfig);
const ffEnableOrderbookCandles = useStatsigGateValue(StatsigFlags.ffEnableOhlc);
useEffect(() => {
abacusStateManager.toggleOrderbookCandles(ffEnableOrderbookCandles);
}, [ffEnableOrderbookCandles]);

const savedResolution = useMemo(
() => getSavedResolution({ savedConfig: savedTvChartConfig }),
Expand Down Expand Up @@ -156,7 +150,7 @@ export const useTradingView = ({
store,
getCandlesForDatafeed,
initialPriceScale,
orderbookCandlesToggleOn,
ffEnableOrderbookCandles,
{ decimal, group },
selectedLocale,
stringGetter
Expand Down Expand Up @@ -190,26 +184,6 @@ export const useTradingView = ({
}),
});

if (ffEnableOrderbookCandles) {
// Orderbook Candles (OHLC)
const getOhlcTooltipString = tooltipStrings.ohlc;
const { title: ohlcTitle, body: ohlcBody } = getOhlcTooltipString({
stringGetter,
stringParams: {},
urlConfigs,
featureFlags,
});

initializeToggle({
toggleRef: orderbookCandlesToggleRef,
widget: tvChartWidget,
isOn: orderbookCandlesToggleOn,
setToggleOn: setOrderbookCandlesToggleOn,
label: `${ohlcTitle}*`,
tooltip: ohlcBody as string,
});
}

// Buy/Sell Marks
initializeToggle({
toggleRef: buySellMarksToggleRef,
Expand All @@ -236,8 +210,6 @@ export const useTradingView = ({
return () => {
orderLineToggleRef.current?.remove();
orderLineToggleRef.current = null;
orderbookCandlesToggleRef.current?.remove();
orderbookCandlesToggleRef.current = null;
buySellMarksToggleRef.current?.remove();
buySellMarksToggleRef.current = null;
tvWidget?.remove();
Expand All @@ -248,12 +220,9 @@ export const useTradingView = ({
!!marketId,
tickSizeDecimals !== undefined,
orderLineToggleRef,
orderbookCandlesToggleRef,
buySellMarksToggleRef,
setBuySellMarksToggleOn,
setOrderLinesToggleOn,
setOrderbookCandlesToggleOn,
orderbookCandlesToggleOn,
tvWidget,
setTvWidget,
]);
Expand Down
13 changes: 0 additions & 13 deletions src/hooks/tradingView/useTradingViewToggles.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
import { useState } from 'react';

import { StatsigFlags } from '@/constants/statsig';

import { useStatsigGateValue } from '../useStatsig';

export const useTradingViewToggles = () => {
const ffEnableOrderbookCandles = useStatsigGateValue(StatsigFlags.ffEnableOhlc);
// When the orderbook candles (displayed as OHLC) toggle is on, empty (0 trade) candles in markets will show
// O(pen) H(igh) L(ow) C(lose) data via orderbook mid-price.
// Otherwise, candles calculate OHLC data from historical trades.
const [orderbookCandlesToggleOn, setOrderbookCandlesToggleOn] =
useState(ffEnableOrderbookCandles);
const [orderLinesToggleOn, setOrderLinesToggleOn] = useState(true);
const [buySellMarksToggleOn, setBuySellMarksToggleOn] = useState(true);

return {
// Orderbook Candles
orderbookCandlesToggleOn,
setOrderbookCandlesToggleOn,
// Chart Order Lines
orderLinesToggleOn,
setOrderLinesToggleOn,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/abacus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ class AbacusStateManager {
this.websocket.send(requestText);
};

toggleOrderbookCandles = (useOrderbookCandles: boolean) => {
this.websocket.orderbookCandlesToggleOn = useOrderbookCandles;
toggleOrderbookCandles = (showOrderbookCandles: boolean) => {
this.websocket.orderbookCandlesToggleOn = showOrderbookCandles;
};

getChainById = (chainId: string) => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/abacus/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AbacusWebsocket implements Omit<AbacusWebsocketProtocol, '__doNotUseOrImpl

private isConnecting: boolean = false;

orderbookCandlesToggleOn: boolean = false;
orderbookCandlesToggleOn: boolean = true;

connect(url: string, connected: (p0: boolean) => void, received: (p0: string) => void): void {
this.url = url;
Expand Down
15 changes: 8 additions & 7 deletions src/lib/tradingView/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import { AppTheme, type AppColorMode } from '@/state/appUiConfigs';

import { getDisplayableTickerFromMarket } from '../assetUtils';

const MIN_NUM_TRADES_FOR_ORDERBOOK_PRICES = 10;
// Show order book candles instead of trade candles if there are no trades in that time period
const MAX_NUM_TRADES_FOR_ORDERBOOK_PRICES = 1;

const getOhlcValues = ({
orderbookCandlesToggleOn,
Expand All @@ -37,17 +38,17 @@ const getOhlcValues = ({
orderbookOpen?: number;
orderbookClose?: number;
}) => {
const useOrderbookCandles =
const showOrderbookCandles =
orderbookCandlesToggleOn &&
trades <= MIN_NUM_TRADES_FOR_ORDERBOOK_PRICES &&
trades <= MAX_NUM_TRADES_FOR_ORDERBOOK_PRICES &&
orderbookOpen !== undefined &&
orderbookClose !== undefined;

return {
low: useOrderbookCandles ? Math.min(orderbookOpen, orderbookClose) : tradeLow,
high: useOrderbookCandles ? Math.max(orderbookOpen, orderbookClose) : tradeHigh,
open: useOrderbookCandles ? orderbookOpen : tradeOpen,
close: useOrderbookCandles ? orderbookClose : tradeClose,
low: showOrderbookCandles ? Math.min(orderbookOpen, orderbookClose) : tradeLow,
high: showOrderbookCandles ? Math.max(orderbookOpen, orderbookClose) : tradeHigh,
open: showOrderbookCandles ? orderbookOpen : tradeOpen,
close: showOrderbookCandles ? orderbookClose : tradeClose,
};
};

Expand Down
13 changes: 0 additions & 13 deletions src/views/charts/TradingView/TvChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type { TvWidget } from '@/constants/tvchart';
import { useBuySellMarks } from '@/hooks/tradingView/useBuySellMarks';
import { useChartLines } from '@/hooks/tradingView/useChartLines';
import { useChartMarketAndResolution } from '@/hooks/tradingView/useChartMarketAndResolution';
import { useOrderbookCandles } from '@/hooks/tradingView/useOrderbookCandles';
import { useTradingView } from '@/hooks/tradingView/useTradingView';
import { useTradingViewTheme } from '@/hooks/tradingView/useTradingViewTheme';
import { useTradingViewToggles } from '@/hooks/tradingView/useTradingViewToggles';
Expand All @@ -24,16 +23,12 @@ export const TvChart = () => {
const orderLineToggleRef = useRef<HTMLElement | null>(null);
const orderLineToggle = orderLineToggleRef.current;

const orderbookCandlesToggleRef = useRef<HTMLElement | null>(null);
const orderbookCandlesToggle = orderbookCandlesToggleRef.current;
const buySellMarksToggleRef = useRef<HTMLElement | null>(null);
const buySellMarksToggle = buySellMarksToggleRef.current;

const {
orderLinesToggleOn,
setOrderLinesToggleOn,
orderbookCandlesToggleOn,
setOrderbookCandlesToggleOn,
setBuySellMarksToggleOn,
buySellMarksToggleOn,
} = useTradingViewToggles();
Expand All @@ -44,9 +39,6 @@ export const TvChart = () => {
orderLineToggleRef,
orderLinesToggleOn,
setOrderLinesToggleOn,
orderbookCandlesToggleRef,
orderbookCandlesToggleOn,
setOrderbookCandlesToggleOn,
buySellMarksToggleRef,
buySellMarksToggleOn,
setBuySellMarksToggleOn,
Expand All @@ -60,11 +52,6 @@ export const TvChart = () => {
orderLineToggle,
orderLinesToggleOn,
});
useOrderbookCandles({
orderbookCandlesToggle,
orderbookCandlesToggleOn,
tvWidget,
});
useBuySellMarks({
buySellMarksToggle,
buySellMarksToggleOn,
Expand Down

0 comments on commit 7b65342

Please sign in to comment.