TradingView API

repository·main·Indexed 26 days ago

https://github.com/mathieu2301/tradingview-api

A library for fetching real-time market prices, indicator values, and technical analysis data from TradingView. It supports built-in and custom Pine Script indicators, automated backtesting, and real-time data retrieval. Key features include access to invite-only indicators, chart drawing retrieval, and a Client class for WebSocket interaction. Version 3.5.2.

Tokens
4.6K
Snippets
2
Records
39
Agent score
88%

What's inside @mathieuc/tradingview

  1. Overview of TradingView API features

    main

    The TradingView API allows you to retrieve real-time market prices and indicator values. Key features include:

    • Access to premium features and invite-only indicators.
    • Support for unlimited simultaneous indicators.
    • Real-time data retrieval.
    • Access to TradingView's technical analysis.
    • Ability to retrieve drawings made on charts.
    • Replay mode and Fake Replay mode (available for free plans).
    • Ability to get values from a specific date range.
    • Automated backtesting of multiple strategies and settings.
  2. Explore TradingView API examples

    main
    Detailed usage examples and code snippets are provided in the ./examples directory of the repository. Use these to understand how to implement specific functionalities like trading bots, Discord alerts, or machine learning-based indicators.
  3. Get full indicator details with getIndicator()

    main

    Retrieve detailed information about a specific Pine Script indicator, including its inputs and plots.

    Parameters:

    • id (string): Indicator ID (e.g., PUB;XXXXXXXXXXXXXXXXXXXXX).
    • version (string, optional): Specific version to fetch. Defaults to 'last'.
    • session (string, optional): User sessionid cookie for private indicators.
    • signature (string, optional): User sessionid_sign cookie for private indicators.

    Returns:

    • Promise<PineIndicator>: An object containing pineId, pineVersion, description, shortDescription, inputs (configuration options), plots (output data), and the script (source template).
  4. Create Quote and Chart sessions

    main

    The Client class exposes a Session namespace to generate specialized sessions for different data types:

    • Client.Session.Quote(clientBridge): Generates a Quote session.
    • Client.Session.Chart(clientBridge): Generates a Chart session.

    Note: These are typically used internally by the library's session generators, but they are accessible via the Session property on the Client instance.

  5. Get technical analysis with getTA()

    main

    Retrieve technical analysis advice for a specific market ID. The results include recommendations (Other, All, MA) across multiple timeframes.

    Parameters:

    • id (string): The full market ID (e.g., COINBASE:BTCEUR).

    Returns:

    • Promise<Periods>: An object where keys are timeframes (1, 5, 15, 60, 240, 1D, 1W, 1M) and values are objects containing advice for Other, All, and MA.
  6. Use the QuoteMarket class for real-time market data

    main

    The QuoteMarket class is used to subscribe to real-time market quotes for a specific symbol and session. It is initialized by passing a quoteSession (an instance of QuoteSessionBridge).

    Constructor Parameters:

    • symbol (string): The market symbol (e.g., 'BTCEUR' or 'KRAKEN:BTCEUR').
    • session (string, optional): The market session, defaults to 'regular'. Options include 'regular' or 'extended'.

    Event Listeners:

    • onLoaded(cb): Triggered when the quote market has finished loading.
    • onData(cb): Triggered when new quote data is received. The callback receives the merged data object.
    • onEvent(cb): Triggered for all market events. Receives the event type ('loaded', 'data', 'error') and the associated data.
    • onError(cb): Triggered when a market error occurs.

    To stop receiving updates and free up resources, call the close() method.

  7. Configure BuiltInIndicator options using setOption()

    main

    Use the setOption(key, value, FORCE) method to modify indicator settings.

    • key: The name of the option to change (e.g., rows, volume, vaVolume).
    • value: The new value for the option.
    • FORCE (optional): A boolean. If true, it bypasses type checking and verification of whether the key is valid for the specific indicator type.

    By default, setOption performs strict type checking against the indicator's expected value type and ensures the key is valid for the current indicator type. If validation fails, it throws an error.

  8. Update indicator inputs with setOption()

    main

    Use setOption(key, value) to change the value of an indicator input. The key can be the internal ID (e.g., in_{ID}), the inline name, or the internalID.

    Validation Rules:

    • Type Safety: The value must match the input's type. Supported type mappings:
      • bool -> boolean
      • integer -> number
      • float -> number
      • text -> string
    • Options: If the input has an options array, the value must be included in that array.

    Errors:

    • Throws an error if the key is not found.
    • Throws an error if the type is incorrect.
    • Throws an error if the value is not in the allowed options list.
  9. Initialize the Client class

    main

    The Client class is the main entry point for interacting with TradingView. You can initialize it with an optional ClientOptions object to provide authentication tokens, custom headers, or specify the server type.

    ClientOptions properties:

    • token (string, optional): User auth token (from the sessionid cookie).
    • signature (string, optional): User auth token signature (from the sessionid_sign cookie).
    • DEBUG (boolean, optional): Enable debug mode (sets global.TW_DEBUG).
    • server (string, optional): Server type. Options: 'data', 'prodata', or 'widgetdata'. Defaults to 'data'.
    • location (string, optional): Auth page location (e.g., https://fr.tradingview.com/ for France).
    • headers (Object<string, string>, optional): Custom WebSocket headers.
  10. Access Client state and close connection

    main

    Use the following methods to check the current state of the client or to terminate the connection:

    • get isLogged(): Returns true if the client is logged in.
    • get isOpen(): Returns true if the WebSocket connection is currently open.
    • end(): Returns a Promise that resolves when the WebSocket connection is closed.