Skip to content

Commit

Permalink
CT-425 + TRCL-2154 Add orderlines to TradingView + fix colors (#287)
Browse files Browse the repository at this point in the history
* wip

* fix deleted line issue

* compress tgz changes

* fix dependency hook

* simplify

* typo

* add trailing percent

* fix bug

* fix order

* merge order types
  • Loading branch information
moo-onthelawn authored Feb 12, 2024
1 parent b839cf6 commit a7addda
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 47 deletions.
74 changes: 54 additions & 20 deletions src/constants/trade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { AlertType } from '@/constants/alerts';
import { STRING_KEYS } from '@/constants/localization';
import { TimeUnitShort } from '@/constants/time';

// TODO: rename to OrderType
export enum TradeTypes {
MARKET = 'MARKET',
LIMIT = 'LIMIT',
Expand All @@ -15,6 +14,16 @@ export enum TradeTypes {
TRAILING_STOP = 'TRAILING_STOP',
}

enum ClosingTradeTypes {
LIQUIDATED = 'LIQUIDATED',
LIQUIDATION = 'LIQUIDATION',
OFFSETTING = 'OFFSETTING',
DELEVERAGED = 'DELEVERAGED',
FINAL_SETTLEMENT = 'FINAL_SETTLEMENT',
}

export type OrderType = TradeTypes | ClosingTradeTypes;

export enum TimeInForceOptions {
GTT = 'GTT',
FOK = 'FOK',
Expand All @@ -40,49 +49,74 @@ export const POSITION_SIDE_STRINGS: Record<PositionSide, string> = {
[PositionSide.Short]: STRING_KEYS.SHORT_POSITION_SHORT,
};

export const TRADE_TYPE_STRINGS: Record<
TradeTypes,
export const ORDER_TYPE_STRINGS: Record<
OrderType,
{
tradeTypeKeyShort: string;
tradeTypeKey: string;
descriptionKey: string;
orderTypeKeyShort: string;
orderTypeKey: string;
descriptionKey: string | null;
}
> = {
[TradeTypes.LIMIT]: {
tradeTypeKeyShort: STRING_KEYS.LIMIT_ORDER_SHORT,
tradeTypeKey: STRING_KEYS.LIMIT_ORDER,
orderTypeKeyShort: STRING_KEYS.LIMIT_ORDER_SHORT,
orderTypeKey: STRING_KEYS.LIMIT_ORDER,
descriptionKey: STRING_KEYS.LIMIT_ORDER_DESCRIPTION,
},
[TradeTypes.MARKET]: {
tradeTypeKeyShort: STRING_KEYS.MARKET_ORDER_SHORT,
tradeTypeKey: STRING_KEYS.MARKET_ORDER,
orderTypeKeyShort: STRING_KEYS.MARKET_ORDER_SHORT,
orderTypeKey: STRING_KEYS.MARKET_ORDER,
descriptionKey: STRING_KEYS.MARKET_ORDER_DESCRIPTION,
},
[TradeTypes.STOP_LIMIT]: {
tradeTypeKeyShort: STRING_KEYS.STOP_LIMIT,
tradeTypeKey: STRING_KEYS.STOP_LIMIT,
orderTypeKeyShort: STRING_KEYS.STOP_LIMIT,
orderTypeKey: STRING_KEYS.STOP_LIMIT,
descriptionKey: STRING_KEYS.STOP_LIMIT_DESCRIPTION,
},
[TradeTypes.STOP_MARKET]: {
tradeTypeKeyShort: STRING_KEYS.STOP_MARKET,
tradeTypeKey: STRING_KEYS.STOP_MARKET,
orderTypeKeyShort: STRING_KEYS.STOP_MARKET,
orderTypeKey: STRING_KEYS.STOP_MARKET,
descriptionKey: STRING_KEYS.STOP_MARKET_DESCRIPTION,
},
[TradeTypes.TAKE_PROFIT]: {
tradeTypeKeyShort: STRING_KEYS.TAKE_PROFIT_LIMIT,
tradeTypeKey: STRING_KEYS.TAKE_PROFIT_LIMIT,
orderTypeKeyShort: STRING_KEYS.TAKE_PROFIT_LIMIT,
orderTypeKey: STRING_KEYS.TAKE_PROFIT_LIMIT,
descriptionKey: STRING_KEYS.TAKE_PROFIT_LIMIT_DESCRIPTION,
},
[TradeTypes.TAKE_PROFIT_MARKET]: {
tradeTypeKeyShort: STRING_KEYS.TAKE_PROFIT_MARKET,
tradeTypeKey: STRING_KEYS.TAKE_PROFIT_MARKET,
orderTypeKeyShort: STRING_KEYS.TAKE_PROFIT_MARKET,
orderTypeKey: STRING_KEYS.TAKE_PROFIT_MARKET,
descriptionKey: STRING_KEYS.TAKE_PROFIT_MARKET_DESCRIPTION,
},
[TradeTypes.TRAILING_STOP]: {
tradeTypeKeyShort: STRING_KEYS.TRAILING_STOP,
tradeTypeKey: STRING_KEYS.TRAILING_STOP,
orderTypeKeyShort: STRING_KEYS.TRAILING_STOP,
orderTypeKey: STRING_KEYS.TRAILING_STOP,
descriptionKey: STRING_KEYS.TRAILING_STOP_DESCRIPTION,
},
[ClosingTradeTypes.LIQUIDATED]: {
orderTypeKeyShort: STRING_KEYS.LIQUIDATED,
orderTypeKey: STRING_KEYS.LIQUIDATED,
descriptionKey: null,
},
[ClosingTradeTypes.LIQUIDATION]: {
orderTypeKeyShort: STRING_KEYS.LIQUIDATION,
orderTypeKey: STRING_KEYS.LIQUIDATION,
descriptionKey: null,
},
[ClosingTradeTypes.OFFSETTING]: {
orderTypeKeyShort: STRING_KEYS.OFFSETTING,
orderTypeKey: STRING_KEYS.OFFSETTING,
descriptionKey: null,
},
[ClosingTradeTypes.DELEVERAGED]: {
orderTypeKeyShort: STRING_KEYS.DELEVERAGED,
orderTypeKey: STRING_KEYS.DELEVERAGED,
descriptionKey: null,
},
[ClosingTradeTypes.FINAL_SETTLEMENT]: {
orderTypeKeyShort: STRING_KEYS.FINAL_SETTLEMENT,
orderTypeKey: STRING_KEYS.FINAL_SETTLEMENT,
descriptionKey: null,
},
};

export const GOOD_TIL_TIME_TIMESCALE_STRINGS: Record<TimeUnitShort, string> = {
Expand Down
27 changes: 23 additions & 4 deletions src/hooks/tradingView/useTradingView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import isEmpty from 'lodash/isEmpty';
import { LanguageCode, ResolutionString, widget } from 'public/tradingview/charting_library';

import { DEFAULT_RESOLUTION } from '@/constants/candles';
import { SUPPORTED_LOCALE_BASE_TAGS } from '@/constants/localization';
import { SUPPORTED_LOCALE_BASE_TAGS, STRING_KEYS } from '@/constants/localization';

import { LocalStorageKey } from '@/constants/localStorage';
import { useDydxClient, useLocalStorage } from '@/hooks';

import { useDydxClient, useLocalStorage, useStringGetter } from '@/hooks';
import { store } from '@/state/_store';

import { getSelectedNetwork } from '@/state/appSelectors';
Expand All @@ -23,14 +25,19 @@ import { getSavedResolution, getWidgetOptions, getWidgetOverrides } from '@/lib/
*/
export const useTradingView = ({
tvWidgetRef,
displayButtonRef,
setIsChartReady,
}: {
tvWidgetRef: React.MutableRefObject<any>;
displayButtonRef: React.MutableRefObject<any>;
setIsChartReady: React.Dispatch<React.SetStateAction<boolean>>;
}) => {
const marketId = useSelector(getCurrentMarketId);
const stringGetter = useStringGetter();

const appTheme = useSelector(getAppTheme);
const appColorMode = useSelector(getAppColorMode);

const marketId = useSelector(getCurrentMarketId);
const marketIds = useSelector(getMarketIds, shallowEqual);
const selectedLocale = useSelector(getSelectedLocale);
const selectedNetwork = useSelector(getSelectedNetwork);
Expand All @@ -49,7 +56,6 @@ export const useTradingView = ({
const widgetOptions = getWidgetOptions();
const widgetOverrides = getWidgetOverrides({ appTheme, appColorMode });
const options = {
// debug: true,
...widgetOptions,
...widgetOverrides,
datafeed: getDydxDatafeed(store, getCandlesForDatafeed),
Expand All @@ -63,6 +69,17 @@ export const useTradingView = ({
tvWidgetRef.current = tvChartWidget;

tvWidgetRef.current.onChartReady(() => {
tvWidgetRef?.current?.headerReady().then(() => {
displayButtonRef.current = tvWidgetRef?.current?.createButton();
displayButtonRef.current.innerHTML = `<span>${stringGetter({
key: STRING_KEYS.ORDER_LINES,
})}</span> <div class="displayOrdersButton-toggle"></div>`;
displayButtonRef.current.setAttribute(
'title',
stringGetter({ key: STRING_KEYS.ORDER_LINES_TOOLTIP })
);
});

tvWidgetRef?.current?.subscribe('onAutoSaveNeeded', () =>
tvWidgetRef?.current?.save((chartConfig: object) => setTvChartConfig(chartConfig))
);
Expand All @@ -72,6 +89,8 @@ export const useTradingView = ({
}

return () => {
displayButtonRef.current?.remove();
displayButtonRef.current = null;
tvWidgetRef.current?.remove();
tvWidgetRef.current = null;
setIsChartReady(false);
Expand Down
45 changes: 37 additions & 8 deletions src/hooks/tradingView/useTradingViewTheme.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { useEffect } from 'react';
import { useSelector } from 'react-redux';

import type { IChartingLibraryWidget, ThemeName } from 'public/tradingview/charting_library';
import type {
IChartingLibraryWidget,
IOrderLineAdapter,
ThemeName,
} from 'public/tradingview/charting_library';

import { AppColorMode, AppTheme } from '@/state/configs';
import { getAppTheme, getAppColorMode } from '@/state/configsSelectors';

import { getWidgetOverrides } from '@/lib/tradingView/utils';
import { getWidgetOverrides, getOrderLineColors } from '@/lib/tradingView/utils';

/**
* @description Method to define a type guard and check that an element is an IFRAME
Expand All @@ -22,9 +26,11 @@ const isIFrame = (element: HTMLElement | null): element is HTMLIFrameElement =>
* In order to support our Classic along with Dark/Light, we are directly accessing the <html> within the iFrame.
*/
export const useTradingViewTheme = ({
orderLines,
tvWidget,
isWidgetReady,
}: {
orderLines: Record<string, IOrderLineAdapter>;
tvWidget: (IChartingLibraryWidget & { _id?: string; _ready?: boolean }) | null;
isWidgetReady?: boolean;
}) => {
Expand All @@ -37,8 +43,8 @@ export const useTradingViewTheme = ({
.changeTheme?.(
{
[AppTheme.Classic]: '',
[AppTheme.Dark]: 'Dark',
[AppTheme.Light]: 'Light',
[AppTheme.Dark]: 'dark',
[AppTheme.Light]: 'light',
}[appTheme] as ThemeName
)
.then(() => {
Expand All @@ -49,9 +55,17 @@ export const useTradingViewTheme = ({

if (isIFrame(frame) && frame.contentWindow) {
const innerHtml = frame.contentWindow.document.documentElement;

if (appTheme === AppTheme.Classic) {
innerHtml?.classList.remove('theme-dark', 'theme-light');
switch (appTheme) {
case AppTheme.Classic:
innerHtml?.classList.remove('theme-dark', 'theme-light');
break;
case AppTheme.Dark:
innerHtml?.classList.remove('theme-light');
innerHtml?.classList.add('theme-dark');
break;
case AppTheme.Light:
innerHtml?.classList.remove('theme-dark');
innerHtml?.classList.add('theme-light');
}
}
}
Expand All @@ -73,7 +87,22 @@ export const useTradingViewTheme = ({
'volume.color.1': studies_overrides['volume.volume.color.1'],
});
}

// Necessary to update existing chart lines
Object.entries(orderLines).forEach(([key, line]) => {
const { orderColor, borderColor, backgroundColor, textColor, textButtonColor } =
getOrderLineColors({ side: key.split('-')[0], appTheme, appColorMode });

line
.setLineColor(orderColor)
.setQuantityBackgroundColor(orderColor)
.setQuantityBorderColor(borderColor)
.setBodyBackgroundColor(backgroundColor)
.setBodyBorderColor(borderColor)
.setBodyTextColor(textColor)
.setQuantityTextColor(textButtonColor);
});
});
}
}, [appTheme, appColorMode]);
}, [appTheme, appColorMode, isWidgetReady]);
};
29 changes: 28 additions & 1 deletion src/lib/tradingView/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { OrderSide } from '@dydxprotocol/v4-client-js';

import { Candle, TradingViewBar, TradingViewSymbol } from '@/constants/candles';

import type { AppTheme, AppColorMode } from '@/state/configs';
import { AppTheme, type AppColorMode } from '@/state/configs';

import { Themes } from '@/styles/themes';

Expand Down Expand Up @@ -47,6 +49,30 @@ export const getHistorySlice = ({
return bars.filter(({ time }) => time >= fromMs);
};

export const getOrderLineColors = ({
appTheme,
appColorMode,
side,
}: {
appTheme: AppTheme;
appColorMode: AppColorMode;
side: OrderSide;
}) => {
const theme = Themes[appTheme][appColorMode];
const orderColor = {
[OrderSide.BUY]: theme.positive,
[OrderSide.SELL]: theme.negative,
}[side];

return {
orderColor,
borderColor: theme.borderDefault,
backgroundColor: theme.layer1,
textColor: theme.textTertiary,
textButtonColor: theme.textButton,
};
};

export const getWidgetOverrides = ({
appTheme,
appColorMode,
Expand All @@ -57,6 +83,7 @@ export const getWidgetOverrides = ({
const theme = Themes[appTheme][appColorMode];

return {
theme: appTheme === AppTheme.Dark ? 'dark' : AppTheme.Light ? 'light' : '',
overrides: {
'paneProperties.background': theme.layer2,
'paneProperties.horzGridProperties.color': theme.layer3,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/trade/TradeDialogTrigger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useSelector, shallowEqual } from 'react-redux';
import styled, { AnyStyledComponent } from 'styled-components';

import { STRING_KEYS } from '@/constants/localization';
import { TRADE_TYPE_STRINGS } from '@/constants/trade';
import { ORDER_TYPE_STRINGS } from '@/constants/trade';

import { layoutMixins } from '@/styles/layoutMixins';

Expand Down Expand Up @@ -43,7 +43,7 @@ export const TradeDialogTrigger = () => {
<Styled.TradeSummary>
<Styled.TradeType>
<span>
{stringGetter({ key: TRADE_TYPE_STRINGS[selectedTradeType].tradeTypeKey })}
{stringGetter({ key: ORDER_TYPE_STRINGS[selectedTradeType].orderTypeKey })}
</span>
<OrderSideTag size={TagSize.Medium} orderSide={selectedOrderSide} />
</Styled.TradeType>
Expand Down
Loading

0 comments on commit a7addda

Please sign in to comment.