tradingview / charting-library-tutorial

This tutorial explains step by step how to connect your data to the Charting Library
MIT License
434 stars 317 forks source link

How to get selected instrument symbol event #112

Closed vandercijr closed 6 months ago

vandercijr commented 6 months ago

Hey.

I am using the datafeed's resolveSymbol event to get the symbol of the selected instrument, but I am facing the following problem. The first time I select the instrument, resolveSymbol is triggered, if I search and select another different instrument, the event is also triggered, but if I try to select an instrument that I have already selected previously, the event resolveSymbol does not fire. Is there another event I should use or is this a bug?

Thanks

romfrancois commented 6 months ago

There's different ways to read your message/query.

but if I try to select an instrument that I have already selected previously, the event resolveSymbol does not fire.

Most likely all data were cached during the first request and therefore it was unnecessary to trigger another request. Unless you meant that it didn't work and nothing happened. Could you please clarify?

vandercijr commented 6 months ago

There's different ways to read your message/query.

but if I try to select an instrument that I have already selected previously, the event resolveSymbol does not fire.

Most likely all data were cached during the first request and therefore it was unnecessary to trigger another request. Unless you meant that it didn't work and nothing happened. Could you please clarify?

I realized that the data is probably cached, which would explain the resolveSymbol event only firing the first time I select an instrument, and if I do it again (select same instrument as previously in that sequence for example instrument A, B and A again) the resolveSymbol is not firing. For me ok, if this was implemented this way, however I need to update a component external to the tradingview library, for this I am using the resolveSymbol event as a way of knowing that an instrument has been selected, so if it doesn't fire, I can't do that. Is there another way to check if an instrument is selected?

romfrancois commented 6 months ago

If you're after an event that triggers every time there's a symbol change I would advise using onSymbolChanged subscription - doc here

vandercijr commented 6 months ago

If you're after an event that triggers every time there's a symbol change I would advise using onSymbolChanged subscription - doc here

Thanks !! It could be a solution but I need to know what symbol was selected and this function don't provide it to me widget.activeChart().onSymbolChanged().subscribe(null, () => console.log('The symbol is changed'));

as you can see

romfrancois commented 6 months ago

You could then check if current symbol === widget.activeChart().symbol() (doc here)

vandercijr commented 6 months ago

You could then check if current symbol === widget.activeChart().symbol() (doc here)

Thank you a lot. It fits perfectly, just widget.activeChart().symbol() as the current selected symbol used inside callback solved the problem for me