You can fetch OHLC (Open, High, Low, Close) data from TradingView using different methods, depending on your requirements.
Method 1: Using TradingView's Charting Library API (WebSocket)
TradingView provides a WebSocket-based API to retrieve live market data, but it requires reverse engineering since they don't provide official access for free users.
Open TradingView in your browser.
Open the Developer Console (F12 > Network > WS).
Find the WebSocket connection and analyze the data structure.
Use Python with websocket-client to connect and retrieve OHLC data.
Method 2: Using TradingView unofficial API (Pine Script)
If you want historical data:
Use TradingView's Pine Script to create a custom indicator that logs OHLC values.
Export the data manually or send it to a server.
Method 3: Using Third-Party APIs
Some third-party APIs offer TradingView-like data:
tradingview-ta Python Library
Install the library:
bash
Copy
Edit
pip install tradingview-ta
Fetch OHLC:
python
Copy
Edit
from tradingview_ta import TA_Handler, Interval
ohlc = TA_Handler(
symbol="CRUDEOIL",
exchange="NSE",
screener="india",
interval=Interval.INTERVAL_15_MINUTES
)
print(ohlc.get_analysis().indicators)
Investing.com or Yahoo Finance APIs
If TradingView data is unavailable, you can fetch similar OHLC data using:
python
Copy
Edit
import yfinance as yf
data = yf.download("CL=F", interval="15m", period="1d")
print(data)