Fetch data from tradingview using python
TradingView doesn’t provide a free API for direct data extraction, but you can still access data using various methods, including:
1. **Using `tvdatafeed`**: A Python library that allows you to fetch historical data from TradingView.
2. **Web Scraping with Selenium or BeautifulSoup** (for browser automation).
3. **Using TradingView’s unofficial WebSocket API** (advanced users only).
Here’s how to use these methods:
---
### Method 1: `tvdatafeed` Library
The `tvdatafeed` library is an unofficial Python library that can be used to fetch TradingView data for free, as long as you have a TradingView account.
1. **Install `tvdatafeed`**:
```bash
pip install tvdatafeed
```
2. **Use `tvdatafeed` to Fetch Data**:
```python
from tvDatafeed import TvDatafeed, Interval
# Log in to TradingView
tv = TvDatafeed(username='your_tradingview_username', password='your_tradingview_password')
# Fetch historical data for a specific symbol (e.g., Nifty50) and interval (e.g., 1 minute)
data = tv.get_hist(symbol='NSE:NIFTY', exchange='NSE', interval=Interval.in_1_minute, n_bars=500)
print(data)
```
Replace `'your_tradingview_username'` and `'your_tradingview_password'` with your TradingView account credentials. This will return a DataFrame with historical data that you can analyze further.
---
### Method 2: Web Scraping with Selenium
Selenium can automate a browser to log in and fetch data from TradingView’s website. However, this method is fragile and may violate TradingView’s terms of service.
1. **Install Selenium**:
```bash
pip install selenium
```
2. **Automate Login and Data Retrieval**:
- Configure the WebDriver (e.g., Chrome) to automate browser interactions.
- Log in, search for the symbol, and extract chart data.
Here’s a basic outline, but note that TradingView may block you if it detects scraping:
```python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
# Configure Selenium WebDriver
driver = webdriver.Chrome() # Ensure ChromeDriver is installed
# Go to TradingView and log in
driver.get('https://www.tradingview.com')
time.sleep(3)
# Additional code to navigate, search for the asset, and retrieve data
```
---
### Method 3: WebSocket API (Advanced)
TradingView uses WebSockets to communicate with its server. You can capture WebSocket messages using a tool like `websockets` in Python, though this requires some expertise in reverse-engineering.
1. **Install WebSocket Client**:
```bash
pip install websockets
```
2. **Capture Data** (Requires you to understand WebSocket payloads used by TradingView, which may be subject to change).
```python
import websockets
import asyncio
async def fetch_data():
uri = "wss://data.tradingview.com/socket.io/websocket"
async with websockets.connect(uri) as websocket:
await websocket.send('{"type": "ping"}') # Example request
response = await websocket.recv()
print(response)
asyncio.run(fetch_data())
```
This approach requires specific payloads that TradingView sends and receives, so it can be complex to maintain.
---
**Note:** These methods can access data, but it’s essential to respect TradingView’s terms of service, as scraping or reverse engineering WebSockets may violate them. Alternatively, many brokers (like Alpaca, Interactive Brokers) offer APIs with access to TradingView-like data.
No Comments have been Posted.