You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unable to use request_cache to cache the yfinance response #2486
I want to cache the yfinance response so I am using request_cache and overriding the session, But I am getting
Too Many Requests. Rate limited. Try after a while.
Yes, I solved it, those are pieces of my code it's not completed, I tried to adapted here, let me copy and paste more, these pieces os code belongs an extensive class which I cannot paste here totally:
You're right, rate-limiting is not being taken into account at all, I cehcked with a list of tickers and all of them were taken quickly without restrictions.
Checking on the Internet, I remember to see pieces of code where that worked in the past.
Do you know if currently there is some way or customization to make it operative again?
And could you tell me the file#line where I have to print it when you have a while?
I'm new using this library and almost python, any information here about if you know if that will be updated/fixed is appreciated.
Hi, I put several tests, only these two worked for me, one without session and other with custom session.
Take into account rate-limiting is not working.
import yfinance as yf
try:
stock = yf.Ticker("AAPL")
print(stock.history(period="5d")) # it works
except Exception as e:
print(e)
# #############################################################
import yfinance as yf
from curl_cffi import requests
from requests_cache import install_cache
from requests_ratelimiter import LimiterSession, MemoryQueueBucket
from pyrate_limiter import Duration, RequestRate, Limiter
session = requests.Session(impersonate="chrome")
install_cache(
cache_name="_yfinance_file_cache",
session=session
)
limiter = Limiter(RequestRate(2, Duration.SECOND * 5))
limited_session = LimiterSession(
session=session,
limiter=limiter,
bucket_class=MemoryQueueBucket,
)
def limited_request(*args, timeout=15, **kwargs):
return session.request(*args, timeout=timeout, **kwargs)
limited_session.request = limited_request
try:
stock = yf.Ticker("AAPL", session=limited_session) # it works !!
print(stock.history(period="5d"))
except Exception as e:
print(e)
# ###################################################################
@deeleeramone I just tried curl_adapter, obviously had to disable some things inside data.py. If it works that's great, can restore requests support, but I can't get it working:
EDIT: I got it working with 0.2.57. I'll restore requests support asap.
But how to combine with requests_cache or requests_ratelimiter?
Activity
ValueRaider commentedon May 11, 2025
Because then you are overriding using curl_cffi session
requests_cache officially does not work with curl_cffi JWCook/requests-ratelimiter#112
Will have to remove from docs, and maybe disable session argument
cybtow commentedon May 11, 2025
Hi, I solved it specifying manually the headers, here my pices of code:
...
...
ValueRaider commentedon May 11, 2025
@cybtow Did you really solve it?
_get_internal_cacheisn't returning anything.cybtow commentedon May 11, 2025
Yes, I solved it, those are pieces of my code it's not completed, I tried to adapted here, let me copy and paste more, these pieces os code belongs an extensive class which I cannot paste here totally:
Based on https://ranaroussi.github.io/yfinance/advanced/caching.html, your code should be something like this (note, your code is using self.requestSession rather than requestCacheSession ):
or try this:
cybtow commentedon May 11, 2025
I just ran my script right now and it's not working again. Yesterday, it was doing that.
I'll research a bit more if I find a solution.
ValueRaider commentedon May 11, 2025
Research what session is actually being used by yfinance, I think you will be surprised
cybtow commentedon May 11, 2025
I've tried some options, only two worked, one without specifying a session, and other (the last one) specifying a custom session.
I think it'll be nice that yfinance could be updated to work with others options too.
Here my basic code, just tested using the v0.2.59 of yfinance
cybtow commentedon May 11, 2025
@ValueRaider curious to know that session ;)
ValueRaider commentedon May 11, 2025
I can't tell you. You edit the source code to print.
Does it actually enforce rate-limiting? I never got it working, and officially it will not work.
cybtow commentedon May 11, 2025
You're right, rate-limiting is not being taken into account at all, I cehcked with a list of tickers and all of them were taken quickly without restrictions.
Checking on the Internet, I remember to see pieces of code where that worked in the past.
Do you know if currently there is some way or customization to make it operative again?
And could you tell me the file#line where I have to print it when you have a while?
I'm new using this library and almost python, any information here about if you know if that will be updated/fixed is appreciated.
Thank you in advance.
ronak66 commentedon May 12, 2025
@cybtow I tried your code and I am getting the following error
deeleeramone commentedon May 12, 2025
Please don't remove the session argument, I would say it's a pretty important feature.
You can mount your Requests Session object to use CFFI with
curl-adapterhttps://pypi.org/project/curl-adapter/1.0.0.post3/
This works, and you can pass the session object as your normal Requests Session.
ronak66 commentedon May 12, 2025
Can we add caching logic in yfinance library and extend it via a functional parameter if users would like to cache the data for faster response time
ronak66 commentedon May 12, 2025
@cybtow, from the test, using the v0.2.59 of finance, which piece of code worked for you?
cybtow commentedon May 12, 2025
Hi, I put several tests, only these two worked for me, one without session and other with custom session.
Take into account rate-limiting is not working.
ValueRaider commentedon May 17, 2025
@deeleeramone I just tried curl_adapter, obviously had to disable some things inside
data.py. If it works that's great, can restorerequestssupport, but I can't get it working:EDIT: I got it working with 0.2.57. I'll restore requests support asap.
But how to combine with requests_cache or requests_ratelimiter?