Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add buys and sells as marks on tv chart #927

Merged
merged 13 commits into from
Aug 19, 2024

Conversation

tinaszheng
Copy link
Contributor

@tinaszheng tinaszheng commented Aug 15, 2024

This PR adds Marks to the TradingView chart for historical buy and sell orders.

image image
  • Adding supports_marks: true to the datafeed configuration will tell TV to query the new getMarks method
  • Implemented getMarks
  • Also did some minor cleanup in our custom css file by combining a bunch of duplicate toggle classnames into singular .toggle and .toggle-active

Testing plan

  • Create a bunch of orders and close them (or view a market for which you have a bunch of historic orders)
  • Verify that the buy/sell marks show up on the chart for the correct time interval
  • Verify that the text in the tooltip shows the correct size and average price

@tinaszheng tinaszheng requested a review from a team as a code owner August 15, 2024 21:55
Copy link

linear bot commented Aug 15, 2024

CT-1055 Show buys/sells

Copy link

vercel bot commented Aug 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
v4-staging ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 19, 2024 7:42pm
v4-testnet ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 19, 2024 7:42pm

.toNumber();
}

function averageFillPrice(fills: SubaccountFills) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to consolidate this method with logic from this PR

Copy link
Contributor

@moo-onthelawn moo-onthelawn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

'1': timeUnits.second,
'5': 5 * timeUnits.minute,
'15': 15 * timeUnits.minute,
'30': 30 * timeUnits.minute,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, unrelated to this PR specifically, I wonder if it'd be better to convert CandleResolution, RESOLUTION_MAP, and RESOLUTION_CHART_CONFIGS to functions where we swtich case on the keys (that we could convert to an enum) just to make sure we catch all the cases. But honestly I don't imagine this code will change too much so just a light consider

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like all of these as enums! nice way to get free typing so people don't put in something crazy (unless resolutionStrings already gives us that? that type has a few layers of abstraction to it so it's not immediately clear what it refers to

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh yeah, there's actually a way we can do that with just typescript by converting the library's ResolutionString (which is unfort just a generic string type) to an enum with the same values and changing the type of these maps. for example if you do:

enum InterfaceResolutionString {
  ONE_MINUTE = '1',
  FIVE_MINUTES = '5',
  FIFTEEN_MINUTES = '15',
  THIRTY_MINUTES = '30',
  ONE_HOUR = '60',
  FOUR_HOURS = '240',
  ONE_DAY = '1D',
}

/**
 * @description ResolutionStrings used with TradingView's charting library mapped to time interval per candle in ms
 */
export const RESOLUTION_TO_INTERVAL_MS: { [resolution in InterfaceResolutionString]: number } = {
  '1': timeUnits.second,
  '5': 5 * timeUnits.minute,
  '15': 15 * timeUnits.minute,
  '30': 30 * timeUnits.minute,
  '60': timeUnits.hour,
  '240': 4 * timeUnits.hour,
  // '1D': timeUnits.day,
};

you'll see that this gives a typescript error because the key 1D is missing. we can address this separately if we'd like though :P

we can alternatively also combine all three maps into one map, so something like

type ResolutionConfig = { candleResolution: CandleResolution, intervalMs: number, defaultRange: number }
export const RESOLUTION_MAP: { [resolution: ResolutionString]: ResolutionConfig } = {
  '1': { candleResolution: CandleResolution.ONE_MINUTE, intervalMs: timeUnits.second, defaultRange: timeUnits.hour },
  ...
};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeahhh that's very much in line with what i was envisioning. i dont think either have to be done in this PR but good to keep in mind for a cleanup task 👍

} else {
orderLineToggle?.classList?.remove('order-lines-active');
orderLineToggle?.classList?.remove('toggle-active');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

orderbookCandlesToggleRef.current.setAttribute('title', ohlcBody as string);
}
if (buySellMarksToggleRef) {
buySellMarksToggleRef.current = tvWidgetRef.current.createButton();
buySellMarksToggleRef.current.innerHTML = `<span>Buys/Sells</span> <div class="toggle"></div>`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we localize Buys/Sells? i.e. like ORDER_LINES does it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OMG yes i forgot! i even added it to v4-localization already 😂

buySellMarksToggleRef.current.innerHTML = `<span>Buys/Sells</span> <div class="toggle"></div>`;
buySellMarksToggleRef.current.setAttribute(
'title',
'Display historic buys and sells'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

localize?

const timestampInSeconds = getBarTime(1716091200000, 1723573418524, '1D' as ResolutionString);
expect(timestampInSeconds).toBe(1723521600);
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yessss 🔥

tvWidget,
isChartReady,
currentMarketFills,
theme.positive,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the theme.positive dependency for display pref changes? if so, maybe we update it to theme (because I imagine the red shades and green shades can also be diff for diff themes)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oo yes!

@@ -119,7 +126,7 @@ export const useTradingView = ({
orderLineToggleRef.current = tvWidgetRef.current.createButton();
orderLineToggleRef.current.innerHTML = `<span>${stringGetter({
key: STRING_KEYS.ORDER_LINES,
})}</span> <div class="displayOrdersButton-toggle"></div>`;
})}</span> <div class="toggle"></div>`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I wonder if it's worth factoring out toggleHtml() helper

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think as a followup im going to investigate just combining a lot of the logic here for the three settings that we have :P so combining the html here can be part of that!

time: getBarTime(barStartMs, fill.createdAtMilliseconds, resolution) ?? 0,
minSize: 16,
text,
labelFontColor: 'white',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be follow up, but instead of white, can we use one of the text-color-x variables? (to have it better align with themeing)! also nit, but any chance we can also update the border color to better align?

Screenshot 2024-08-16 at 9 36 48 AM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok unfort theres no exposed way for us to change the border color until we upgrade the library but i can add that to the TODO!

will update the text color based on theme tho :D

@@ -0,0 +1,26 @@
import { ResolutionString } from 'public/tradingview/charting_library';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love the tests!
i have a preference for test file names matching the file they are testing, to prevent overlap, duplication, or gaps in testing.
i'd recommend creating a __test__ folder in the lib/tradingView/dydxfeed path, what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yesss for sure

Copy link
Contributor

@yogurtandjam yogurtandjam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work! have some q's and thoughts but nothing major and it looks like you've already gotten a stamp :)

@@ -140,3 +142,13 @@ export const getHydratedTradingData = <

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

export const getAverageFillPrice = (fills: SubaccountFills) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a quick test for this?

@@ -96,6 +116,41 @@ export const getDydxDatafeed = (
setTimeout(() => onSymbolResolvedCallback(symbolInfo), 0);
},

getMarks: async (
symbolInfo: LibrarySymbolInfo,
from: number,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we rename these fromSeconds and toSeconds or some other name that describes denomination

.toNumber();
}

export const getMarkForOrderFills = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[q] i noticed we tested getBarTime but i don't see a test for getMarkForOrderFills in the spec file. is this because the overhead to create all the necessary fixtures is too high?

if so, could we make a ticket + comment it in code as a TODO. If we find that there's a lot of instances where we're unable to test due to lack of features it may be worth actively investing time to remediate this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm so i personally dont think it's that important to add a unit test here since this is just a function that prepares all the little UI components - probably more interesting to add unit tests separately for each piece (like i did for getBarTime)

Copy link
Contributor

@yogurtandjam yogurtandjam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

@tinaszheng tinaszheng merged commit b1e5697 into main Aug 19, 2024
8 checks passed
@tinaszheng tinaszheng deleted the tina/ct-1055-buy-sell-marks branch August 19, 2024 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants