TradingView-API icon indicating copy to clipboard operation
TradingView-API copied to clipboard

Where can I find the chart_id for getDrawings?

Open gazsiazasz opened this issue 2 years ago • 15 comments

Hi,

I am trying to load my drawings from trading view, but it the api requires a chart_id, which is in my case not 1, as the default value for the call, but something like this: qtNCHx5Z6fbT. Tried to look up in the debugger where this value come from, but I had no luck. And there's no example for this unfortunately in the examples folder.

So is it still possible with this api to load my custom drawings from the tv? Thanks!

gazsiazasz avatar Mar 05 '22 17:03 gazsiazasz

I'm also interested in getting my drawings (trendlines and horizontal lines) from my charts. But I'm not figuring out how to do it. Is there any documentation or is it just the examples? I can't see anything about the drawings.

Thank you in advance.

joplomacedo avatar Mar 05 '22 18:03 joplomacedo

Hey, I just added an example here.

The chartID parameter is optional. The layoutID can be found in the URL of your chart.

Mathieu2301 avatar Mar 05 '22 20:03 Mathieu2301

If I skip the chartID, only set the userid and sessionid, I get an empty array. But if I specify the chartID parameter(which I can see in the browsers inspector) also, then I get all 332 drawings. (I have google associated account, not plain tv account.)

gazsiazasz avatar Mar 05 '22 20:03 gazsiazasz

Are you sure it's the chartID and not the layoutID ? The layoutID is normally required.

Mathieu2301 avatar Mar 05 '22 20:03 Mathieu2301

Yes, also I noticed that https://www.tradingview.com/chart/{layoutID}/ contains the chartID on line 2138 (for me)

gazsiazasz avatar Mar 05 '22 20:03 gazsiazasz

Hey, I just added an example here.

The chartID parameter is optional. The layoutID can be found in the URL of your chart.

Thank you! And thanks for the work you've put into this.

joplomacedo avatar Mar 05 '22 22:03 joplomacedo

I'm sorry to bother you again.

There's this comment:

Third parameter must be the userid (you can use getUser function)

I'm really lost. Is there any documentation or something I can read to find out about these functions? I've tried googling and can't find anything.

joplomacedo avatar Mar 05 '22 22:03 joplomacedo

Ok. I'm managing. But any idea why I'm getting the wrong credentials warning? What comes from TradingView is this:

{code: 'forbidden', detail: 'You are trying to get token for different user. Please refresh the page and try again.'}

I got the session from the cookies in my browser. I got the user id from the getUser function.

I refreshed the page as suggested in the warning (I even logged out and logged back in) and I'm still getting the same error.

joplomacedo avatar Mar 05 '22 23:03 joplomacedo

I got the session from the cookies in my browser. I got the user id from the getUser function.

So if you have both :

TradingView.getDrawings(YOUR_LAYOUT_ID, null, {
  session: YOUR_SESSION_ID,
  id: YOUR_USER_ID,
}).then((drawings) => {
  console.log(`Found ${drawings.length} drawings:`, drawings.map((d) => ({
    id: d.id,
    symbol: d.symbol,
    type: d.type,
    text: d.state.text,
  })));
}).catch((err) => {
  console.error('Error:', err.message);
});

Or run node examples/GetDrawings.js YOUR_LAYOUT_ID YOUR_SESSION_ID YOUR_USER_ID.

Mathieu2301 avatar Mar 06 '22 02:03 Mathieu2301

Yes. That's exactly what I'm doing and I still receive that error message. Any idea why?

Thank you.

On Sun, Mar 6, 2022, 2:05 AM Mathieu Colmon @.***> wrote:

I got the session from the cookies in my browser. I got the user id from the getUser function.

If you have both :

TradingView.getDrawings(YOUR_LAYOUT_ID, null, { session: YOUR_SESSION_ID, id: YOUR_USER_ID,}).then((drawings) => { console.log(Found ${drawings.length} drawings:, drawings.map((d) => ({ id: d.id, symbol: d.symbol, type: d.type, text: d.state.text, })));}).catch((err) => { console.error('Error:', err.message);});

Or run node examples/GetDrawings.js YOUR_LAYOUT_ID YOUR_SESSION_ID YOUR_USER_ID.

— Reply to this email directly, view it on GitHub https://github.com/Mathieu2301/TradingView-API/issues/79#issuecomment-1059877590, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAO7ZF4543JY37GB3NCYHILU6QHHDANCNFSM5P76ZLJA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you commented.Message ID: @.***>

joplomacedo avatar Mar 06 '22 02:03 joplomacedo

It's working today. No clue.

joplomacedo avatar Mar 06 '22 13:03 joplomacedo

It stopped working again without me changing anything. Any clue as to why this might be happening?

joplomacedo avatar Mar 06 '22 16:03 joplomacedo

Maybe your are using a temp sessionid (you didn't check the 'remember me' checkbox) or maybe you entered the wrong userid...

Mathieu2301 avatar Mar 06 '22 16:03 Mathieu2301

With the getUser, I also have issues, it looks like there are more userids/usernames on the page and the code is matching the wrong one in my case. So I replaced the https://github.com/Mathieu2301/TradingView-API/blob/5e769af35d4e3de1281044a620de354650bb94a2/src/miscRequests.js#L416-L433

with this experimental code: cb(JSON.parse(rs.match("var user = ({.*})")[1])); It gives me the correct user info, but that is not formatted in the same way like the code here in the repo.

But I still have to fetch the chartId too, because without that I cannot get any drawing from the tv api.

gazsiazasz avatar Mar 06 '22 19:03 gazsiazasz

For me it only lists the drawings with the following code snippet (I used axios here because I am not familiar with https module):

import TradingView from '@mathieuc/tradingview'
import axios from 'axios';

let session = 'xxxx';
let cl = axios.create({baseURL: 'https://www.tradingview.com', headers: {Cookie: `sessionid=${session}`}})

let userId_req = await cl.get('/')
let user = JSON.parse(userId_req.data.match(/var user = ({.*})/)[1])
console.log(user.id)

let layoutId_req = await cl.get('/chart')
let layoutId = layoutId_req.request.path.split('/')[2]
console.log(layoutId)

let initData = JSON.parse(layoutId_req.data.match(/initData.content = ({.*})/)[1])
let chartId = initData.charts[0].chartId
console.log(chartId)

TradingView.getDrawings(layoutId, user.settings['editchart.model.symbol'], {
    session: session,
    id: user.id,
}, chartId).then((drawings) => {
    console.log(`Found ${drawings.length} drawings:`, drawings.map((d) => ({
        id: d.id,
        symbol: d.symbol,
        type: d.type,
        text: d.state.text,
        points: d.points
    })));
}).catch((err) => {
    console.error('Error:', err.message);
});

gazsiazasz avatar Aug 26 '22 22:08 gazsiazasz