shner-elmo / TradingView-Screener

A package that lets you create TradingView screeners in Python
https://shner-elmo.github.io/TradingView-Screener/2.5.0/tradingview_screener.html
MIT License
237 stars 42 forks source link

Time lag in the result #28

Closed darwin-zeng closed 3 months ago

darwin-zeng commented 5 months ago

thanks for the work for this great tool and it works like a charm.

Just realized that the result from the API has a few minutes lag compare to the result directly form the tradingview.

I am a premium user of tradingview. From the website you can't do much as a guest, which is the API's role fetching the data.

No sure what cause the lag. If we need to login to get realtime result, can the tool add this feature in the future? Thanks.

darwin-zeng commented 5 months ago

The code already mentioned this subject and this worked for me:

import browser_cookie3

cookies = browser_cookie3.chrome(domain_name='.tradingview.com')

rows, max_cap = (Query() .select('name', 'close', 'volume','market_cap_basic') .where( Column('market_cap_basic') > 1_000_000_000, # cap > 1B ) .order_by('market_cap_basic', ascending=False) .limit(10) .get_scanner_data(cookies=cookies))

print(rows,max_cap)

Stail7 commented 5 months ago

Hi! I can see about 15-minute delay too. So without a paid TV subscription, there is no way to get non-delayed data from this screener? Do you have only a TV plan or an additional US market data package too?

darwin-zeng commented 5 months ago

No, you have to pay to get realtime data. You need both trandingview plan and data pacakge.

Stail7 commented 5 months ago

No, you have to pay to get realtime data. You need both trandingview plan and data pacakge.

I just checked again, we don't need a paid subscription, realtime quotes are available on TV website with a simple user login (without paid sub), but when I sign out - quotes become 15 minutes delayed on the website. As for the Python screener, your solution above didn't help me to get realtime data with browser_cookie3... don't know why... @shner-elmo maybe you can please take a look into that?

shner-elmo commented 5 months ago

Hey, first of all thank you very much for sharing this awesome solution @darwin-zeng I tested it and it works great for me.

shner-elmo commented 5 months ago

@Stail7 I'm not sure on which stocks you get real-time data for free, but you might want to have a look at this. https://www.tradingview.com/data-coverage/

Now, if you notice that the data on the Screener is different from your browser, its because you need to sign in before you perform the request to the API, you do that by passing the cookies.

You can use the same cookies you are currently using in your browser with the browser_cookie3 library (thanks to Darwin for sharing this).

Try this:

from tradingview_screener import Query, Column
import browser_cookie3

# type the name of your browser instead of 'chrome'
cookies = browser_cookie3.chrome(domain_name='.tradingview.com')

query = Query().select(
    'update_mode',
    'update_mode|1',
    'update_mode|5',
    'update_mode|15',
    'update_mode|30',
    'update_mode|60',
    'update_mode|120',
    'update_mode|240',
    'update_mode|1W',
    'update_mode|1M',
)

Without passing the cookies:

query.get_scanner_data()
(17774,
           ticker  ...         update_mode|1M
 0    NASDAQ:NVDA  ...  delayed_streaming_900
 1       AMEX:SPY  ...  delayed_streaming_900
 2    NASDAQ:AAPL  ...  delayed_streaming_900
 3    NASDAQ:TSLA  ...  delayed_streaming_900
 4     NASDAQ:QQQ  ...  delayed_streaming_900
 5    NASDAQ:AMZN  ...  delayed_streaming_900
 6     NYSE:BRK.A  ...  delayed_streaming_900
 7     NASDAQ:AMD  ...  delayed_streaming_900
 8    NASDAQ:MSFT  ...  delayed_streaming_900
 9    NASDAQ:META  ...  delayed_streaming_900
 10      AMEX:IWM  ...  delayed_streaming_900

And with the cookies:

query.get_scanner_data(cookies=cookies)
(17773,
           ticker update_mode  ... update_mode|1W update_mode|1M
 0    NASDAQ:NVDA   streaming  ...      streaming      streaming
 1       AMEX:SPY   streaming  ...      streaming      streaming
 2    NASDAQ:AAPL   streaming  ...      streaming      streaming
 3    NASDAQ:TSLA   streaming  ...      streaming      streaming
 4     NASDAQ:QQQ   streaming  ...      streaming      streaming
 5    NASDAQ:AMZN   streaming  ...      streaming      streaming
 6     NYSE:BRK.A   streaming  ...      streaming      streaming
 7     NASDAQ:AMD   streaming  ...      streaming      streaming
 8    NASDAQ:MSFT   streaming  ...      streaming      streaming
 9    NASDAQ:META   streaming  ...      streaming      streaming
 10      AMEX:IWM   streaming  ...      streaming      streaming

As you can see, we have successfully signed-in, and we are getting streaming data.

Let me know if this worked for you.

Stail7 commented 5 months ago

@shner-elmo Thanks a lot for the detailed explanation! On a free TV subscription API data is delayed in both cases (with and without cookies). But the website screener page shows real-time data when I signed in with a free account. Website data is delayed only if I sign out completely. It seems like an API response is different from what we can see on the website, which is a shame... Or the free account cookies sent through API don't let me be signed in (how to check it tho?).

2024-05-03

shner-elmo commented 4 months ago

delayed in both cases (with and without cookies). But the website screener page shows real-time data when I signed in with a free account.

So something is wrong with your cookies. By passing the cookies you are logging in, and you should get the same exact data as in the browser (when logged in).

It seems like an API response is different from what we can see on the website

Wrong, the website performs the same exact API request as the TV screener (to the same endpoint, with the same headers and cookies)

Or the free account cookies sent through API don't let me be signed in

Yeah, it's gotta be this.

shner-elmo commented 4 months ago

@Stail7 Can you please show me the output of the query I wrote above (with passing your cookies)?

Also, print the cookies and make sure its not empty, like this (don't share them tho):

<CookieJar[]>
Stail7 commented 4 months ago

I'm fairly new to Python and it turns out my Chrome cookies were indeed empty (I've been using Chrome all the time). Everything worked with Firefox tho! Free account and streaming non-delayed data! Much appreciate your work and help @shner-elmo! Thank you very much!

>>> from tradingview_screener import Query, Column
>>> import browser_cookie3
>>>
>>> cookies = browser_cookie3.firefox(domain_name='.tradingview.com')
>>> print(cookies)
<CookieJar[<Cookie cookie...
>>>
>>> query = (Query().select(
...     'update_mode',
...     'update_mode|1',
...     'update_mode|5',
...     'update_mode|15',
...     'update_mode|30',
...     'update_mode|60',
...     'update_mode|120',
...     'update_mode|240',
...     'update_mode|1W',
...     'update_mode|1M',
... )
... .limit(3))
>>>
>>> query.get_scanner_data(cookies=cookies)

(17789,         ticker update_mode update_mode|1 update_mode|5 update_mode|15 update_mode|30 update_mode|60 update_mode|120 update_mode|240 update_mode|1W update_mode
|1M
0  NASDAQ:NVDA   streaming     streaming     streaming      streaming      streaming      streaming       streaming       streaming      streaming      streaming
1     AMEX:SPY   streaming     streaming     streaming      streaming      streaming      streaming       streaming       streaming      streaming      streaming
2  NASDAQ:TSLA   streaming     streaming     streaming      streaming      streaming      streaming       streaming       streaming      streaming      streaming
shner-elmo commented 4 months ago

Nice! I'm glad you got it working.

On a side note, maybe you are using Chromium instead of Chrome? If so try the chromium function.

Stail7 commented 4 months ago

On a side note, maybe you are using Chromium instead of Chrome? If so try the chromium function.

No, Chrome, I tried chromium function - didn’t work for me, firefox did. Thank you again and have a great week!

shner-elmo commented 3 months ago

So I guess we have found a solution so I will close the issue.

The library will always allow users to pass their cookies and headers if they choose so, for cases like this. However, it would be nice to document it, by adding some examples of how to check if your data is streaming, and how to log in by passing your cookies...

(As a side note, I noticed that TradingView's browser cookies last almost a year, before you need to log in again. So this is a very reliable method.)