tradingview-scraper

repository·main·Indexed 19 days ago

https://github.com/mnwato/tradingview-scraper

A Python library for scraping data from TradingView.com. Key features include extracting trading ideas, news, technical indicators (e.g., RSI, Stoch.K), and fundamental financial metrics. It provides capabilities for real-time OHLCV and indicator data via WebSocket, market mover analysis (gainers, losers), and a flexible Screener for filtering financial instruments across global markets. Supports data export in CSV and JSON formats and provides tools for scraping calendar events like earnings and dividends.

Tokens
6.6K
Snippets
15
Records
26
Agent score
15%

What's inside tradingview-scraper

  1. Overview of TradingView Scraper features

    main

    The tradingview-scraper is a Python library designed to scrape various data points from TradingView.com. Key capabilities include:

    • Idea & News Scraping: Extract titles, authors, content, timestamps, and engagement metrics (comments, boosts) from Idea and News pages.
    • Indicator Extraction: Retrieve values for technical indicators like RSI, Stoch.K, etc.
    • Real-Time Data: Access OHLCV, Watchlist, and Indicator data via WebSocket.
    • Market Data: Scrape Market Movers (Gainers, Losers, Most Active), Screener data with custom filters, and Symbol Markets.
    • Symbol & Market Analysis: Comprehensive symbol overviews (financials, statistics, technicals) and market-wide overviews (top stocks by cap, volume, etc.).
    • Fundamental Data: Detailed financial metrics including Income Statements, Balance Sheets, Cash Flow, and various profitability/valuation ratios.
    • Community Data: Scrape Minds community discussions including user info and engagement.
    • Exporting: Data can be exported in CSV or JSON formats.
  2. Install the TradingView Scraper

    main

    Install the tradingview-scraper library using pip. If you need to upgrade to the latest version or ensure a clean installation, use the --upgrade and --no-cache flags.

    # Standard installation
    pip install tradingview-scraper
    
    # Upgrade to the latest version
    pip install --upgrade --no-cache tradingview-scraper
  3. Bypass Captcha using TRADINGVIEW_COOKIE

    main

    To avoid captcha challenges during scraping, you can provide your TradingView session cookie via an environment variable.

    1. Open the ideas page for a symbol (e.g., https://www.tradingview.com/symbols/BTCUSD/ideas/) in your browser.
    2. Open Developer Tools (F12) and navigate to the Network tab.
    3. Refresh the page and locate the GET request for the ideas URL.
    4. Copy the value of the Cookie header.
    5. Set the environment variable TRADINGVIEW_COOKIE in your .env file or system environment.
    TRADINGVIEW_COOKIE=your_cookie_here
  4. Filter by price range using 'in_range'

    main

    Use the in_range operation to find instruments within a specific numeric boundary. The right parameter must be a list containing [min, max] values.

    from tradingview_scraper.symbols.screener import Screener
    
    screener = Screener()
    
    # Screen stocks in price range $50-$200
    range_filters = [
        {'left': 'close', 'operation': 'in_range', 'right': [50, 200]}
    ]
    
    range_results = screener.screen(
        market='america',
        filters=range_filters,
        limit=30
    )
    
    print("Stocks in Range:", range_results['data'])
  5. Get symbol overview and technical data

    main
    The Symbol Overview feature provides comprehensive data for a specific symbol across 9 field categories (including profile, statistics, financials, performance, and technical data), covering over 70 data points. Specific helper methods are provided to access these data categories.
  6. Scrape Market Movers Data

    main

    The MarketMovers class allows you to scrape market trends like gainers, losers, and penny stocks.

    scrape() Method Parameters:

    • market (str): The market to scrape. Supported: 'stocks-usa', 'stocks-uk', 'stocks-india', 'stocks-australia', 'stocks-canada', 'crypto', 'forex', 'bonds', 'futures'.
    • category (str): The category of movers. Supported: 'gainers', 'losers', 'most-active', 'penny-stocks', 'pre-market-gainers', 'pre-market-losers', 'after-hours-gainers', 'after-hours-losers'.
    • limit (int): Number of results to return.
    • fields (list[str]): Specific fields to include in the output.

    Output Format: A dictionary containing status and data (a list of objects containing symbol, name, close, change, volume, etc.).

    from tradingview_scraper.symbols.market_movers import MarketMovers
    
    market_movers = MarketMovers()
    
    # Get top gainers from US stock market
    gainers = market_movers.scrape(
        market='stocks-usa',
        category='gainers',
        limit=20
    )
    print("Top Gainers:", gainers['data'])
  7. Scrape Technical Indicators Status

    main

    Use the Indicators class from tradingview_scraper.symbols.technicals to retrieve the current values of specific technical indicators or all available indicators for a symbol.

    Initialization Parameters:

    • export_result (bool): If True, saves results to the /export directory.
    • export_type (str): 'json' or 'csv'.

    scrape() Method Parameters:

    • exchange (str): The exchange name (e.g., 'BINANCE').
    • symbol (str): The trading symbol (e.g., 'BTCUSD').
    • timeframe (str): The candle timeframe (e.g., '1d', '4h').
    • indicators (list[str]): A list of specific indicator names (e.g., ["RSI", "Stoch.K"]).
    • allIndicators (bool): If True, retrieves all available indicators for the symbol instead of the specified list.

    Output Format: A dictionary containing status and data (a mapping of indicator names to their values).

    from tradingview_scraper.symbols.technicals import Indicators
    
    indicators_scraper = Indicators(export_result=True, export_type='json')
    indicators = indicators_scraper.scrape(
        exchange="BINANCE",
        symbol="BTCUSD",
        timeframe="1d",
        indicators=["RSI", "Stoch.K"]
    )
    print("Indicators:", indicators)
  8. Access fundamental financial data

    main

    The FundamentalGraphs class provides deep financial data for stocks. Note: This class does not support crypto, forex, or other non-stock symbols.

    Available Data Methods:

    • get_fundamentals(symbol, fields=None): Comprehensive fundamental data.
    • get_income_statement(symbol): Revenue, Gross Profit, Operating Income, Net Income, EPS.
    • get_balance_sheet(symbol): Total Assets, Cash, Total Debt, Stockholders Equity, Book Value.
    • get_cash_flow(symbol): Operating, Investing, and Financing activities, Free Cash Flow.
    • get_profitability(symbol): ROE, ROA, ROI.
    • get_margins(symbol): Gross, Operating, Net, and EBITDA margins.
    • get_liquidity(symbol): Current and Quick ratios.
    • get_leverage(symbol): Debt-to-Equity and Debt-to-Assets.
    • get_valuation(symbol): Market Cap, Enterprise Value, P/E, P/B, P/S, P/FCF.
    • get_dividends(symbol): Yield, Dividends per Share, Payout Ratio.

    Advanced Usage:

    • compare_fundamentals(symbols, fields): Compares specific metrics across a list of symbols.
    • export_result=True, export_type='json': Enables automatic JSON export upon initialization.
    from tradingview_scraper.symbols.fundamental_graphs import FundamentalGraphs
    
    fundamentals = FundamentalGraphs()
    
    # Get income statement
    income_data = fundamentals.get_income_statement(symbol='NASDAQ:AAPL')
    
    # Compare metrics across multiple symbols
    comparison = fundamentals.compare_fundamentals(
        symbols=['NASDAQ:AAPL', 'NASDAQ:MSFT', 'NASDAQ:GOOGL'],
        fields=['total_revenue', 'net_income', 'EBITDA']
    )
    
    # Export to JSON
    fundamentals_exporter = FundamentalGraphs(export_result=True, export_type='json')
    fundamentals_exporter.get_fundamentals(symbol='NASDAQ:AAPL')
  9. Use the Screener to filter financial instruments

    main

    The Screener class allows you to filter and screen financial instruments across various markets using custom criteria. You can perform basic screening with a market and limit, or apply complex filters using specific operations on fields like close, volume, or market_cap_calc.

    from tradingview_scraper.symbols.screener import Screener
    
    screener = Screener()
    
    # Basic screening of US stocks
    results = screener.screen(market='america', limit=50)
    print("Results:", results['data'])
  10. Get community discussions from Minds

    main

    Use the Minds class to scrape community discussions, questions, and trading ideas associated with a symbol.

    Sort Options:

    • recent: Most recent discussions (default)
    • popular: Sorted by engagement (likes + comments)
    • trending: Currently trending discussions

    Key Features:

    • get_all_minds(symbol, sort, max_results): Retrieves discussions across multiple pages.
    • export_result=True, export_type='json': When initializing Minds, you can enable automatic export of results to a JSON file.
    • The response includes symbol_info containing the short name, exchange, and instrument name.
    from tradingview_scraper.symbols.minds import Minds
    
    # Get popular discussions
    minds = Minds()
    popular = minds.get_minds(symbol='NASDAQ:TSLA', sort='popular', limit=15)
    
    # Get all discussions with pagination
    all_discussions = minds.get_all_minds(symbol='NASDAQ:AAPL', sort='popular', max_results=100)
    
    # Enable export to JSON
    minds_exporter = Minds(export_result=True, export_type='json')
    minds_exporter.get_minds(symbol='NASDAQ:AAPL', sort='popular', limit=100)
  11. Stream OHLCV and indicators simultaneously with Streamer

    main
    The Streamer class integrates real-time data and historical exporters, allowing you to retrieve OHLCV (Open, High, Low, Close, Volume) and indicator data at the same time. When exporting streamer data, you can specify a timeframe parameter.
  12. Configure Screener filters and sorting

    main

    To perform advanced screening, pass a list of filter dictionaries to the screen method. Each filter dictionary requires:

    • left: The field name (e.g., 'close', 'volume').
    • operation: The comparison logic (e.g., 'greater', 'in_range').
    • right: The value or list of values to compare against.

    You can also use sort_by and sort_order to organize your results.

    from tradingview_scraper.symbols.screener import Screener
    
    screener = Screener()
    
    # Screen stocks with price > $100 and volume > 1M
    filters = [
        {'left': 'close', 'operation': 'greater', 'right': 100},
        {'left': 'volume', 'operation': 'greater', 'right': 1000000}
    ]
    
    results = screener.screen(
        market='america',
        filters=filters,
        sort_by='volume',
        sort_order='desc',
        limit=20
    )
    
    print("Filtered Results:", results['data'])