Install tradingview-scraper
masterYou can install the tradingview-scraper package using npm, yarn, or pnpm.
# npm
npm install tradingview-scraper
# yarn
yarn add tradingview-scraper
# pnpm
pnpm add tradingview-scraperrepository·master·Indexed 43 days ago
https://github.com/imxeno/tradingview-scraperA Node.js scraper for extracting real-time data from TradingView's socket.io interface. Version 1.0.4 features TypeScript typings, HTTP Origin header spoofing, and the TradingViewAPI class for managing TickerSubscription instances and WebSocket connections to retrieve price data and instrument metadata.
You can install the tradingview-scraper package using npm, yarn, or pnpm.
# npm
npm install tradingview-scraper
# yarn
yarn add tradingview-scraper
# pnpm
pnpm add tradingview-scraperTo use the TradingViewAPI, you must first call the setup() method. This method establishes the underlying WebSocket connection and prepares the client to handle incoming data streams. To stop all connections and clean up resources, call cleanup().
import { TradingViewAPI } from './TradingViewAPI';
const api = new TradingViewAPI();
await api.setup();
// ... use the API ...
await api.cleanup();The TradingViewWebSocket class provides a high-level interface for connecting to TradingView's WebSocket server and subscribing to real-time symbol data. It extends EventEmitter and emits a data event whenever new ticker information is received.
connect(): Establishes the WebSocket connection and waits for the session to be ready.registerSymbol(symbol): Subscribes to real-time updates for a specific symbol.unregisterSymbol(symbol): Stops receiving updates for a specific symbol.disconnect(): Closes the connection and clears all active subscriptions.data: Emitted when new market data arrives.(simpleOrProName: string, status: string, data: any)import { TradingViewWebSocket } from './TradingViewWebSocket';
const tvWs = new TradingViewWebSocket();
tvWs.on('data', (name, status, data) => {
console.log(`Symbol: ${name}, Status: ${status}, Data:`, data);
});
await tvWs.connect();
await tvWs.registerSymbol('BINANCE:BTCUSDT');
// Later...
// await tvWs.unregisterSymbol('BINANCE:BTCUSDT');
// tvWs.disconnect();The TradingViewAPI class is the primary entrypoint for interacting with the TradingView scraper. You can import it using named imports or as a default import to initialize the API and begin scraping data.
import { TradingViewAPI } from 'tradingview-scraper';
// Or using default import
import TradingViewAPI from 'tradingview-scraper';import { TradingViewAPI } from 'tradingview-scraper';A TickerSubscription instance can be cleaned up when it is no longer needed. The property canBeDestroyed (getter) indicates if the subscription is eligible for destruction.
A subscription is considered eligible for destruction if:
due timer has expired (5 seconds after the last refresh).update event (listenerCount('update') === 0).Use getTicker(simpleOrProName: string) to obtain a TickerSubscription instance for a specific symbol. If a subscription for that symbol already exists, it returns the existing one; otherwise, it creates a new one, fetches the initial data, and returns it. The simpleOrProName is the identifier used by TradingView (e.g., a symbol name).
const ticker = await api.getTicker('BINANCE:BTCUSDT');
ticker.on('update', (data) => {
console.log('New data:', data);
});The ensureRegistered(ticker: TickerSubscription) method ensures that a specific TickerSubscription instance is actively being tracked by the WebSocket connection. If the ticker is not already registered, the method registers the symbol and waits up to 3000ms for the first data update to confirm successful registration. If the update does not arrive within 3 seconds, the promise rejects with 'Timed out.'.
const ticker = await api.getTicker('BINANCE:BTCUSDT');
await api.ensureRegistered(ticker);The TickerSubscription class is an EventEmitter used to manage real-time data for a specific ticker. You can listen for the update event, which is emitted whenever new TickerData is received or patched.
Key methods:
on('update', (tickerData: TickerData) => void): Subscribe to data updates.fetch(): Returns a Promise<TickerData> containing the current ticker state and ensures the subscription is registered with the API.updateData(tickerDataPatch: TickerData): Manually patches the current ticker data and emits the update event.// Assuming 'api' is an instance of TradingViewAPI
const subscription = new TickerSubscription(api, 'BTCUSDT');
subscription.on('update', (tickerData) => {
console.log('New ticker data:', tickerData);
});
// Or fetch the current state
const data = await subscription.fetch();When subscribing to data or requesting specific quote information, you can use the following field identifiers. These fields cover price data, instrument metadata, fundamental data, and session information.
// Available quote fields:
'ch',
'chp',
'current_session',
'description',
'local_description',
'language',
'exchange',
'fractional',
'is_tradable',
'lp',
'lp_time',
'minmov',
'minmove2',
'original_name',
'pricescale',
'pro_name',
'short_name',
'type',
'update_mode',
'volume',
'currency_code',
'ask',
'bid',
'fundamentals',
'high_price',
'is_tradable',
'low_price',
'open_price',
'prev_close_price',
'rch',
'rchp',
'rtc',
'rtc_time',
'status',
'basic_eps_net_income',
'beta_1_year',
'earnings_per_share_basic_ttm',
'industry',
'market_cap_basic',
'price_earnings_ttm',
'sector',
'volume',
'dividends_yield',
'timezone'The following public methods are available on the TradingViewWebSocket class:
| Method | Signature | Description |
|---|---|---|
connect | async connect() | Initializes the WebSocket connection to wss://data.tradingview.com/socket.io/websocket and waits for the session to be ready. |
disconnect | disconnect() | Closes the WebSocket connection and clears all internal subscriptions and sessions. |
registerSymbol | async registerSymbol(symbol: string) | Adds a symbol to the active quote session to start receiving real-time data. |
unregisterSymbol | async unregisterSymbol(symbol: string) | Removes a symbol from the active quote session. |