get data from tradingview api

Published: 2026-06-16 03:48:03

Retrieving Data from TradingView API: A Comprehensive Guide

TradingView is a popular platform for traders and investors, known for its user-friendly interface and rich set of features that allow users to analyze the market. One of the unique aspects of TradingView is its Application Programming Interface (API), which offers access to real-time data and historical analysis tools. In this article, we will explore how to retrieve data from TradingView API using Python, a popular choice for data manipulation and analysis due to its simplicity and extensive libraries like Pandas and Requests.

Understanding the TradingView API

TradingView provides two types of APIs:

1. Trade API: For trading purposes directly through TradingView's interface.

2. Public API: To access historical market data, live data from public charts, and more in a standardized JSON format.

The focus of this article will be on the Public API, which is crucial for building custom analysis tools or educational projects without direct interaction with financial markets.

Getting Started: Setting Up Your Environment

Before diving into data retrieval, ensure you have Python installed along with the following libraries:

`requests` for sending HTTP requests and handling JSON responses.

`pandas` for data manipulation and analysis.

You can install these using pip:

```shell

pip install requests pandas

```

Authentication: Getting an API Key

To use TradingView's Public API, you need to authenticate with your TradingView account by generating an API key. Go to `https://www.tradingview.com/api/settings/` and click "Create new API key". Keep this key secure as it grants access to public data, including real-time updates and historical charts for all symbols you request.

Retrieving Historical Data

To fetch historical data from TradingView API, use the following format in your HTTP GET request:

```shell

https://api.tradingview.com/public/stocks/history/{symbol}/?period=1m&size=5000&interval=1d&apikey={YOUR_API_KEY}

```

Replace `{symbol}` with the stock symbol you're interested in, and `{YOUR_API_KEY}` with your actual API key. Here's how to implement this in Python:

```python

import requests

import pandas as pd

def get_historical_data(symbol, api_key):

url = f"https://api.tradingview.com/public/stocks/history/{symbol}/?period=1m&size=5000&interval=1d&apikey={api_key}"

response = requests.get(url)

if response.status_code == 200:

data = response.json()['chart']

df = pd.DataFrame(data['timeseries'])

df["Date"] = pd.to_datetime(df["time"], unit="s")

df = df.drop(columns=["time"])

df = df.rename(columns={"o": "Open", "h": "High", "l": "Low", "c": "Close"})

return df

else:

print(f"Error {response.status_code}: {response.reason}")

```

This function retrieves 5000 daily data points for a given symbol (up to one year of data depending on the trading activity of the stock) and returns it as a pandas DataFrame with columns named "Date", "Open", "High", "Low", and "Close".

Live Feeds: Retrieving Real-Time Updates

While TradingView's Public API does not support real-time data feeds, it offers live updates from public charts accessible through a similar format:

```shell

https://api.tradingview.com/public/chart/getcandles/{symbol}@{chart_id}/?period=1m&apikey={YOUR_API_KEY}

```

Replace `{symbol}` with the stock symbol and `{chart_id}` with the unique identifier for a specific public chart you are interested in. Note that this requires access to a live TradingView public chart, typically found on social media platforms or community discussions where users share their charts publicly.

Conclusion: Harnessing Power of Data

TradingView's API provides a powerful toolkit for data-driven analysis and research without direct financial risk exposure. By leveraging Python and its rich ecosystem, one can easily retrieve historical market data for deep learning or model development. However, it's crucial to respect the terms of use outlined by TradingView regarding the usage of this data.

Remember, while API keys grant access to valuable resources, they also come with responsibility - misuse of these tools not only risks your account but also the reputation and integrity of the platform as a whole. Always ensure that your analysis complies with ethical standards and regulatory requirements in your jurisdiction.

Recommended for You

🔥 Recommended Platforms