TradingView itself does not offer a public API for fetching candlestick data directly, but you can use the following approaches:
### 1. **Third-Party Data APIs**
Several APIs provide financial market data, similar to what TradingView displays:
#### Popular APIs:
1. **Alpha Vantage**
- Offers historical and intraday stock data.
- [Get API Key here](https://www.alphavantage.co/support/#api-key).
Example (Intraday Candlestick Data for
AAPL
):
python
import requests
API_KEY = "your_alpha_vantage_api_key"
symbol = "AAPL"
url = f"https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={symbol}&interval=1min&apikey={API_KEY}"
response = requests.get(url)
data = response.json()
# Accessing candlestick data
time_series = data.get("Time Series (1min)", {})
for time, ohlc in time_series.items():
print(f"Time: {time}, Open: {ohlc['1. open']}, High: {ohlc['2. high']}, Low: {ohlc['3. low']}, Close: {ohlc['4. close']}")
2. **Yahoo Finance API**
- Fetch OHLC data using a library like
yfinance
in Python.
python
import yfinance as yf
ticker = "AAPL"
data = yf.download(ticker, interval="1m", period="1d")
print(data.head())