Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query to filter with "above of below %" #25

Closed
jn11 opened this issue Apr 8, 2024 · 6 comments
Closed

Query to filter with "above of below %" #25

jn11 opened this issue Apr 8, 2024 · 6 comments

Comments

@jn11
Copy link

jn11 commented Apr 8, 2024

Hi, the new v2 screener of Trading view allows to have filters like this: "Price above New Low 52 Weeks by 100% or more"

I try to implement this filter with the Query() I add a multiplier (*2) at the end of Column('price_52_week_low'):

(Query()
.select('name', 'close', 'price_52_week_low')
.where(
Column('close') >= (Column('price_52_week_low')*2 ) ,
)
.get_scanner_data())

but get this error:

Traceback (most recent call last):
File "", line 4, in
TypeError: unsupported operand type(s) for +: 'int' and 'Column'

Is there any other way to implement this kind of filters using the "above of below %"?

Thank you

@shner-elmo
Copy link
Owner

Hey, first of all thanks for letting me know.

allows to have filters like this: "Price above New Low 52 Weeks by 100% or more"

So you can compare a column to another column or to a value, i.e.:

Column('close') > 125.0
# or
Column('close') > Column('price_52_week_low')

not both.

Can you show me how you would accomplish what you mentioned through the website? (please share a screenshot)

@jn11
Copy link
Author

jn11 commented Apr 10, 2024

Find attached 2 screenshots of the filter with TradingView Screener v2 using the app in my desktop computer:

Capture2
Capture1

Will be great if if we can do the same with this python code. In the documentation I see:

A Column supports all the comparison operations: <, <=, >, >=, ==, !=, and also other methods like between(), isin(), etc.

Maybe a solution for the filter "Price above New Low 52 Weeks by 100% or more" can be a new method like abovePercentage() / belowPercentage() ?

Column('close').abovePercentage(100, Column('price_52_week_low')

Thank you

@shner-elmo
Copy link
Owner

I now understand what you mean, there is a whole new version of the screener (also the syntax of the backend changed).
Interestingly, the feature you mentioned is only available in the screener at https://www.tradingview.com/screener/, but not under the chart.

I'm gonna open a new branch in which we can add this functionality. And I'm probably gonna release it in a major-breaking-version (v3.0.0).

@shner-elmo
Copy link
Owner

Never mind, I'm still not done with 3.0.0, so I added this method (above_pct()) to version 2.5.0.

https://github.com/shner-elmo/TradingView-Screener/releases/tag/v2.5.0
https://shner-elmo.github.io/TradingView-Screener/tradingview_screener/query.html#Column.above_pct

@jn11
Copy link
Author

jn11 commented Jun 20, 2024

Good job with this update! I just tested the new methods above_pct(), below_pct() and between_pct() but I found some issues if I set the tickers:

E.g. query 1:

q = Query().select('name', 'close')
q .where(     Column('close').above_pct('VWAP', 1.40)  )
q.get_scanner_data()

Return is OK:

(14, ticker name close
0 OTC:RDTCF RDTCF 0.12990
1 OTC:BNSOF BNSOF 1.50000
2 OTC:FDVXF FDVXF 0.08000
3 OTC:ZTLLF ZTLLF 0.00854
4 OTC:SRNE SRNE 0.01210
5 OTC:KRTL KRTL 0.00800
6 OTC:WEJOF WEJOF 0.00500
7 OTC:CBGL CBGL 0.00010
8 OTC:EHVVF EHVVF 0.00100
9 OTC:GGTHF GGTHF 0.10000
10 OTC:BLTH BLTH 0.30000
11 OTC:BRGO BRGO 0.00010
12 OTC:HBRM HBRM 0.00010
13 OTC:GRGR GRGR 0.00001)

E.g. query 2 (with set_tickers()):

q = Query().select('name', 'close')
q .where( Column('close').above_pct('VWAP', 1.40) )
q.set_tickers('OTC:SRNE','OTC:BNSOF','OTC:RDTCF')
q.get_scanner_data()

Return this:

>>> q.get_scanner_data()
Traceback (most recent call last):
File "", line 1, in
File "C:\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\tradingview_screener\query.py", line 668, in get_scanner_data
r.raise_for_status()
File "C:\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\requests\models.py", line 1021, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request
Body: {"totalCount":0,"error":"could not process request: Incompatible types: number and null","data":null}
for url: https://scanner.tradingview.com/global/scan

@shner-elmo
Copy link
Owner

Thanks for the feedback. There is a bug in the code that doesn't set the markets properly.
Simply call set_markets() after calling set_tickers():

q = Query().select('name', 'close')
q.where(Column('close').above_pct('VWAP', 1.40))
q.set_tickers('OTC:SRNE','OTC:BNSOF','OTC:RDTCF')
q.set_markets('america')
q.get_scanner_data()
(3,
       ticker   name   close
 0  OTC:RDTCF  RDTCF  0.1299
 1  OTC:BNSOF  BNSOF  1.5000
 2   OTC:SRNE   SRNE  0.0121)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants