JerBouma / FinanceToolkit

Transparent and Efficient Financial Analysis
https://www.jeroenbouma.com/projects/financetoolkit
MIT License
2.84k stars 347 forks source link

[IMPROVE] get_historical_data call fails with `No data found for the following tickers: ^TNX` #81

Closed laurynasas closed 10 months ago

laurynasas commented 10 months ago

hello! Thank you for such a great project!

I have been experimenting on Google Colab with some API calls and everything works okay, so I decided to move locally to my MacBook, however, I encounter issues with the historical data (which is required for the ratios module), financial statements WAI.

from financetoolkit import Toolkit
companies = Toolkit(tickers=['GOOG'], api_key='KEY', start_date="2018-12-31")
companies.get_historical_data()

fails with:

No data found for the following tickers: ^TNX @ financetoolkit/toolkit_controller.py", line 2187, in get_treasury_data ...

I use financetoolkit==1.6.1 and Python 3.10.11. I tried new API key, different IP addresses (within same country), clean virtualenv but no luck. This call works in the Google Colab without issues with the same API key.

Any ideas?

thank you!

JerBouma commented 10 months ago

Hi!

I'll look into this soon. Are you using the Free plan? Most likely the issue is that I try to collect the risk free rate but the Free plan is not allowed to get indices which is why it fails.

It should automatically change the source (to Yahoo Finance) but it seems it doesn't so I'll need to fix that!

Does it prevent you from using the Ratios functionality? What's the error message?

laurynasas commented 10 months ago

Thank you for such a prompt answer.

Are you using the Free plan? Most likely the issue is that I try to collect the risk free rate but the Free plan is not allowed to get indices which is why it fails.

Yes. I'm experimenting with a free plan for the moment. I don't think it's the issue, because with the same free plan API key I'm able to retrieve historical information in Google Colab - but perhaps that fallbacks to Yahoo Finance API? I'm not sure which API fails.

Does it prevent you from using the Ratios functionality? What's the error message?

Yes, some of the ratio calculations (that require risk-free rate) fails, the others - don't.

This is the full stack:

No data found for the following tickers: ^TNX
Traceback (most recent call last):
  File "/testing.py", line 8, in <module>
    print(companies.ratios.collect_liquidity_ratios())
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/financetoolkit/toolkit_controller.py", line 494, in ratios
    self.get_historical_data(period="yearly")
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/financetoolkit/toolkit_controller.py", line 1652, in get_historical_data
    self.get_treasury_data(
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/financetoolkit/toolkit_controller.py", line 2187, in get_treasury_data
    ) = _get_historical_data(
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/financetoolkit/historical_model.py", line 181, in get_historical_data
    historical_data = pd.concat(historical_data_dict).unstack(level=0)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/reshape/concat.py", line 380, in concat
    op = _Concatenator(
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/reshape/concat.py", line 443, in __init__
    objs, keys = self._clean_keys_and_objs(objs, keys)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/reshape/concat.py", line 505, in _clean_keys_and_objs
    raise ValueError("No objects to concatenate")
ValueError: No objects to concatenate

Thank you for the help!

laurynasas commented 10 months ago

So I had some time to do debugging, as you said the FMP API call fails with:

{
  "Error Message": "Free plan is limited to US stocks only please visit our subscription page to upgrade your plan at https://site.financialmodelingprep.com/developer/docs/pricing"
}

The Yahoo Finance call was failing to due expired SSL certificates which now works when I updated them:

<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)

So one suggestion would be to catch the errors in the try-except block and bubble them up, cause now the api calls fail silently.

JerBouma commented 10 months ago

Thank you for providing this error message. I'll add it into the code. It is hard for me to debug these things since they never occur for me 😅.

In any case, I've worked on a "fix" in which if it fails, it will convert to a 0% risk free rate so that at least everything can be calculated. Just to ensure that if others run into issues, at least the impact isn't as big.

JerBouma commented 10 months ago

@laurynasas Just letting you know I have patched this issue in v.1.6.3: https://github.com/JerBouma/FinanceToolkit/releases/tag/v1.6.3

See the code that patches it: https://github.com/JerBouma/FinanceToolkit/blob/main/financetoolkit/historical_model.py#L182-L191