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

Screening by industries #2272

Open
ggeeoorrgg opened this issue Feb 17, 2025 · 8 comments · May be fixed by #2273
Open

Screening by industries #2272

ggeeoorrgg opened this issue Feb 17, 2025 · 8 comments · May be fixed by #2273

Comments

@ggeeoorrgg
Copy link

Hi!
Is it possible to add a feature for screening by industries?
Currently, I do it like this:

   from yfinance.const import EQUITY_SCREENER_EQ_MAP, EQUITY_SCREENER_FIELDS
   from yfinance import EquityQuery
   sector = "Basic Materials"
   industry = "Chemicals"
   EQUITY_SCREENER_FIELDS.get("eq_fields").update(["industry"])
   EQUITY_SCREENER_EQ_MAP.update({"industry": f"{industry}"})
   query =  EquityQuery("and", [EquityQuery("is-in", ["sector", f"{sector}"]), EquityQuery("is-in", ["industry", f"{industry}"]), EquityQuery("is-in", ["exchange", "NYQ", "NMS"])])
   resp = yf.screen(query)

But it looks a bit awkward, doesn't it?

@R5dan
Copy link
Contributor

R5dan commented Feb 17, 2025

Do you know what industries are available?

@ggeeoorrgg
Copy link
Author

Do you know what industries are available?

I guess the ones used by Yahoo Screener.
See here: https://finance.yahoo.com/research-hub/screener/equity
In requests, they should be specified exactly as they appear on Yahoo Finance.

@R5dan R5dan linked a pull request Feb 17, 2025 that will close this issue
@ggeeoorrgg
Copy link
Author

I keep experimenting with industry screening.
It seems to work except for ones with "—" in their names.
Is there any idea how to fix it?

@rhpl
Copy link

rhpl commented Feb 19, 2025

The problem here is that yahoo uses specifically the 'em dash' character. When calling the query request the body gets passed as a json to the requests library. Down the chain the 'json.dumbs' function gets called, which changes the 'em dash' to '\u2014'. Obviously the resulting string is not a valid industry for yahoo, so it returns 0 items.

One fix (temporary) would be to go into the requests library to file models.py to line 511. Add 'ensure_ascii=False' as parameter to the complexjson.dumps function.
Obviously since it is a underlying library it might have impact on other requests in the code. So somebody with more knowledge in python project structuring has to come in now.

@ggeeoorrgg
Copy link
Author

The problem here is that yahoo uses specifically the 'em dash' character. When calling the query request the body gets passed as a json to the requests library. Down the chain the 'json.dumbs' function gets called, which changes the 'em dash' to '\u2014'. Obviously the resulting string is not a valid industry for yahoo, so it returns 0 items.

One fix (temporary) would be to go into the requests library to file models.py to line 511. Add 'ensure_ascii=False' as parameter to the complexjson.dumps function. Obviously since it is a underlying library it might have impact on other requests in the code. So somebody with more knowledge in python project structuring has to come in now.

I see, thanks.

@R5dan
Copy link
Contributor

R5dan commented Feb 19, 2025

@ggeeoorrgg What do you think of my pr? Does it work how you would expect?

@ggeeoorrgg
Copy link
Author

ggeeoorrgg commented Feb 20, 2025

@ggeeoorrgg What do you think of my pr? Does it work how you would expect?

I really appreciate your efforts, thanks.
I just haven't had enough time to explore that, but I'll try.
By the way, could you please show an example of how to use your improvements?

P.S. I've just tried to explore your suggestion, but unfortunately, I didn’t quite get it. I still need a use case.

@ggeeoorrgg
Copy link
Author

One fix (temporary) would be to go into the requests library...

Well, to avoid digging too deep, it should be sufficient to do something like this:
In line 390 of Data.py, replace:
request_args['json'] = body
with:

body = json.dumps(body, cls=CustomJSONEncoder, ensure_ascii=True)
request_args['data'] = body
request_args["headers"].update({"Content-Type": "application/json"})

And define this somewhere:

class CustomJSONEncoder(json.JSONEncoder):
    def encode(self, obj):
        json_str = super().encode(obj)
        return json_str.replace(r'\u2014', '—')

It works, but I haven’t done extensive testing.
I also discovered that industry names with an em dash should be specified without spaces, e.g., "REIT—Mortgage".

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

Successfully merging a pull request may close this issue.

3 participants