The Problem
Getting real-time market data from TradingView typically requires either a paid API subscription or scraping HTML pages. TradingView's internal WebSocket protocol is undocumented and changes frequently, making it impractical to reverse-engineer for each project. This library wraps that protocol so developers can pull live prices, indicator values, and chart data without maintaining their own TradingView integration.
What This Does
TradingView-API is a JavaScript/TypeScript client for TradingView's private WebSocket API. It handles authentication (including invite-only indicators), real-time chart subscriptions, technical analysis scans, replay mode, and quote sessions. The src/ directory contains the core: client.js manages the connection lifecycle, src/chart/session.js handles chart data, src/quote/market.js and src/quote/session.js manage quote streams, and src/protocol.js parses/format WebSocket packets. The examples/ folder has 15 runnable snippets covering everything from simple charts to Pine permission management.
This is a fork of Mathieu2301/TradingView-API (4,290 stars upstream), with no additional commits visible in the analysis. The README still points to the upstream author's badges and support links.
How It Is Wired
Execution starts at main.js (24 modules import it, making it the hub with a 0.17 instability score). The entry point exports the Client class and helper functions like getIndicator and getIndicData. From there, control flows into src/client.js, which owns the WebSocket connection and handles onConnected, onLogged, and onError events. src/utils.js provides genAuthCookies — called from 8 places, the widest blast radius in the codebase — and genSessionID, both essential for establishing a session.
The internal call graph shows 65 resolved edges. The critical path is: main.js → src/client.js → src/protocol.js (formatWSPacket/parseWSPacket) → WebSocket. Outbound network calls happen in src/miscRequests.js (16 functions, including fetchScanData and searchMarket) and src/classes/PinePermManager.js. Tests exercise the API heavily: tests/ calls onError 8 times and setMarket 6 times, confirming the test suite drives real behavior.
The module graph has no circular dependencies, which is good. The hub risk is concentrated in main.js — any change there ripples through 24 dependents.
How To Use It
git clone https://github.com/moses-y/TradingView-API
cd TradingView-API
npm install
Configuration lives in .env.sample (copy to .env). Authentication requires TradingView credentials — see examples/UserLogin.js for the login flow. Run any example directly:
node examples/SimpleChart.js
The package's public API is the Client class from main.js. There's no CLI — you write a script that imports the library and subscribes to symbols.
Real-World Use
A trading bot that watches a symbol and alerts on Pine indicator crossovers:
const { Client } = require('./main.js');
const client = new Client();
client.onReady().then(() => {
const chart = client.createChartSession();
chart.setMarket('BINANCE:BTCUSDT', { type: 'crypto' });
chart.setSeries('close');
chart.onUpdate(data => {
if (data.close > 50000) console.log('BTC crossed 50k');
});
});
Code Health & Issues
Static analysis found 2 findings (1 high, 1 medium):
- High — Duplicated code blocks: 36 repeated 6-line blocks across 16 files, concentrated in
examples/. Extract shared helpers. - Medium — Hub module:
main.jshas 24 dependents; keep it stable and move volatile logic out.
SDLC observations:
- High — No LICENSE file at root; redistribution rights are undefined.
- Medium — CI workflow
.github/workflows/tests.ymllacks least-privilegepermissions,persist-credentials: false,timeout-minutes, and dependency scanning. - Medium — No Dependabot/Renovate configured despite 1,322 dependencies in the lockfile.
The Bottom Line
A functional TradingView client that solves a genuinely hard problem — the WebSocket protocol is undocumented and fragile. The code is reasonably organized with good test coverage (12 test files), but it's an unmaintained fork with no license and duplicated example code. Use it if you need TradingView data without a paid API; expect to maintain the WebSocket layer yourself as TradingView changes their protocol.