TradingView Snapshot with REST API (Part1)

CHART-IMG
7 min readMar 2, 2023

--

In this tutorial, I will show you how to use CHART-IMG REST API Version 2 to capture TradingView Advanced Chart Snapshots. I will provide examples using CURL to make the process easy to understand and follow.

If you want to read the API documentation, visit doc.chart-img.com.

Version 2

The new release provides a broader selection of fully customizable indicators and drawing features. Additionally, you can access private layouts for specific indicators, including community and personal scripts.

Authentication

Although the API is free for anyone to use, you must include your personal access key in every request header. To obtain your key, log in to chart-img.com using your Google account and generate it.

Let’s try a simple POST request using CURL to see if your API key works. Don’t forget to replace {YOUR_API_KEY} with your actual key value in the request header.

## for Linux based
curl -X POST https://api.chart-img.com/v2/tradingview/advanced-chart \
-H "x-api-key: {YOUR_API_KEY}" \
-o chart-img-01.png

## for Windows, you have to merge all lines into one by removing the backslashes
curl -X POST https://api.chart-img.com/v2/tradingview/advanced-chart -H "x-api-key: {YOUR_API_KEY}" -o chart-img-01.png

This should download an image with the file name chart-img-01.png.

CHART-IMG-01.PNG

If you have tried the previous version, you may have noticed that the default theme color changed to light, and the volume indicator no longer exists by default.

Request Payload

You can customize the chart by specifying multiple parameters in the payload, such as symbol, interval, width, height, style, theme, scale, session, timezone, format, range, override, studies, and drawings.

Let’s include some payload to customize the chart. The request body content-type must be in a valid JSON format.

{
"symbol": "BINANCE:ETHUSDT",
"interval": "4h",
"studies": [
{
"name": "Volume",
"forceOverlay": true
},
{
"name": "MACD"
}
]
}

You do not need to include the same values as the default value. For more detailed information on default values, refer to the documentation.

CHART-IMG-02.PNG
## for Linux based
curl -X POST https://api.chart-img.com/v2/tradingview/advanced-chart \
-H "x-api-key: {YOUR_API_KEY}" \
-H "content-type: application/json" \
-d "{\"symbol\":\"BINANCE:ETHUSDT\",\"interval\":\"4h\",\"studies\":[{\"name\":\"Volume\",\"forceOverlay\":true},{\"name\":\"MACD\"}]}" \
-o chart-img-02.png

## for Windows, you have to merge all lines into one by removing the backslashes
curl -X POST https://api.chart-img.com/v2/tradingview/advanced-chart -H "x-api-key: {YOUR_API_KEY}" -H "content-type: application/json" -d "{\"symbol\":\"BINANCE:ETHUSDT\",\"interval\":\"4h\",\"studies\":[{\"name\":\"Volume\",\"forceOverlay\":true},{\"name\":\"MACD\"}]}" -o chart-img-02.png

You can use freeformatter.com to escape JSON when you need to include it between double quotes.

Override Style

You can change the default style of the chart and indicators using the override options.

Let’s make a unique style chart with a symbol watermark, different colors, and extended session hours from day to date.

{
"range": "DTD",
"session": "extended",
"symbol": "NASDAQ:AAPL",
"timezone": "America/New_York",
"override": {
"showSymbolWatermark": true,
"style": {
"candleStyle.upColor": "rgb(0,255,0)",
"candleStyle.downColor": "rgb(255,0,0)"
}
},
"studies": [
{
"name": "Volume",
"forceOverlay": false,
"override": {
"Volume.color.0": "rgba(255,0,0,0.5)",
"Volume.color.1": "rgba(0,255,0,0.5)"
}
}
]
}

The volume will show as an indicator by not forcing overlay on the chart. For colors, you can use RGB or RGBA to change the opacity. The to-date range will automatically set an appropriate interval to fit the chart, but if you require a specific range and interval, you can use the object range option. Currently, the from max range should be within approximately 1,000 interval bars from now. For more detailed information about the range, refer to the documentation.

CHART-IMG-03.PNG
## for Linux based
curl -X POST https://api.chart-img.com/v2/tradingview/advanced-chart \
-H "x-api-key: {YOUR_API_KEY}" \
-H "content-type: application/json" \
-d "{\"range\":\"DTD\",\"session\":\"extended\",\"symbol\":\"NASDAQ:AAPL\",\"timezone\":\"America\/New_York\",\"override\":{\"showSymbolWatermark\":true,\"style\":{\"candleStyle.upColor\":\"rgb(0,255,0)\",\"candleStyle.downColor\":\"rgb(255,0,0)\"}},\"studies\":[{\"name\":\"Volume\",\"forceOverlay\":false,\"override\":{\"Volume.color.0\":\"rgba(255,0,0,0.5)\",\"Volume.color.1\":\"rgba(0,255,0,0.5)\"}}]}" \
-o chart-img-03.png

## for Windows, you have to merge all lines into one by removing the backslashes
curl -X POST https://api.chart-img.com/v2/tradingview/advanced-chart -H "x-api-key: {YOUR_API_KEY}" -H "content-type: application/json" -d "{\"range\":\"DTD\",\"session\":\"extended\",\"symbol\":\"NASDAQ:AAPL\",\"timezone\":\"America\/New_York\",\"override\":{\"showSymbolWatermark\":true,\"style\":{\"candleStyle.upColor\":\"rgb(0,255,0)\",\"candleStyle.downColor\":\"rgb(255,0,0)\"}},\"studies\":[{\"name\":\"Volume\",\"forceOverlay\":false,\"override\":{\"Volume.color.0\":\"rgba(255,0,0,0.5)\",\"Volume.color.1\":\"rgba(0,255,0,0.5)\"}}]}" -o chart-img-03.png

Chart Studies & Drawings

In addition to chart indicators, you can also add custom drawings on top of the chart. The available drawing tools are currently limited, but I plan to add more upon request.

{
"symbol": "BINANCE:ETHUSDT",
"interval": "4h",
"studies": [
{
"name": "Volume",
"forceOverlay": true,
"override": {
"Volume.color.0": "rgba(247,82,95,0.4)",
"Volume.color.1": "rgba(34,171,148,0.4)"
}
},
{
"name": "Moving Average Exponential",
"input": {
"length": 50
},
"override": {
"Plot.linewidth": 2
}
},
{
"name": "Relative Strength Index"
}
],
"drawings": [
{
"name": "Order Line",
"input": {
"text": "BUY ORDER #1",
"quantity": "5 ETH",
"price": 1565
},
"override": {
"fontSize": 12,
"lineColor": "rgb(34,171,148)",
"bodyBorderColor": "rgb(34,171,148)",
"bodyTextColor": "rgb(34,171,148)",
"quantityBorderColor": "rgb(34,171,148)",
"quantityBackgroundColor": "rgb(34,171,148)"
}
},
{
"name": "Order Line",
"input": {
"text": "SELL ORDER #1",
"quantity": "5 ETH",
"price": 1720
},
"override": {
"fontSize": 12,
"lineColor": "rgb(255,0,0)",
"bodyBorderColor": "rgb(255,0,0)",
"bodyTextColor": "rgb(255,0,0)",
"quantityBorderColor": "rgb(255,0,0)",
"quantityBackgroundColor": "rgb(255,0,0)"
}
}
]
}

Please remember that the total number of studies and drawings depends on your subscription plan and cannot exceed your maximum limit. Since this example uses total of more than 3 studies and drawings, BASIC plan will return an error message. For more detailed information on parameter limits, refer to the documentation.

CHART-IMG-04.PNG
## for Linux based
curl -X POST https://api.chart-img.com/v2/tradingview/advanced-chart \
-H "x-api-key: {YOUR_API_KEY}" \
-H "content-type: application/json" \
-d "{\"symbol\":\"BINANCE:ETHUSDT\",\"interval\":\"4h\",\"studies\":[{\"name\":\"Volume\",\"forceOverlay\":true,\"override\":{\"Volume.color.0\":\"rgba(247,82,95,0.4)\",\"Volume.color.1\":\"rgba(34,171,148,0.4)\"}},{\"name\":\"Moving Average Exponential\",\"input\":{\"length\":50},\"override\":{\"Plot.linewidth\":2}},{\"name\":\"Relative Strength Index\"}],\"drawings\":[{\"name\":\"Order Line\",\"input\":{\"text\":\"BUY ORDER #1\",\"quantity\":\"5 ETH\",\"price\":1565},\"override\":{\"fontSize\":12,\"lineColor\":\"rgb(34,171,148)\",\"bodyBorderColor\":\"rgb(34,171,148)\",\"bodyTextColor\":\"rgb(34,171,148)\",\"quantityBorderColor\":\"rgb(34,171,148)\",\"quantityBackgroundColor\":\"rgb(34,171,148)\"}},{\"name\":\"Order Line\",\"input\":{\"text\":\"SELL ORDER #1\",\"quantity\":\"5 ETH\",\"price\":1720},\"override\":{\"fontSize\":12,\"lineColor\":\"rgb(255,0,0)\",\"bodyBorderColor\":\"rgb(255,0,0)\",\"bodyTextColor\":\"rgb(255,0,0)\",\"quantityBorderColor\":\"rgb(255,0,0)\",\"quantityBackgroundColor\":\"rgb(255,0,0)\"}}]}" \
-o chart-img-04.png

## for Windows, you have to merge all lines into one by removing the backslashes
curl -X POST https://api.chart-img.com/v2/tradingview/advanced-chart -H "x-api-key: {YOUR_API_KEY}" -H "content-type: application/json" -d "{\"symbol\":\"BINANCE:ETHUSDT\",\"interval\":\"4h\",\"studies\":[{\"name\":\"Volume\",\"forceOverlay\":true,\"override\":{\"Volume.color.0\":\"rgba(247,82,95,0.4)\",\"Volume.color.1\":\"rgba(34,171,148,0.4)\"}},{\"name\":\"Moving Average Exponential\",\"input\":{\"length\":50},\"override\":{\"Plot.linewidth\":2}},{\"name\":\"Relative Strength Index\"}],\"drawings\":[{\"name\":\"Order Line\",\"input\":{\"text\":\"BUY ORDER #1\",\"quantity\":\"5 ETH\",\"price\":1565},\"override\":{\"fontSize\":12,\"lineColor\":\"rgb(34,171,148)\",\"bodyBorderColor\":\"rgb(34,171,148)\",\"bodyTextColor\":\"rgb(34,171,148)\",\"quantityBorderColor\":\"rgb(34,171,148)\",\"quantityBackgroundColor\":\"rgb(34,171,148)\"}},{\"name\":\"Order Line\",\"input\":{\"text\":\"SELL ORDER #1\",\"quantity\":\"5 ETH\",\"price\":1720},\"override\":{\"fontSize\":12,\"lineColor\":\"rgb(255,0,0)\",\"bodyBorderColor\":\"rgb(255,0,0)\",\"bodyTextColor\":\"rgb(255,0,0)\",\"quantityBorderColor\":\"rgb(255,0,0)\",\"quantityBackgroundColor\":\"rgb(255,0,0)\"}}]}" -o chart-img-04.png

Public Storage

Instead of downloading the chart image directly, you can request the API to save the static image in the storage and provide you with the public URL in JSON. Add /storage to your endpoint.

## for Linux based
curl -X POST https://api.chart-img.com/v2/tradingview/advanced-chart/storage \
-H "x-api-key: {YOUR_API_KEY}" \
-H "content-type: application/json" \
-d "{\"symbol\":\"BINANCE:BTCUSDTPERP\",\"studies\":[{\"name\":\"Bollinger Bands\"},{\"name\":\"Volume\"},{\"name\":\"Relative Strength Index\"}]}" \
-o chart-img-05.json

## for Windows, you have to merge all lines into one by removing the backslashes
curl -X POST https://api.chart-img.com/v2/tradingview/advanced-chart/storage -H "x-api-key: {YOUR_API_KEY}" -H "content-type: application/json" -d "{\"symbol\":\"BINANCE:BTCUSDTPERP\",\"studies\":[{\"name\":\"Bollinger Bands\"},{\"name\":\"Volume\"},{\"name\":\"Relative Strength Index\"}]}" -o chart-img-05.json

After submitting the request, you will receive a JSON response that includes the URL of the chart image. The expiration datetime of the image depends on your subscription plan. For more detailed information on the storage limit, refer to the documentation.

{
"url": "https://r2.chart-img.com/tradingview/advanced-chart/snapshot/WnVkPLllOpE.png",
"etag": "70f9a67accc747ebeb5f824ad669a8bf",
"size": 67742,
"expire": "2023-05-31T03:42:55.116Z"
}

TradingView Session ID

At the time of this writing, CHART-IMG API provides additional real-time charts for NASDAQ, NYSE, NYSE Arca, OTC, CME, CBOT, COMEX and NYMEX. If you have a TradingView data subscription plan for exchanges not fully supported in the documentation, you may include your TradingView sessionid in the request header to get the real-time chart.

An example of delayed charts

KRX:005930
XETR:ADS

How to get your TradingView Session ID

  1. Open TradingView in your web browser.
  2. Log in to your TradingView account if you haven’t already done so.
  3. Open your browser’s developer tools by pressing F12.
  4. In the developer tools, navigate to the Application tab.
  5. In the left-hand panel, click on the Cookies dropdown, and then click on https://www.tradingview.com.
  6. Look for the sessionid and sessionid_sign cookie and copy its value.

Use sessionid and sessionid_sign with the current login session to avoid session disconnect. TradingView does not allow the simultaneous use of multiple sessionidand sessionid_sign, which will result in a session disconnect message on your chart. Additional, you need to update these values before they expires.

THE VALUES ARE REDACTED FOR PRIVACY

Don’t forget to replace {YOUR_SESSION_ID} and {YOUR_SESSION_ID_SIGN} with your actual values in your request header. This should get your chart in real-time with your subscription data. If your sessionid is expired or invalid, the request will default to without one.

## for Linux based
curl -X POST https://api.chart-img.com/v2/tradingview/advanced-chart \
-H "x-api-key: {YOUR_API_KEY}" \
-H "tradingview-session-id: {YOUR_SESSION_ID}" \
-H "tradingview-session-id-sign: {YOUR_SESSION_ID_SIGN}" \
-H "content-type: application/json" -d "{\"symbol\":\"KRX:005930\",\"height\":300}" \
-o chart-img-06.png

## for Windows, you have to merge all lines into one by removing the backslashes
curl -X POST https://api.chart-img.com/v2/tradingview/advanced-chart -H "x-api-key: {YOUR_API_KEY}" -H "tradingview-session-id: {YOUR_SESSION_ID}" -H "tradingview-session-id-sign: {YOUR_SESSION_ID_SIGN}" -H "content-type: application/json" -d "{\"symbol\":\"KRX:005930\",\"height\":300}" -o chart-img-06.png

In Part 2, I will cover Shared/Private Layout features.

--

--