tradingview-scraper

repository·master·Indexed 43 days ago

https://github.com/imxeno/tradingview-scraper

A 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.

Tokens
1.9K
Snippets
8
Records
10
Agent score
55%

What's inside tradingview-scraper

  1. Initialize the TradingViewAPI client

    master

    To 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();
  2. Manage real-time data streams with TradingViewWebSocket

    master

    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.

    Lifecycle

    1. connect(): Establishes the WebSocket connection and waits for the session to be ready.
    2. registerSymbol(symbol): Subscribes to real-time updates for a specific symbol.
    3. unregisterSymbol(symbol): Stops receiving updates for a specific symbol.
    4. disconnect(): Closes the connection and clears all active subscriptions.

    Events

    • data: Emitted when new market data arrives.
      • Arguments: (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();
  3. Use the TradingViewAPI class

    master

    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';
  4. Manage TickerSubscription lifecycle and destruction

    master

    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:

    1. Its internal due timer has expired (5 seconds after the last refresh).
    2. There are no active listeners for the update event (listenerCount('update') === 0).
  5. Get a ticker subscription with getTicker()

    master

    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);
    });
  6. Ensure a ticker is registered with ensureRegistered()

    master

    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);
  7. Handle ticker updates with TickerSubscription

    master

    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();
  8. Available quote fields for data subscription

    master

    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'
  9. TradingViewWebSocket methods reference

    master

    The following public methods are available on the TradingViewWebSocket class:

    MethodSignatureDescription
    connectasync connect()Initializes the WebSocket connection to wss://data.tradingview.com/socket.io/websocket and waits for the session to be ready.
    disconnectdisconnect()Closes the WebSocket connection and clears all internal subscriptions and sessions.
    registerSymbolasync registerSymbol(symbol: string)Adds a symbol to the active quote session to start receiving real-time data.
    unregisterSymbolasync unregisterSymbol(symbol: string)Removes a symbol from the active quote session.