ranaroussi / yfinance

Download market data from Yahoo! Finance's API
https://aroussi.com/post/python-yahoo-finance
Apache License 2.0
14.89k stars 2.44k forks source link

Implement Screener Feature #2066

Closed ericpien closed 1 month ago

ericpien commented 1 month ago

Screener can be used to filter securities on yahoo finance. This can be used to get the top gainers of the day, and more customized queries that a user may want to automate.

Resolves:

Sample Code

import yfinance as yf

# Example query to filter stocks
gt = yf.EquityQuery('gt', ['eodprice', 3])
lt = yf.EquityQuery('lt', ['avgdailyvol3m', 99999999999])
btwn = yf.EquityQuery('btwn', ['intradaymarketcap', 0, 100000000])
eq = yf.EquityQuery('eq', ['sector', 'Technology'])

# Combine queries using AND/OR
qt = yf.EquityQuery('and', [gt, lt])
qf = yf.EquityQuery('or', [qt, btwn, eq])

# Create a screener instance
screener = yf.Screener()

# Set the default body using a custom query
screener.set_default_body(qf)

# Set predefined body
screener.set_predefined_body('day_gainers')

# Set the fully custom body 
# The keys below are required fields for the request body
screener.set_body({
    "offset": 0,
    "size": 100,
    "sortField": "ticker",
    "sortType": "desc",
    "quoteType": "equity",
    "query": qf.to_dict(),
    "userId": "",
    "userIdType": "guid"
})

# Patch parts of the body
screener.patch_body({"offset": 100})

# view the current body of the screener
screener.body

# Fetch and display the result json
result = screener.response
print(results)

# save the queried symbols
symbols = [quote['symbol'] for quote in result['quotes']]
ValueRaider commented 1 month ago

Do you think the README is getting too long now and needs to offload some content to a new home?

ericpien commented 1 month ago

Do you think the README is getting too long now and needs to offload some content to a new home?

I agree. As part of the offloading, I think creating a \doc directory and adding api specific documentation in there would be great for new users and contributors.

Idk what you have in mind but maybe something like below?

doc/
├── CHANGELOG.rst
└── api/
    ├── Ticker
    ├── Sector_Industry
    ├── Screener
    └── General # caching, logging, managing multi-level columns
ValueRaider commented 1 month ago

Move doc discussion to #1330

ericpien commented 1 month ago

@ValueRaider I suspect you've just been busy but in case you lost track, can you review the remainder of the code when you get a chance? I also provided a draft of documentation at https://github.com/ranaroussi/yfinance/discussions/1330#discussioncomment-10875161

ValueRaider commented 1 month ago

Replace the README addition with essentially:

Then I can merge and we sort out documentation later. Don't rebase this time, so we can reuse c048378