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

Yahoo finance support is broken, or do we need a particular version of yfinance lib? #113

Open
trivalent opened this issue Nov 25, 2024 · 2 comments

Comments

@trivalent
Copy link

Running a simple code with the following code results in a failure:

at = AutoTrader()
at.configure(verbosity=1, show_plot=True, feed="yahoo")
at.add_strategy("ema_crossover")
at.backtest(start_dt=datetime.now() - timedelta(days=190), end_dt=datetime.now())
at.virtual_account_config(initial_balance=100000, leverage=1)
at.run()

File "E:\workspace\python\AutoTrader\autotrader\autotrader.py", line 1468, in _main
bot = AutoTraderBot(
File "E:\workspace\python\AutoTrader\autotrader\autobot.py", line 203, in init
my_strat.create_plotting_indicators(data)
File "E:\workspace\python\AutoTrader\strategies\ema_crossover.py", line 31, in create_plotting_indicators
"data": TA.EMA(data, self.parameters["fast_ema"]),
File "C:\Users\rahul\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\finta\finta.py", line 15, in wrap
args[i] = args[i].rename(columns={c: c.lower() for c in args[i].columns})
File "C:\Users\rahul\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\finta\finta.py", line 15, in
args[i] = args[i].rename(columns={c: c.lower() for c in args[i].columns})
AttributeError: 'tuple' object has no attribute 'lower'

yfinance version: yfinance-0.2.50
AutoTrader version: latest/main branch.

@trivalent
Copy link
Author

It appears that yFinance is appending the ticker symbol into the column names as well. Adding following lines in brokers/yahoo.py seems to fix this issue:

        # Remove excess data
        if count is not None and start_time is None and end_time is None:
            data = data.tail(count)
            
        **# fix for appending Ticker Symbol - Tested for Single Ticker download only
        if isinstance(data.columns, pd.MultiIndex):
            data.columns = data.columns.droplevel(1)
            data.drop(columns=['Adj Close'], inplace=True)

        data.columns = [col.split(" ")[-1] for col in data.columns]**

        if data.index.tzinfo is None:

However, this seems to introduce another issue:
Error when updating strategy: Invalid comparison between dtype=datetime64[ns, UTC] and datetime

which looks like the data index here is not in proper format.

@trivalent
Copy link
Author

Adding the below looks to fix this as well:

        data.columns = [col.split(" ")[-1] for col in data.columns]

        if data.index.tzinfo is None:
            # Data is naive, add UTC timezone
            data.index = data.index.tz_localize(timezone.utc)
        else:
            # Convert to UTC
            data.index = data.index.tz_convert(timezone.utc)
            
        # fix the index as well
        **data.index = pd.to_datetime(data.index).tz_localize(None)**

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

1 participant