OpenStock is an open-source alternative to expensive market platforms. Track real-time prices, set personalized alerts, and explore detailed company insights — built openly, for everyone, forever free.
fix(stocks): spell - and & tickers the way TradingView does
ravixalgorithm466a5734b714
diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.tsindex 86d0c28..7c1cddb 100644--- a/__tests__/utils.test.ts+++ b/__tests__/utils.test.ts@@ -57,6 +57,12 @@ describe('formatSymbolForTradingView', () => {expect(formatSymbolForTradingView('RELIANCE.BO')).toBe('BSE:RELIANCE');});+ it('spells Indian tickers with - or & the way TradingView does (#113)', () => {+ expect(formatSymbolForTradingView('BAJAJ-AUTO.NS')).toBe('BSE:BAJAJ_AUTO');+ expect(formatSymbolForTradingView('M&M.NS')).toBe('BSE:M_M');+ expect(formatSymbolForTradingView('M&MFIN.BO')).toBe('BSE:M_MFIN');+ });+it('maps Australia (.AX) to ASX prefix', () => {expect(formatSymbolForTradingView('BHP.AX')).toBe('ASX:BHP');});@@ -68,6 +74,7 @@ describe('formatSymbolForTradingView', () => {// European exchangesit('maps London (.L) to LSE prefix', () => {expect(formatSymbolForTradingView('SHEL.L')).toBe('LSE:SHEL');+ expect(formatSymbolForTradingView('BT-A.L')).toBe('LSE:BT.A');});it('maps Germany/Xetra (.DE) to XETR prefix', () => {@@ -95,12 +102,14 @@ describe('formatSymbolForTradingView', () => {});it('maps Stockholm (.ST) to OMXSTO prefix', () => {- expect(formatSymbolForTradingView('ERIC-B.ST')).toBe('OMXSTO:ERIC-B');+ expect(formatSymbolForTradingView('ERIC-B.ST')).toBe('OMXSTO:ERIC_B');+ expect(formatSymbolForTradingView('NOVO-B.CO')).toBe('OMXCOP:NOVO_B');});// Americasit('maps Toronto (.TO) to TSX prefix', () => {expect(formatSymbolForTradingView('RY.TO')).toBe('TSX:RY');+ expect(formatSymbolForTradingView('RCI-B.TO')).toBe('TSX:RCI-B');});it('maps Brazil (.SA) to BMFBOVESPA prefix', () => {diff --git a/lib/utils.ts b/lib/utils.tsindex 9d383fe..9983550 100644--- a/lib/utils.ts+++ b/lib/utils.ts@@ -225,6 +225,11 @@ export const isChartEmbeddable = (tvSymbol: string) => !CHART_BLOCKED_EXCHANGES.export const tradingViewSymbolUrl = (tvSymbol: string) => `https://www.tradingview.com/symbols/${tvSymbol.replace(':', '-')}/`;+// Finnhub writes share classes and symbols with - or &; on these exchanges TradingView only knows its own spelling+// (the Finnhub one shows "This symbol doesn't exist"): BAJAJ-AUTO.NS → BSE:BAJAJ_AUTO, M&M.NS → BSE:M_M,+// ERIC-B.ST → OMXSTO:ERIC_B, NOVO-B.CO → OMXCOP:NOVO_B, BT-A.L → LSE:BT.A. TSX accepts RCI-B as is.+const TICKER_SEPARATOR: Record<string, string> = { BSE: '_', OMXSTO: '_', OMXCOP: '_', LSE: '.' };+// Listed outside the US (has a known exchange suffix like .L, .T, .NS). Class shares such as BRK.B are not.export function isInternationalSymbol(symbol: string): boolean {const upper = symbol.toUpperCase();@@ -243,8 +248,10 @@ export function formatSymbolForTradingView(symbol: string): string {for (const suffix of suffixes) {if (upperSymbol.endsWith(suffix.toUpperCase())) {const exchange = FINNHUB_TO_TRADINGVIEW_EXCHANGE[suffix];+ let ticker = upperSymbol.slice(0, -suffix.length);// Finnhub pads Hong Kong codes (0700.HK); TradingView doesn't (HKEX:700)- const ticker = exchange === 'HKEX' ? upperSymbol.slice(0, -suffix.length).replace(/^0+(?=\d)/, '') : upperSymbol.slice(0, -suffix.length);+ if (exchange === 'HKEX') ticker = ticker.replace(/^0+(?=\d)/, '');+ if (TICKER_SEPARATOR[exchange]) ticker = ticker.replace(/[-&]/g, TICKER_SEPARATOR[exchange]);return `${exchange}:${ticker}`;}}